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.schema.registries;
21  
22  
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import javax.naming.NamingException;
28  
29  import org.apache.directory.shared.ldap.schema.ObjectClass;
30  import org.apache.directory.shared.ldap.util.StringTools;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * A plain old java object implementation of an ObjectClassRegistry.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 664295 $
40   */
41  public class DefaultObjectClassRegistry implements ObjectClassRegistry
42  {
43      /** static class logger */
44      private static final Logger LOG = LoggerFactory.getLogger( DefaultObjectClassRegistry.class );
45      
46      /** Speedup for DEBUG mode */
47      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
48      
49      /** maps an OID to an ObjectClass */
50      private final Map<String,ObjectClass> byOid;
51      /** the registry used to resolve names to OIDs */
52      private final OidRegistry oidRegistry;
53  
54  
55      // ------------------------------------------------------------------------
56      // C O N S T R U C T O R S
57      // ------------------------------------------------------------------------
58  
59      /**
60       * Creates an empty DefaultObjectClassRegistry.
61       *
62       * @param oidRegistry used by this registry for OID to name resolution of
63       * dependencies and to automatically register and unregister it's aliases and OIDs
64       */
65      public DefaultObjectClassRegistry( OidRegistry oidRegistry )
66      {
67          this.byOid = new HashMap<String,ObjectClass>();
68          this.oidRegistry = oidRegistry;
69      }
70  
71      
72      // ------------------------------------------------------------------------
73      // Service Methods
74      // ------------------------------------------------------------------------
75  
76      
77      public void register( ObjectClass objectClass ) throws NamingException
78      {
79          if ( byOid.containsKey( objectClass.getOid() ) )
80          {
81              throw new NamingException( "objectClass w/ OID " + objectClass.getOid()
82                  + " has already been registered!" );
83          }
84  
85          if ( objectClass.getNamesRef() != null && objectClass.getNamesRef().length > 0 )
86          {
87              oidRegistry.register( objectClass.getName(), objectClass.getOid() );
88          }
89          else
90          {
91              oidRegistry.register( objectClass.getOid(), objectClass.getOid() );
92          }
93          
94          byOid.put( objectClass.getOid(), objectClass );
95          
96          if ( IS_DEBUG )
97          {
98              LOG.debug( "registered objectClass: " + objectClass );
99          }
100     }
101 
102 
103     public ObjectClass lookup( String id ) throws NamingException
104     {
105         if ( StringTools.isEmpty( id ) )
106         {
107             throw new NamingException( "name should not be empty" );
108         }
109         
110         String oid = oidRegistry.getOid( id.toLowerCase() );
111 
112         if ( !byOid.containsKey( oid ) )
113         {
114             throw new NamingException( "objectClass w/ OID " + oid + " not registered!" );
115         }
116 
117         ObjectClass objectClass = byOid.get( oid );
118         
119         if ( IS_DEBUG )
120         {
121             LOG.debug( "looked objectClass with OID '" + oid + "' and got back " + objectClass );
122         }
123         return objectClass;
124     }
125 
126 
127     public boolean hasObjectClass( String id )
128     {
129         if ( oidRegistry.hasOid( id ) )
130         {
131             try
132             {
133                 return byOid.containsKey( oidRegistry.getOid( id ) );
134             }
135             catch ( NamingException e )
136             {
137                 return false;
138             }
139         }
140 
141         return false;
142     }
143 
144 
145     public String getSchemaName( String id ) throws NamingException
146     {
147         id = oidRegistry.getOid( id );
148         ObjectClass oc = byOid.get( id );
149         if ( oc != null )
150         {
151             return oc.getSchema();
152         }
153 
154         throw new NamingException( "OID " + id + " not found in oid to " + "ObjectClass map!" );
155     }
156 
157 
158     public Iterator<ObjectClass> iterator()
159     {
160         return byOid.values().iterator();
161     }
162     
163     
164     public void unregister( String numericOid ) throws NamingException
165     {
166         if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
167         {
168             throw new NamingException( "Looks like the arg is not a numeric OID" );
169         }
170 
171         byOid.remove( numericOid );
172     }
173 }