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.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.directory.api.i18n.I18n;
32 import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants;
33 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
34 import org.apache.directory.api.ldap.model.entry.Attribute;
35 import org.apache.directory.api.ldap.model.entry.Entry;
36 import org.apache.directory.api.ldap.model.entry.Value;
37 import org.apache.directory.api.util.StringConstants;
38 import org.apache.directory.api.util.Strings;
39
40
41
42
43
44
45
46 public abstract class AbstractSchemaLoader implements SchemaLoader
47 {
48
49
50
51
52
53 protected final Map<String, Schema> schemaMap = new LowerCaseKeyMap();
54
55
56
57
58 private static class LowerCaseKeyMap extends HashMap<String, Schema>
59 {
60 private static final long serialVersionUID = 1L;
61
62
63 @Override
64 public Schema put( String key, Schema value )
65 {
66 return super.put( Strings.lowerCase( key ), value );
67 }
68
69
70 @Override
71 public void putAll( Map<? extends String, ? extends Schema> map )
72 {
73 for ( Map.Entry<? extends String, ? extends Schema> e : map.entrySet() )
74 {
75 put( e.getKey(), e.getValue() );
76 }
77 }
78 }
79
80
81
82
83
84 public final Collection<Schema> getAllEnabled() throws Exception
85 {
86 Collection<Schema> enabledSchemas = new ArrayList<Schema>();
87
88 for ( Schema schema : schemaMap.values() )
89 {
90 if ( schema.isEnabled() )
91 {
92 enabledSchemas.add( schema );
93 }
94 }
95
96 return enabledSchemas;
97 }
98
99
100
101
102
103 public final Collection<Schema> getAllSchemas() throws Exception
104 {
105 return schemaMap.values();
106 }
107
108
109
110
111
112 public Schema getSchema( String schemaName )
113 {
114 return schemaMap.get( Strings.toLowerCase( schemaName ) );
115 }
116
117
118
119
120
121 public void addSchema( Schema schema )
122 {
123 schemaMap.put( schema.getSchemaName(), schema );
124 }
125
126
127
128
129
130 public void removeSchema( Schema schema )
131 {
132 schemaMap.remove( Strings.toLowerCase( schema.getSchemaName() ) );
133 }
134
135
136
137
138
139
140
141
142
143 protected Schema getSchema( Entry entry ) throws Exception
144 {
145 if ( entry == null )
146 {
147 throw new IllegalArgumentException( I18n.err( I18n.ERR_04261 ) );
148 }
149
150 Attribute objectClasses = entry.get( SchemaConstants.OBJECT_CLASS_AT );
151 boolean isSchema = false;
152
153 for ( Value<?> value : objectClasses )
154 {
155 if ( MetaSchemaConstants.META_SCHEMA_OC.equalsIgnoreCase( value.getString() ) )
156 {
157 isSchema = true;
158 break;
159 }
160 }
161
162 if ( !isSchema )
163 {
164 return null;
165 }
166
167 String name;
168 String owner;
169 String[] dependencies = StringConstants.EMPTY_STRINGS;
170 boolean isDisabled = false;
171
172 if ( entry.get( SchemaConstants.CN_AT ) == null )
173 {
174 throw new IllegalArgumentException( I18n.err( I18n.ERR_04262 ) );
175 }
176
177 name = entry.get( SchemaConstants.CN_AT ).getString();
178
179 Attribute creatorsName = entry.get( SchemaConstants.CREATORS_NAME_AT );
180
181 if ( creatorsName == null )
182 {
183 owner = null;
184 }
185 else
186 {
187 owner = creatorsName.getString();
188 }
189
190 if ( entry.get( MetaSchemaConstants.M_DISABLED_AT ) != null )
191 {
192 String value = entry.get( MetaSchemaConstants.M_DISABLED_AT ).getString();
193 value = value.toUpperCase();
194 isDisabled = value.equals( "TRUE" );
195 }
196
197 if ( entry.get( MetaSchemaConstants.M_DEPENDENCIES_AT ) != null )
198 {
199 Set<String> depsSet = new HashSet<String>();
200 Attribute depsAttr = entry.get( MetaSchemaConstants.M_DEPENDENCIES_AT );
201
202 for ( Value<?> value : depsAttr )
203 {
204 depsSet.add( value.getString() );
205 }
206
207 dependencies = depsSet.toArray( StringConstants.EMPTY_STRINGS );
208 }
209
210 return new DefaultSchema( name, owner, dependencies, isDisabled );
211 }
212
213
214 private Schema[] buildSchemaArray( String... schemaNames ) throws Exception
215 {
216 Schema[] schemas = new Schema[schemaNames.length];
217 int pos = 0;
218
219 for ( String schemaName : schemaNames )
220 {
221 schemas[pos++] = getSchema( schemaName );
222 }
223
224 return schemas;
225 }
226
227
228
229
230
231 public List<Entry> loadAttributeTypes( String... schemaNames ) throws Exception
232 {
233 if ( schemaNames == null )
234 {
235 return new ArrayList<Entry>();
236 }
237
238 return loadAttributeTypes( buildSchemaArray( schemaNames ) );
239 }
240
241
242
243
244
245 public List<Entry> loadComparators( String... schemaNames ) throws Exception
246 {
247 if ( schemaNames == null )
248 {
249 return new ArrayList<Entry>();
250 }
251
252 return loadComparators( buildSchemaArray( schemaNames ) );
253 }
254
255
256
257
258
259 public List<Entry> loadDitContentRules( String... schemaNames ) throws Exception
260 {
261 if ( schemaNames == null )
262 {
263 return new ArrayList<Entry>();
264 }
265
266 return loadDitContentRules( buildSchemaArray( schemaNames ) );
267 }
268
269
270
271
272
273 public List<Entry> loadDitStructureRules( String... schemaNames ) throws Exception
274 {
275 if ( schemaNames == null )
276 {
277 return new ArrayList<Entry>();
278 }
279
280 return loadDitStructureRules( buildSchemaArray( schemaNames ) );
281 }
282
283
284
285
286
287 public List<Entry> loadMatchingRules( String... schemaNames ) throws Exception
288 {
289 if ( schemaNames == null )
290 {
291 return new ArrayList<Entry>();
292 }
293
294 return loadMatchingRules( buildSchemaArray( schemaNames ) );
295 }
296
297
298
299
300
301 public List<Entry> loadMatchingRuleUses( String... schemaNames ) throws Exception
302 {
303 if ( schemaNames == null )
304 {
305 return new ArrayList<Entry>();
306 }
307
308 return loadMatchingRuleUses( buildSchemaArray( schemaNames ) );
309 }
310
311
312
313
314
315 public List<Entry> loadNameForms( String... schemaNames ) throws Exception
316 {
317 if ( schemaNames == null )
318 {
319 return new ArrayList<Entry>();
320 }
321
322 return loadNameForms( buildSchemaArray( schemaNames ) );
323 }
324
325
326
327
328
329 public List<Entry> loadNormalizers( String... schemaNames ) throws Exception
330 {
331 if ( schemaNames == null )
332 {
333 return new ArrayList<Entry>();
334 }
335
336 return loadNormalizers( buildSchemaArray( schemaNames ) );
337 }
338
339
340
341
342
343 public List<Entry> loadObjectClasses( String... schemaNames ) throws Exception
344 {
345 if ( schemaNames == null )
346 {
347 return new ArrayList<Entry>();
348 }
349
350 return loadObjectClasses( buildSchemaArray( schemaNames ) );
351 }
352
353
354
355
356
357 public List<Entry> loadSyntaxes( String... schemaNames ) throws Exception
358 {
359 if ( schemaNames == null )
360 {
361 return new ArrayList<Entry>();
362 }
363
364 return loadSyntaxes( buildSchemaArray( schemaNames ) );
365 }
366
367
368
369
370
371 public List<Entry> loadSyntaxCheckers( String... schemaNames ) throws Exception
372 {
373 if ( schemaNames == null )
374 {
375 return new ArrayList<Entry>();
376 }
377
378 return loadSyntaxCheckers( buildSchemaArray( schemaNames ) );
379 }
380 }