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;
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.common.util.JoinIterator;
28  import org.apache.ldap.server.schema.bootstrap.BootstrapAttributeTypeRegistry;
29  
30  
31  /***
32   * A plain old java object implementation of an AttributeTypeRegistry.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 264732 $
36   */
37  public class GlobalAttributeTypeRegistry implements AttributeTypeRegistry
38  {
39      /*** maps an OID to an AttributeType */
40      private final Map byOid;
41      /*** maps an OID to a schema name*/
42      private final Map oidToSchema;
43      /*** the registry used to resolve names to OIDs */
44      private final OidRegistry oidRegistry;
45      /*** monitor notified via callback events */
46      private AttributeTypeRegistryMonitor monitor;
47      /*** the underlying bootstrap registry to delegate on misses to */
48      private BootstrapAttributeTypeRegistry bootstrap;
49  
50  
51      // ------------------------------------------------------------------------
52      // C O N S T R U C T O R S
53      // ------------------------------------------------------------------------
54  
55  
56      /***
57       * Creates a GlobalAttributeTypeRegistry which accesses data stored within
58       * the system partition and within the bootstrapping registry to service
59       * AttributeType lookup requests.
60       *
61       * @param bootstrap the bootstrapping registry to delegate to
62       */
63      public GlobalAttributeTypeRegistry( BootstrapAttributeTypeRegistry bootstrap, OidRegistry oidRegistry )
64      {
65          this.byOid = new HashMap();
66          this.oidToSchema = new HashMap();
67          this.monitor = new AttributeTypeRegistryMonitorAdapter();
68  
69          this.oidRegistry = oidRegistry;
70          if ( this.oidRegistry == null )
71          {
72              throw new NullPointerException( "the OID registry cannot be null" ) ;
73          }
74  
75          this.bootstrap = bootstrap;
76          if ( this.bootstrap == null )
77          {
78              throw new NullPointerException( "the bootstrap registry cannot be null" ) ;
79          }
80      }
81  
82  
83      /***
84       * Sets the monitor that is to be notified via callback events.
85       *
86       * @param monitor the new monitor to notify of notable events
87       */
88      public void setMonitor( AttributeTypeRegistryMonitor monitor )
89      {
90          this.monitor = monitor;
91      }
92  
93  
94      // ------------------------------------------------------------------------
95      // Service Methods
96      // ------------------------------------------------------------------------
97  
98  
99      public void register( String schema, AttributeType attributeType ) throws NamingException
100     {
101         if ( byOid.containsKey( attributeType.getOid() ) ||
102              bootstrap.hasAttributeType( attributeType.getOid() ) )
103         {
104             NamingException e = new NamingException( "attributeType w/ OID " +
105                 attributeType.getOid() + " has already been registered!" );
106             monitor.registerFailed( attributeType, e );
107             throw e;
108         }
109 
110         String[] names = attributeType.getNames();
111         for ( int ii = 0; ii < names.length; ii++ )
112         {
113             oidRegistry.register( names[ii], attributeType.getOid() );
114         }
115 
116         oidToSchema.put( attributeType.getOid(), schema );
117         byOid.put( attributeType.getOid(), attributeType );
118         monitor.registered( attributeType );
119     }
120 
121 
122     public AttributeType lookup( String id ) throws NamingException
123     {
124         id = oidRegistry.getOid( id );
125 
126         if ( ! ( byOid.containsKey( id ) || bootstrap.hasAttributeType( id ) ) )
127         {
128             NamingException e = new NamingException( "attributeType w/ OID "
129                 + id + " not registered!" );
130             monitor.lookupFailed( id, e );
131             throw e;
132         }
133 
134         AttributeType attributeType = ( AttributeType ) byOid.get( id );
135 
136         if ( attributeType == null )
137         {
138             attributeType = bootstrap.lookup( id );
139         }
140 
141         monitor.lookedUp( attributeType );
142         return attributeType;
143     }
144 
145 
146     public boolean hasAttributeType( String id )
147     {
148         try
149         {
150 
151             if ( oidRegistry.hasOid( id ) )
152             {
153                 return byOid.containsKey( oidRegistry.getOid( id ) ) ||
154                        bootstrap.hasAttributeType( id );
155             }
156         }
157         catch ( NamingException e )
158         {
159             return false;
160         }
161 
162         return false;
163     }
164 
165 
166     public String getSchemaName( String id ) throws NamingException
167     {
168         id = oidRegistry.getOid( id );
169 
170         if ( oidToSchema.containsKey( id ) )
171         {
172             return ( String ) oidToSchema.get( id );
173         }
174 
175         if ( bootstrap.getSchemaName( id ) != null )
176         {
177             return bootstrap.getSchemaName( id );
178         }
179 
180         throw new NamingException( "OID " + id + " not found in oid to " +
181             "schema name map!" );
182     }
183 
184 
185     public Iterator list()
186     {
187         return new JoinIterator( new Iterator[]
188             { byOid.values().iterator(),bootstrap.list() } );
189     }
190 }