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  package org.apache.directory.server.utils; 
21  
22  
23  import java.util.Comparator; 
24  
25  import javax.naming.NamingException;
26  
27  import org.apache.directory.server.constants.MetaSchemaConstants;
28  import org.apache.directory.server.core.entry.DefaultServerAttribute;
29  import org.apache.directory.server.core.entry.DefaultServerEntry;
30  import org.apache.directory.server.core.entry.ServerEntry;
31  import org.apache.directory.server.schema.bootstrap.Schema;
32  import org.apache.directory.server.schema.registries.Registries;
33  import org.apache.directory.shared.ldap.constants.SchemaConstants;
34  import org.apache.directory.shared.ldap.entry.EntryAttribute;
35  import org.apache.directory.shared.ldap.schema.AttributeType;
36  import org.apache.directory.shared.ldap.schema.DITContentRule;
37  import org.apache.directory.shared.ldap.schema.DITStructureRule;
38  import org.apache.directory.shared.ldap.schema.MatchingRule;
39  import org.apache.directory.shared.ldap.schema.MatchingRuleUse;
40  import org.apache.directory.shared.ldap.schema.NameForm;
41  import org.apache.directory.shared.ldap.schema.Normalizer;
42  import org.apache.directory.shared.ldap.schema.ObjectClass;
43  import org.apache.directory.shared.ldap.schema.SchemaObject;
44  import org.apache.directory.shared.ldap.schema.Syntax;
45  import org.apache.directory.shared.ldap.schema.syntax.SyntaxChecker;
46  import org.apache.directory.shared.ldap.util.DateUtils;
47  
48  
49  /**
50   * A factory that generates an entry using the meta schema for schema 
51   * elements.
52   *
53   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
54   * @version $Rev$
55   */
56  public class AttributesFactory
57  {
58      public ServerEntry getAttributes( SchemaObject obj, Schema schema, Registries registries ) throws NamingException
59      {
60          if ( obj instanceof Syntax )
61          {
62              return getAttributes( ( Syntax ) obj, schema, registries );
63          }
64          else if ( obj instanceof MatchingRule )
65          {
66              return getAttributes( ( MatchingRule ) obj, schema, registries );
67          }
68          else if ( obj instanceof AttributeType )
69          {
70              return getAttributes( ( AttributeType ) obj, schema, registries );
71          }
72          else if ( obj instanceof ObjectClass )
73          {
74              return getAttributes( ( ObjectClass ) obj, schema, registries );
75          }
76          else if ( obj instanceof MatchingRuleUse )
77          {
78              return getAttributes( ( MatchingRuleUse ) obj, schema, registries );
79          }
80          else if ( obj instanceof DITStructureRule )
81          {
82              return getAttributes( ( DITStructureRule ) obj, schema, registries );
83          }
84          else if ( obj instanceof DITContentRule )
85          {
86              return getAttributes( ( DITContentRule ) obj, schema, registries );
87          }
88          else if ( obj instanceof NameForm )
89          {
90              return getAttributes( ( NameForm ) obj, schema, registries );
91          }
92          
93          throw new IllegalArgumentException( "Unknown SchemaObject type: " + obj.getClass() );
94      }
95      
96      
97      public ServerEntry getAttributes( Schema schema, Registries registries ) throws NamingException
98      {
99          ServerEntry entry = new DefaultServerEntry( registries );
100 
101         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SCHEMA_OC );
102         entry.put( SchemaConstants.CN_AT, schema.getSchemaName() );
103         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
104         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
105         
106         if ( schema.isDisabled() )
107         {
108             entry.put( MetaSchemaConstants.M_DISABLED_AT, "TRUE" );
109         }
110         
111         String[] dependencies = schema.getDependencies();
112         
113         if ( dependencies != null && dependencies.length > 0 )
114         {
115             EntryAttribute attr = new DefaultServerAttribute( registries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_DEPENDENCIES_AT ) );
116             
117             for ( String dependency:dependencies )
118             {
119                 attr.add( dependency );
120             }
121             
122             entry.put( attr );
123         }
124         
125         return entry;
126     }
127     
128     
129     public ServerEntry getAttributes( SyntaxChecker syntaxChecker, Schema schema, Registries registries )
130     {
131         ServerEntry entry = new DefaultServerEntry( registries );
132 
133         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_CHECKER_OC );
134         entry.put( MetaSchemaConstants.M_OID_AT, syntaxChecker.getSyntaxOid() );
135         entry.put( MetaSchemaConstants.M_FQCN_AT, syntaxChecker.getClass().getName() );
136         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
137         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
138         
139         return entry;
140     }
141 
142     
143     public ServerEntry getAttributes( Syntax syntax, Schema schema, Registries registries ) throws NamingException
144     {
145         ServerEntry entry = new DefaultServerEntry( registries );
146 
147         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_SYNTAX_OC );
148         entry.put( MetaSchemaConstants.X_HUMAN_READABLE_AT, getBoolean( syntax.isHumanReadable() ) );
149         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
150         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
151         injectCommon( syntax, entry, registries );
152         
153         return entry;
154     }
155 
156     
157     public ServerEntry getAttributes( String oid, Normalizer normalizer, Schema schema, Registries registries )
158     {
159         ServerEntry entry = new DefaultServerEntry( registries );
160 
161         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_NORMALIZER_OC );
162         entry.put( MetaSchemaConstants.M_OID_AT, oid );
163         entry.put( MetaSchemaConstants.M_FQCN_AT, normalizer.getClass().getName() );
164         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
165         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
166         return entry;
167     }
168 
169     
170     public ServerEntry getAttributes( String oid, Comparator comparator, Schema schema, Registries registries )
171     {
172         ServerEntry entry = new DefaultServerEntry( registries );
173 
174         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_COMPARATOR_OC );
175         entry.put( MetaSchemaConstants.M_OID_AT, oid );
176         entry.put( MetaSchemaConstants.M_FQCN_AT, comparator.getClass().getName() );
177         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
178         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
179         return entry;
180     }
181 
182 
183     /**
184      * 
185      * @param matchingRule
186      * @return Attributes
187      * @throws NamingException
188      */
189     public ServerEntry getAttributes( MatchingRule matchingRule, Schema schema, Registries registries ) throws NamingException
190     {
191         ServerEntry entry = new DefaultServerEntry( registries );
192 
193         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_MATCHING_RULE_OC );
194         entry.put( MetaSchemaConstants.M_SYNTAX_AT, matchingRule.getSyntax().getOid() );
195         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
196         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
197         injectCommon( matchingRule, entry, registries );
198         return entry;
199     }
200 
201     
202     public ServerEntry getAttributes( MatchingRuleUse matchingRuleUse, Schema schema, Registries registries )
203     {
204         ServerEntry entry = new DefaultServerEntry( registries );
205 
206         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
207         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
208         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
209         return entry;
210     }
211 
212     
213     public ServerEntry getAttributes( DITStructureRule dITStructureRule, Schema schema, Registries registries )
214     {
215         ServerEntry entry = new DefaultServerEntry( registries );
216 
217         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
218         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
219         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
220         return entry;
221     }
222 
223     
224     public ServerEntry getAttributes( DITContentRule dITContentRule, Schema schema, Registries registries )
225     {
226         ServerEntry entry = new DefaultServerEntry( registries );
227 
228         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
229         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
230         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
231         return entry;
232     }
233 
234     
235     public ServerEntry getAttributes( NameForm nameForm, Schema schema, Registries registries )
236     {
237         ServerEntry entry = new DefaultServerEntry( registries );
238 
239         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, "" );
240         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
241         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
242         return entry;
243     }
244 
245 
246     /**
247      * <pre>
248      *    objectclass ( 1.3.6.1.4.1.18060.0.4.0.3.3
249      *       NAME 'metaAttributeType'
250      *       DESC 'meta definition of the AttributeType object'
251      *       SUP metaTop
252      *       STRUCTURAL
253      *       MUST ( m-name $ m-syntax )
254      *       MAY ( m-supAttributeType $ m-obsolete $ m-equality $ m-ordering $ 
255      *             m-substr $ m-singleValue $ m-collective $ m-noUserModification $ 
256      *             m-usage $ m-extensionAttributeType )
257      *    )
258      * </pre>
259      * 
260      * @param attributeType
261      * @return Attributes
262      * @throws NamingException
263      */
264     public ServerEntry getAttributes( AttributeType attributeType, Schema schema, Registries registries ) throws NamingException
265     {
266         ServerEntry entry = new DefaultServerEntry( registries );
267 
268         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_ATTRIBUTE_TYPE_OC );
269         entry.put( MetaSchemaConstants.M_SYNTAX_AT, attributeType.getSyntax().getOid() );
270         entry.put( MetaSchemaConstants.M_COLLECTIVE_AT, getBoolean( attributeType.isCollective() ) );
271         entry.put( MetaSchemaConstants.M_NO_USER_MODIFICATION_AT, getBoolean( ! attributeType.isCanUserModify() ) );
272         entry.put( MetaSchemaConstants.M_SINGLE_VALUE_AT, getBoolean( attributeType.isSingleValue() ) );
273         entry.put( MetaSchemaConstants.M_USAGE_AT, attributeType.getUsage().toString() );
274         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
275         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
276 
277         injectCommon( attributeType, entry, registries );
278         
279         AttributeType superior = attributeType.getSuperior();
280         
281         if ( superior != null )
282         {
283             // use name if we can for clarity
284             String sup = superior.getName();
285             
286             if ( sup == null )
287             {
288                 sup = superior.getOid();
289             }
290             
291             entry.put( MetaSchemaConstants.M_SUP_ATTRIBUTE_TYPE_AT, sup );
292         }
293         
294         if ( attributeType.getEquality() != null )
295         {
296             String equality = attributeType.getEquality().getName();
297             
298             if ( equality == null )
299             {
300                 equality = attributeType.getEquality().getOid();
301             }
302             
303             entry.put( MetaSchemaConstants.M_EQUALITY_AT, equality );
304         }
305 
306         if ( attributeType.getSubstr() != null )
307         {
308             String substr = attributeType.getSubstr().getName();
309             
310             if ( substr == null )
311             {
312                 substr = attributeType.getSubstr().getOid();
313             }
314             
315             entry.put( MetaSchemaConstants.M_SUBSTR_AT, substr );
316         }
317 
318         if ( attributeType.getOrdering() != null )
319         {
320             String ordering = attributeType.getOrdering().getName();
321             
322             if ( ordering == null )
323             {
324                 ordering = attributeType.getOrdering().getOid();
325             }
326             
327             entry.put( MetaSchemaConstants.M_ORDERING_AT, ordering );
328         }
329 
330         return entry;
331     }
332 
333     
334     /**
335      * Creates the attributes of an entry representing an objectClass.
336      * 
337      * <pre>
338      *  objectclass ( 1.3.6.1.4.1.18060.0.4.0.3.2
339      *      NAME 'metaObjectClass'
340      *      DESC 'meta definition of the objectclass object'
341      *      SUP metaTop
342      *      STRUCTURAL
343      *      MUST m-oid
344      *      MAY ( m-name $ m-obsolete $ m-supObjectClass $ m-typeObjectClass $ m-must $ 
345      *            m-may $ m-extensionObjectClass )
346      *  )
347      * </pre>
348      * 
349      * @param objectClass the objectClass to produce a meta schema entry for
350      * @return the attributes of the metaSchema entry representing the objectClass
351      * @throws NamingException if there are any problems
352      */
353     public ServerEntry getAttributes( ObjectClass objectClass, Schema schema, Registries registries ) throws NamingException
354     {
355         ServerEntry entry = new DefaultServerEntry( registries );
356 
357         entry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC, MetaSchemaConstants.META_OBJECT_CLASS_OC );
358         entry.put( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT, objectClass.getType().toString() );
359         entry.put( SchemaConstants.CREATORS_NAME_AT, schema.getOwner() );
360         entry.put( SchemaConstants.CREATE_TIMESTAMP_AT, DateUtils.getGeneralizedTime() );
361         
362         injectCommon( objectClass, entry, registries );
363 
364         // handle the superior objectClasses 
365         if ( objectClass.getSuperClasses() != null && objectClass.getSuperClasses().length != 0 )
366         {
367             EntryAttribute attr = new DefaultServerAttribute( registries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT ) );
368             ObjectClass[] superClasses = objectClass.getSuperClasses();
369             
370             for ( ObjectClass superClass:superClasses )
371             {
372                 attr.add( getNameOrNumericoid( superClass ) ); 
373             }
374             
375             entry.put( attr );
376         }
377 
378         // add the must list
379         if ( objectClass.getMustList() != null && objectClass.getMustList().length != 0 )
380         {
381             EntryAttribute attr = new DefaultServerAttribute( registries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_MUST_AT ) );
382             AttributeType[] mustList = objectClass.getMustList();
383 
384             for ( AttributeType attributeType:mustList )
385             {
386                 attr.add( getNameOrNumericoid( attributeType ) );
387             }
388             
389             entry.put( attr );
390         }
391         
392         // add the may list
393         if ( objectClass.getMayList() != null && objectClass.getMayList().length != 0 )
394         {
395             EntryAttribute attr = new DefaultServerAttribute( registries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_MAY_AT ) );
396             AttributeType[] mayList = objectClass.getMayList();
397 
398             for ( AttributeType attributeType:mayList )
399             {
400                 attr.add( getNameOrNumericoid( attributeType ) );
401             }
402             
403             entry.put( attr );
404         }
405         
406         return entry;
407     }
408 
409     
410     private final String getNameOrNumericoid( SchemaObject object )
411     {
412         // first try to use user friendly name if we can
413         if ( object.getName() != null )
414         {
415             return object.getName();
416         }
417         
418         return object.getOid();
419     }
420     
421     
422     private final void injectCommon( SchemaObject object, ServerEntry entry, Registries registries ) throws NamingException
423     {
424         injectNames( object.getNamesRef(), entry, registries );
425         entry.put( MetaSchemaConstants.M_OBSOLETE_AT, getBoolean( object.isObsolete() ) );
426         entry.put( MetaSchemaConstants.M_OID_AT, object.getOid() );
427         
428         if ( object.getDescription() != null )
429         {
430             entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, object.getDescription() );
431         }
432     }
433     
434     
435     private final void injectNames( String[] names, ServerEntry entry, Registries registries ) throws NamingException
436     {
437         if ( names == null || names.length == 0 )
438         {
439             return;
440         }
441         
442         EntryAttribute attr = new DefaultServerAttribute( registries.getAttributeTypeRegistry().lookup( MetaSchemaConstants.M_NAME_AT ) );
443 
444         for ( String name:names )
445         {
446             attr.add( name );
447         }
448         
449         entry.put( attr );
450     }
451 
452     
453     private final String getBoolean( boolean value )
454     {
455         if ( value ) 
456         {
457             return "TRUE";
458         }
459         else
460         {
461             return "FALSE";
462         }
463     }
464 }