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.Map;
22  
23  import javax.naming.NamingException;
24  
25  import org.apache.ldap.common.schema.Normalizer;
26  import org.apache.ldap.server.schema.bootstrap.BootstrapNormalizerRegistry;
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: 264732 $
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  
46  
47      // ------------------------------------------------------------------------
48      // C O N S T R U C T O R S
49      // ------------------------------------------------------------------------
50  
51  
52      /***
53       * Creates a default NormalizerRegistry by initializing the map and the
54       * montior.
55       */
56      public GlobalNormalizerRegistry( BootstrapNormalizerRegistry bootstrap )
57      {
58          this.oidToSchema = new HashMap();
59          this.normalizers = new HashMap();
60          this.monitor = new NormalizerRegistryMonitorAdapter();
61  
62          this.bootstrap = bootstrap;
63          if ( this.bootstrap == null )
64          {
65              throw new NullPointerException( "the bootstrap registry cannot be null" ) ;
66          }
67      }
68  
69  
70      /***
71       * Sets the monitor used by this registry.
72       *
73       * @param monitor the monitor to set for registry event callbacks
74       */
75      public void setMonitor( NormalizerRegistryMonitor monitor )
76      {
77          this.monitor = monitor;
78      }
79  
80  
81      // ------------------------------------------------------------------------
82      // Service Methods
83      // ------------------------------------------------------------------------
84  
85  
86      public void register( String schema, String oid, Normalizer normalizer )
87              throws NamingException
88      {
89          if ( normalizers.containsKey( oid ) || bootstrap.hasNormalizer( oid ) )
90          {
91              NamingException e = new NamingException( "Normalizer with OID "
92                  + oid + " already registered!" );
93              monitor.registerFailed( oid, normalizer, e );
94              throw e;
95          }
96  
97          oidToSchema.put( oid, schema );
98          normalizers.put( oid, normalizer );
99          monitor.registered( oid, normalizer );
100     }
101 
102 
103     public Normalizer lookup( String oid ) throws NamingException
104     {
105         Normalizer c;
106         NamingException e;
107 
108         if ( normalizers.containsKey( oid ) )
109         {
110             c = ( Normalizer ) normalizers.get( oid );
111             monitor.lookedUp( oid, c );
112             return c;
113         }
114 
115         if ( bootstrap.hasNormalizer( oid ) )
116         {
117             c = bootstrap.lookup( oid );
118             monitor.lookedUp( oid, c );
119             return c;
120         }
121 
122         e = new NamingException( "Normalizer not found for OID: " + oid );
123         monitor.lookupFailed( oid, e );
124         throw e;
125     }
126 
127 
128     public boolean hasNormalizer( String oid )
129     {
130         return normalizers.containsKey( oid ) || bootstrap.hasNormalizer( oid );
131     }
132 
133 
134     public String getSchemaName( String oid ) throws NamingException
135     {
136         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
137         {
138             throw new NamingException( "OID " + oid + " is not a numeric OID" );
139         }
140         
141         if ( oidToSchema.containsKey( oid ) )
142         {
143             return ( String ) oidToSchema.get( oid );
144         }
145 
146         if ( bootstrap.hasNormalizer( oid ) )
147         {
148             return bootstrap.getSchemaName( oid );
149         }
150 
151         throw new NamingException( "OID " + oid + " not found in oid to " +
152             "schema name map!" );
153     }
154 }