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.helper;
21
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.directory.api.i18n.I18n;
27 import org.apache.directory.api.ldap.model.exception.LdapException;
28 import org.apache.directory.api.ldap.model.exception.LdapSchemaException;
29 import org.apache.directory.api.ldap.model.exception.LdapSchemaExceptionCodes;
30 import org.apache.directory.api.ldap.model.schema.AttributeType;
31 import org.apache.directory.api.ldap.model.schema.LdapSyntax;
32 import org.apache.directory.api.ldap.model.schema.MatchingRule;
33 import org.apache.directory.api.ldap.model.schema.MutableAttributeType;
34 import org.apache.directory.api.ldap.model.schema.UsageEnum;
35 import org.apache.directory.api.ldap.model.schema.registries.AttributeTypeRegistry;
36 import org.apache.directory.api.ldap.model.schema.registries.Registries;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46 public final class AttributeTypeHelper
47 {
48
49 private static final Logger LOG = LoggerFactory.getLogger( AttributeTypeHelper.class );
50
51 private AttributeTypeHelper()
52 {
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public static void addToRegistries( MutableAttributeType attributeType, List<Throwable> errors, Registries registries ) throws LdapException
68 {
69 if ( registries != null )
70 {
71 try
72 {
73 attributeType.unlock();
74 AttributeTypeRegistry attributeTypeRegistry = registries.getAttributeTypeRegistry();
75
76
77 if ( !buildSuperior( attributeType, errors, registries ) )
78 {
79
80 return;
81 }
82
83
84 buildSyntax( attributeType, errors, registries );
85
86
87 buildEquality( attributeType, errors, registries );
88
89
90 buildOrdering( attributeType, errors, registries );
91
92
93 buildSubstring( attributeType, errors, registries );
94
95
96 checkUsage( attributeType, errors );
97
98
99 checkCollective( attributeType, errors );
100
101
102 attributeTypeRegistry.addMappingFor( attributeType );
103
104
105 attributeTypeRegistry.registerDescendants( attributeType, attributeType.getSuperior() );
106
107
108
109
110
111
112
113 if ( attributeType.getEquality() != null )
114 {
115 registries.addReference( attributeType, attributeType.getEquality() );
116 }
117
118 if ( attributeType.getOrdering() != null )
119 {
120 registries.addReference( attributeType, attributeType.getOrdering() );
121 }
122
123 if ( attributeType.getSubstring() != null )
124 {
125 registries.addReference( attributeType, attributeType.getSubstring() );
126 }
127
128 if ( attributeType.getSyntax() != null )
129 {
130 registries.addReference( attributeType, attributeType.getSyntax() );
131 }
132
133 if ( attributeType.getSuperior() != null )
134 {
135 registries.addReference( attributeType, attributeType.getSuperior() );
136 }
137 }
138 finally
139 {
140 attributeType.lock();
141 }
142 }
143 }
144
145
146
147
148
149 private static boolean buildSuperior( MutableAttributeType attributeType, List<Throwable> errors, Registries registries )
150 {
151 MutableAttributeType currentSuperior;
152 AttributeTypeRegistry attributeTypeRegistry = registries.getAttributeTypeRegistry();
153
154 String superiorOid = attributeType.getSuperiorOid();
155
156 if ( superiorOid != null )
157 {
158
159 try
160 {
161 currentSuperior = ( MutableAttributeType ) attributeTypeRegistry.lookup( superiorOid );
162 }
163 catch ( Exception e )
164 {
165
166 String msg = I18n.err( I18n.ERR_04303, superiorOid, attributeType.getName() );
167
168 LdapSchemaException ldapSchemaException = new LdapSchemaException(
169 LdapSchemaExceptionCodes.AT_NONEXISTENT_SUPERIOR, msg, e );
170 ldapSchemaException.setSourceObject( attributeType );
171 ldapSchemaException.setRelatedId( superiorOid );
172 errors.add( ldapSchemaException );
173 LOG.info( msg );
174
175
176 return false;
177 }
178
179 if ( currentSuperior != null )
180 {
181
182 if ( currentSuperior.isCollective() )
183 {
184 String msg = I18n.err( I18n.ERR_04482_CANNOT_SUBTYPE_COLLECTIVE,
185 currentSuperior, attributeType.getName() );
186
187 LdapSchemaException ldapSchemaException = new LdapSchemaException(
188 LdapSchemaExceptionCodes.AT_CANNOT_SUBTYPE_COLLECTIVE_AT, msg );
189 ldapSchemaException.setSourceObject( attributeType );
190 errors.add( ldapSchemaException );
191 LOG.info( msg );
192
193 return false;
194 }
195
196 attributeType.setSuperior( currentSuperior );
197
198
199
200
201 if ( currentSuperior.getSuperior() == null )
202 {
203 registries.buildReference( errors, currentSuperior );
204 }
205
206
207 try
208 {
209 attributeTypeRegistry.registerDescendants( attributeType, currentSuperior );
210 }
211 catch ( LdapException ne )
212 {
213 errors.add( ne );
214 LOG.info( ne.getMessage() );
215
216 return false;
217 }
218
219
220 Set<String> superiors = new HashSet<>();
221 superiors.add( attributeType.getOid() );
222 AttributeType tmp = currentSuperior;
223 boolean isOk = true;
224
225 while ( tmp != null )
226 {
227 if ( superiors.contains( tmp.getOid() ) )
228 {
229
230
231 String msg = I18n.err( I18n.ERR_04304, attributeType.getName() );
232
233 LdapSchemaException ldapSchemaException = new LdapSchemaException(
234 LdapSchemaExceptionCodes.AT_CYCLE_TYPE_HIERARCHY, msg );
235 ldapSchemaException.setSourceObject( attributeType );
236 errors.add( ldapSchemaException );
237 LOG.info( msg );
238 isOk = false;
239
240 break;
241 }
242 else
243 {
244 superiors.add( tmp.getOid() );
245 tmp = tmp.getSuperior();
246 }
247 }
248
249 superiors.clear();
250
251 return isOk;
252 }
253 else
254 {
255
256 String msg = I18n.err( I18n.ERR_04305, superiorOid, attributeType.getName() );
257
258 LdapSchemaException ldapSchemaException = new LdapSchemaException(
259 LdapSchemaExceptionCodes.AT_NONEXISTENT_SUPERIOR, msg );
260 ldapSchemaException.setSourceObject( attributeType );
261 ldapSchemaException.setRelatedId( superiorOid );
262 errors.add( ldapSchemaException );
263 LOG.info( msg );
264
265
266 return false;
267 }
268 }
269 else
270 {
271
272 return true;
273 }
274 }
275
276
277
278
279
280 private static void buildSyntax( MutableAttributeType attributeType, List<Throwable> errors, Registries registries )
281 {
282 String syntaxOid = attributeType.getSyntaxOid();
283
284 if ( syntaxOid != null )
285 {
286 LdapSyntax currentSyntax = null;
287
288 try
289 {
290 currentSyntax = registries.getLdapSyntaxRegistry().lookup( syntaxOid );
291 }
292 catch ( LdapException ne )
293 {
294
295 String msg = I18n.err( I18n.ERR_04306, syntaxOid, attributeType.getName() );
296
297 LdapSchemaException ldapSchemaException = new LdapSchemaException(
298 LdapSchemaExceptionCodes.AT_NONEXISTENT_SYNTAX, msg, ne );
299 ldapSchemaException.setSourceObject( attributeType );
300 ldapSchemaException.setRelatedId( syntaxOid );
301 errors.add( ldapSchemaException );
302 LOG.info( msg );
303
304 return;
305 }
306
307 if ( currentSyntax != null )
308 {
309
310 attributeType.setSyntax( currentSyntax );
311 }
312 else
313 {
314
315 String msg = I18n.err( I18n.ERR_04306, syntaxOid, attributeType.getName() );
316
317 LdapSchemaException ldapSchemaException = new LdapSchemaException(
318 LdapSchemaExceptionCodes.AT_NONEXISTENT_SYNTAX, msg );
319 ldapSchemaException.setSourceObject( attributeType );
320 ldapSchemaException.setRelatedId( syntaxOid );
321 errors.add( ldapSchemaException );
322 LOG.info( msg );
323
324 return;
325 }
326 }
327 else
328 {
329
330 if ( attributeType.getSuperior() != null )
331 {
332 if ( attributeType.getSuperior().getSyntax() != null )
333 {
334 attributeType.setSyntax( attributeType.getSuperior().getSyntax() );
335 }
336 else
337 {
338 String msg = I18n.err( I18n.ERR_04306, syntaxOid, attributeType.getName() );
339
340 LdapSchemaException ldapSchemaException = new LdapSchemaException(
341 LdapSchemaExceptionCodes.AT_NONEXISTENT_SYNTAX, msg );
342 ldapSchemaException.setSourceObject( attributeType );
343 ldapSchemaException.setRelatedId( syntaxOid );
344 errors.add( ldapSchemaException );
345 LOG.info( msg );
346
347 return;
348 }
349 }
350 else
351 {
352
353 String msg = I18n.err( I18n.ERR_04307, attributeType.getName() );
354
355 LdapSchemaException ldapSchemaException = new LdapSchemaException(
356 LdapSchemaExceptionCodes.AT_SYNTAX_OR_SUPERIOR_REQUIRED, msg );
357 ldapSchemaException.setSourceObject( attributeType );
358 errors.add( ldapSchemaException );
359 LOG.info( msg );
360
361 return;
362 }
363 }
364 }
365
366
367
368
369
370 private static void buildEquality( MutableAttributeType attributeType, List<Throwable> errors, Registries registries )
371 {
372 String equalityOid = attributeType.getEqualityOid();
373
374
375 if ( equalityOid != null )
376 {
377 MatchingRule currentEquality = null;
378
379 try
380 {
381 currentEquality = registries.getMatchingRuleRegistry().lookup( equalityOid );
382 }
383 catch ( LdapException ne )
384 {
385
386 String msg = I18n.err( I18n.ERR_04308, equalityOid, attributeType.getName() );
387
388 LdapSchemaException ldapSchemaException = new LdapSchemaException(
389 LdapSchemaExceptionCodes.AT_NONEXISTENT_EQUALITY_MATCHING_RULE, msg, ne );
390 ldapSchemaException.setSourceObject( attributeType );
391 ldapSchemaException.setRelatedId( equalityOid );
392 errors.add( ldapSchemaException );
393 LOG.info( msg );
394
395 return;
396 }
397
398 if ( currentEquality != null )
399 {
400 attributeType.setEquality( currentEquality );
401
402
403 attributeType.setEqualityOid( equalityOid );
404 }
405 else
406 {
407
408 String msg = I18n.err( I18n.ERR_04309, equalityOid, attributeType.getName() );
409
410 LdapSchemaException ldapSchemaException = new LdapSchemaException(
411 LdapSchemaExceptionCodes.AT_NONEXISTENT_EQUALITY_MATCHING_RULE, msg );
412 ldapSchemaException.setSourceObject( attributeType );
413 ldapSchemaException.setRelatedId( equalityOid );
414 errors.add( ldapSchemaException );
415 LOG.info( msg );
416 }
417 }
418 else
419 {
420 AttributeType superior = attributeType.getSuperior();
421
422
423 if ( ( superior != null ) && ( superior.getEquality() != null ) )
424 {
425 attributeType.setEquality( superior.getEquality() );
426 }
427 }
428 }
429
430
431
432
433
434 private static void buildSubstring( MutableAttributeType attributeType, List<Throwable> errors, Registries registries )
435 {
436 String substringOid = attributeType.getSubstringOid();
437
438
439 if ( substringOid != null )
440 {
441 MatchingRule currentSubstring = null;
442
443 try
444 {
445 currentSubstring = registries.getMatchingRuleRegistry().lookup( substringOid );
446 }
447 catch ( LdapException ne )
448 {
449
450 String msg = I18n.err( I18n.ERR_04312, substringOid, attributeType.getName() );
451
452 LdapSchemaException ldapSchemaException = new LdapSchemaException(
453 LdapSchemaExceptionCodes.AT_NONEXISTENT_SUBSTRING_MATCHING_RULE, msg, ne );
454 ldapSchemaException.setSourceObject( attributeType );
455 ldapSchemaException.setRelatedId( substringOid );
456 errors.add( ldapSchemaException );
457 LOG.info( msg );
458
459 return;
460 }
461
462 if ( currentSubstring != null )
463 {
464 attributeType.setSubstring( currentSubstring );
465 }
466 else
467 {
468
469 String msg = I18n.err( I18n.ERR_04313, substringOid, attributeType.getName() );
470
471 LdapSchemaException ldapSchemaException = new LdapSchemaException(
472 LdapSchemaExceptionCodes.AT_NONEXISTENT_SUBSTRING_MATCHING_RULE, msg );
473 ldapSchemaException.setSourceObject( attributeType );
474 ldapSchemaException.setRelatedId( substringOid );
475 errors.add( ldapSchemaException );
476 LOG.info( msg );
477
478 return;
479 }
480 }
481 else
482 {
483 AttributeType superior = attributeType.getSuperior();
484
485
486 if ( ( superior != null ) && ( superior.getSubstring() != null ) )
487 {
488 attributeType.setSubstring( superior.getSubstring() );
489 }
490 }
491 }
492
493
494
495
496
497
498
499
500
501 private static void buildOrdering( MutableAttributeType attributeType, List<Throwable> errors, Registries registries )
502 {
503 String orderingOid = attributeType.getOrderingOid();
504
505 if ( orderingOid != null )
506 {
507 MatchingRule currentOrdering = null;
508
509 try
510 {
511 currentOrdering = registries.getMatchingRuleRegistry().lookup( orderingOid );
512 }
513 catch ( LdapException ne )
514 {
515
516 String msg = I18n.err( I18n.ERR_04310, orderingOid, attributeType.getName() );
517
518 LdapSchemaException ldapSchemaException = new LdapSchemaException(
519 LdapSchemaExceptionCodes.AT_NONEXISTENT_ORDERING_MATCHING_RULE, msg, ne );
520 ldapSchemaException.setSourceObject( attributeType );
521 ldapSchemaException.setRelatedId( orderingOid );
522 errors.add( ldapSchemaException );
523 LOG.info( msg );
524
525 return;
526 }
527
528 if ( currentOrdering != null )
529 {
530 attributeType.setOrdering( currentOrdering );
531 }
532 else
533 {
534
535 String msg = I18n.err( I18n.ERR_04311, orderingOid, attributeType.getName() );
536
537 LdapSchemaException ldapSchemaException = new LdapSchemaException(
538 LdapSchemaExceptionCodes.AT_NONEXISTENT_ORDERING_MATCHING_RULE, msg );
539 ldapSchemaException.setSourceObject( attributeType );
540 ldapSchemaException.setRelatedId( orderingOid );
541 errors.add( ldapSchemaException );
542 LOG.info( msg );
543 }
544 }
545 else
546 {
547 AttributeType superior = attributeType.getSuperior();
548
549
550 if ( ( superior != null ) && ( superior.getOrdering() != null ) )
551 {
552 attributeType.setOrdering( superior.getOrdering() );
553 }
554 }
555 }
556
557
558
559
560
561 private static void checkUsage( AttributeType attributeType, List<Throwable> errors )
562 {
563 AttributeType superior = attributeType.getSuperior();
564
565
566 if ( ( superior != null ) && ( attributeType.getUsage() != superior.getUsage() ) )
567 {
568
569 String msg = I18n.err( I18n.ERR_04314, attributeType.getName() );
570
571 LdapSchemaException ldapSchemaException = new LdapSchemaException(
572 LdapSchemaExceptionCodes.AT_MUST_HAVE_SAME_USAGE_THAN_SUPERIOR, msg );
573 ldapSchemaException.setSourceObject( attributeType );
574 errors.add( ldapSchemaException );
575 LOG.info( msg );
576
577 return;
578 }
579
580
581 if ( !attributeType.isUserModifiable() && ( attributeType.getUsage() == UsageEnum.USER_APPLICATIONS ) )
582 {
583
584 String msg = I18n.err( I18n.ERR_04315, attributeType.getName() );
585
586 LdapSchemaException ldapSchemaException = new LdapSchemaException(
587 LdapSchemaExceptionCodes.AT_USER_APPLICATIONS_USAGE_MUST_BE_USER_MODIFIABLE, msg );
588 ldapSchemaException.setSourceObject( attributeType );
589 errors.add( ldapSchemaException );
590 LOG.info( msg );
591 }
592 }
593
594
595
596
597
598 private static void checkCollective( MutableAttributeType attributeType, List<Throwable> errors )
599 {
600 AttributeType superior = attributeType.getSuperior();
601
602 if ( ( superior != null ) && superior.isCollective() )
603 {
604
605 attributeType.setCollective( true );
606 }
607
608 if ( attributeType.isCollective() && ( attributeType.getUsage() != UsageEnum.USER_APPLICATIONS ) )
609 {
610
611 String msg = I18n.err( I18n.ERR_04316, attributeType.getName() );
612
613 LdapSchemaException ldapSchemaException = new LdapSchemaException(
614 LdapSchemaExceptionCodes.AT_COLLECTIVE_MUST_HAVE_USER_APPLICATIONS_USAGE, msg );
615 ldapSchemaException.setSourceObject( attributeType );
616 errors.add( ldapSchemaException );
617 LOG.info( msg );
618 }
619
620 if ( attributeType.isCollective() && attributeType.isSingleValued() )
621 {
622
623 String msg = I18n.err( I18n.ERR_04483_COLLECTIVE_NOT_MULTI_VALUED, attributeType.getName() );
624
625 LdapSchemaException ldapSchemaException = new LdapSchemaException(
626 LdapSchemaExceptionCodes.AT_COLLECTIVE_CANNOT_BE_SINGLE_VALUED, msg );
627 ldapSchemaException.setSourceObject( attributeType );
628 errors.add( ldapSchemaException );
629 LOG.info( msg );
630 }
631 }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646 public static void removeFromRegistries( AttributeType attributeType, List<Throwable> errors, Registries registries ) throws LdapException
647 {
648 if ( registries != null )
649 {
650 AttributeTypeRegistry attributeTypeRegistry = registries.getAttributeTypeRegistry();
651
652
653 attributeTypeRegistry.removeMappingFor( attributeType );
654
655
656 attributeTypeRegistry.unregisterDescendants( attributeType, attributeType.getSuperior() );
657
658
659
660
661
662
663
664 if ( attributeType.getEquality() != null )
665 {
666 registries.delReference( attributeType, attributeType.getEquality() );
667 }
668
669 if ( attributeType.getOrdering() != null )
670 {
671 registries.delReference( attributeType, attributeType.getOrdering() );
672 }
673
674 if ( attributeType.getSubstring() != null )
675 {
676 registries.delReference( attributeType, attributeType.getSubstring() );
677 }
678
679 if ( attributeType.getSyntax() != null )
680 {
681 registries.delReference( attributeType, attributeType.getSyntax() );
682 }
683
684 if ( attributeType.getSuperior() != null )
685 {
686 registries.delReference( attributeType, attributeType.getSuperior() );
687 }
688 }
689 }
690 }