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