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 if ( entry.get( SchemaConstants.CREATORS_NAME_AT ) == null )
180 {
181 throw new IllegalArgumentException( "entry must have a valid " + SchemaConstants.CREATORS_NAME_AT
182 + " attribute" );
183 }
184
185 owner = entry.get( SchemaConstants.CREATORS_NAME_AT ).getString();
186
187 if ( entry.get( MetaSchemaConstants.M_DISABLED_AT ) != null )
188 {
189 String value = entry.get( MetaSchemaConstants.M_DISABLED_AT ).getString();
190 value = value.toUpperCase();
191 isDisabled = value.equals( "TRUE" );
192 }
193
194 if ( entry.get( MetaSchemaConstants.M_DEPENDENCIES_AT ) != null )
195 {
196 Set<String> depsSet = new HashSet<String>();
197 Attribute depsAttr = entry.get( MetaSchemaConstants.M_DEPENDENCIES_AT );
198
199 for ( Value<?> value : depsAttr )
200 {
201 depsSet.add( value.getString() );
202 }
203
204 dependencies = depsSet.toArray( StringConstants.EMPTY_STRINGS );
205 }
206
207 return new DefaultSchema( name, owner, dependencies, isDisabled );
208 }
209
210
211 private Schema[] buildSchemaArray( String... schemaNames ) throws Exception
212 {
213 Schema[] schemas = new Schema[schemaNames.length];
214 int pos = 0;
215
216 for ( String schemaName : schemaNames )
217 {
218 schemas[pos++] = getSchema( schemaName );
219 }
220
221 return schemas;
222 }
223
224
225
226
227
228 public List<Entry> loadAttributeTypes( String... schemaNames ) throws Exception
229 {
230 if ( schemaNames == null )
231 {
232 return new ArrayList<Entry>();
233 }
234
235 return loadAttributeTypes( buildSchemaArray( schemaNames ) );
236 }
237
238
239
240
241
242 public List<Entry> loadComparators( String... schemaNames ) throws Exception
243 {
244 if ( schemaNames == null )
245 {
246 return new ArrayList<Entry>();
247 }
248
249 return loadComparators( buildSchemaArray( schemaNames ) );
250 }
251
252
253
254
255
256 public List<Entry> loadDitContentRules( String... schemaNames ) throws Exception
257 {
258 if ( schemaNames == null )
259 {
260 return new ArrayList<Entry>();
261 }
262
263 return loadDitContentRules( buildSchemaArray( schemaNames ) );
264 }
265
266
267
268
269
270 public List<Entry> loadDitStructureRules( String... schemaNames ) throws Exception
271 {
272 if ( schemaNames == null )
273 {
274 return new ArrayList<Entry>();
275 }
276
277 return loadDitStructureRules( buildSchemaArray( schemaNames ) );
278 }
279
280
281
282
283
284 public List<Entry> loadMatchingRules( String... schemaNames ) throws Exception
285 {
286 if ( schemaNames == null )
287 {
288 return new ArrayList<Entry>();
289 }
290
291 return loadMatchingRules( buildSchemaArray( schemaNames ) );
292 }
293
294
295
296
297
298 public List<Entry> loadMatchingRuleUses( String... schemaNames ) throws Exception
299 {
300 if ( schemaNames == null )
301 {
302 return new ArrayList<Entry>();
303 }
304
305 return loadMatchingRuleUses( buildSchemaArray( schemaNames ) );
306 }
307
308
309
310
311
312 public List<Entry> loadNameForms( String... schemaNames ) throws Exception
313 {
314 if ( schemaNames == null )
315 {
316 return new ArrayList<Entry>();
317 }
318
319 return loadNameForms( buildSchemaArray( schemaNames ) );
320 }
321
322
323
324
325
326 public List<Entry> loadNormalizers( String... schemaNames ) throws Exception
327 {
328 if ( schemaNames == null )
329 {
330 return new ArrayList<Entry>();
331 }
332
333 return loadNormalizers( buildSchemaArray( schemaNames ) );
334 }
335
336
337
338
339
340 public List<Entry> loadObjectClasses( String... schemaNames ) throws Exception
341 {
342 if ( schemaNames == null )
343 {
344 return new ArrayList<Entry>();
345 }
346
347 return loadObjectClasses( buildSchemaArray( schemaNames ) );
348 }
349
350
351
352
353
354 public List<Entry> loadSyntaxes( String... schemaNames ) throws Exception
355 {
356 if ( schemaNames == null )
357 {
358 return new ArrayList<Entry>();
359 }
360
361 return loadSyntaxes( buildSchemaArray( schemaNames ) );
362 }
363
364
365
366
367
368 public List<Entry> loadSyntaxCheckers( String... schemaNames ) throws Exception
369 {
370 if ( schemaNames == null )
371 {
372 return new ArrayList<Entry>();
373 }
374
375 return loadSyntaxCheckers( buildSchemaArray( schemaNames ) );
376 }
377 }