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