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.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import org.apache.directory.api.asn1.util.Oid;
28 import org.apache.directory.api.i18n.I18n;
29 import org.apache.directory.api.ldap.model.exception.LdapException;
30 import org.apache.directory.api.ldap.model.exception.LdapSchemaException;
31 import org.apache.directory.api.ldap.model.exception.LdapSchemaExceptionCodes;
32 import org.apache.directory.api.ldap.model.schema.LoadableSchemaObject;
33 import org.apache.directory.api.ldap.model.schema.SchemaObject;
34 import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
35 import org.apache.directory.api.util.Strings;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45 public abstract class DefaultSchemaObjectRegistry<T extends SchemaObject> implements SchemaObjectRegistry<T>,
46 Iterable<T>
47 {
48
49 private static final Logger LOG = LoggerFactory.getLogger( DefaultSchemaObjectRegistry.class );
50
51
52 private static final boolean DEBUG = LOG.isDebugEnabled();
53
54
55 protected Map<String, T> byName;
56
57
58 protected SchemaObjectType schemaObjectType;
59
60
61 protected OidRegistry<T> oidRegistry;
62
63
64
65
66
67 protected DefaultSchemaObjectRegistry( SchemaObjectType schemaObjectType, OidRegistry<T> oidRegistry )
68 {
69 byName = new HashMap<String, T>();
70 this.schemaObjectType = schemaObjectType;
71 this.oidRegistry = oidRegistry;
72 }
73
74
75
76
77
78 public boolean contains( String oid )
79 {
80 if ( !byName.containsKey( oid ) )
81 {
82 return byName.containsKey( Strings.toLowerCase( oid ) );
83 }
84
85 return true;
86 }
87
88
89
90
91
92 public String getSchemaName( String oid ) throws LdapException
93 {
94 if ( !Oid.isOid( oid ) )
95 {
96 String msg = I18n.err( I18n.ERR_04267 );
97 LOG.warn( msg );
98 throw new LdapException( msg );
99 }
100
101 SchemaObject schemaObject = byName.get( oid );
102
103 if ( schemaObject != null )
104 {
105 return schemaObject.getSchemaName();
106 }
107
108 String msg = I18n.err( I18n.ERR_04268_OID_NOT_FOUND, oid );
109 LOG.warn( msg );
110 throw new LdapException( msg );
111 }
112
113
114
115
116
117 public void renameSchema( String originalSchemaName, String newSchemaName )
118 {
119
120
121 for ( T schemaObject : this )
122 {
123 if ( originalSchemaName.equalsIgnoreCase( schemaObject.getSchemaName() ) )
124 {
125 schemaObject.setSchemaName( newSchemaName );
126
127 if ( DEBUG )
128 {
129 LOG.debug( "Renamed {} schemaName to {}", schemaObject, newSchemaName );
130 }
131 }
132 }
133 }
134
135
136
137
138
139 public Iterator<T> iterator()
140 {
141 return oidRegistry.iterator();
142 }
143
144
145
146
147
148 public Iterator<String> oidsIterator()
149 {
150 return byName.keySet().iterator();
151 }
152
153
154
155
156
157 public T lookup( String oid ) throws LdapException
158 {
159 if ( oid == null )
160 {
161 return null;
162 }
163
164 T schemaObject = byName.get( oid );
165
166 if ( schemaObject == null )
167 {
168
169 schemaObject = byName.get( Strings.trim( Strings.toLowerCase( oid ) ) );
170 }
171
172 if ( schemaObject == null )
173 {
174 String msg = I18n.err( I18n.ERR_04269, schemaObjectType.name(), oid );
175 LOG.debug( msg );
176 throw new LdapException( msg );
177 }
178
179 if ( DEBUG )
180 {
181 LOG.debug( "Found {} with oid: {}", schemaObject, oid );
182 }
183
184 return schemaObject;
185 }
186
187
188
189
190
191 public void register( T schemaObject ) throws LdapException
192 {
193 String oid = schemaObject.getOid();
194
195 if ( byName.containsKey( oid ) )
196 {
197 String msg = I18n.err( I18n.ERR_04270, schemaObjectType.name(), oid );
198 LOG.warn( msg );
199 LdapSchemaException ldapSchemaException = new LdapSchemaException(
200 LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, msg );
201 ldapSchemaException.setSourceObject( schemaObject );
202 throw ldapSchemaException;
203 }
204
205 byName.put( oid, schemaObject );
206
207
208
209
210
211 for ( String name : schemaObject.getNames() )
212 {
213 String lowerName = Strings.trim( Strings.toLowerCase( name ) );
214
215 if ( byName.containsKey( lowerName ) )
216 {
217 String msg = I18n.err( I18n.ERR_04271, schemaObjectType.name(), name );
218 LOG.warn( msg );
219 LdapSchemaException ldapSchemaException = new LdapSchemaException(
220 LdapSchemaExceptionCodes.NAME_ALREADY_REGISTERED, msg );
221 ldapSchemaException.setSourceObject( schemaObject );
222 throw ldapSchemaException;
223 }
224 else
225 {
226 byName.put( lowerName, schemaObject );
227 }
228 }
229
230
231 oidRegistry.register( schemaObject );
232
233 if ( LOG.isDebugEnabled() )
234 {
235 LOG.debug( "registered " + schemaObject.getName() + " for OID {}", oid );
236 }
237 }
238
239
240
241
242
243 public T unregister( String numericOid ) throws LdapException
244 {
245 if ( !Oid.isOid( numericOid ) )
246 {
247 String msg = I18n.err( I18n.ERR_04272, numericOid );
248 LOG.error( msg );
249 throw new LdapException( msg );
250 }
251
252 T schemaObject = byName.remove( numericOid );
253
254 for ( String name : schemaObject.getNames() )
255 {
256 byName.remove( name );
257 }
258
259
260 oidRegistry.unregister( numericOid );
261
262 if ( DEBUG )
263 {
264 LOG.debug( "Removed {} with oid {} from the registry", schemaObject, numericOid );
265 }
266
267 return schemaObject;
268 }
269
270
271
272
273
274 public T unregister( T schemaObject ) throws LdapException
275 {
276 String oid = schemaObject.getOid();
277
278 if ( !byName.containsKey( oid ) )
279 {
280 String msg = I18n.err( I18n.ERR_04273, schemaObjectType.name(), oid );
281 LOG.warn( msg );
282 throw new LdapException( msg );
283 }
284
285
286 T removed = byName.remove( oid );
287
288
289
290
291
292 for ( String name : schemaObject.getNames() )
293 {
294 byName.remove( Strings.trim( Strings.toLowerCase( name ) ) );
295 }
296
297
298 oidRegistry.unregister( oid );
299
300 return removed;
301 }
302
303
304
305
306
307 public void unregisterSchemaElements( String schemaName ) throws LdapException
308 {
309 if ( schemaName == null )
310 {
311 return;
312 }
313
314
315
316 for ( T schemaObject : this )
317 {
318 if ( schemaName.equalsIgnoreCase( schemaObject.getSchemaName() ) )
319 {
320 String oid = schemaObject.getOid();
321 SchemaObject removed = unregister( oid );
322
323 if ( DEBUG )
324 {
325 LOG.debug( "Removed {} with oid {} from the registry", removed, oid );
326 }
327 }
328 }
329 }
330
331
332
333
334
335 public String getOidByName( String name ) throws LdapException
336 {
337 T schemaObject = byName.get( name );
338
339 if ( schemaObject == null )
340 {
341
342 String lowerCased = Strings.toLowerCase( name );
343
344 schemaObject = byName.get( lowerCased );
345
346
347 if ( schemaObject == null )
348 {
349 throw new LdapException( I18n.err( I18n.ERR_04274, name ) );
350 }
351 }
352
353
354 return schemaObject.getOid();
355 }
356
357
358
359
360
361
362 @SuppressWarnings("unchecked")
363 public SchemaObjectRegistry<T> copy( SchemaObjectRegistry<T> original )
364 {
365
366 for ( String key : ( ( DefaultSchemaObjectRegistry<T> ) original ).byName.keySet() )
367 {
368
369 T value = ( ( DefaultSchemaObjectRegistry<T> ) original ).byName.get( key );
370
371 if ( value instanceof LoadableSchemaObject )
372 {
373
374
375
376 byName.put( key, value );
377
378
379 oidRegistry.put( value );
380 }
381 else
382 {
383 T copiedValue = null;
384
385
386 if ( oidRegistry.contains( value.getOid() ) )
387 {
388 try
389 {
390 copiedValue = oidRegistry.getSchemaObject( value.getOid() );
391 }
392 catch ( LdapException ne )
393 {
394
395 }
396 }
397 else
398 {
399 copiedValue = ( T ) value.copy();
400 }
401
402
403 byName.put( key, copiedValue );
404
405
406 oidRegistry.put( copiedValue );
407 }
408 }
409
410 return this;
411 }
412
413
414
415
416
417 public T get( String oid )
418 {
419 try
420 {
421 return oidRegistry.getSchemaObject( oid );
422 }
423 catch ( LdapException ne )
424 {
425 return null;
426 }
427 }
428
429
430
431
432
433 public SchemaObjectType getType()
434 {
435 return schemaObjectType;
436 }
437
438
439
440
441
442 public int size()
443 {
444 return oidRegistry.size();
445 }
446
447
448
449
450
451 public String toString()
452 {
453 StringBuilder sb = new StringBuilder();
454
455 sb.append( schemaObjectType ).append( ": " );
456 boolean isFirst = true;
457
458 for ( String name : byName.keySet() )
459 {
460 if ( isFirst )
461 {
462 isFirst = false;
463 }
464 else
465 {
466 sb.append( ", " );
467 }
468
469 T schemaObject = byName.get( name );
470
471 sb.append( '<' ).append( name ).append( ", " ).append( schemaObject.getOid() ).append( '>' );
472 }
473
474 return sb.toString();
475 }
476
477
478
479
480
481 public void clear()
482 {
483
484 for ( SchemaObject schemaObject : oidRegistry )
485 {
486
487 if ( !( schemaObject instanceof LoadableSchemaObject ) )
488 {
489 schemaObject.clear();
490 }
491 }
492
493
494 byName.clear();
495
496
497 oidRegistry.clear();
498 }
499 }