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