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