View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.schema.bootstrap;
18  
19  
20  import org.apache.ldap.common.schema.AttributeType;
21  import org.apache.ldap.common.schema.MatchingRule;
22  import org.apache.ldap.common.schema.ObjectClass;
23  import org.apache.ldap.common.schema.Syntax;
24  import org.apache.ldap.server.schema.*;
25  
26  import javax.naming.NamingException;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  
32  /***
33   * A set of boostrap registries used to fire up the server.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev: 159259 $
37   */
38  public class BootstrapRegistries implements Registries
39  {
40      private BootstrapAttributeTypeRegistry attributeTypeRegistry;
41      private BootstrapComparatorRegistry comparatorRegistry;
42      private BootstrapDitContentRuleRegistry ditContentRuleRegistry;
43      private BootstrapDitStructureRuleRegistry ditStructureRuleRegistry;
44      private BootstrapMatchingRuleRegistry matchingRuleRegistry;
45      private BootstrapMatchingRuleUseRegistry matchingRuleUseRegistry;
46      private BootstrapNameFormRegistry nameFormRegistry;
47      private BootstrapNormalizerRegistry normalizerRegistry;
48      private BootstrapObjectClassRegistry objectClassRegistry;
49      private BootstrapOidRegistry oidRegistry;
50      private BootstrapSyntaxCheckerRegistry syntaxCheckerRegistry;
51      private BootstrapSyntaxRegistry syntaxRegistry;
52      private BootstrapObjectFactoryRegistry objectFactoryRegistry;
53      private BootstrapStateFactoryRegistry stateFactoryRegistry;
54  
55  
56      public BootstrapRegistries()
57      {
58          oidRegistry = new BootstrapOidRegistry();
59          normalizerRegistry = new BootstrapNormalizerRegistry();
60          comparatorRegistry = new BootstrapComparatorRegistry();
61          syntaxCheckerRegistry = new BootstrapSyntaxCheckerRegistry();
62          syntaxRegistry = new BootstrapSyntaxRegistry( getOidRegistry() );
63          matchingRuleRegistry = new BootstrapMatchingRuleRegistry( getOidRegistry() );
64          attributeTypeRegistry = new BootstrapAttributeTypeRegistry( getOidRegistry() );
65          objectClassRegistry = new BootstrapObjectClassRegistry( getOidRegistry() );
66          ditContentRuleRegistry = new BootstrapDitContentRuleRegistry( getOidRegistry() );
67          ditStructureRuleRegistry = new BootstrapDitStructureRuleRegistry( getOidRegistry() );
68          matchingRuleUseRegistry = new BootstrapMatchingRuleUseRegistry();
69          nameFormRegistry = new BootstrapNameFormRegistry( getOidRegistry() );
70          objectFactoryRegistry = new BootstrapObjectFactoryRegistry( getOidRegistry() );
71          stateFactoryRegistry = new BootstrapStateFactoryRegistry();
72      }
73  
74  
75      public AttributeTypeRegistry getAttributeTypeRegistry()
76      {
77          return attributeTypeRegistry;
78      }
79  
80      public ComparatorRegistry getComparatorRegistry()
81      {
82          return comparatorRegistry;
83      }
84  
85      public DITContentRuleRegistry getDitContentRuleRegistry()
86      {
87          return ditContentRuleRegistry;
88      }
89  
90      public DITStructureRuleRegistry getDitStructureRuleRegistry()
91      {
92          return ditStructureRuleRegistry;
93      }
94  
95      public MatchingRuleRegistry getMatchingRuleRegistry()
96      {
97          return matchingRuleRegistry;
98      }
99  
100     public MatchingRuleUseRegistry getMatchingRuleUseRegistry()
101     {
102         return matchingRuleUseRegistry;
103     }
104 
105     public NameFormRegistry getNameFormRegistry()
106     {
107         return nameFormRegistry;
108     }
109 
110     public NormalizerRegistry getNormalizerRegistry()
111     {
112         return normalizerRegistry;
113     }
114 
115     public ObjectClassRegistry getObjectClassRegistry()
116     {
117         return objectClassRegistry;
118     }
119 
120     public OidRegistry getOidRegistry()
121     {
122         return oidRegistry;
123     }
124 
125     public SyntaxCheckerRegistry getSyntaxCheckerRegistry()
126     {
127         return syntaxCheckerRegistry;
128     }
129 
130     public SyntaxRegistry getSyntaxRegistry()
131     {
132         return syntaxRegistry;
133     }
134 
135     public ObjectFactoryRegistry getObjectFactoryRegistry()
136     {
137         return objectFactoryRegistry;
138     }
139 
140     public StateFactoryRegistry getStateFactoryRegistry()
141     {
142         return stateFactoryRegistry;
143     }
144 
145 
146     // ------------------------------------------------------------------------
147     // Code used to sanity check the resolution of entities in registries
148     // ------------------------------------------------------------------------
149 
150 
151     /***
152      * Attempts to resolve the dependent schema objects of all entities that
153      * refer to other objects within the registries.  Null references will be
154      * handed appropriately.
155      *
156      * @return a list of exceptions encountered while resolving entities
157      */
158     public List checkRefInteg()
159     {
160         ArrayList errors = new ArrayList();
161 
162         Iterator list = objectClassRegistry.list();
163         while ( list.hasNext() )
164         {
165             ObjectClass oc = ( ObjectClass ) list.next();
166             resolve( oc, errors );
167         }
168 
169         list = attributeTypeRegistry.list();
170         while ( list.hasNext() )
171         {
172             AttributeType at = ( AttributeType ) list.next();
173             resolve( at, errors );
174         }
175 
176         list = matchingRuleRegistry.list();
177         while ( list.hasNext() )
178         {
179             MatchingRule mr = ( MatchingRule ) list.next();
180             resolve( mr, errors );
181         }
182 
183         list = syntaxRegistry.list();
184         while ( list.hasNext() )
185         {
186             Syntax syntax = ( Syntax ) list.next();
187             resolve( syntax, errors );
188         }
189 
190         return errors;
191     }
192 
193 
194     /***
195      * Attempts to resolve the SyntaxChecker associated with a Syntax.
196      *
197      * @param syntax the Syntax to resolve the SyntaxChecker of
198      * @param errors the list of errors to add exceptions to
199      * @return true if it succeeds, false otherwise
200      */
201     private boolean resolve( Syntax syntax, List errors )
202     {
203         if ( syntax == null )
204         {
205             return true;
206         }
207 
208         try
209         {
210             syntax.getSyntaxChecker();
211             return true;
212         }
213         catch ( NamingException e )
214         {
215             errors.add( e );
216             return false;
217         }
218     }
219 
220 
221     private boolean resolve( MatchingRule mr, List errors )
222     {
223         boolean isSuccess = true;
224 
225         if ( mr == null )
226         {
227             return true;
228         }
229 
230         try
231         {
232             if ( mr.getComparator() == null )
233             {
234                 String schema = matchingRuleRegistry.getSchemaName( mr.getOid() );
235                 errors.add( new NullPointerException( "matchingRule "
236                         + mr.getName() + " in schema " + schema + " with OID "
237                         + mr.getOid() + " has a null comparator" ) );
238                 isSuccess = false;
239             }
240         }
241         catch ( NamingException e )
242         {
243             errors.add( e );
244             isSuccess = false;
245         }
246 
247         try
248         {
249             if ( mr.getNormalizer() == null )
250             {
251                 String schema = matchingRuleRegistry.getSchemaName( mr.getOid() );
252                 errors.add( new NullPointerException( "matchingRule "
253                         + mr.getName() + " in schema " + schema + " with OID "
254                         + mr.getOid() + " has a null normalizer" ) );
255                 isSuccess = false;
256             }
257         }
258         catch ( NamingException e )
259         {
260             errors.add( e );
261             isSuccess = false;
262         }
263 
264         try
265         {
266             isSuccess &= resolve( mr.getSyntax(), errors );
267 
268             if ( mr.getSyntax() == null )
269             {
270                 String schema = matchingRuleRegistry.getSchemaName( mr.getOid() );
271                 errors.add( new NullPointerException( "matchingRule "
272                         + mr.getName() + " in schema " + schema + " with OID " + mr.getOid()
273                         + " has a null Syntax" ) );
274                 isSuccess = false;
275             }
276         }
277         catch ( NamingException e )
278         {
279             errors.add( e );
280             isSuccess = false;
281         }
282 
283         return isSuccess;
284     }
285 
286 
287     private boolean resolve( AttributeType at, List errors )
288     {
289         boolean isSuccess = true;
290 
291         boolean hasMatchingRule = false;
292 
293         if ( at == null )
294         {
295             return true;
296         }
297 
298         try
299         {
300             isSuccess &= resolve( at.getSuperior(), errors );
301         }
302         catch ( NamingException e )
303         {
304             errors.add( e );
305 
306             isSuccess = false;
307         }
308 
309         try
310         {
311             isSuccess &= resolve( at.getEquality(), errors );
312 
313             if ( at.getEquality() != null )
314             {
315                 hasMatchingRule |= true;
316             }
317         }
318         catch ( NamingException e )
319         {
320             errors.add( e );
321 
322             isSuccess = false;
323         }
324 
325         try
326         {
327             isSuccess &= resolve( at.getOrdering(), errors );
328 
329             if ( at.getOrdering() != null )
330             {
331                 hasMatchingRule |= true;
332             }
333         }
334         catch ( NamingException e )
335         {
336             errors.add( e );
337 
338             isSuccess = false;
339         }
340 
341         try
342         {
343             isSuccess &= resolve( at.getSubstr(), errors );
344 
345             if ( at.getSubstr() != null )
346             {
347                 hasMatchingRule |= true;
348             }
349         }
350         catch ( NamingException e )
351         {
352             errors.add( e );
353 
354             isSuccess = false;
355         }
356 
357         try
358         {
359             isSuccess &= resolve( at.getSyntax(), errors );
360 
361             if ( at.getSyntax() == null )
362             {
363                 String schema = attributeTypeRegistry.getSchemaName( at.getOid() );
364 
365                 errors.add( new NullPointerException( "attributeType "
366                         + at.getName() + " in schema " + schema + " with OID "
367                         + at.getOid() + " has a null Syntax" ) );
368 
369                 isSuccess = false;
370             }
371         }
372         catch ( NamingException e )
373         {
374             errors.add( e );
375 
376             isSuccess = false;
377         }
378 
379 
380 //        try
381 //        {
382 //            String schema = attributeTypeRegistry.getSchemaName( at.getOid() );
383 //            if ( ! hasMatchingRule && at.getSyntax().isHumanReadible() )
384 //            {
385 //                errors.add( new NullPointerException( "attributeType "
386 //                        + at.getName() + " in schema " + schema + " with OID "
387 //                        + at.getOid() + " has a no matchingRules defined" ) );
388 //                isSuccess = false;
389 //            }
390 //        }
391 //        catch ( NamingException e )
392 //        {
393 //            errors.add( e );
394 //            isSuccess = false;
395 //        }
396 
397         return isSuccess;
398     }
399 
400 
401     private boolean resolve( ObjectClass oc, List errors )
402     {
403         boolean isSuccess = true;
404 
405         if ( oc == null )
406         {
407             return true;
408         }
409 
410         ObjectClass[] superiors = new org.apache.ldap.common.schema.ObjectClass[0];
411 
412         try
413         {
414             superiors = oc.getSuperClasses();
415         }
416         catch ( NamingException e )
417         {
418             superiors = new ObjectClass[0];
419             isSuccess = false;
420             errors.add( e );
421         }
422 
423         for ( int ii = 0; ii < superiors.length; ii++ )
424         {
425             isSuccess &= resolve( superiors[ii], errors ) ;
426         }
427 
428         AttributeType[] mayList = new org.apache.ldap.common.schema.AttributeType[0];
429 
430         try
431         {
432             mayList = oc.getMayList();
433         }
434         catch ( NamingException e )
435         {
436             mayList = new AttributeType[0];
437             isSuccess = false;
438             errors.add( e );
439         }
440 
441         for ( int ii = 0; ii < mayList.length; ii++ )
442         {
443             isSuccess &= resolve( mayList[ii], errors ) ;
444         }
445 
446 
447         AttributeType[] mustList = new org.apache.ldap.common.schema.AttributeType[0];
448 
449         try
450         {
451             mustList = oc.getMustList();
452         }
453         catch ( NamingException e )
454         {
455             mustList = new AttributeType[0];
456             isSuccess = false;
457             errors.add( e );
458         }
459 
460         for ( int ii = 0; ii < mustList.length; ii++ )
461         {
462             isSuccess &= resolve( mustList[ii], errors ) ;
463         }
464 
465         return isSuccess;
466     }
467 }