1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }