KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > schema > GlobalComparatorRegistry


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 JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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: 169198 $
34  */

35 public class GlobalComparatorRegistry implements ComparatorRegistry
36 {
37     /** the comparators in this registry */
38     private final Map JavaDoc comparators;
39     /** maps an OID to a schema name*/
40     private final Map JavaDoc 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 JavaDoc();
62         this.comparators = new HashMap JavaDoc();
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 JavaDoc( "the bootstrap registry cannot be null" ) ;
72         }
73
74         this.systemPartition = systemPartition;
75         if ( this.systemPartition == null )
76         {
77             throw new NullPointerException JavaDoc( "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 JavaDoc schema, String JavaDoc oid, Comparator JavaDoc comparator )
99             throws NamingException JavaDoc
100     {
101         if ( comparators.containsKey( oid ) || bootstrap.hasComparator( oid ) )
102         {
103             NamingException JavaDoc e = new NamingException JavaDoc( "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 JavaDoc lookup( String JavaDoc oid ) throws NamingException JavaDoc
116     {
117         Comparator JavaDoc c;
118         NamingException JavaDoc e;
119
120         if ( comparators.containsKey( oid ) )
121         {
122             c = ( Comparator JavaDoc ) 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 JavaDoc( "Comparator not found for OID: " + oid );
135         monitor.lookupFailed( oid, e );
136         throw e;
137     }
138
139
140     public boolean hasComparator( String JavaDoc oid )
141     {
142         return comparators.containsKey( oid ) || bootstrap.hasComparator( oid );
143     }
144
145
146     public String JavaDoc getSchemaName( String JavaDoc oid ) throws NamingException JavaDoc
147     {
148         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
149         {
150             throw new NamingException JavaDoc( "OID " + oid + " is not a numeric OID" );
151         }
152         
153         if ( oidToSchema.containsKey( oid ) )
154         {
155             return ( String JavaDoc ) oidToSchema.get( oid );
156         }
157
158         if ( bootstrap.hasComparator( oid ) )
159         {
160             return bootstrap.getSchemaName( oid );
161         }
162
163         throw new NamingException JavaDoc( "OID " + oid + " not found in oid to " +
164             "schema name map!" );
165     }
166 }
167
Popular Tags