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