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.Map;
22  
23  import javax.naming.NamingException;
24  
25  import org.apache.ldap.common.schema.Normalizer;
26  import org.apache.ldap.server.schema.NormalizerRegistry;
27  import org.apache.ldap.server.schema.NormalizerRegistryMonitor;
28  import org.apache.ldap.server.schema.NormalizerRegistryMonitorAdapter;
29  
30  
31  /***
32   * The POJO implementation for the NormalizerRegistry service.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 264732 $
36   */
37  public class BootstrapNormalizerRegistry implements NormalizerRegistry
38  {
39      /*** a map of Normalizers looked up by OID */
40      private final Map byOid;
41      /*** maps an OID to a schema name*/
42      private final Map oidToSchema;
43      /*** the monitor used to deliver callback notification events */
44      private NormalizerRegistryMonitor monitor;
45  
46  
47      // ------------------------------------------------------------------------
48      // C O N S T R U C T O R S
49      // ------------------------------------------------------------------------
50  
51  
52      /***
53       * Creates a default normalizer registry.
54       */
55      public BootstrapNormalizerRegistry()
56      {
57          this.byOid = new HashMap();
58          this.oidToSchema = new HashMap();
59          this.monitor = new NormalizerRegistryMonitorAdapter();
60      }
61  
62  
63      /***
64       * Sets the monitor used to deliver notification events to via callbacks.
65       *
66       * @param monitor the monitor to recieve callback events
67       */
68      public void setMonitor( NormalizerRegistryMonitor monitor )
69      {
70          this.monitor = monitor;
71      }
72  
73  
74      // ------------------------------------------------------------------------
75      // Service Methods
76      // ------------------------------------------------------------------------
77  
78  
79      public void register( String schema, String oid, Normalizer normalizer )
80          throws NamingException
81      {
82          if ( byOid.containsKey( oid ) )
83          {
84              NamingException e = new NamingException( "Normalizer already " +
85                  "registered for OID " + oid );
86              monitor.registerFailed( oid, normalizer, e );
87              throw e;
88          }
89  
90          oidToSchema.put( oid, schema );
91          byOid.put( oid, normalizer );
92          monitor.registered( oid, normalizer );
93      }
94  
95  
96      public Normalizer lookup( String oid ) throws NamingException
97      {
98          if ( ! byOid.containsKey( oid ) )
99          {
100             NamingException e = new NamingException( "Normalizer for OID "
101                 + oid + " does not exist!" );
102             monitor.lookupFailed( oid, e );
103             throw e;
104         }
105 
106         Normalizer normalizer = ( Normalizer ) byOid.get( oid );
107         monitor.lookedUp( oid, normalizer );
108         return normalizer;
109     }
110 
111 
112     public boolean hasNormalizer( String oid )
113     {
114         return byOid.containsKey( oid );
115     }
116 
117 
118     public String getSchemaName( String oid ) throws NamingException
119     {
120         if ( Character.isDigit( oid.charAt( 0 ) ) )
121         {
122             throw new NamingException( "Looks like the arg is not a numeric OID" );
123         }
124 
125         if ( oidToSchema.containsKey( oid ) )
126         {
127             return ( String ) oidToSchema.get( oid );
128         }
129 
130         throw new NamingException( "OID " + oid + " not found in oid to " +
131             "schema name map!" );
132     }
133 }