1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.schema.registries;
21
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.lang3.ArrayUtils;
30 import org.apache.directory.api.asn1.util.Oid;
31 import org.apache.directory.api.i18n.I18n;
32 import org.apache.directory.api.ldap.model.exception.LdapException;
33 import org.apache.directory.api.ldap.model.schema.SchemaErrorHandler;
34 import org.apache.directory.api.ldap.model.schema.SchemaObject;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46
47 public class OidRegistry<T extends SchemaObject> implements Iterable<T>
48 {
49
50 private static final Logger LOG = LoggerFactory.getLogger( OidRegistry.class );
51
52
53 private Map<String, T> byOid = new HashMap<>();
54
55
56 private boolean isRelaxed = Registries.STRICT;
57
58 private SchemaErrorHandler errorHandler;
59
60
61
62
63
64
65
66 public boolean contains( String oid )
67 {
68 return byOid.containsKey( oid );
69 }
70
71
72
73
74
75
76
77
78
79
80 public String getPrimaryName( String oid ) throws LdapException
81 {
82 SchemaObject schemaObject = byOid.get( oid );
83
84 if ( schemaObject != null )
85 {
86 return schemaObject.getName();
87 }
88 else
89 {
90 String msg = I18n.err( I18n.ERR_13741_OID_NOT_FOUND_IN_REGISTRY, oid );
91 LdapException error = new LdapException( msg );
92 errorHandler.handle( LOG, msg, error );
93 throw error;
94 }
95 }
96
97
98
99
100
101
102
103
104
105 public T getSchemaObject( String oid ) throws LdapException
106 {
107 T schemaObject = byOid.get( oid );
108
109 if ( schemaObject != null )
110 {
111 return schemaObject;
112 }
113 else
114 {
115 String msg = I18n.err( I18n.ERR_13742_NO_SCHEMA_OBJECT_WITH_OID, oid );
116 LdapException error = new LdapException( msg );
117 errorHandler.handle( LOG, msg, error );
118 throw error;
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public List<String> getNameSet( String oid ) throws LdapException
136 {
137 SchemaObject schemaObject = byOid.get( oid );
138
139 if ( null == schemaObject )
140 {
141 String msg = I18n.err( I18n.ERR_13741_OID_NOT_FOUND_IN_REGISTRY, oid );
142 LdapException error = new LdapException( msg );
143 errorHandler.handle( LOG, msg, error );
144 throw error;
145 }
146
147 List<String> names = schemaObject.getNames();
148
149 if ( LOG.isDebugEnabled() )
150 {
151 LOG.debug( I18n.msg( I18n.MSG_13756_LOOKED_UP_NAME, ArrayUtils.toString( names ), oid ) );
152 }
153
154 return names;
155 }
156
157
158
159
160
161
162
163 public Iterator<String> iteratorOids()
164 {
165 return Collections.unmodifiableSet( byOid.keySet() ).iterator();
166 }
167
168
169
170
171
172
173
174 @Override
175 public Iterator<T> iterator()
176 {
177 return byOid.values().iterator();
178 }
179
180
181
182
183
184
185
186
187 public boolean isRelaxed()
188 {
189 return isRelaxed;
190 }
191
192
193
194
195
196
197
198 public boolean isStrict()
199 {
200 return !isRelaxed;
201 }
202
203
204
205
206
207
208 public void setRelaxed()
209 {
210 isRelaxed = Registries.RELAXED;
211 }
212
213
214
215
216
217
218 public void setStrict()
219 {
220 isRelaxed = Registries.STRICT;
221 }
222
223
224 public SchemaErrorHandler getErrorHandler()
225 {
226 return errorHandler;
227 }
228
229
230 public void setErrorHandler( SchemaErrorHandler errorHandler )
231 {
232 this.errorHandler = errorHandler;
233 }
234
235
236
237
238
239
240
241
242 public void register( T schemaObject ) throws LdapException
243 {
244 if ( schemaObject == null )
245 {
246 String message = I18n.err( I18n.ERR_13743_CANNOT_REGISTER_NULL_SCHEMA_OBJECT );
247
248 if ( LOG.isDebugEnabled() )
249 {
250 LOG.debug( message );
251 }
252
253 throw new LdapException( message );
254 }
255
256 String oid = schemaObject.getOid();
257
258 if ( isStrict() )
259 {
260 if ( !Oid.isOid( oid ) )
261 {
262 String message = I18n.err( I18n.ERR_13744_SCHEMA_OBJECT_HAS_NO_VALID_OID );
263
264 if ( LOG.isDebugEnabled() )
265 {
266 LOG.debug( message );
267 }
268
269 throw new LdapException( message );
270 }
271 }
272 else
273 {
274 if ( ( oid == null ) || oid.isEmpty() )
275 {
276 throw new LdapException( I18n.err( I18n.ERR_00003_INVALID_OID, "" ) );
277 }
278 }
279
280
281
282
283 if ( byOid.containsKey( oid ) )
284 {
285 errorHandler.handle( LOG, I18n.err( I18n.ERR_13745_SCHEMA_OBJECT_WITH_OID_ALREADY_EXIST, oid ), null );
286 }
287 else
288 {
289 byOid.put( oid, schemaObject );
290
291 if ( LOG.isDebugEnabled() )
292 {
293 LOG.debug( I18n.msg( I18n.MSG_13742_REGISTERED_SCHEMA_OBJECT, schemaObject, oid ) );
294 }
295 }
296 }
297
298
299
300
301
302
303
304
305
306 void put( T schemaObject )
307 {
308 byOid.put( schemaObject.getOid(), schemaObject );
309 }
310
311
312
313
314
315
316
317
318 public void unregister( String oid ) throws LdapException
319 {
320
321 SchemaObject removed = byOid.remove( oid );
322
323 if ( LOG.isDebugEnabled() )
324 {
325 LOG.debug( I18n.msg( I18n.MSG_13736_UNREGISTERED_SCHEMA_OBJECT, removed, oid ) );
326 }
327 }
328
329
330
331
332
333
334
335 public OidRegistry<T> copy()
336 {
337 OidRegistry<T> copy = new OidRegistry<>();
338
339
340 copy.byOid = new HashMap<>();
341
342 return copy;
343 }
344
345
346
347
348
349 public int size()
350 {
351 return byOid.size();
352 }
353
354
355
356
357
358 public void clear()
359 {
360
361 byOid.clear();
362 }
363
364
365
366
367
368 @Override
369 public String toString()
370 {
371 StringBuilder sb = new StringBuilder();
372
373 if ( byOid != null )
374 {
375 boolean isFirst = true;
376
377 for ( Map.Entry<String, T> entry : byOid.entrySet() )
378 {
379 if ( isFirst )
380 {
381 isFirst = false;
382 }
383 else
384 {
385 sb.append( ", " );
386 }
387
388 sb.append( "<" );
389
390 SchemaObject schemaObject = entry.getValue();
391
392 if ( schemaObject != null )
393 {
394 sb.append( schemaObject.getObjectType() );
395 sb.append( ", " );
396 sb.append( schemaObject.getOid() );
397 sb.append( ", " );
398 sb.append( schemaObject.getName() );
399 }
400
401 sb.append( ">" );
402 }
403 }
404
405 return sb.toString();
406 }
407 }