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