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