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 public class OidRegistry<T extends SchemaObject> implements Iterable<T>
45 {
46
47 private static final Logger LOG = LoggerFactory.getLogger( OidRegistry.class );
48
49
50 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
51
52
53 private Map<String, T> byOid = new HashMap<String, T>();
54
55
56
57
58
59
60
61
62 public boolean contains( String oid )
63 {
64 return byOid.containsKey( oid );
65 }
66
67
68
69
70
71
72
73
74
75
76 public String getPrimaryName( String oid ) throws LdapException
77 {
78 SchemaObject schemaObject = byOid.get( oid );
79
80 if ( schemaObject != null )
81 {
82 return schemaObject.getName();
83 }
84 else
85 {
86 String msg = I18n.err( I18n.ERR_04286, oid );
87 LOG.error( msg );
88 throw new LdapException( msg );
89 }
90 }
91
92
93
94
95
96
97
98
99
100 public T getSchemaObject( String oid ) throws LdapException
101 {
102 T schemaObject = byOid.get( oid );
103
104 if ( schemaObject != null )
105 {
106 return schemaObject;
107 }
108 else
109 {
110 String msg = I18n.err( I18n.ERR_04287, oid );
111 LOG.error( msg );
112 throw new LdapException( msg );
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public List<String> getNameSet( String oid ) throws LdapException
130 {
131 SchemaObject schemaObject = byOid.get( oid );
132
133 if ( null == schemaObject )
134 {
135 String msg = I18n.err( I18n.ERR_04288, oid );
136 LOG.error( msg );
137 throw new LdapException( msg );
138 }
139
140 List<String> names = schemaObject.getNames();
141
142 if ( IS_DEBUG )
143 {
144 LOG.debug( "looked up names '{}' for OID '{}'", ArrayUtils.toString( names ), oid );
145 }
146
147 return names;
148 }
149
150
151
152
153
154
155
156 public Iterator<String> iteratorOids()
157 {
158 return Collections.unmodifiableSet( byOid.keySet() ).iterator();
159 }
160
161
162
163
164
165
166
167 public Iterator<T> iterator()
168 {
169 return byOid.values().iterator();
170 }
171
172
173
174
175
176
177
178 public void register( T schemaObject ) throws LdapException
179 {
180 if ( schemaObject == null )
181 {
182 String message = I18n.err( I18n.ERR_04289 );
183
184 LOG.debug( message );
185 throw new LdapException( message );
186 }
187
188 String oid = schemaObject.getOid();
189
190 if ( !Oid.isOid( oid ) )
191 {
192 String message = I18n.err( I18n.ERR_04290 );
193
194 LOG.debug( message );
195 throw new LdapException( message );
196 }
197
198
199
200
201 if ( byOid.containsKey( oid ) )
202 {
203 String message = I18n.err( I18n.ERR_04291, oid );
204 LOG.info( message );
205 return;
206 }
207 else
208 {
209 byOid.put( oid, schemaObject );
210
211 if ( IS_DEBUG )
212 {
213 LOG.debug( "registed SchemaObject '" + schemaObject + "' with OID: " + oid );
214 }
215 }
216 }
217
218
219
220
221
222
223
224
225
226 void put( T schemaObject )
227 {
228 byOid.put( schemaObject.getOid(), schemaObject );
229 }
230
231
232
233
234
235
236
237
238 public void unregister( String oid ) throws LdapException
239 {
240
241 SchemaObject removed = byOid.remove( oid );
242
243 if ( IS_DEBUG )
244 {
245 LOG.debug( "Unregisted SchemaObject '{}' with OID: {}", removed, oid );
246 }
247 }
248
249
250
251
252
253
254
255 public OidRegistry<T> copy()
256 {
257 OidRegistry<T> copy = new OidRegistry<T>();
258
259
260 copy.byOid = new HashMap<String, T>();
261
262 return copy;
263 }
264
265
266
267
268
269 public int size()
270 {
271 return byOid.size();
272 }
273
274
275 public void clear()
276 {
277
278 byOid.clear();
279 }
280
281
282
283
284
285 public String toString()
286 {
287 StringBuilder sb = new StringBuilder();
288
289 if ( byOid != null )
290 {
291 boolean isFirst = true;
292
293 for ( String oid : byOid.keySet() )
294 {
295 if ( isFirst )
296 {
297 isFirst = false;
298 }
299 else
300 {
301 sb.append( ", " );
302 }
303
304 sb.append( "<" );
305
306 SchemaObject schemaObject = byOid.get( oid );
307
308 if ( schemaObject != null )
309 {
310 sb.append( schemaObject.getObjectType() );
311 sb.append( ", " );
312 sb.append( schemaObject.getOid() );
313 sb.append( ", " );
314 sb.append( schemaObject.getName() );
315 }
316
317 sb.append( ">" );
318 }
319 }
320
321 return sb.toString();
322 }
323 }