1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.registries;
21
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.naming.NamingException;
31 import javax.naming.directory.NoSuchAttributeException;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.apache.directory.shared.asn1.primitives.OID;
36 import org.apache.directory.shared.ldap.util.StringTools;
37
38
39
40
41
42
43
44
45
46 public class DefaultOidRegistry implements OidRegistry
47 {
48
49 private static final Logger LOG = LoggerFactory.getLogger( DefaultOidRegistry.class );
50
51
52 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
53
54
55 private Map<String, List<String>> byOid = new HashMap<String, List<String>>();
56
57
58 private Map<String,String> byName = new HashMap<String,String>();
59
60
61
62
63
64 public String getOid( String name ) throws NamingException
65 {
66 if ( StringTools.isEmpty( name ) )
67 {
68 throw new NamingException( "name should not be empty" );
69 }
70
71
72
73
74
75 if ( StringTools.isDigit( name.charAt( 0 ) ) )
76 {
77 return name;
78 }
79
80
81 if ( byName.containsKey( name ) )
82 {
83 String oid = byName.get( name );
84
85 if ( IS_DEBUG )
86 {
87 LOG.debug( "looked up OID '" + oid + "' with id '" + name + "'" );
88 }
89
90 return oid;
91 }
92
93
94
95
96
97
98
99
100 String lowerCase = name.trim().toLowerCase();
101
102 String oid = byName.get( lowerCase );
103
104 if ( oid != null )
105 {
106 if ( IS_DEBUG )
107 {
108 LOG.debug( "looked up OID '" + oid + "' with id '" + name + "'" );
109 }
110
111 return oid;
112 }
113
114 NamingException fault = new NoSuchAttributeException( "OID for name '" + name + "' was not "
115 + "found within the OID registry" );
116 LOG.error( fault.getMessage() );
117 throw fault;
118 }
119
120
121
122
123
124 public boolean hasOid( String name )
125 {
126 if ( StringTools.isEmpty( name ) )
127 {
128 return false;
129 }
130
131 String normalized = name.trim().toLowerCase();
132
133 return byName.containsKey( normalized );
134 }
135
136
137
138
139
140 public String getPrimaryName( String oid ) throws NamingException
141 {
142 List<String> value = byOid.get( oid );
143
144 if ( null == value )
145 {
146 throw new NamingException( "OID '" + oid + "' was not found within the OID registry" );
147 }
148
149 String name = value.get( 0 );
150
151 if ( IS_DEBUG )
152 {
153 LOG.debug( "looked up primary name '" + name + "' with OID '" + oid + "'" );
154 }
155
156 return name;
157 }
158
159
160
161
162
163 public List<String> getNameSet( String oid ) throws NamingException
164 {
165 List<String> value = byOid.get( oid );
166
167 if ( null == value )
168 {
169 throw new NamingException( "OID '" + oid + "' was not found within the OID registry" );
170 }
171
172 if ( IS_DEBUG )
173 {
174 LOG.debug( "looked up names '" + value + "' for OID '" + oid + "'" );
175 }
176
177 return value;
178 }
179
180
181
182
183
184 @SuppressWarnings("unchecked")
185 public Iterator list()
186 {
187 return Collections.unmodifiableSet( byOid.keySet() ).iterator();
188 }
189
190
191
192
193
194
195 public Map<String, String> getOidByName()
196 {
197 return byName;
198 }
199
200
201
202
203
204
205 public Map<String, List<String>> getNameByOid()
206 {
207 return byOid;
208 }
209
210
211
212
213
214 @SuppressWarnings("unchecked")
215 public void register( String name, String oid ) throws NamingException
216 {
217 if ( !OID.isOID( oid ) )
218 {
219 String message = "Swap the parameter order: the oid " +
220 "does not start with a digit, or is not an OID!";
221
222 LOG.debug( message );
223 throw new NamingException( message );
224 }
225
226 if ( StringTools.isEmpty( name ) )
227 {
228 String message = "The name is empty";
229 LOG.error( message );
230 throw new NamingException( message );
231 }
232
233
234
235
236
237 String lowerCase = name.trim().toLowerCase();
238
239
240 byName.put( lowerCase, oid );
241 byName.put( oid, oid );
242
243
244
245
246
247
248
249
250
251
252
253 List<String> value;
254
255 if ( !byOid.containsKey( oid ) )
256 {
257 value = new ArrayList<String>( 1 );
258 value.add( lowerCase );
259 }
260 else
261 {
262 value = byOid.get( oid );
263
264 if ( value.contains( lowerCase ) )
265 {
266 return;
267 }
268 else
269 {
270 value.add( lowerCase );
271 }
272 }
273
274 byOid.put( oid, value );
275
276 if ( IS_DEBUG )
277 {
278 LOG.debug( "registed name '" + name + "' with OID: " + oid );
279 }
280 }
281
282
283 public void unregister( String numericOid ) throws NamingException
284 {
285
286 List<String> names = byOid.remove( numericOid );
287
288
289 if ( names != null )
290 {
291 for ( String name:names )
292 {
293 byName.remove( name );
294 }
295 }
296
297
298 byName.remove( numericOid );
299 }
300 }