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.server.schema.ComparatorRegistry;
21  import org.apache.ldap.server.schema.ComparatorRegistryMonitor;
22  import org.apache.ldap.server.schema.ComparatorRegistryMonitorAdapter;
23  import org.apache.ldap.server.schema.SerializableComparator;
24  
25  import javax.naming.NamingException;
26  import java.util.Comparator;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  
31  /***
32   * A simple POJO implementation of the ComparatorRegistry service interface.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 159259 $
36   */
37  public class BootstrapComparatorRegistry implements ComparatorRegistry
38  {
39      /*** the comparators in this registry */
40      private final Map comparators;
41      /*** maps an OID to a schema name*/
42      private final Map oidToSchema;
43      /*** the monitor for delivering callback events */
44      private ComparatorRegistryMonitor 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 ComparatorRegistry by initializing the map and the
54       * montior.
55       */
56      public BootstrapComparatorRegistry()
57      {
58          this.oidToSchema = new HashMap();
59          this.comparators = new HashMap();
60          this.monitor = new ComparatorRegistryMonitorAdapter();
61  
62          SerializableComparator.setRegistry( this );
63      }
64  
65  
66      /***
67       * Sets the monitor used by this registry.
68       *
69       * @param monitor the monitor to set for registry event callbacks
70       */
71      public void setMonitor( ComparatorRegistryMonitor monitor )
72      {
73          this.monitor = monitor;
74      }
75  
76  
77      // ------------------------------------------------------------------------
78      // Service Methods
79      // ------------------------------------------------------------------------
80  
81  
82      public void register( String schema, String oid, Comparator comparator ) throws NamingException
83      {
84          if ( comparators.containsKey( oid ) )
85          {
86              NamingException e = new NamingException( "Comparator with OID "
87                  + oid + " already registered!" );
88              monitor.registerFailed( oid, comparator, e );
89              throw e;
90          }
91  
92          oidToSchema.put( oid, schema );
93          comparators.put( oid, comparator );
94          monitor.registered( oid, comparator );
95      }
96  
97  
98      public Comparator lookup( String oid ) throws NamingException
99      {
100         if ( comparators.containsKey( oid ) )
101         {
102             Comparator c = ( Comparator ) comparators.get( oid );
103             monitor.lookedUp( oid, c );
104             return c;
105         }
106 
107 
108         NamingException e = new NamingException( "Comparator not found for OID: " + oid );
109         monitor.lookupFailed( oid, e );
110         throw e;
111     }
112 
113 
114     public boolean hasComparator( String oid )
115     {
116         return comparators.containsKey( oid );
117     }
118 
119 
120     public String getSchemaName( String oid ) throws NamingException
121     {
122         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
123         {
124             throw new NamingException( "OID " + oid + " is not a numeric OID" );
125         }
126         
127         if ( oidToSchema.containsKey( oid ) )
128         {
129             return ( String ) oidToSchema.get( oid );
130         }
131 
132         throw new NamingException( "OID " + oid + " not found in oid to " +
133             "schema name map!" );
134     }
135 }