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