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.bootstrap;
18  
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.naming.NamingException;
25  
26  import org.apache.ldap.common.schema.NameForm;
27  import org.apache.ldap.server.schema.NameFormRegistry;
28  import org.apache.ldap.server.schema.NameFormRegistryMonitor;
29  import org.apache.ldap.server.schema.NameFormRegistryMonitorAdapter;
30  import org.apache.ldap.server.schema.OidRegistry;
31  
32  
33  /***
34   * A plain old java object implementation of an NameFormRegistry.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 264732 $
38   */
39  public class BootstrapNameFormRegistry implements NameFormRegistry
40  {
41      /*** maps an OID to an NameForm */
42      private final Map byOid;
43      /*** maps an OID to a schema name*/
44      private final Map oidToSchema;
45      /*** the registry used to resolve names to OIDs */
46      private final OidRegistry oidRegistry;
47      /*** monitor notified via callback events */
48      private NameFormRegistryMonitor monitor;
49  
50  
51      // ------------------------------------------------------------------------
52      // C O N S T R U C T O R S
53      // ------------------------------------------------------------------------
54  
55  
56      /***
57       * Creates an empty BootstrapNameFormRegistry.
58       */
59      public BootstrapNameFormRegistry( OidRegistry oidRegistry )
60      {
61          this.byOid = new HashMap();
62          this.oidToSchema = new HashMap();
63          this.oidRegistry = oidRegistry;
64          this.monitor = new NameFormRegistryMonitorAdapter();
65      }
66  
67  
68      /***
69       * Sets the monitor that is to be notified via callback events.
70       *
71       * @param monitor the new monitor to notify of notable events
72       */
73      public void setMonitor( NameFormRegistryMonitor monitor )
74      {
75          this.monitor = monitor;
76      }
77  
78  
79      // ------------------------------------------------------------------------
80      // Service Methods
81      // ------------------------------------------------------------------------
82  
83  
84      public void register( String schema, NameForm nameForm ) throws NamingException
85      {
86          if ( byOid.containsKey( nameForm.getOid() ) )
87          {
88              NamingException e = new NamingException( "nameForm w/ OID " +
89                  nameForm.getOid() + " has already been registered!" );
90              monitor.registerFailed( nameForm, e );
91              throw e;
92          }
93  
94          oidToSchema.put( nameForm.getOid(), schema );
95          oidRegistry.register( nameForm.getName(), nameForm.getOid() );
96          byOid.put( nameForm.getOid(), nameForm );
97          monitor.registered( nameForm );
98      }
99  
100 
101     public NameForm lookup( String id ) throws NamingException
102     {
103         id = oidRegistry.getOid( id );
104 
105         if ( ! byOid.containsKey( id ) )
106         {
107             NamingException e = new NamingException( "nameForm w/ OID " + id
108                 + " not registered!" );
109             monitor.lookupFailed( id, e );
110             throw e;
111         }
112 
113         NameForm nameForm = ( NameForm ) byOid.get( id );
114         monitor.lookedUp( nameForm );
115         return nameForm;
116     }
117 
118 
119     public boolean hasNameForm( String id )
120     {
121         if ( oidRegistry.hasOid( id ) )
122         {
123             try
124             {
125                 return byOid.containsKey( oidRegistry.getOid( id ) );
126             }
127             catch ( NamingException e )
128             {
129                 return false;
130             }
131         }
132 
133         return false;
134     }
135 
136 
137     public String getSchemaName( String id ) throws NamingException
138     {
139         id = oidRegistry.getOid( id );
140         if ( oidToSchema.containsKey( id ) )
141         {
142             return ( String ) oidToSchema.get( id );
143         }
144 
145         throw new NamingException( "OID " + id + " not found in oid to " +
146             "schema name map!" );
147     }
148 
149 
150     public Iterator list()
151     {
152         return byOid.values().iterator();
153     }
154 }