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.lang.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.SchemaObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43
44
45
46 public class OidRegistry<T extends SchemaObject> implements Iterable<T>
47 {
48
49 private static final Logger LOG = LoggerFactory.getLogger( OidRegistry.class );
50
51
52 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
53
54
55 private Map<String, T> byOid = new HashMap<>();
56
57
58 private boolean isRelaxed = Registries.STRICT;
59
60
61
62
63
64
65
66
67 public boolean contains( String oid )
68 {
69 return byOid.containsKey( oid );
70 }
71
72
73
74
75
76
77
78
79
80
81 public String getPrimaryName( String oid ) throws LdapException
82 {
83 SchemaObject schemaObject = byOid.get( oid );
84
85 if ( schemaObject != null )
86 {
87 return schemaObject.getName();
88 }
89 else
90 {
91 String msg = I18n.err( I18n.ERR_04286, oid );
92 LOG.error( msg );
93 throw new LdapException( msg );
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_04287, oid );
116 LOG.error( msg );
117 throw new LdapException( msg );
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public List<String> getNameSet( String oid ) throws LdapException
135 {
136 SchemaObject schemaObject = byOid.get( oid );
137
138 if ( null == schemaObject )
139 {
140 String msg = I18n.err( I18n.ERR_04288, oid );
141 LOG.error( msg );
142 throw new LdapException( msg );
143 }
144
145 List<String> names = schemaObject.getNames();
146
147 if ( IS_DEBUG )
148 {
149 LOG.debug( "looked up names '{}' for OID '{}'", ArrayUtils.toString( names ), oid );
150 }
151
152 return names;
153 }
154
155
156
157
158
159
160
161 public Iterator<String> iteratorOids()
162 {
163 return Collections.unmodifiableSet( byOid.keySet() ).iterator();
164 }
165
166
167
168
169
170
171
172 @Override
173 public Iterator<T> iterator()
174 {
175 return byOid.values().iterator();
176 }
177
178
179
180
181
182
183
184
185 public boolean isRelaxed()
186 {
187 return isRelaxed;
188 }
189
190
191
192
193
194
195
196 public boolean isStrict()
197 {
198 return !isRelaxed;
199 }
200
201
202
203
204
205
206 public void setRelaxed()
207 {
208 isRelaxed = Registries.RELAXED;
209 }
210
211
212
213
214
215
216 public void setStrict()
217 {
218 isRelaxed = Registries.STRICT;
219 }
220
221
222
223
224
225
226
227 public void register( T schemaObject ) throws LdapException
228 {
229 if ( schemaObject == null )
230 {
231 String message = I18n.err( I18n.ERR_04289 );
232
233 LOG.debug( message );
234 throw new LdapException( message );
235 }
236
237 String oid = schemaObject.getOid();
238
239 if ( isStrict() )
240 {
241 if ( !Oid.isOid( oid ) )
242 {
243 String message = I18n.err( I18n.ERR_04290 );
244
245 LOG.debug( message );
246 throw new LdapException( message );
247 }
248 }
249 else
250 {
251 if ( ( oid == null ) || oid.isEmpty() )
252 {
253 throw new LdapException( I18n.err( I18n.ERR_00033_INVALID_OID, "" ) );
254 }
255 }
256
257
258
259
260 if ( byOid.containsKey( oid ) )
261 {
262 String message = I18n.err( I18n.ERR_04291, oid );
263 LOG.info( message );
264 return;
265 }
266 else
267 {
268 byOid.put( oid, schemaObject );
269
270 if ( IS_DEBUG )
271 {
272 LOG.debug( "registed SchemaObject '" + schemaObject + "' with OID: " + oid );
273 }
274 }
275 }
276
277
278
279
280
281
282
283
284
285 void put( T schemaObject )
286 {
287 byOid.put( schemaObject.getOid(), schemaObject );
288 }
289
290
291
292
293
294
295
296
297 public void unregister( String oid ) throws LdapException
298 {
299
300 SchemaObject removed = byOid.remove( oid );
301
302 if ( IS_DEBUG )
303 {
304 LOG.debug( "Unregisted SchemaObject '{}' with OID: {}", removed, oid );
305 }
306 }
307
308
309
310
311
312
313
314 public OidRegistry<T> copy()
315 {
316 OidRegistry<T> copy = new OidRegistry<>();
317
318
319 copy.byOid = new HashMap<>();
320
321 return copy;
322 }
323
324
325
326
327
328 public int size()
329 {
330 return byOid.size();
331 }
332
333
334
335
336
337 public void clear()
338 {
339
340 byOid.clear();
341 }
342
343
344
345
346
347 @Override
348 public String toString()
349 {
350 StringBuilder sb = new StringBuilder();
351
352 if ( byOid != null )
353 {
354 boolean isFirst = true;
355
356 for ( Map.Entry<String, T> entry : byOid.entrySet() )
357 {
358 if ( isFirst )
359 {
360 isFirst = false;
361 }
362 else
363 {
364 sb.append( ", " );
365 }
366
367 sb.append( "<" );
368
369 SchemaObject schemaObject = entry.getValue();
370
371 if ( schemaObject != null )
372 {
373 sb.append( schemaObject.getObjectType() );
374 sb.append( ", " );
375 sb.append( schemaObject.getOid() );
376 sb.append( ", " );
377 sb.append( schemaObject.getName() );
378 }
379
380 sb.append( ">" );
381 }
382 }
383
384 return sb.toString();
385 }
386 }