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.core.schema;
21  
22  
23  import javax.naming.NamingException;
24  
25  import org.apache.directory.server.schema.registries.Registries;
26  import org.apache.directory.shared.ldap.exception.LdapNamingException;
27  import org.apache.directory.shared.ldap.message.ResultCodeEnum;
28  import org.apache.directory.shared.ldap.schema.AbstractAttributeType;
29  import org.apache.directory.shared.ldap.schema.AttributeType;
30  import org.apache.directory.shared.ldap.schema.MatchingRule;
31  import org.apache.directory.shared.ldap.schema.MutableSchemaObject;
32  import org.apache.directory.shared.ldap.schema.Syntax;
33  import org.apache.directory.shared.ldap.schema.UsageEnum;
34  
35  
36  /**
37   * An AttributeType implementation.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev$, $Date$
41   */
42  class AttributeTypeImpl extends AbstractAttributeType implements MutableSchemaObject
43  {
44      private static final long serialVersionUID = 1L;
45  
46      private final Registries registries;
47      
48      /** The syntax OID associated with this AttributeType */
49      private String syntaxOid;
50      
51      /** The syntax associated with the syntaxID */
52      private Syntax syntax;
53      
54      /** The equality OID associated with this AttributeType */
55      private String equalityOid;
56  
57      /** The equality MatchingRule associated with the equalityID */
58      private MatchingRule equalityMR;
59      
60      /** The substring OID associated with this AttributeType */
61      private String substrOid;
62  
63      /** The substring MatchingRule associated with the substringID */
64      private MatchingRule substringMR;
65      
66      /** The ordering OID associated with this AttributeType */
67      private String orderingOid;
68      
69      /** The ordering MatchingRule associated with the orderingID */
70      private MatchingRule orderingMR;
71      
72      private String superiorOid;
73      
74      
75      public AttributeTypeImpl( String oid, Registries registries )
76      {
77          super( oid );
78          this.registries = registries;
79      }
80  
81  
82      /* (non-Javadoc)
83       * @see org.apache.directory.shared.ldap.schema.AttributeType#getEquality()
84       */
85      public MatchingRule getEquality() throws NamingException
86      {
87          if ( equalityMR == null )
88          {
89              if ( equalityOid == null )
90              {
91                  equalityMR = findEquality( getSuperior() );
92              }
93              else
94              {
95                  equalityMR = registries.getMatchingRuleRegistry().lookup( equalityOid );
96              }
97          }
98          
99          return equalityMR;
100     }
101 
102 
103     /**
104      * Recursively the equality matchingRule if one exists within the attribute heirarchy.
105      * 
106      * @param at the attribute to find a equality matchingRule for
107      * @return the equality MatchingRule or null if none exists for the attributeType
108      * @throws NamingException if there are problems accessing the attribute heirarchy
109      */
110     private MatchingRule findEquality( AttributeType at ) throws NamingException
111     {
112         if ( at == null )
113         {
114             return null;
115         }
116         
117         MatchingRule mr = at.getEquality();
118         
119         if ( mr == null )
120         {
121             return findEquality( at.getSuperior() );
122         }
123         else
124         {
125             return mr;
126         }
127     }
128 
129 
130     /* (non-Javadoc)
131      * @see org.apache.directory.shared.ldap.schema.AttributeType#getOrdering()
132      */
133     public MatchingRule getOrdering() throws NamingException
134     {
135         if ( orderingMR == null )
136         {
137             if ( orderingOid == null )
138             {
139                 orderingMR = findOrdering( getSuperior() );
140             }
141             else
142             {
143                 orderingMR = registries.getMatchingRuleRegistry().lookup( orderingOid );
144             }
145         }
146         
147         return orderingMR;
148     }
149 
150 
151     /**
152      * Recursively the ordering matchingRule if one exists within the attribute heirarchy.
153      * 
154      * @param at the attribute to find a ordering matchingRule for
155      * @return the ordering MatchingRule or null if none exists for the attributeType
156      * @throws NamingException if there are problems accessing the attribute heirarchy
157      */
158     private MatchingRule findOrdering( AttributeType at ) throws NamingException
159     {
160         if ( at == null )
161         {
162             return null;
163         }
164         
165         MatchingRule mr = at.getOrdering();
166         if ( mr == null )
167         {
168             return findOrdering( at.getSuperior() );
169         }
170         else
171         {
172             return mr;
173         }
174     }
175 
176 
177     /* (non-Javadoc)
178      * @see org.apache.directory.shared.ldap.schema.AttributeType#getSubstr()
179      */
180     public MatchingRule getSubstr() throws NamingException
181     {
182         if ( substringMR == null )
183         {
184             if ( substrOid == null )
185             {
186                 substringMR = findSubstr( getSuperior() );
187             }
188             else
189             {
190                 substringMR = registries.getMatchingRuleRegistry().lookup( substrOid );
191             }
192         }
193         
194         return substringMR;
195     }
196 
197 
198     /**
199      * Recursively gets the substring matchingRule if one exists within the attribute heirarchy.
200      * 
201      * @param at the attribute to find a substring matchingRule for
202      * @return the substring MatchingRule or null if none exists for the attributeType
203      * @throws NamingException if there are problems accessing the attribute heirarchy
204      */
205     private MatchingRule findSubstr( AttributeType at ) throws NamingException
206     {
207         if ( at == null )
208         {
209             return null;
210         }
211         
212         MatchingRule mr = at.getSubstr();
213         if ( mr == null )
214         {
215             return findSubstr( at.getSuperior() );
216         }
217         else
218         {
219             return mr;
220         }
221     }
222 
223 
224     /* (non-Javadoc)
225      * @see org.apache.directory.shared.ldap.schema.AttributeType#getSuperior()
226      */
227     public AttributeType getSuperior() throws NamingException
228     {
229         if ( superiorOid == null )
230         {
231             return null;
232         }
233         
234         return registries.getAttributeTypeRegistry().lookup( superiorOid );
235     }
236 
237 
238     /* (non-Javadoc)
239      * @see org.apache.directory.shared.ldap.schema.AttributeType#getSyntax()
240      */
241     public Syntax getSyntax() throws NamingException
242     {
243         if ( syntax == null )
244         {
245             if ( syntaxOid == null )
246             {
247                 syntax = findSyntax( getSuperior() );
248             }
249             else
250             {
251                 syntax = registries.getSyntaxRegistry().lookup( syntaxOid );
252             }
253         }
254         
255         return syntax;
256     }
257     
258     
259     /**
260      * Recursively walks up the ancestors to find the syntax for an attributeType.
261      * 
262      * @param at the attributeType to get the syntax for
263      * @return the Syntax for the attributeType
264      * @throws NamingException if no syntax can be found for the attributeType
265      */
266     private Syntax findSyntax( AttributeType at ) throws NamingException
267     {
268         if ( at == null )
269         {
270             throw new LdapNamingException( "Cannot find syntax for attributeType " + getName() 
271                 + " after walking ancestors.", ResultCodeEnum.OTHER );
272         }
273         
274         if ( at.getSyntax() != null )
275         {
276             return at.getSyntax();
277         }
278         
279         return findSyntax( at.getSuperior() );
280     }
281     
282 
283     public void setSyntaxOid( String syntaxOid )
284     {
285         this.syntaxOid = syntaxOid;
286     }
287     
288     
289     public void setSchema( String schema )
290     {
291         super.setSchema( schema );
292     }
293     
294     
295     public void setSuperiorOid( String superiorOid )
296     {
297         this.superiorOid = superiorOid;
298     }
299 
300     
301     public void setEqualityOid( String equalityOid )
302     {
303         this.equalityOid = equalityOid;
304     }
305 
306     
307     public void setSubstrOid( String substrOid )
308     {
309         this.substrOid = substrOid;
310     }
311     
312     
313     public void setOrderingOid( String orderingOid )
314     {
315         this.orderingOid = orderingOid;
316     }
317     
318     
319     public void setDescription( String description )
320     {
321         super.setDescription( description );
322     }
323     
324     
325     public void setNames( String[] names )
326     {
327         super.setNames( names );
328     }
329     
330     
331     public void setObsolete( boolean obsolete )
332     {
333         super.setObsolete( obsolete );
334     }
335     
336     
337     public void setCollective( boolean collective )
338     {
339         super.setCollective( collective );
340     }
341     
342     
343     public void setCanUserModify( boolean canUserModify )
344     {
345         super.setCanUserModify( canUserModify );
346     }
347     
348     
349     public void setLength( int length )
350     {
351         super.setLength( length );
352     }
353     
354     
355     public void setSingleValue( boolean singleValue )
356     {
357         super.setSingleValue( singleValue );
358     }
359     
360     
361     public void setUsage( UsageEnum usage )
362     {
363         super.setUsage( usage );
364     }
365 }