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