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 org.apache.ldap.common.schema.MatchingRule;
21 import org.apache.ldap.server.schema.MatchingRuleRegistry;
22 import org.apache.ldap.server.schema.MatchingRuleRegistryMonitor;
23 import org.apache.ldap.server.schema.MatchingRuleRegistryMonitorAdapter;
24 import org.apache.ldap.server.schema.OidRegistry;
25
26 import javax.naming.NamingException;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30
31
32 /***
33 * A MatchingRuleRegistry service used to lookup matching rules by OID.
34 *
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev: 159259 $
37 */
38 public class BootstrapMatchingRuleRegistry implements MatchingRuleRegistry
39 {
40 /*** a map using an OID for the key and a MatchingRule for the value */
41 private final Map byOid;
42 /*** maps an OID to a schema name*/
43 private final Map oidToSchema;
44 /*** the registry used to resolve names to OIDs */
45 private final OidRegistry oidRegistry;
46 /*** a monitor used to track noteable registry events */
47 private MatchingRuleRegistryMonitor monitor = null;
48
49
50
51
52
53
54
55 /***
56 * Creates a BootstrapMatchingRuleRegistry using existing MatchingRulees
57 * for lookups.
58 *
59 */
60 public BootstrapMatchingRuleRegistry( OidRegistry oidRegistry )
61 {
62 this.oidToSchema = new HashMap();
63 this.oidRegistry = oidRegistry;
64 this.byOid = new HashMap();
65 this.monitor = new MatchingRuleRegistryMonitorAdapter();
66 }
67
68
69
70
71
72
73
74 /***
75 * @see org.apache.ldap.server.schema.MatchingRuleRegistry#lookup(String)
76 */
77 public MatchingRule lookup( String id ) throws NamingException
78 {
79 id = oidRegistry.getOid( id );
80
81 if ( byOid.containsKey( id ) )
82 {
83 MatchingRule MatchingRule = ( MatchingRule ) byOid.get( id );
84 monitor.lookedUp( MatchingRule );
85 return MatchingRule;
86 }
87
88 NamingException fault = new NamingException( "Unknown MatchingRule OID " + id );
89 monitor.lookupFailed( id, fault );
90 throw fault;
91 }
92
93
94 /***
95 * @see MatchingRuleRegistry#register(String, MatchingRule)
96 */
97 public void register( String schema, MatchingRule matchingRule ) throws NamingException
98 {
99 if ( byOid.containsKey( matchingRule.getOid() ) )
100 {
101 NamingException e = new NamingException( "matchingRule w/ OID " +
102 matchingRule.getOid() + " has already been registered!" );
103 monitor.registerFailed( matchingRule, e );
104 throw e;
105 }
106
107 oidToSchema.put( matchingRule.getOid(), schema );
108
109 String[] names = matchingRule.getNames();
110 for ( int ii = 0; ii < names.length; ii++ )
111 {
112 oidRegistry.register( names[ii], matchingRule.getOid() );
113 }
114
115 byOid.put( matchingRule.getOid(), matchingRule );
116 monitor.registered( matchingRule );
117 }
118
119
120 /***
121 * @see org.apache.ldap.server.schema.MatchingRuleRegistry#hasMatchingRule(String)
122 */
123 public boolean hasMatchingRule( String id )
124 {
125 if ( oidRegistry.hasOid( id ) )
126 {
127 try
128 {
129 return byOid.containsKey( oidRegistry.getOid( id ) );
130 }
131 catch ( NamingException e )
132 {
133 return false;
134 }
135 }
136
137 return false;
138 }
139
140
141 public String getSchemaName( String id ) throws NamingException
142 {
143 id = oidRegistry.getOid( id );
144 if ( oidToSchema.containsKey( id ) )
145 {
146 return ( String ) oidToSchema.get( id );
147 }
148
149 throw new NamingException( "OID " + id + " not found in oid to " +
150 "schema name map!" );
151 }
152
153
154
155
156
157
158
159 /***
160 * Gets the monitor for this registry.
161 *
162 * @return the monitor
163 */
164 MatchingRuleRegistryMonitor getMonitor()
165 {
166 return monitor;
167 }
168
169
170 /***
171 * Sets the monitor for this registry.
172 *
173 * @param monitor the monitor to set
174 */
175 void setMonitor( MatchingRuleRegistryMonitor monitor )
176 {
177 this.monitor = monitor;
178 }
179
180
181 public Iterator list()
182 {
183 return byOid.values().iterator();
184 }
185 }