1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.config;
21
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.lang.reflect.Field;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34
35 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
36 import org.apache.directory.api.ldap.model.entry.Attribute;
37 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
38 import org.apache.directory.api.ldap.model.exception.LdapException;
39 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
40 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
41 import org.apache.directory.api.ldap.model.ldif.LdifEntry;
42 import org.apache.directory.api.ldap.model.name.Dn;
43 import org.apache.directory.api.ldap.model.name.Rdn;
44 import org.apache.directory.api.ldap.model.schema.ObjectClass;
45 import org.apache.directory.api.ldap.model.schema.SchemaManager;
46 import org.apache.directory.api.util.Strings;
47 import org.apache.directory.server.config.beans.AdsBaseBean;
48 import org.apache.directory.server.config.beans.ConfigBean;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class ConfigWriter
63 {
64
65 private SchemaManager schemaManager;
66
67
68 private ConfigBean configBean;
69
70
71 private List<LdifEntry> entries;
72
73
74
75
76
77
78
79
80
81
82 public ConfigWriter( SchemaManager schemaManager, ConfigBean configBean )
83 {
84 this.schemaManager = schemaManager;
85 this.configBean = configBean;
86 }
87
88
89
90
91
92 private void convertConfigurationBeanToLdifEntries() throws ConfigurationException
93 {
94 try
95 {
96 if ( entries == null )
97 {
98 entries = new ArrayList<>();
99
100
101 LdifEntry configRootEntry = new LdifEntry();
102 configRootEntry.setDn( new Dn( SchemaConstants.OU_AT + "=" + "config" ) );
103 addObjectClassAttribute( schemaManager, configRootEntry, "organizationalUnit" );
104 addAttributeTypeValues( SchemaConstants.OU_AT, "config", configRootEntry );
105 entries.add( configRootEntry );
106
107
108 List<AdsBaseBean> directoryServiceBeans = configBean.getDirectoryServiceBeans();
109 for ( AdsBaseBean adsBaseBean : directoryServiceBeans )
110 {
111 addBean( configRootEntry.getDn(), schemaManager, adsBaseBean, entries );
112 }
113 }
114 }
115 catch ( Exception e )
116 {
117 throw new ConfigurationException( "Unable to convert the configuration bean to LDIF entries", e );
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 public void writeToPath( String path ) throws ConfigurationException, IOException
133 {
134 writeToFile( new File( path ) );
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public void writeToFile( File file ) throws ConfigurationException, IOException
149 {
150
151 try ( Writer writer = Files.newBufferedWriter( file.toPath(), StandardCharsets.UTF_8 ) )
152 {
153 writer.append( writeToString() );
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public String writeToString() throws ConfigurationException
168 {
169
170 convertConfigurationBeanToLdifEntries();
171
172
173 StringBuilder sb = new StringBuilder();
174 sb.append( "version: 1\n" );
175 for ( LdifEntry entry : entries )
176 {
177 sb.append( entry.toString() );
178 }
179
180 return sb.toString();
181 }
182
183
184
185
186
187
188
189
190
191
192 public List<LdifEntry> getConvertedLdifEntries() throws ConfigurationException
193 {
194
195 convertConfigurationBeanToLdifEntries();
196
197
198 return entries;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 private void addObjectClassAttribute( SchemaManager schemaManager, LdifEntry entry, String objectClass )
214 throws LdapException
215 {
216 ObjectClass objectClassObject = schemaManager.lookupObjectClassRegistry( objectClass );
217 if ( objectClassObject != null )
218 {
219
220 Set<String> objectClassAttributeValues = new HashSet<>();
221 computeObjectClassAttributeValues( schemaManager, objectClassAttributeValues, objectClassObject );
222
223
224 addAttributeTypeValues( SchemaConstants.OBJECT_CLASS_AT, objectClassAttributeValues, entry );
225 }
226 else
227 {
228 throw new IllegalStateException( "Missing object class " + objectClass );
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244 private void computeObjectClassAttributeValues( SchemaManager schemaManager,
245 Set<String> objectClassAttributeValues,
246 ObjectClass objectClass ) throws LdapException
247 {
248 ObjectClass topObjectClass = schemaManager.lookupObjectClassRegistry( SchemaConstants.TOP_OC );
249 if ( topObjectClass == null )
250 {
251 throw new IllegalStateException( "Missing top object class." );
252 }
253
254 if ( topObjectClass.equals( objectClass ) )
255 {
256 objectClassAttributeValues.add( objectClass.getName() );
257 }
258 else
259 {
260 objectClassAttributeValues.add( objectClass.getName() );
261
262 List<ObjectClass> superiors = objectClass.getSuperiors();
263 if ( ( superiors != null ) && !superiors.isEmpty() )
264 {
265 for ( ObjectClass superior : superiors )
266 {
267 computeObjectClassAttributeValues( schemaManager, objectClassAttributeValues, superior );
268 }
269 }
270 else
271 {
272 objectClassAttributeValues.add( topObjectClass.getName() );
273 }
274 }
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 private void addBean( Dn rootDn, SchemaManager schemaManager, AdsBaseBean bean, List<LdifEntry> entries )
292 throws Exception
293 {
294 addBean( rootDn, schemaManager, bean, entries, null, null );
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 private void addBean( Dn rootDn, SchemaManager schemaManager, AdsBaseBean bean, List<LdifEntry> entries,
317 LdifEntry parentEntry, String attributeTypeForParentEntry )
318 throws Exception
319 {
320 if ( bean != null )
321 {
322
323 Class<?> beanClass = bean.getClass();
324
325
326 LdifEntry entry = new LdifEntry();
327 entry.setDn( getDn( rootDn, bean ) );
328 addObjectClassAttribute( schemaManager, entry, getObjectClassNameForBean( beanClass ) );
329 entries.add( entry );
330
331
332
333 boolean adsBaseBeanClassFound = false;
334
335
336 while ( !adsBaseBeanClassFound )
337 {
338
339 if ( beanClass == AdsBaseBean.class )
340 {
341 adsBaseBeanClassFound = true;
342 }
343
344
345 Field[] fields = beanClass.getDeclaredFields();
346 for ( Field field : fields )
347 {
348
349 field.setAccessible( true );
350
351
352 Class<?> fieldClass = field.getType();
353 Object fieldValue = field.get( bean );
354
355
356 ConfigurationElement configurationElement = field.getAnnotation( ConfigurationElement.class );
357 if ( configurationElement != null )
358 {
359
360 String attributeType = configurationElement.attributeType();
361 String objectClass = configurationElement.objectClass();
362 String container = configurationElement.container();
363 boolean isOptional = configurationElement.isOptional();
364 String defaultValue = configurationElement.defaultValue();
365
366
367 if ( ( attributeType != null ) && ( !"".equals( attributeType ) ) )
368 {
369
370 if ( isOptional
371 && ( defaultValue != null ) && ( fieldValue != null )
372 && ( defaultValue.equalsIgnoreCase( fieldValue.toString() ) ) )
373 {
374
375 continue;
376 }
377
378
379 addAttributeTypeValues( configurationElement.attributeType(), fieldValue, entry );
380 }
381
382 else if ( ( objectClass != null ) && ( !"".equals( objectClass ) ) )
383 {
384
385 if ( ( container != null ) && ( !"".equals( container ) ) )
386 {
387
388 LdifEntry containerEntry = new LdifEntry();
389 containerEntry.setDn( entry.getDn().add( new Rdn( SchemaConstants.OU_AT, container ) ) );
390 addObjectClassAttribute( schemaManager, containerEntry,
391 SchemaConstants.ORGANIZATIONAL_UNIT_OC );
392 addAttributeTypeValues( SchemaConstants.OU_AT, container, containerEntry );
393 entries.add( containerEntry );
394
395 if ( Collection.class.isAssignableFrom( fieldClass ) )
396 {
397
398 @SuppressWarnings("unchecked")
399 Collection<Object> collection = ( Collection<Object> ) fieldValue;
400 if ( collection != null )
401 {
402 for ( Object object : collection )
403 {
404 if ( object instanceof AdsBaseBean )
405 {
406
407 addBean( containerEntry.getDn(), schemaManager, ( AdsBaseBean ) object,
408 entries, entry, attributeType );
409 }
410 else
411 {
412
413 throw new Exception();
414 }
415 }
416 }
417 }
418 else
419 {
420
421 throw new Exception();
422 }
423 }
424 else
425 {
426
427 addBean( entry.getDn(), schemaManager, ( AdsBaseBean ) fieldValue, entries, entry,
428 attributeType );
429 }
430 }
431 }
432 }
433
434
435 beanClass = beanClass.getSuperclass();
436 }
437 }
438 }
439
440
441
442
443
444
445
446
447
448
449 private String getObjectClassNameForBean( Class<?> c )
450 {
451 String classNameWithPackage = getClassNameWithoutPackageName( c );
452 return "ads-" + classNameWithPackage.substring( 0, classNameWithPackage.length() - 4 );
453 }
454
455
456
457
458
459
460
461
462
463
464 private String getClassNameWithoutPackageName( Class<?> c )
465 {
466 String className = c.getName();
467
468 int firstChar = className.lastIndexOf( '.' ) + 1;
469 if ( firstChar > 0 )
470 {
471 return className.substring( firstChar );
472 }
473
474 return className;
475 }
476
477
478
479
480
481
482
483
484
485
486
487 private boolean isMultiple( Class<?> clazz )
488 {
489 return Collection.class.isAssignableFrom( clazz );
490 }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 private Dn getDn( Dn baseDn, AdsBaseBean bean ) throws LdapInvalidDnException,
507 IllegalAccessException, LdapInvalidAttributeValueException
508 {
509
510 Class<?> beanClass = bean.getClass();
511
512
513
514 boolean adsBaseBeanClassFound = false;
515
516
517 while ( !adsBaseBeanClassFound )
518 {
519
520 if ( beanClass == AdsBaseBean.class )
521 {
522 adsBaseBeanClassFound = true;
523 }
524
525
526 Field[] fields = beanClass.getDeclaredFields();
527 for ( Field field : fields )
528 {
529
530 field.setAccessible( true );
531
532
533
534 ConfigurationElement configurationElement = field.getAnnotation( ConfigurationElement.class );
535 if ( ( configurationElement != null ) && ( configurationElement.isRdn() ) )
536 {
537 return baseDn.add( new Rdn( configurationElement.attributeType(), field.get( bean ).toString() ) );
538 }
539 }
540
541
542 beanClass = beanClass.getSuperclass();
543 }
544
545 return Dn.EMPTY_DN;
546 }
547
548
549
550
551
552
553
554
555
556
557
558
559
560 private void addAttributeTypeValues( String attributeType, Object o, LdifEntry entry )
561 throws LdapException
562 {
563
564 if ( o != null )
565 {
566
567 if ( isMultiple( o.getClass() ) )
568 {
569
570 Collection<?> values = ( Collection<?> ) o;
571
572 for ( Object value : values )
573 {
574 addAttributeTypeValue( attributeType, value, entry );
575 }
576 }
577 else
578 {
579
580 addAttributeTypeValue( attributeType, o, entry );
581 }
582 }
583 }
584
585
586
587
588
589
590
591
592
593
594
595
596
597 private void addAttributeTypeValue( String attributeType, Object value, LdifEntry entry ) throws LdapException
598 {
599
600 if ( value != null )
601 {
602
603 Attribute attribute = entry.get( attributeType );
604
605
606 if ( attribute == null )
607 {
608 attribute = new DefaultAttribute( attributeType );
609 entry.addAttribute( attribute );
610 }
611
612
613 if ( value instanceof byte[] )
614 {
615
616 attribute.add( ( byte[] ) value );
617 }
618
619 else if ( value instanceof Boolean )
620 {
621
622 attribute.add( Strings.toUpperCaseAscii( value.toString() ) );
623 }
624 else
625 {
626
627
628 attribute.add( value.toString() );
629 }
630 }
631 }
632 }