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 org.apache.ldap.common.schema.Normalizer;
21  import org.apache.ldap.server.SystemPartition;
22  import org.apache.ldap.server.schema.bootstrap.BootstrapNormalizerRegistry;
23  
24  import javax.naming.NamingException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  
29  /***
30   * A simple POJO implementation of the NormalizerRegistry service interface.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 159259 $
34   */
35  public class GlobalNormalizerRegistry implements NormalizerRegistry
36  {
37      /*** the normalizers in this registry */
38      private final Map normalizers;
39      /*** maps an OID to a schema name*/
40      private final Map oidToSchema;
41      /*** the monitor for delivering callback events */
42      private NormalizerRegistryMonitor monitor;
43      /*** the underlying bootstrap registry to delegate on misses to */
44      private BootstrapNormalizerRegistry bootstrap;
45      /*** the system partition where we keep attributeType updates */
46      private SystemPartition systemPartition;
47  
48  
49      // ------------------------------------------------------------------------
50      // C O N S T R U C T O R S
51      // ------------------------------------------------------------------------
52  
53  
54      /***
55       * Creates a default NormalizerRegistry by initializing the map and the
56       * montior.
57       */
58      public GlobalNormalizerRegistry( SystemPartition systemPartition,
59              BootstrapNormalizerRegistry bootstrap )
60      {
61          this.oidToSchema = new HashMap();
62          this.normalizers = new HashMap();
63          this.monitor = new NormalizerRegistryMonitorAdapter();
64  
65          this.bootstrap = bootstrap;
66          if ( this.bootstrap == null )
67          {
68              throw new NullPointerException( "the bootstrap registry cannot be null" ) ;
69          }
70  
71          this.systemPartition = systemPartition;
72          if ( this.systemPartition == null )
73          {
74              throw new NullPointerException( "the system partition cannot be null" ) ;
75          }
76      }
77  
78  
79      /***
80       * Sets the monitor used by this registry.
81       *
82       * @param monitor the monitor to set for registry event callbacks
83       */
84      public void setMonitor( NormalizerRegistryMonitor monitor )
85      {
86          this.monitor = monitor;
87      }
88  
89  
90      // ------------------------------------------------------------------------
91      // Service Methods
92      // ------------------------------------------------------------------------
93  
94  
95      public void register( String schema, String oid, Normalizer normalizer )
96              throws NamingException
97      {
98          if ( normalizers.containsKey( oid ) || bootstrap.hasNormalizer( oid ) )
99          {
100             NamingException e = new NamingException( "Normalizer with OID "
101                 + oid + " already registered!" );
102             monitor.registerFailed( oid, normalizer, e );
103             throw e;
104         }
105 
106         oidToSchema.put( oid, schema );
107         normalizers.put( oid, normalizer );
108         monitor.registered( oid, normalizer );
109     }
110 
111 
112     public Normalizer lookup( String oid ) throws NamingException
113     {
114         Normalizer c;
115         NamingException e;
116 
117         if ( normalizers.containsKey( oid ) )
118         {
119             c = ( Normalizer ) normalizers.get( oid );
120             monitor.lookedUp( oid, c );
121             return c;
122         }
123 
124         if ( bootstrap.hasNormalizer( oid ) )
125         {
126             c = bootstrap.lookup( oid );
127             monitor.lookedUp( oid, c );
128             return c;
129         }
130 
131         e = new NamingException( "Normalizer not found for OID: " + oid );
132         monitor.lookupFailed( oid, e );
133         throw e;
134     }
135 
136 
137     public boolean hasNormalizer( String oid )
138     {
139         return normalizers.containsKey( oid ) || bootstrap.hasNormalizer( oid );
140     }
141 
142 
143     public String getSchemaName( String oid ) throws NamingException
144     {
145         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
146         {
147             throw new NamingException( "OID " + oid + " is not a numeric OID" );
148         }
149         
150         if ( oidToSchema.containsKey( oid ) )
151         {
152             return ( String ) oidToSchema.get( oid );
153         }
154 
155         if ( bootstrap.hasNormalizer( oid ) )
156         {
157             return bootstrap.getSchemaName( oid );
158         }
159 
160         throw new NamingException( "OID " + oid + " not found in oid to " +
161             "schema name map!" );
162     }
163 }