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.AttributeType;
27  import org.apache.ldap.server.schema.AttributeTypeRegistry;
28  import org.apache.ldap.server.schema.AttributeTypeRegistryMonitor;
29  import org.apache.ldap.server.schema.AttributeTypeRegistryMonitorAdapter;
30  import org.apache.ldap.server.schema.OidRegistry;
31  
32  
33  /***
34   * A plain old java object implementation of an AttributeTypeRegistry.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 264732 $
38   */
39  public class BootstrapAttributeTypeRegistry implements AttributeTypeRegistry
40  {
41      /*** maps an OID to an AttributeType */
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 AttributeTypeRegistryMonitor 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 BootstrapAttributeTypeRegistry.
58       */
59      public BootstrapAttributeTypeRegistry( OidRegistry oidRegistry )
60      {
61          this.byOid = new HashMap();
62          this.oidToSchema = new HashMap();
63          this.oidRegistry = oidRegistry;
64          this.monitor = new AttributeTypeRegistryMonitorAdapter();
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( AttributeTypeRegistryMonitor monitor )
74      {
75          this.monitor = monitor;
76      }
77  
78  
79      // ------------------------------------------------------------------------
80      // Service Methods
81      // ------------------------------------------------------------------------
82  
83  
84      public void register( String schema, AttributeType attributeType ) throws NamingException
85      {
86          if ( byOid.containsKey( attributeType.getOid() ) )
87          {
88              NamingException e = new NamingException( "attributeType w/ OID " +
89                  attributeType.getOid() + " has already been registered!" );
90              monitor.registerFailed( attributeType, e );
91              throw e;
92          }
93  
94          String[] names = attributeType.getNames();
95          for ( int ii = 0; ii < names.length; ii++ )
96          {
97              oidRegistry.register( names[ii], attributeType.getOid() );
98          }
99  
100         oidToSchema.put( attributeType.getOid(), schema );
101         byOid.put( attributeType.getOid(), attributeType );
102         monitor.registered( attributeType );
103     }
104 
105 
106     public AttributeType lookup( String id ) throws NamingException
107     {
108         id = oidRegistry.getOid( id );
109 
110         if ( ! byOid.containsKey( id ) )
111         {
112             NamingException e = new NamingException( "attributeType w/ OID "
113                 + id + " not registered!" );
114             monitor.lookupFailed( id, e );
115             throw e;
116         }
117 
118         AttributeType attributeType = ( AttributeType ) byOid.get( id );
119         monitor.lookedUp( attributeType );
120         return attributeType;
121     }
122 
123 
124     public boolean hasAttributeType( String id )
125     {
126         if ( oidRegistry.hasOid( id ) )
127         {
128             try
129             {
130                 return byOid.containsKey( oidRegistry.getOid( id ) );
131             }
132             catch ( NamingException e )
133             {
134                 return false;
135             }
136         }
137 
138         return false;
139     }
140 
141 
142     public String getSchemaName( String id ) throws NamingException
143     {
144         id = oidRegistry.getOid( id );
145         if ( oidToSchema.containsKey( id ) )
146         {
147             return ( String ) oidToSchema.get( id );
148         }
149 
150         throw new NamingException( "OID " + id + " not found in oid to " +
151             "schema name map!" );
152     }
153 
154 
155     public Iterator list()
156     {
157         return byOid.values().iterator();
158     }
159 }