mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
23.02.2016 ec6fcea7eb3b1013db8b26ff65327d3ab24077c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.server.schema;
 
import static java.util.Collections.emptyList;
import static org.opends.messages.ConfigMessages.*;
import static org.opends.messages.SchemaMessages.*;
import static org.opends.server.util.SchemaUtils.getElementSchemaFile;
import static org.opends.server.schema.SchemaConstants.*;
import static org.opends.server.util.ServerConstants.SCHEMA_PROPERTY_FILENAME;
import static org.opends.server.util.StaticUtils.getExceptionMessage;
import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
import org.opends.server.api.AlertGenerator;
import org.opends.server.core.ServerContext;
import org.opends.server.replication.plugin.HistoricalCsnOrderingMatchingRuleImpl;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.adapter.server3x.Converters;
import org.forgerock.opendj.config.ClassPropertyDefinition;
import org.forgerock.opendj.config.server.ConfigException;
import org.forgerock.opendj.ldap.AttributeDescription;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.ModificationType;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.forgerock.opendj.ldap.schema.ConflictingSchemaElementException;
import org.forgerock.opendj.ldap.schema.CoreSchema;
import org.forgerock.opendj.ldap.schema.DITContentRule;
import org.forgerock.opendj.ldap.schema.DITStructureRule;
import org.forgerock.opendj.ldap.schema.MatchingRule;
import org.forgerock.opendj.ldap.schema.MatchingRuleUse;
import org.forgerock.opendj.ldap.schema.NameForm;
import org.forgerock.opendj.ldap.schema.ObjectClass;
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.SchemaBuilder;
import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
import org.forgerock.opendj.ldap.schema.Syntax;
import org.forgerock.opendj.ldap.schema.AttributeType.Builder;
import org.forgerock.opendj.ldap.schema.SchemaBuilder.SchemaBuilderHook;
import org.forgerock.opendj.ldif.EntryReader;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.forgerock.opendj.server.config.meta.SchemaProviderCfgDefn;
import org.forgerock.opendj.server.config.server.RootCfg;
import org.forgerock.opendj.server.config.server.SchemaProviderCfg;
import org.forgerock.util.Option;
import org.forgerock.util.Utils;
import org.opends.server.types.Attribute;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.InitializationException;
import org.opends.server.types.Modification;
import org.opends.server.util.SchemaUtils;
import org.opends.server.util.StaticUtils;
 
/**
 * Responsible for access to the server's schema.
 * <p>
 * The schema handler initializes the schema in four steps :
 * <ul>
 *   <li>Start from the core schema.</li>
 *   <li>Add server specific syntaxes and matching rules</li>
 *   <li>Load schema elements from the schema providers defined in configuration.</li>
 *   <li>Load all schema files located in the schema directory.</li>
 * </ul>
 * <p>
 * The schema handler provides read and write access to the schema.
 * <p>
 * As schema is immutable, there is no risk to alter it outside this handler.
 * However, no long-lived reference should be kept on the schema because further schema
 * updates would be missed. It is advised to retrieve the server's schema from the handler
 * for any operation.
 * <p>
 * The server's schema can be updated using one of the {@code updateSchema()} method.
 */
public final class SchemaHandler
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  private static final String CORE_SCHEMA_PROVIDER_NAME = "Core Schema";
  private static final String CORE_SCHEMA_FILE = "00-core.ldif";
  private static final String RFC_3112_SCHEMA_FILE = "03-rfc3112.ldif";
  private static final String CONFIG_SCHEMA_ELEMENTS_FILE = "02-config.ldif";
  private static final String CORE_SCHEMA_ELEMENTS_FILE = "00-core.ldif";
 
  private static final AttributeType attributeTypesType = CoreSchema.getAttributeTypesAttributeType();
  private static final AttributeType objectClassesType = CoreSchema.getObjectClassesAttributeType();
  private static final AttributeType ditStructureRulesType = CoreSchema.getDITStructureRulesAttributeType();
  private static final AttributeType ditContentRulesType = CoreSchema.getDITContentRulesAttributeType();
  private static final AttributeType ldapSyntaxesType = CoreSchema.getLDAPSyntaxesAttributeType();
  private static final AttributeType matchingRuleUsesType = CoreSchema.getMatchingRuleUseAttributeType();
  private static final AttributeType nameFormsType = CoreSchema.getNameFormsAttributeType();
 
  private ServerContext serverContext;
 
  /**
   * The schema.
   * <p>
   * @GuardedBy("exclusiveLock")
   */
  private volatile Schema schema;
 
  /** Writer which persists schema to disk. */
  private SchemaFilesWriter schemaWriter;
 
  /**
   * The list of offline modifications made to the schema.
   * This list is built when initializing the schema handler.
   */
  private List<Modification> offlineSchemaModifications = emptyList();
 
  /**
   * A set of extra attributes that are not used directly by the schema but may
   * be used by other component to store information in the schema.
   * <p>
   * Example: replication uses this to store its state and GenerationID.
   */
  private Map<String, Attribute> extraAttributes = new HashMap<>();
 
  /** Guards updates to the schema. */
  private final Lock exclusiveLock = new ReentrantLock();
 
  /** The oldest modification time for any schema configuration file. */
  private long oldestModificationTime;
 
  /** The youngest modification time for any schema configuration file. */
  private long youngestModificationTime;
 
 
  /**
   * Creates a new instance.
   */
  public SchemaHandler()
  {
    oldestModificationTime = System.currentTimeMillis();
    youngestModificationTime = oldestModificationTime;
 
    // use a default schema
    schema = Schema.getCoreSchema();
  }
 
  /**
   * Returns the youngest modification time for schema files.
   *
   * @return the youngest modification time for any schema configuration file
   */
  public long getYoungestModificationTime()
  {
    return youngestModificationTime;
  }
 
  /**
   * Returns the oldest modification time for schema files.
   *
   * @return the oldest modification time for any schema configuration file
   */
  public long getOldestModificationTime()
  {
    return oldestModificationTime;
  }
 
  /**
   * Initializes this schema handler.
   *
   * @param serverContext
   *          The server context.
   * @throws ConfigException
   *           If a configuration problem arises in the process of performing
   *           the initialization.
   * @throws InitializationException
   *           If a problem that is not configuration-related occurs during
   *           initialization.
   */
  public void initialize(final ServerContext serverContext) throws InitializationException, ConfigException
  {
    this.serverContext = serverContext;
    this.schemaWriter = new SchemaFilesWriter(serverContext);
 
      // Start from the core schema
      final SchemaBuilder schemaBuilder = new SchemaBuilder(Schema.getCoreSchema());
 
      loadSchemaFromProviders(serverContext.getRootConfig(), schemaBuilder);
 
      addServerSyntaxesAndMatchingRules(schemaBuilder);
 
      loadSchemaFromFiles(schemaBuilder);
 
      try
      {
        updateSchema(schemaBuilder.toSchema());
      }
      catch (DirectoryException e)
      {
        throw new ConfigException(e.getMessageObject(), e);
      }
  }
 
  /**
   * Adds server's specific syntaxes and matching rules not provided by the SDK to the provided schema
   * builder.
   *
   * @param schemaBuilder
   *            The schema builder
   * @throws ConfigException
   *            If there is a schema conflict for the added elements
   */
  public static void addServerSyntaxesAndMatchingRules(final SchemaBuilder schemaBuilder) throws ConfigException
  {
    try
    {
      addAciSyntax(schemaBuilder);
      addSubtreeSpecificationSyntax(schemaBuilder);
 
      addHistoricalCsnOrderingMatchingRule(schemaBuilder);
      addAuthPasswordEqualityMatchingRule(schemaBuilder);
      addUserPasswordEqualityMatchingRule(schemaBuilder);
    }
    catch (ConflictingSchemaElementException e)
    {
      throw new ConfigException(e.getMessageObject(), e);
    }
  }
 
  private static void addAciSyntax(SchemaBuilder builder)
  {
    builder
      .buildSyntax(SYNTAX_ACI_OID)
      .description(SYNTAX_ACI_DESCRIPTION)
      .implementation(new AciSyntaxImpl())
      .addToSchema();
  }
 
  private static void addSubtreeSpecificationSyntax(SchemaBuilder builder)
  {
    builder
      .buildSyntax(SYNTAX_SUBTREE_SPECIFICATION_OID)
      .description(SYNTAX_SUBTREE_SPECIFICATION_DESCRIPTION)
      .implementation(new SubtreeSpecificationSyntaxImpl())
      .addToSchema();
  }
 
  private static void addHistoricalCsnOrderingMatchingRule(SchemaBuilder builder)
  {
    builder
      .buildMatchingRule("1.3.6.1.4.1.26027.1.4.4")
      .names("historicalCsnOrderingMatch")
      .syntaxOID("1.3.6.1.4.1.1466.115.121.1.40")
      .implementation(new HistoricalCsnOrderingMatchingRuleImpl())
      .addToSchema();
  }
 
  private static void addAuthPasswordEqualityMatchingRule(SchemaBuilder builder)
  {
    builder.buildMatchingRule(EMR_AUTH_PASSWORD_OID)
      .names(EMR_AUTH_PASSWORD_NAME)
      .syntaxOID(SYNTAX_AUTH_PASSWORD_OID).description(EMR_AUTH_PASSWORD_DESCRIPTION)
      .implementation(new AuthPasswordEqualityMatchingRule())
      .addToSchema();
  }
 
  private static void addUserPasswordEqualityMatchingRule(SchemaBuilder builder)
  {
    builder.buildMatchingRule(EMR_USER_PASSWORD_OID)
      .names(EMR_USER_PASSWORD_NAME)
      .syntaxOID(SYNTAX_OCTET_STRING_OID).description(EMR_USER_PASSWORD_DESCRIPTION)
      .implementation(new UserPasswordEqualityMatchingRule())
      .addToSchema();
  }
 
  /**
   * Detects offline schema changes by comparing schema files and concatenated schema.
   * <p>
   * Updates the concatenated schema if changes are detected.
   *
   * @throws InitializationException
   *            If an error occurs while updating the concatenated schema
   */
  public void detectChangesOnInitialization() throws InitializationException
  {
    offlineSchemaModifications = Collections.unmodifiableList(schemaWriter.updateConcatenatedSchemaIfChangesDetected());
  }
 
  /**
   * Returns the schema.
   *
   * @return the schema
   */
  public Schema getSchema()
  {
    return schema;
  }
 
  /**
   * Retrieves the path to the directory containing the server schema files.
   *
   * @return The path to the directory containing the server schema files.
   * @throws InitializationException
   *            If the directory path does not exists or is not a directory
   */
  public File getSchemaDirectoryPath() throws InitializationException
  {
    final File dir = serverContext.getEnvironment().getSchemaDirectory();
    if (dir == null)
    {
      throw new InitializationException(ERR_CONFIG_SCHEMA_NO_SCHEMA_DIR.get(null));
    }
    if (!dir.exists())
    {
      throw new InitializationException(ERR_CONFIG_SCHEMA_NO_SCHEMA_DIR.get(dir.getPath()));
    }
    if (!dir.isDirectory())
    {
      throw new InitializationException(ERR_CONFIG_SCHEMA_DIR_NOT_DIRECTORY.get(dir.getPath()));
    }
    return dir;
  }
 
  /**
   * Returns the list of offline modifications made to the schema, which is built once when
   * initializing the schema.
   *
   * @return the offline schema modifications list
   */
  public List<Modification> getOfflineSchemaModifications()
  {
    return offlineSchemaModifications;
  }
 
  /**
   * Updates the schema using the provided schema updater.
   * <p>
   * The schema files are not updated.
   *
   * @param updater
   *          the updater that performs modifications on the schema builder
   * @throws DirectoryException if there is any problem updating the schema
   */
  public void updateSchema(SchemaUpdater updater) throws DirectoryException
  {
    exclusiveLock.lock();
    try
    {
      SchemaBuilder schemaBuilder = new SchemaBuilder(schema);
      updater.update(schemaBuilder);
      switchSchema(schemaBuilder.toSchema());
    }
    finally
    {
      exclusiveLock.unlock();
    }
  }
 
  /**
   * Replaces the schema with the provided schema.
   * <p>
   * The schema files are not updated.
   *
   * @param schema
   *          the new schema to use
   * @throws DirectoryException
   *            if the provided schema contains warnings
   */
  public void updateSchema(Schema schema) throws DirectoryException
  {
    exclusiveLock.lock();
    try
    {
      switchSchema(schema);
    }
    finally
    {
      exclusiveLock.unlock();
    }
  }
 
  /**
   * Replaces the schema with the provided schema and updates the provided set of schema files.
   * <p>
   * The concatenated schema file is updated as well.
   *
   * @param newSchema
   *            The new schema to use
   * @param newExtraAttributes
   *            The new map of extra attributes
   * @param modifiedSchemaFileNames
   *            The set of names of schema files that need to be updated
   * @param alertGenerator
   *            The generator to use for alerts
   * @throws DirectoryException
   *            If an error occurs during update of schema or schema files
   */
  public void updateSchemaAndSchemaFiles(Schema newSchema, Map<String, Attribute> newExtraAttributes,
      TreeSet<String> modifiedSchemaFileNames, AlertGenerator alertGenerator) throws DirectoryException
  {
    exclusiveLock.lock();
    try
    {
      switchSchema(newSchema);
      this.extraAttributes = newExtraAttributes;
      schemaWriter.updateSchemaFiles(schema, newExtraAttributes.values(), modifiedSchemaFileNames, alertGenerator);
      youngestModificationTime = System.currentTimeMillis();
    }
    finally
    {
      exclusiveLock.unlock();
    }
  }
 
  /**
   * Replaces the schema with the provided schema and update the concatenated schema file.
   *
   * @param newSchema
   *            The new schema to use
   * @throws DirectoryException
   *            If an error occurs during update of schema or schema files
   */
  public void updateSchemaAndConcatenatedSchemaFile(Schema newSchema) throws DirectoryException
  {
    exclusiveLock.lock();
    try
    {
      switchSchema(newSchema);
      schemaWriter.writeConcatenatedSchema();
      youngestModificationTime = System.currentTimeMillis();
    }
    finally
    {
      exclusiveLock.unlock();
    }
  }
 
  /**
   * Updates the schema option  if the new value differs from the old value.
   *
   * @param <T> the schema option's type
   * @param option the schema option to update
   * @param newValue the new value for the schema option
   * @throws DirectoryException if there is any problem updating the schema
   */
  public <T> void updateSchemaOption(final Option<T> option, final T newValue) throws DirectoryException
  {
    final T oldValue = schema.getOption(option);
    if (!oldValue.equals(newValue))
    {
      updateSchema(new SchemaUpdater()
      {
        @Override
        public void update(SchemaBuilder builder)
        {
          builder.setOption(option, newValue);
        }
      });
    }
  }
 
  /**
   * Returns the extra attributes stored in this schema handler.
   *
   * @return  The extra attributes.
   */
  public Map<String, Attribute> getExtraAttributes()
  {
    return new HashMap<>(extraAttributes);
  }
 
  /** Takes an exclusive lock on the schema. */
  public void exclusiveLock()
  {
    exclusiveLock.lock();
  }
 
  /** Releases an exclusive lock on the schema. */
  public void exclusiveUnlock()
  {
    exclusiveLock.unlock();
  }
 
  /**
   * Destroys the structures maintained by this handler so that they are no longer usable.
   * <p>
   * This should only be called at the end of the server shutdown process, and it can help detect
   * inappropriate cached references.
   */
  public void destroy()
  {
    exclusiveLock.lock();
    try
    {
      if (schema != null)
      {
        schema = null;
      }
      if (extraAttributes != null)
      {
        extraAttributes.clear();
        extraAttributes = null;
      }
    }
    finally
    {
      exclusiveLock.unlock();
    }
  }
 
  /**
   * Imports the provided schema entry in the schema.
   * <p>
   * The behavior is:
   * <ul>
   *  <li>iterate over each element of the newSchemaEntry and compare with the existing schema</li>
   *  <li>if the new schema element does not exist in current schema, add it to the schema</li>
   *  <li>if an element of current schema is not in the new schema entry: delete it</li>
   * </ul>
   * <p>
   * FIXME: currently, attributeTypes and objectClasses are the only elements taken into account.
   *
   * @param newSchemaEntry
   *          The schema entry to be imported.
   * @param alertGenerator
   *          Alert generator to use.
   * @throws DirectoryException
   *           If an error occurs during the import
   */
  public void importEntry(org.opends.server.types.Entry newSchemaEntry, AlertGenerator alertGenerator)
      throws DirectoryException
  {
    // work on a fixed schema version
    Schema currentSchema = schema;
    SchemaBuilder newSchemaBuilder = new SchemaBuilder(currentSchema);
    TreeSet<String> modifiedSchemaFiles = new TreeSet<>();
 
    // loop on the attribute types in the entry just received
    // and add them in the existing schema.
    Set<String> oidList = new HashSet<>(1000);
    for (Attribute a : newSchemaEntry.getAllAttributes(attributeTypesType))
    {
      // Look for attribute types that could have been added to the schema
      // or modified in the schema
      for (ByteString v : a)
      {
        String definition = v.toString();
        String schemaFile = SchemaUtils.parseSchemaFileFromElementDefinition(definition);
        if (is02ConfigLdif(schemaFile))
        {
          // Do not import the file containing the definitions of the Schema elements used for configuration
          // because these definitions may vary between versions of OpenDJ.
          continue;
        }
 
        String oid = SchemaUtils.parseAttributeTypeOID(definition);
        oidList.add(oid);
        try
        {
          // Register this attribute type in the new schema
          // unless it is already defined with the same syntax.
          if (hasAttributeTypeDefinitionChanged(currentSchema, oid, definition))
          {
            newSchemaBuilder.addAttributeType(definition, true);
            addElementIfNotNull(modifiedSchemaFiles, schemaFile);
          }
        }
        catch (Exception e)
        {
          logger.info(NOTE_SCHEMA_IMPORT_FAILED, definition, e.getMessage());
        }
      }
    }
 
    // loop on all the attribute types in the current schema and delete
    // them from the new schema if they are not in the imported schema entry.
    for (AttributeType removeType : currentSchema.getAttributeTypes())
    {
      String schemaFile = getElementSchemaFile(removeType);
      if (is02ConfigLdif(schemaFile) || CORE_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
      {
        // Also never delete anything from the core schema file.
        continue;
      }
      if (!oidList.contains(removeType.getOID()))
      {
        newSchemaBuilder.removeAttributeType(removeType.getOID());
        addElementIfNotNull(modifiedSchemaFiles, schemaFile);
      }
    }
 
    // loop on the objectClasses from the entry, search if they are
    // already in the current schema, add them if not.
    oidList.clear();
    for (Attribute a : newSchemaEntry.getAllAttributes(objectClassesType))
    {
      for (ByteString v : a)
      {
        // It IS important here to allow the unknown elements that could
        // appear in the new config schema.
        String definition = v.toString();
        String schemaFile = SchemaUtils.parseSchemaFileFromElementDefinition(definition);
        if (is02ConfigLdif(schemaFile))
        {
          continue;
        }
        String oid = SchemaUtils.parseObjectClassOID(definition);
        oidList.add(oid);
        try
        {
          // Register this ObjectClass in the new schema
          // unless it is already defined with the same syntax.
          if (hasObjectClassDefinitionChanged(currentSchema, oid, definition))
          {
            newSchemaBuilder.addObjectClass(definition, true);
            addElementIfNotNull(modifiedSchemaFiles, schemaFile);
          }
        }
        catch (Exception e)
        {
          logger.info(NOTE_SCHEMA_IMPORT_FAILED, definition, e.getMessage());
        }
      }
    }
 
    // loop on all the object classes in the current schema and delete
    // them from the new schema if they are not in the imported schema entry.
    for (ObjectClass removeClass : currentSchema.getObjectClasses())
    {
      String schemaFile = getElementSchemaFile(removeClass);
      if (is02ConfigLdif(schemaFile))
      {
        continue;
      }
      if (!oidList.contains(removeClass.getOID()))
      {
        newSchemaBuilder.removeObjectClass(removeClass.getOID());
        addElementIfNotNull(modifiedSchemaFiles, schemaFile);
      }
    }
 
    // Finally, if there were some modifications, save the new schema
    if (!modifiedSchemaFiles.isEmpty())
    {
      Schema newSchema = newSchemaBuilder.toSchema();
      updateSchemaAndSchemaFiles(newSchema, getExtraAttributes(), modifiedSchemaFiles, alertGenerator);
    }
  }
 
  private boolean is02ConfigLdif(String schemaFile)
  {
    return CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile);
  }
 
  private <T> void addElementIfNotNull(Collection<T> col, T element)
  {
    if (element != null)
    {
      col.add(element);
    }
  }
 
  private boolean hasAttributeTypeDefinitionChanged(Schema schema, String oid, String definition)
  {
    if (schema.hasAttributeType(oid))
    {
      AttributeType oldAttrType = schema.getAttributeType(oid);
      return !oldAttrType.toString().equals(definition);
 
    }
    return true;
  }
 
  private boolean hasObjectClassDefinitionChanged(Schema schema, String oid, String definition)
  {
    if (schema.hasObjectClass(oid))
    {
      ObjectClass oldObjectClass = schema.getObjectClass(oid);
      return !oldObjectClass.toString().equals(definition);
    }
    return true;
  }
 
  /**
   * Load the schema from provided root configuration.
   *
   * @param rootConfiguration
   *          The root to retrieve schema provider configurations.
   * @param schemaBuilder
   *          The schema builder that providers should update.
   * @param schemaUpdater
   *          The updater that providers should use when applying a configuration change.
   */
  private void loadSchemaFromProviders(final RootCfg rootConfiguration, final SchemaBuilder schemaBuilder)
      throws ConfigException, InitializationException {
    for (final String name : rootConfiguration.listSchemaProviders())
    {
      final SchemaProviderCfg config = rootConfiguration.getSchemaProvider(name);
      if (config.isEnabled())
      {
        loadSchemaProvider(config.getJavaClass(), config, schemaBuilder, true);
      }
      else if (name.equals(CORE_SCHEMA_PROVIDER_NAME))
      {
        throw new ConfigException(ERR_CONFIG_CORE_SCHEMA_PROVIDER_DISABLED.get(config.dn()));
      }
    }
  }
 
  /**
   * Load the schema provider from the provided class name.
   * <p>
   * If {@code} initialize} is {@code true}, then the provider is initialized,
   * and the provided schema builder is updated with schema elements from the provider.
   */
  private <T extends SchemaProviderCfg> SchemaProvider<T> loadSchemaProvider(final String className,
      final T config, final SchemaBuilder schemaBuilder, final boolean initialize)
      throws InitializationException
  {
    try
    {
      final ClassPropertyDefinition propertyDef = SchemaProviderCfgDefn.getInstance().getJavaClassPropertyDefinition();
      final Class<? extends SchemaProvider> providerClass = propertyDef.loadClass(className, SchemaProvider.class);
      final SchemaProvider<T> provider = providerClass.newInstance();
 
      if (initialize)
      {
        provider.initialize(serverContext, config, schemaBuilder);
      }
      else
      {
        final List<LocalizableMessage> unacceptableReasons = new ArrayList<>();
        if (!provider.isConfigurationAcceptable(config, unacceptableReasons))
        {
          final String reasons = Utils.joinAsString(".  ", unacceptableReasons);
          throw new InitializationException(ERR_CONFIG_SCHEMA_PROVIDER_CONFIG_NOT_ACCEPTABLE.get(config.dn(), reasons));
        }
      }
      return provider;
    }
    catch (Exception e)
    {
      throw new InitializationException(ERR_CONFIG_SCHEMA_PROVIDER_CANT_BE_INITIALIZED.get(
          className, config.dn(), stackTraceToSingleLineString(e)), e);
    }
  }
 
  /** Returns the LDIF reader on provided LDIF file. The caller must ensure the reader is closed. */
  private EntryReader getLDIFReader(final File ldifFile, final Schema schema)
      throws InitializationException
  {
    try
    {
      final LDIFEntryReader reader = new LDIFEntryReader(new FileReader(ldifFile));
      reader.setSchema(schema);
      reader.setSchemaValidationPolicy(SchemaValidationPolicy.ignoreAll());
      return reader;
    }
    catch (FileNotFoundException e)
    {
      throw new InitializationException(WARN_CONFIG_SCHEMA_CANNOT_OPEN_FILE.get(ldifFile.getPath(),
          ldifFile.getParent(), StaticUtils.getExceptionMessage(e)));
    }
  }
 
  /**
   * Completes the schema with schema files.
   *
   * @param schemaBuilder
   *          The schema builder to update with the content of the schema files.
   * @throws ConfigException
   *           If a configuration problem causes the schema element
   *           initialization to fail.
   * @throws InitializationException
   *           If a problem occurs while initializing the schema elements that
   *           is not related to the server configuration.
   */
  private void loadSchemaFromFiles(final SchemaBuilder schemaBuilder)
      throws ConfigException, InitializationException
  {
    final File schemaDirectory = getSchemaDirectoryPath();
    final File[] schemaFiles = SchemaUtils.getSchemaFiles(schemaDirectory);
    final List<String> schemaFileNames = StaticUtils.getFileNames(schemaFiles);
    updateModificationTimes(schemaFiles);
 
    for (String schemaFile : schemaFileNames)
    {
      loadSchemaFile(new File(schemaDirectory, schemaFile), schemaBuilder, Schema.getDefaultSchema(), false);
    }
  }
 
  private void updateModificationTimes(final File[] schemaFiles)
  {
    for (final File file : schemaFiles)
    {
 
      final long modificationTime = file.lastModified();
      if (oldestModificationTime <= 0L
          || oldestModificationTime >= modificationTime)
      {
        oldestModificationTime = modificationTime;
      }
 
      if (youngestModificationTime <= 0
          || youngestModificationTime <= modificationTime)
      {
        youngestModificationTime = modificationTime;
      }
    }
    // If the oldest and youngest modification timestamps didn't get set
    // then set them to the current time.
    if (oldestModificationTime <= 0)
    {
      oldestModificationTime = System.currentTimeMillis();
    }
 
    if (youngestModificationTime <= 0)
    {
      youngestModificationTime = oldestModificationTime;
    }
  }
 
  /** Returns the schema entry from the provided reader, which may be {@code null} if file is empty. */
  private Entry readSchemaEntry(final File schemaFile, final Schema readSchema) throws InitializationException
  {
    try (EntryReader reader = getLDIFReader(schemaFile, readSchema))
    {
      if (!reader.hasNext())
      {
        // empty file, just skip it
        return null;
      }
      Entry entry = reader.readEntry();
      if (reader.hasNext())
      {
        logger.warn(WARN_CONFIG_SCHEMA_MULTIPLE_ENTRIES_IN_FILE, schemaFile.getPath(), schemaFile.getParent());
      }
      return entry;
    }
    catch (IOException e)
    {
      throw new InitializationException(WARN_CONFIG_SCHEMA_CANNOT_READ_LDIF_ENTRY.get(schemaFile.getPath(), schemaFile
          .getParent(), getExceptionMessage(e)), e);
    }
  }
 
  /**
   * Loads the contents of the provided schema file into the provided schema builder.
   * <p>
   * This method has no effect on the current schema.
   *
   * @param schemaFile
   *            The schema file to load.
   * @param schemaBuilder
   *            The schema builder to update.
   * @param readSchema
   *            The schema used to read the schema file.
   * @throws InitializationException
   *            If a problem occurs when reading the schema file.
   * @throws ConfigException
   *            If a problem occurs when updating the schema builder.
   */
  public void loadSchemaFileIntoSchemaBuilder(final File schemaFile, final SchemaBuilder schemaBuilder,
      final Schema readSchema) throws InitializationException, ConfigException
  {
    loadSchemaFile(schemaFile, schemaBuilder, readSchema, true);
  }
 
  /**
   * Loads the contents of the provided schema file into the provided schema builder and returns the list
   * of modifications.
   * <p>
   * This method has no effect on the current schema.
   *
   * @param schemaFile
   *          The schema file to load.
   * @param schemaBuilder
   *            The schema builder to update.
   * @param readSchema
   *            The schema used to read the schema file.
   * @return A list of the modifications that could be performed in order to obtain the contents of
   *         the file.
   * @throws ConfigException
   *           If a configuration problem causes the schema element initialization to fail.
   * @throws InitializationException
   *           If a problem occurs while initializing the schema elements that is not related to the
   *           server configuration.
   */
  public List<Modification> loadSchemaFileIntoSchemaBuilderAndReturnModifications(final File schemaFile,
      final SchemaBuilder schemaBuilder, final Schema readSchema) throws InitializationException, ConfigException
  {
    final Entry entry = loadSchemaFile(schemaFile, schemaBuilder, readSchema, true);
    if (entry != null)
    {
      return createAddModifications(entry,
          ldapSyntaxesType,
          attributeTypesType,
          objectClassesType,
          nameFormsType,
          ditContentRulesType,
          ditStructureRulesType,
          matchingRuleUsesType);
    }
    return Collections.emptyList();
  }
 
  private List<Modification> createAddModifications(Entry entry, AttributeType... attrTypes)
  {
    List<Modification> mods = new ArrayList<>(entry.getAttributeCount());
    for (AttributeType attrType : attrTypes)
    {
      for (org.forgerock.opendj.ldap.Attribute a : entry.getAllAttributes(AttributeDescription.create(attrType)))
      {
        mods.add(new Modification(ModificationType.ADD, Converters.toAttribute(a)));
      }
    }
    return mods;
  }
 
  /**
   * Add the schema from the provided schema file to the provided schema
   * builder.
   *
   * @param schemaFile
   *          the schema file to be loaded
   * @param schemaBuilder
   *          The schema builder in which the contents of the schema file are to
   *          be loaded.
   * @param readSchema
   *          The schema used to read the file.
   * @param failOnError
   *          Indicates whether an exception must be thrown if an error occurs
   * @return the schema entry that has been read from the schema file
   * @throws InitializationException
   *           If a problem occurs while initializing the schema elements.
   * @throws ConfigException
   *           If a problem occurs when updating the schema builder.
   */
  private Entry loadSchemaFile(final File schemaFile, final SchemaBuilder schemaBuilder, final Schema readSchema,
      boolean failOnError)
         throws InitializationException, ConfigException
  {
    final Entry entry = readSchemaEntry(schemaFile, readSchema);
    if (entry != null)
    {
      updateSchemaBuilderWithEntry(schemaBuilder, entry, schemaFile.getName(), failOnError);
    }
    return entry;
 
  }
 
  private void updateSchemaBuilderWithEntry(SchemaBuilder schemaBuilder, Entry schemaEntry, String schemaFile,
      boolean failOnError) throws ConfigException
  {
 
    // immediately overwrite these definitions which are already defined in the SDK core schema
    final boolean overwriteCoreSchemaDefinitions =
        CORE_SCHEMA_FILE.equals(schemaFile) || RFC_3112_SCHEMA_FILE.equals(schemaFile);
 
    updateSchemaBuilderWithEntry0(schemaBuilder, schemaEntry, schemaFile, overwriteCoreSchemaDefinitions);
 
    // check that the update is correct
    Collection<LocalizableMessage> warnings = schemaBuilder.toSchema().getWarnings();
    if (!warnings.isEmpty())
    {
      if (!overwriteCoreSchemaDefinitions)
      {
        logger.warn(WARN_CONFIG_SCHEMA_FILE_HAS_SCHEMA_WARNING, schemaFile, warnings);
        // try to update again with overwriting
        updateSchemaBuilderWithEntry0(schemaBuilder, schemaEntry, schemaFile, true);
        warnings = schemaBuilder.toSchema().getWarnings();
        if (!warnings.isEmpty())
        {
          reportSchemaWarnings(WARN_CONFIG_SCHEMA_FILE_HAS_SCHEMA_WARNING_WITH_OVERWRITE.get(schemaFile, warnings),
              failOnError);
        }
      }
      else
      {
        reportSchemaWarnings(WARN_CONFIG_SCHEMA_FILE_HAS_SCHEMA_WARNING_WITH_OVERWRITE.get(schemaFile, warnings),
            failOnError);
      }
    }
  }
 
  private void updateSchemaBuilderWithEntry0(final SchemaBuilder schemaBuilder, final Entry schemaEntry,
      final String schemaFile, final boolean overwrite)
  {
    schemaBuilder.addSchema(schemaEntry, overwrite, new SchemaBuilderHook()
    {
      @Override
      public void beforeAddSyntax(Syntax.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddObjectClass(ObjectClass.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddNameForm(NameForm.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddMatchingRuleUse(MatchingRuleUse.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddMatchingRule(MatchingRule.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddDitStructureRule(DITStructureRule.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddDitContentRule(DITContentRule.Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
 
      @Override
      public void beforeAddAttribute(Builder builder)
      {
        builder.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
      }
    });
  }
 
  private void switchSchema(Schema newSchema) throws DirectoryException
  {
    rejectSchemaWithWarnings(newSchema);
    schema = newSchema.asNonStrictSchema();
    Schema.setDefaultSchema(schema);
  }
 
  private void rejectSchemaWithWarnings(Schema newSchema) throws DirectoryException
  {
    Collection<LocalizableMessage> warnings = newSchema.getWarnings();
    if (!warnings.isEmpty())
    {
      throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION,
          ERR_SCHEMA_HAS_WARNINGS.get(warnings.size(), Utils.joinAsString("; ", warnings)));
    }
  }
 
  private void reportSchemaWarnings(LocalizableMessage message, boolean failOnError) throws ConfigException
  {
    if (failOnError)
    {
      throw new ConfigException(message);
    }
    logger.error(message);
  }
 
  /** Interface to update a schema provided a schema builder. */
  public interface SchemaUpdater
  {
    /**
     * Updates the schema using the provided schema builder.
     *
     * @param builder
     *          The builder on the current schema
     * @throws DirectoryException
     *          If an error occurs during the schema update
     */
    void update(SchemaBuilder builder) throws DirectoryException;
  }
}