View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.ldap.client.api;
22  
23  
24  import java.io.IOException;
25  import java.text.ParseException;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants;
31  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
32  import org.apache.directory.api.ldap.model.entry.Attribute;
33  import org.apache.directory.api.ldap.model.entry.DefaultEntry;
34  import org.apache.directory.api.ldap.model.entry.Entry;
35  import org.apache.directory.api.ldap.model.entry.Value;
36  import org.apache.directory.api.ldap.model.exception.LdapException;
37  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
38  import org.apache.directory.api.ldap.model.name.Dn;
39  import org.apache.directory.api.ldap.model.schema.AttributeType;
40  import org.apache.directory.api.ldap.model.schema.AttributesFactory;
41  import org.apache.directory.api.ldap.model.schema.DitContentRule;
42  import org.apache.directory.api.ldap.model.schema.DitStructureRule;
43  import org.apache.directory.api.ldap.model.schema.LdapSyntax;
44  import org.apache.directory.api.ldap.model.schema.MatchingRule;
45  import org.apache.directory.api.ldap.model.schema.MatchingRuleUse;
46  import org.apache.directory.api.ldap.model.schema.NameForm;
47  import org.apache.directory.api.ldap.model.schema.ObjectClass;
48  import org.apache.directory.api.ldap.model.schema.SchemaObject;
49  import org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper;
50  import org.apache.directory.api.ldap.model.schema.parsers.AttributeTypeDescriptionSchemaParser;
51  import org.apache.directory.api.ldap.model.schema.parsers.DitContentRuleDescriptionSchemaParser;
52  import org.apache.directory.api.ldap.model.schema.parsers.DitStructureRuleDescriptionSchemaParser;
53  import org.apache.directory.api.ldap.model.schema.parsers.LdapComparatorDescription;
54  import org.apache.directory.api.ldap.model.schema.parsers.LdapComparatorDescriptionSchemaParser;
55  import org.apache.directory.api.ldap.model.schema.parsers.LdapSyntaxDescriptionSchemaParser;
56  import org.apache.directory.api.ldap.model.schema.parsers.MatchingRuleDescriptionSchemaParser;
57  import org.apache.directory.api.ldap.model.schema.parsers.MatchingRuleUseDescriptionSchemaParser;
58  import org.apache.directory.api.ldap.model.schema.parsers.NameFormDescriptionSchemaParser;
59  import org.apache.directory.api.ldap.model.schema.parsers.NormalizerDescription;
60  import org.apache.directory.api.ldap.model.schema.parsers.NormalizerDescriptionSchemaParser;
61  import org.apache.directory.api.ldap.model.schema.parsers.ObjectClassDescriptionSchemaParser;
62  import org.apache.directory.api.ldap.model.schema.parsers.SyntaxCheckerDescription;
63  import org.apache.directory.api.ldap.model.schema.parsers.SyntaxCheckerDescriptionSchemaParser;
64  import org.apache.directory.api.ldap.model.schema.registries.AbstractSchemaLoader;
65  import org.apache.directory.api.ldap.model.schema.registries.DefaultSchema;
66  import org.apache.directory.api.ldap.model.schema.registries.Schema;
67  import org.apache.directory.api.util.Base64;
68  import org.apache.directory.api.util.Strings;
69  import org.apache.directory.ldap.client.api.exception.InvalidConnectionException;
70  import org.slf4j.Logger;
71  import org.slf4j.LoggerFactory;
72  
73  
74  /**
75   * A schema loader which uses LdapConnection to load schema from a ApacheDS serveur
76   *
77   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
78   */
79  public class DefaultSchemaLoader extends AbstractSchemaLoader
80  {
81      private static final String DEFAULT_APACHEDS_VENDOR_NAME = "Apache Software Foundation";
82  
83      /** the logger */
84      private static final Logger LOG = LoggerFactory.getLogger( DefaultSchemaLoader.class );
85  
86      /** the connection to the ldap server */
87      private LdapConnection connection;
88  
89      /** the subschemaSubentry DN */
90      private Dn subschemaSubentryDn;
91  
92      /** The SubschemaSubentry descriptions parsers */
93      private static AttributeTypeDescriptionSchemaParser AT_DESCR_SCHEMA_PARSER = new AttributeTypeDescriptionSchemaParser();
94      private static DitStructureRuleDescriptionSchemaParser DSR_DESCR_SCHEMA_PARSER = new DitStructureRuleDescriptionSchemaParser();
95      private static DitContentRuleDescriptionSchemaParser DCR_DESCR_SCHEMA_PARSER = new DitContentRuleDescriptionSchemaParser();
96      private static MatchingRuleDescriptionSchemaParser MR_DESCR_SCHEMA_PARSER = new MatchingRuleDescriptionSchemaParser();
97      private static MatchingRuleUseDescriptionSchemaParser MRU_DESCR_SCHEMA_PARSER = new MatchingRuleUseDescriptionSchemaParser();
98      private static NameFormDescriptionSchemaParser NF_DESCR_SCHEMA_PARSER = new NameFormDescriptionSchemaParser();
99      private static ObjectClassDescriptionSchemaParser OC_DESCR_SCHEMA_PARSER = new ObjectClassDescriptionSchemaParser();
100     private static LdapSyntaxDescriptionSchemaParser LS_DESCR_SCHEMA_PARSER = new LdapSyntaxDescriptionSchemaParser();
101 
102     private static LdapComparatorDescriptionSchemaParser C_DESCR_SCHEMA_PARSER = new LdapComparatorDescriptionSchemaParser();
103     private static NormalizerDescriptionSchemaParser N_DESCR_SCHEMA_PARSER = new NormalizerDescriptionSchemaParser();
104     private static SyntaxCheckerDescriptionSchemaParser SC_DESCR_SCHEMA_PARSER = new SyntaxCheckerDescriptionSchemaParser();
105 
106 
107     /**
108      * Creates a new instance of DefaultSchemaLoader.
109      *
110      * @param connection the LDAP connection
111      * @throws Exception if the connection is not authenticated or if there are any problems
112      *                   while loading the schema entries
113      */
114     public DefaultSchemaLoader( LdapConnection connection ) throws LdapException
115     {
116         this( connection, false );
117     }
118 
119 
120     /**
121      * Creates a new instance of DefaultSchemaLoader.
122      *
123      * @param connection the LDAP connection
124      * @param initial setting for the quirks mode
125      * @throws Exception if the connection is not authenticated or if there are any problems
126      *                   while loading the schema entries
127      */
128     public DefaultSchemaLoader( LdapConnection connection, boolean quirksMode ) throws LdapException
129     {
130         if ( connection == null )
131         {
132             throw new InvalidConnectionException( "Cannot connect on the server, the connection is null" );
133         }
134 
135         this.connection = connection;
136         setQuirksMode( quirksMode );
137 
138         // Flagging if the connection was already connected
139         boolean wasConnected = connection.isConnected();
140 
141         try
142         {
143             // Connecting (if needed)
144             if ( !wasConnected )
145             {
146                 connection.connect();
147             }
148 
149             // Getting the subschemaSubentry DN from the rootDSE
150             Entry rootDse = connection.lookup( Dn.ROOT_DSE, SchemaConstants.SUBSCHEMA_SUBENTRY_AT,
151                 SchemaConstants.VENDOR_NAME_AT );
152 
153             if ( rootDse != null )
154             {
155                 // Checking if this is an ApacheDS server
156                 if ( isApacheDs( rootDse ) )
157                 {
158                     // Getting the subSchemaSubEntry attribute
159                     Attribute subschemaSubentryAttribute = rootDse.get( SchemaConstants.SUBSCHEMA_SUBENTRY_AT );
160 
161                     if ( ( subschemaSubentryAttribute != null ) && ( subschemaSubentryAttribute.size() > 0 ) )
162                     {
163                         subschemaSubentryDn = new Dn( connection.getSchemaManager(),
164                             subschemaSubentryAttribute.getString() );
165 
166                         loadSchemas();
167                     }
168                 }
169                 else
170                 {
171                     try
172                     {
173                         // No matter what, first try to search the schema from the rootDSE
174                         // Getting the subSchemaSubEntry attribute
175                         Attribute subschemaSubentryAttribute = rootDse.get( SchemaConstants.SUBSCHEMA_SUBENTRY_AT );
176 
177                         if ( ( subschemaSubentryAttribute != null ) && ( subschemaSubentryAttribute.size() > 0 ) )
178                         {
179                             subschemaSubentryDn = new Dn( connection.getSchemaManager(),
180                                 subschemaSubentryAttribute.getString() );
181 
182                             loadSchemas();
183                         }
184                     }
185                     catch ( LdapException le )
186                     {
187                         // TODO : if we can't read the schema from the rootDSE, just try to read the 
188                         // schema from cn=schema
189                         throw le;
190                     }
191                 }
192             }
193         }
194         finally
195         {
196             // Checking if the connection needs to be closed
197             if ( ( !wasConnected ) && ( connection.isConnected() ) )
198             {
199                 try
200                 {
201                     connection.close();
202                 }
203                 catch ( IOException e )
204                 {
205                     throw new LdapException( e );
206                 }
207             }
208         }
209     }
210 
211 
212     /**
213      * Indicates if the given Root DSE corresponds to an ApacheDS server.
214      *
215      * @param rootDse the Root DSE
216      * @return <code>true</code> if this is an ApacheDS server,
217      *         <code>false</code> if not.
218      * @throws LdapInvalidAttributeValueException
219      */
220     private boolean isApacheDs( Entry rootDse ) throws LdapInvalidAttributeValueException
221     {
222         if ( rootDse != null )
223         {
224             Attribute vendorNameAttribute = rootDse.get( SchemaConstants.VENDOR_NAME_AT );
225 
226             if ( ( vendorNameAttribute != null ) && vendorNameAttribute.size() == 1 )
227             {
228                 return DEFAULT_APACHEDS_VENDOR_NAME.equalsIgnoreCase( vendorNameAttribute.getString() );
229             }
230         }
231 
232         return false;
233     }
234 
235 
236     /**
237      * Creates a new instance of NetworkSchemaLoader.
238      *
239      * @param connection the LDAP connection
240      * @throws Exception if the connection is not authenticated or if there are any problems
241      *                   while loading the schema entries
242      */
243     public DefaultSchemaLoader( LdapConnection connection, Dn subschemaSubentryDn ) throws Exception
244     {
245         if ( !connection.isAuthenticated() )
246         {
247             throw new IllegalArgumentException( "connection is not authenticated" );
248         }
249 
250         this.connection = connection;
251         this.subschemaSubentryDn = subschemaSubentryDn;
252 
253         loadSchemas();
254     }
255 
256 
257     /**
258      * Load all the schemas.
259      * 
260      * @param subschemaSubentryDn
261      * @throws Exception
262      */
263     private void loadSchemas() throws LdapException
264     {
265         LOG.debug( "initializing schemas" );
266 
267         // Load all the elements from the SubschemaSubentry
268         Entry subschemaSubentry = connection.lookup( subschemaSubentryDn,
269             SchemaConstants.ATTRIBUTE_TYPES_AT,
270             SchemaConstants.COMPARATORS_AT,
271             SchemaConstants.DIT_CONTENT_RULES_AT,
272             SchemaConstants.DIT_STRUCTURE_RULES_AT,
273             SchemaConstants.LDAP_SYNTAXES_AT,
274             SchemaConstants.MATCHING_RULES_AT,
275             SchemaConstants.MATCHING_RULE_USE_AT,
276             SchemaConstants.NAME_FORMS_AT,
277             SchemaConstants.NORMALIZERS_AT,
278             SchemaConstants.OBJECT_CLASSES_AT,
279             SchemaConstants.SYNTAX_CHECKERS_AT
280             );
281 
282         // Load all the AT
283         Attribute attributeTypes = subschemaSubentry.get( SchemaConstants.ATTRIBUTE_TYPES_AT );
284         loadAttributeTypes( attributeTypes );
285 
286         // Load all the C
287         Attribute comparators = subschemaSubentry.get( SchemaConstants.COMPARATORS_AT );
288         loadComparators( comparators );
289 
290         // Load all the DCR
291         Attribute ditContentRules = subschemaSubentry.get( SchemaConstants.DIT_CONTENT_RULES_AT );
292         loadDitContentRules( ditContentRules );
293 
294         // Load all the DSR
295         Attribute ditStructureRules = subschemaSubentry.get( SchemaConstants.DIT_STRUCTURE_RULES_AT );
296         loadDitStructureRules( ditStructureRules );
297 
298         // Load all the LS
299         Attribute ldapSytaxes = subschemaSubentry.get( SchemaConstants.LDAP_SYNTAXES_AT );
300         loadLdapSyntaxes( ldapSytaxes );
301 
302         // Load all the MR
303         Attribute matchingRules = subschemaSubentry.get( SchemaConstants.MATCHING_RULES_AT );
304         loadMatchingRules( matchingRules );
305 
306         // Load all the MRU
307         Attribute matchingRuleUse = subschemaSubentry.get( SchemaConstants.MATCHING_RULE_USE_AT );
308         loadMatchingRuleUses( matchingRuleUse );
309 
310         // Load all the N
311         Attribute normalizers = subschemaSubentry.get( SchemaConstants.NORMALIZERS_AT );
312         loadNormalizers( normalizers );
313 
314         // Load all the NF
315         Attribute nameForms = subschemaSubentry.get( SchemaConstants.NAME_FORMS_AT );
316         loadNameForms( nameForms );
317 
318         // Load all the OC
319         Attribute objectClasses = subschemaSubentry.get( SchemaConstants.OBJECT_CLASSES_AT );
320         loadObjectClasses( objectClasses );
321 
322         // Load all the SC
323         Attribute syntaxCheckers = subschemaSubentry.get( SchemaConstants.SYNTAX_CHECKERS_AT );
324         loadSyntaxCheckers( syntaxCheckers );
325     }
326 
327 
328     /**
329      * {@inheritDoc}
330      */
331     private void loadAttributeTypes( Attribute attributeTypes ) throws LdapException
332     {
333         if ( attributeTypes == null )
334         {
335             return;
336         }
337 
338         for ( Value<?> value : attributeTypes )
339         {
340             String desc = value.getString();
341 
342             try
343             {
344                 AttributeType attributeType = AT_DESCR_SCHEMA_PARSER.parseAttributeTypeDescription( desc );
345 
346                 updateSchemas( attributeType );
347             }
348             catch ( ParseException pe )
349             {
350                 throw new LdapException( pe );
351             }
352         }
353     }
354 
355 
356     /**
357      * {@inheritDoc}
358      */
359     private void loadComparators( Attribute comparators ) throws LdapException
360     {
361         if ( comparators == null )
362         {
363             return;
364         }
365 
366         for ( Value<?> value : comparators )
367         {
368             String desc = value.getString();
369 
370             try
371             {
372                 LdapComparatorDescription comparator = C_DESCR_SCHEMA_PARSER.parseComparatorDescription( desc );
373 
374                 updateSchemas( comparator );
375             }
376             catch ( ParseException pe )
377             {
378                 throw new LdapException( pe );
379             }
380         }
381     }
382 
383 
384     /**
385      * {@inheritDoc}
386      */
387     private void loadDitContentRules( Attribute ditContentRules ) throws LdapException
388     {
389         if ( ditContentRules == null )
390         {
391             return;
392         }
393 
394         for ( Value<?> value : ditContentRules )
395         {
396             String desc = value.getString();
397 
398             try
399             {
400                 DitContentRule ditContentRule = DCR_DESCR_SCHEMA_PARSER.parseDITContentRuleDescription( desc );
401 
402                 updateSchemas( ditContentRule );
403             }
404             catch ( ParseException pe )
405             {
406                 throw new LdapException( pe );
407             }
408         }
409     }
410 
411 
412     /**
413      * {@inheritDoc}
414      */
415     private void loadDitStructureRules( Attribute ditStructureRules ) throws LdapException
416     {
417         if ( ditStructureRules == null )
418         {
419             return;
420         }
421 
422         for ( Value<?> value : ditStructureRules )
423         {
424             String desc = value.getString();
425 
426             try
427             {
428                 DitStructureRule ditStructureRule = DSR_DESCR_SCHEMA_PARSER.parseDITStructureRuleDescription( desc );
429 
430                 updateSchemas( ditStructureRule );
431             }
432             catch ( ParseException pe )
433             {
434                 throw new LdapException( pe );
435             }
436         }
437     }
438 
439 
440     /**
441      * {@inheritDoc}
442      */
443     private void loadLdapSyntaxes( Attribute ldapSyntaxes ) throws LdapException
444     {
445         if ( ldapSyntaxes == null )
446         {
447             return;
448         }
449 
450         for ( Value<?> value : ldapSyntaxes )
451         {
452             String desc = value.getString();
453 
454             try
455             {
456                 LdapSyntax ldapSyntax = LS_DESCR_SCHEMA_PARSER.parseLdapSyntaxDescription( desc );
457 
458                 updateSchemas( ldapSyntax );
459             }
460             catch ( ParseException pe )
461             {
462                 throw new LdapException( pe );
463             }
464         }
465     }
466 
467 
468     /**
469      * {@inheritDoc}
470      */
471     private void loadMatchingRules( Attribute matchingRules ) throws LdapException
472     {
473         if ( matchingRules == null )
474         {
475             return;
476         }
477 
478         for ( Value<?> value : matchingRules )
479         {
480             String desc = value.getString();
481 
482             try
483             {
484                 MatchingRule matchingRule = MR_DESCR_SCHEMA_PARSER.parseMatchingRuleDescription( desc );
485 
486                 updateSchemas( matchingRule );
487             }
488             catch ( ParseException pe )
489             {
490                 throw new LdapException( pe );
491             }
492         }
493     }
494 
495 
496     /**
497      * {@inheritDoc}
498      */
499     private void loadMatchingRuleUses( Attribute matchingRuleUses ) throws LdapException
500     {
501         if ( matchingRuleUses == null )
502         {
503             return;
504         }
505 
506         for ( Value<?> value : matchingRuleUses )
507         {
508             String desc = value.getString();
509 
510             try
511             {
512                 MatchingRuleUse matchingRuleUse = MRU_DESCR_SCHEMA_PARSER.parseMatchingRuleUseDescription( desc );
513 
514                 updateSchemas( matchingRuleUse );
515             }
516             catch ( ParseException pe )
517             {
518                 throw new LdapException( pe );
519             }
520         }
521     }
522 
523 
524     /**
525      * {@inheritDoc}
526      */
527     private void loadNameForms( Attribute nameForms ) throws LdapException
528     {
529         if ( nameForms == null )
530         {
531             return;
532         }
533 
534         for ( Value<?> value : nameForms )
535         {
536             String desc = value.getString();
537 
538             try
539             {
540                 NameForm nameForm = NF_DESCR_SCHEMA_PARSER.parseNameFormDescription( desc );
541 
542                 updateSchemas( nameForm );
543             }
544             catch ( ParseException pe )
545             {
546                 throw new LdapException( pe );
547             }
548         }
549     }
550 
551 
552     /**
553      * {@inheritDoc}
554      */
555     private void loadNormalizers( Attribute normalizers ) throws LdapException
556     {
557         if ( normalizers == null )
558         {
559             return;
560         }
561 
562         for ( Value<?> value : normalizers )
563         {
564             String desc = value.getString();
565 
566             try
567             {
568                 NormalizerDescription normalizer = N_DESCR_SCHEMA_PARSER.parseNormalizerDescription( desc );
569 
570                 updateSchemas( normalizer );
571             }
572             catch ( ParseException pe )
573             {
574                 throw new LdapException( pe );
575             }
576         }
577     }
578 
579 
580     /**
581      * {@inheritDoc}
582      */
583     private void loadObjectClasses( Attribute objectClasses ) throws LdapException
584     {
585         if ( objectClasses == null )
586         {
587             return;
588         }
589 
590         for ( Value<?> value : objectClasses )
591         {
592             String desc = value.getString();
593 
594             try
595             {
596                 ObjectClass objectClass = OC_DESCR_SCHEMA_PARSER.parseObjectClassDescription( desc );
597 
598                 updateSchemas( objectClass );
599             }
600             catch ( ParseException pe )
601             {
602                 throw new LdapException( pe );
603             }
604         }
605     }
606 
607 
608     /**
609      * {@inheritDoc}
610      */
611     private void loadSyntaxCheckers( Attribute syntaxCheckers ) throws LdapException
612     {
613         if ( syntaxCheckers == null )
614         {
615             return;
616         }
617 
618         for ( Value<?> value : syntaxCheckers )
619         {
620             String desc = value.getString();
621 
622             try
623             {
624                 SyntaxCheckerDescription syntaxChecker = SC_DESCR_SCHEMA_PARSER.parseSyntaxCheckerDescription( desc );
625 
626                 updateSchemas( syntaxChecker );
627             }
628             catch ( ParseException pe )
629             {
630                 throw new LdapException( pe );
631             }
632         }
633     }
634 
635 
636     private void updateSchemas( SchemaObject schemaObject )
637     {
638         String schemaName = schemaObject.getSchemaName();
639         Schema schema = null;
640 
641         if ( Strings.isEmpty( schemaName ) || Strings.equals( "null", schemaName ) )
642         {
643             schemaName = "default";
644             schema = schemaMap.get( schemaName );
645         }
646         else
647         {
648             schema = schemaMap.get( schemaName );
649         }
650 
651         if ( schema == null )
652         {
653             schema = new DefaultSchema( schemaName );
654 
655             schemaMap.put( schemaName, schema );
656         }
657 
658         schema.getContent().add( new SchemaObjectWrapper( schemaObject ) );
659 
660     }
661 
662 
663     /**
664      * {@inheritDoc}
665      */
666     public List<Entry> loadAttributeTypes( Schema... schemas ) throws LdapException, IOException
667     {
668         List<Entry> attributeTypeEntries = new ArrayList<Entry>();
669 
670         if ( schemas == null )
671         {
672             return attributeTypeEntries;
673         }
674 
675         AttributesFactory factory = new AttributesFactory();
676 
677         for ( Schema schema : schemas )
678         {
679             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
680 
681             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
682             {
683                 SchemaObject schemaObject = schemaObjectWrapper.get();
684 
685                 if ( schemaObject instanceof AttributeType )
686                 {
687                     AttributeType attributeType = ( AttributeType ) schemaObject;
688 
689                     Entry attributeTypeEntry = factory.convert( attributeType, schema, null );
690 
691                     attributeTypeEntries.add( attributeTypeEntry );
692                 }
693             }
694         }
695 
696         return attributeTypeEntries;
697     }
698 
699 
700     /**
701      * {@inheritDoc}
702      */
703     public List<Entry> loadComparators( Schema... schemas ) throws LdapException, IOException
704     {
705         List<Entry> comparatorEntries = new ArrayList<Entry>();
706 
707         if ( schemas == null )
708         {
709             return comparatorEntries;
710         }
711 
712         for ( Schema schema : schemas )
713         {
714             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
715 
716             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
717             {
718                 SchemaObject schemaObject = schemaObjectWrapper.get();
719 
720                 if ( schemaObject instanceof LdapComparatorDescription )
721                 {
722                     LdapComparatorDescription ldapComparatorDescription = ( LdapComparatorDescription ) schemaObject;
723                     Entry lcEntry = getEntry( ldapComparatorDescription );
724 
725                     comparatorEntries.add( lcEntry );
726                 }
727             }
728         }
729 
730         return comparatorEntries;
731     }
732 
733 
734     /**
735      * {@inheritDoc}
736      */
737     public List<Entry> loadDitContentRules( Schema... schemas ) throws LdapException, IOException
738     {
739         List<Entry> ditContentRuleEntries = new ArrayList<Entry>();
740 
741         if ( schemas == null )
742         {
743             return ditContentRuleEntries;
744         }
745 
746         AttributesFactory factory = new AttributesFactory();
747 
748         for ( Schema schema : schemas )
749         {
750             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
751 
752             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
753             {
754                 SchemaObject schemaObject = schemaObjectWrapper.get();
755 
756                 if ( schemaObject instanceof DitContentRule )
757                 {
758                     DitContentRule ditContentRule = ( DitContentRule ) schemaObject;
759 
760                     Entry ditContentRuleEntry = factory.convert( ditContentRule, schema, null );
761 
762                     ditContentRuleEntries.add( ditContentRuleEntry );
763                 }
764             }
765         }
766 
767         return ditContentRuleEntries;
768     }
769 
770 
771     /**
772      * {@inheritDoc}
773      */
774     public List<Entry> loadDitStructureRules( Schema... schemas ) throws LdapException, IOException
775     {
776         List<Entry> ditStructureRuleEntries = new ArrayList<Entry>();
777 
778         if ( schemas == null )
779         {
780             return ditStructureRuleEntries;
781         }
782 
783         AttributesFactory factory = new AttributesFactory();
784 
785         for ( Schema schema : schemas )
786         {
787             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
788 
789             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
790             {
791                 SchemaObject schemaObject = schemaObjectWrapper.get();
792 
793                 if ( schemaObject instanceof DitStructureRule )
794                 {
795                     DitStructureRule ditStructureRule = ( DitStructureRule ) schemaObject;
796 
797                     Entry ditStructureRuleEntry = factory.convert( ditStructureRule, schema, null );
798 
799                     ditStructureRuleEntries.add( ditStructureRuleEntry );
800                 }
801             }
802         }
803 
804         return ditStructureRuleEntries;
805     }
806 
807 
808     /**
809      * {@inheritDoc}
810      */
811     public List<Entry> loadMatchingRuleUses( Schema... schemas ) throws LdapException, IOException
812     {
813         List<Entry> matchingRuleUseEntries = new ArrayList<Entry>();
814 
815         if ( schemas == null )
816         {
817             return matchingRuleUseEntries;
818         }
819 
820         AttributesFactory factory = new AttributesFactory();
821 
822         for ( Schema schema : schemas )
823         {
824             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
825 
826             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
827             {
828                 SchemaObject schemaObject = schemaObjectWrapper.get();
829 
830                 if ( schemaObject instanceof MatchingRuleUse )
831                 {
832                     MatchingRuleUse matchingRuleUse = ( MatchingRuleUse ) schemaObject;
833 
834                     Entry matchingRuleUseEntry = factory.convert( matchingRuleUse, schema, null );
835 
836                     matchingRuleUseEntries.add( matchingRuleUseEntry );
837                 }
838             }
839         }
840 
841         return matchingRuleUseEntries;
842     }
843 
844 
845     /**
846      * {@inheritDoc}
847      */
848     public List<Entry> loadMatchingRules( Schema... schemas ) throws LdapException, IOException
849     {
850         List<Entry> matchingRuleEntries = new ArrayList<Entry>();
851 
852         if ( schemas == null )
853         {
854             return matchingRuleEntries;
855         }
856 
857         AttributesFactory factory = new AttributesFactory();
858 
859         for ( Schema schema : schemas )
860         {
861             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
862 
863             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
864             {
865                 SchemaObject schemaObject = schemaObjectWrapper.get();
866 
867                 if ( schemaObject instanceof MatchingRule )
868                 {
869                     MatchingRule matchingRule = ( MatchingRule ) schemaObject;
870 
871                     Entry matchingRuleEntry = factory.convert( matchingRule, schema, null );
872 
873                     matchingRuleEntries.add( matchingRuleEntry );
874                 }
875             }
876         }
877 
878         return matchingRuleEntries;
879     }
880 
881 
882     /**
883      * {@inheritDoc}
884      */
885     public List<Entry> loadNameForms( Schema... schemas ) throws LdapException, IOException
886     {
887         List<Entry> nameFormEntries = new ArrayList<Entry>();
888 
889         if ( schemas == null )
890         {
891             return nameFormEntries;
892         }
893 
894         AttributesFactory factory = new AttributesFactory();
895 
896         for ( Schema schema : schemas )
897         {
898             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
899 
900             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
901             {
902                 SchemaObject schemaObject = schemaObjectWrapper.get();
903 
904                 if ( schemaObject instanceof NameForm )
905                 {
906                     NameForm nameForm = ( NameForm ) schemaObject;
907 
908                     Entry nameFormEntry = factory.convert( nameForm, schema, null );
909 
910                     nameFormEntries.add( nameFormEntry );
911                 }
912             }
913         }
914 
915         return nameFormEntries;
916     }
917 
918 
919     /**
920      * {@inheritDoc}
921      */
922     public List<Entry> loadNormalizers( Schema... schemas ) throws LdapException, IOException
923     {
924         List<Entry> normalizerEntries = new ArrayList<Entry>();
925 
926         if ( schemas == null )
927         {
928             return normalizerEntries;
929         }
930 
931         for ( Schema schema : schemas )
932         {
933             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
934 
935             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
936             {
937                 SchemaObject schemaObject = schemaObjectWrapper.get();
938 
939                 if ( schemaObject instanceof NormalizerDescription )
940                 {
941                     NormalizerDescription normalizerDescription = ( NormalizerDescription ) schemaObject;
942                     Entry normalizerEntry = getEntry( normalizerDescription );
943 
944                     normalizerEntries.add( normalizerEntry );
945                 }
946             }
947         }
948 
949         return normalizerEntries;
950     }
951 
952 
953     /**
954      * {@inheritDoc}
955      */
956     public List<Entry> loadObjectClasses( Schema... schemas ) throws LdapException, IOException
957     {
958         List<Entry> objectClassEntries = new ArrayList<Entry>();
959 
960         if ( schemas == null )
961         {
962             return objectClassEntries;
963         }
964 
965         AttributesFactory factory = new AttributesFactory();
966 
967         for ( Schema schema : schemas )
968         {
969             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
970 
971             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
972             {
973                 SchemaObject schemaObject = schemaObjectWrapper.get();
974 
975                 if ( schemaObject instanceof ObjectClass )
976                 {
977                     ObjectClass objectClass = ( ObjectClass ) schemaObject;
978 
979                     Entry objectClassEntry = factory.convert( objectClass, schema, null );
980 
981                     objectClassEntries.add( objectClassEntry );
982                 }
983             }
984         }
985 
986         return objectClassEntries;
987     }
988 
989 
990     /**
991      * {@inheritDoc}
992      */
993     public List<Entry> loadSyntaxCheckers( Schema... schemas ) throws LdapException, IOException
994     {
995         List<Entry> syntaxCheckerEntries = new ArrayList<Entry>();
996 
997         if ( schemas == null )
998         {
999             return syntaxCheckerEntries;
1000         }
1001 
1002         for ( Schema schema : schemas )
1003         {
1004             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
1005 
1006             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
1007             {
1008                 SchemaObject schemaObject = schemaObjectWrapper.get();
1009 
1010                 if ( schemaObject instanceof SyntaxCheckerDescription )
1011                 {
1012                     SyntaxCheckerDescription syntaxCheckerDescription = ( SyntaxCheckerDescription ) schemaObject;
1013                     Entry syntaxCheckerEntry = getEntry( syntaxCheckerDescription );
1014 
1015                     syntaxCheckerEntries.add( syntaxCheckerEntry );
1016                 }
1017             }
1018         }
1019 
1020         return syntaxCheckerEntries;
1021     }
1022 
1023 
1024     /**
1025      * {@inheritDoc}
1026      */
1027     public List<Entry> loadSyntaxes( Schema... schemas ) throws LdapException, IOException
1028     {
1029         List<Entry> syntaxEntries = new ArrayList<Entry>();
1030 
1031         if ( schemas == null )
1032         {
1033             return syntaxEntries;
1034         }
1035 
1036         AttributesFactory factory = new AttributesFactory();
1037 
1038         for ( Schema schema : schemas )
1039         {
1040             Set<SchemaObjectWrapper> schemaObjectWrappers = schema.getContent();
1041 
1042             for ( SchemaObjectWrapper schemaObjectWrapper : schemaObjectWrappers )
1043             {
1044                 SchemaObject schemaObject = schemaObjectWrapper.get();
1045 
1046                 if ( schemaObject instanceof LdapSyntax )
1047                 {
1048                     LdapSyntax ldapSyntax = ( LdapSyntax ) schemaObject;
1049 
1050                     Entry ldapSyntaxEntry = factory.convert( ldapSyntax, schema, null );
1051 
1052                     syntaxEntries.add( ldapSyntaxEntry );
1053                 }
1054             }
1055         }
1056 
1057         return syntaxEntries;
1058     }
1059 
1060 
1061     private Entry getEntry( LdapComparatorDescription comparatorDescription )
1062     {
1063         Entry entry = new DefaultEntry();
1064 
1065         entry.put( SchemaConstants.OBJECT_CLASS_AT,
1066             SchemaConstants.TOP_OC,
1067             MetaSchemaConstants.META_TOP_OC,
1068             MetaSchemaConstants.META_COMPARATOR_OC );
1069 
1070         entry.put( MetaSchemaConstants.M_OID_AT, comparatorDescription.getOid() );
1071         entry.put( MetaSchemaConstants.M_FQCN_AT, comparatorDescription.getFqcn() );
1072 
1073         if ( comparatorDescription.getBytecode() != null )
1074         {
1075             entry.put( MetaSchemaConstants.M_BYTECODE_AT,
1076                 Base64.decode( comparatorDescription.getBytecode().toCharArray() ) );
1077         }
1078 
1079         if ( comparatorDescription.getDescription() != null )
1080         {
1081             entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, comparatorDescription.getDescription() );
1082         }
1083 
1084         return entry;
1085     }
1086 
1087 
1088     private Entry getEntry( SyntaxCheckerDescription syntaxCheckerDescription )
1089     {
1090         Entry entry = new DefaultEntry();
1091 
1092         entry.put( SchemaConstants.OBJECT_CLASS_AT,
1093             SchemaConstants.TOP_OC,
1094             MetaSchemaConstants.META_TOP_OC,
1095             MetaSchemaConstants.META_SYNTAX_CHECKER_OC );
1096 
1097         entry.put( MetaSchemaConstants.M_OID_AT, syntaxCheckerDescription.getOid() );
1098         entry.put( MetaSchemaConstants.M_FQCN_AT, syntaxCheckerDescription.getFqcn() );
1099 
1100         if ( syntaxCheckerDescription.getBytecode() != null )
1101         {
1102             entry.put( MetaSchemaConstants.M_BYTECODE_AT,
1103                 Base64.decode( syntaxCheckerDescription.getBytecode().toCharArray() ) );
1104         }
1105 
1106         if ( syntaxCheckerDescription.getDescription() != null )
1107         {
1108             entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, syntaxCheckerDescription.getDescription() );
1109         }
1110 
1111         return entry;
1112     }
1113 
1114 
1115     private Entry getEntry( NormalizerDescription normalizerDescription )
1116     {
1117         Entry entry = new DefaultEntry();
1118 
1119         entry.put( SchemaConstants.OBJECT_CLASS_AT,
1120             SchemaConstants.TOP_OC,
1121             MetaSchemaConstants.META_TOP_OC,
1122             MetaSchemaConstants.META_NORMALIZER_OC );
1123 
1124         entry.put( MetaSchemaConstants.M_OID_AT, normalizerDescription.getOid() );
1125         entry.put( MetaSchemaConstants.M_FQCN_AT, normalizerDescription.getFqcn() );
1126 
1127         if ( normalizerDescription.getBytecode() != null )
1128         {
1129             entry.put( MetaSchemaConstants.M_BYTECODE_AT,
1130                 Base64.decode( normalizerDescription.getBytecode().toCharArray() ) );
1131         }
1132 
1133         if ( normalizerDescription.getDescription() != null )
1134         {
1135             entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, normalizerDescription.getDescription() );
1136         }
1137 
1138         return entry;
1139     }
1140 
1141 
1142     /**
1143      * Sets the quirks mode for all the internal parsers.
1144      *
1145      * If enabled the parser accepts non-numeric OIDs and some
1146      * special characters in descriptions.
1147      *
1148      * @param enabled the new quirks mode
1149      */
1150     public void setQuirksMode( boolean enabled )
1151     {
1152         AT_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1153         C_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1154         DCR_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1155         DSR_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1156         LS_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1157         MR_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1158         MRU_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1159         N_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1160         NF_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1161         OC_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1162         SC_DESCR_SCHEMA_PARSER.setQuirksMode( enabled );
1163     }
1164 }