From 50fa8e53262ac5a526c7e8b9c22adc697e5b1a94 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Fri, 09 Sep 2016 09:45:25 +0000
Subject: [PATCH] OPENDJ-3089 OPENDJ-1237 Refactor SchemaWriter class to remove static calls with strong dependencies

---
 opendj-server-legacy/src/main/java/org/opends/server/core/SchemaHandler.java     |   10 +++++++---
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java |    2 +-
 opendj-server-legacy/src/main/java/org/opends/server/types/SchemaWriter.java     |   36 +++++++++++++++++++++---------------
 3 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
index 013e66c..ee46065 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
@@ -225,7 +225,7 @@
       }
     }
 
-    SchemaWriter.updateConcatenatedSchema();
+    new SchemaWriter(serverContext).updateConcatenatedSchema();
 
     // Register with the Directory Server as a configurable component.
     currentConfig.addSchemaChangeListener(this);
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/SchemaHandler.java b/opendj-server-legacy/src/main/java/org/opends/server/core/SchemaHandler.java
index a6b4652..05d50e2 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/SchemaHandler.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/SchemaHandler.java
@@ -139,6 +139,9 @@
    */
   private volatile Schema schema;
 
+  /** Writer which persists schema to disk. */
+  private SchemaWriter schemaWriter;
+
   /**
    * 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.
@@ -164,6 +167,7 @@
   {
     oldestModificationTime = System.currentTimeMillis();
     youngestModificationTime = oldestModificationTime;
+
     // use a default schema
     schema = Schema.getCoreSchema();
   }
@@ -203,6 +207,7 @@
   public void initialize(final ServerContext serverContext) throws InitializationException, ConfigException
   {
     this.serverContext = serverContext;
+    this.schemaWriter = new SchemaWriter(serverContext);
 
     exclusiveLock.lock();
     try
@@ -346,8 +351,7 @@
     {
       switchSchema(newSchema);
       this.extraAttributes = newExtraAttributes;
-      new SchemaWriter(serverContext)
-        .updateSchemaFiles(schema, newExtraAttributes.values(), modifiedSchemaFileNames, alertGenerator);
+      schemaWriter.updateSchemaFiles(schema, newExtraAttributes.values(), modifiedSchemaFileNames, alertGenerator);
       youngestModificationTime = System.currentTimeMillis();
     }
     finally
@@ -370,7 +374,7 @@
     try
     {
       switchSchema(newSchema);
-      SchemaWriter.writeConcatenatedSchema();
+      schemaWriter.writeConcatenatedSchema();
       youngestModificationTime = System.currentTimeMillis();
     }
     finally
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/SchemaWriter.java b/opendj-server-legacy/src/main/java/org/opends/server/types/SchemaWriter.java
index ef9e02c..8586a77 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/SchemaWriter.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/SchemaWriter.java
@@ -75,7 +75,7 @@
 import org.opends.server.util.SchemaUtils;
 
 /**
- * Provides support to write schema files.
+ * Provides support to write schema files to disk.
  */
 public class SchemaWriter
 {
@@ -116,7 +116,7 @@
    * @param mods
    *          The list of modifications into which any identified differences should be written.
    */
-  public static void compareConcatenatedSchema(Set<String> oldElements, Set<String> newElements,
+  private static void compareConcatenatedSchema(Set<String> oldElements, Set<String> newElements,
       AttributeType elementType, List<Modification> mods)
   {
     AttributeBuilder builder = new AttributeBuilder(elementType);
@@ -147,7 +147,7 @@
    * @throws IOException
    *           If a problem occurs while reading the schema file elements.
    */
-  public static void generateConcatenatedSchema(Set<String> attributeTypes, Set<String> objectClasses,
+  private void generateConcatenatedSchema(Set<String> attributeTypes, Set<String> objectClasses,
       Set<String> nameForms, Set<String> ditContentRules, Set<String> ditStructureRules, Set<String> matchingRuleUses,
       Set<String> ldapSyntaxes) throws IOException
   {
@@ -184,9 +184,9 @@
     }
   }
 
-  private static String getSchemaDirectoryPath()
+  private String getSchemaDirectoryPath()
   {
-    File schemaDir = DirectoryServer.getEnvironmentConfig().getSchemaDirectory();
+    File schemaDir = serverContext.getEnvironment().getSchemaDirectory();
     return schemaDir != null ? schemaDir.getAbsolutePath() : null;
   }
 
@@ -216,7 +216,7 @@
    * @throws IOException
    *           If a problem occurs while reading the schema file elements.
    */
-  public static void readConcatenatedSchema(File concatSchemaFile, Set<String> attributeTypes,
+  private static void readConcatenatedSchema(File concatSchemaFile, Set<String> attributeTypes,
       Set<String> objectClasses, Set<String> nameForms, Set<String> ditContentRules, Set<String> ditStructureRules,
       Set<String> matchingRuleUses, Set<String> ldapSyntaxes) throws IOException
   {
@@ -241,7 +241,7 @@
    * @throws InitializationException
    *            If concatenated schema can't be updated
    */
-  public static void updateConcatenatedSchema() throws InitializationException
+  public void updateConcatenatedSchema() throws InitializationException
   {
     try
     {
@@ -306,7 +306,7 @@
    * Writes a single file containing all schema element definitions, which can be used on startup to
    * determine whether the schema files were edited with the server offline.
    */
-  public static void writeConcatenatedSchema()
+  public void writeConcatenatedSchema()
   {
     String concatFilePath = null;
     try
@@ -321,9 +321,7 @@
       generateConcatenatedSchema(attributeTypes, objectClasses, nameForms, ditContentRules, ditStructureRules,
           matchingRuleUses, ldapSyntaxes);
 
-      File configFile = new File(DirectoryServer.getConfigFile());
-      File configDirectory = configFile.getParentFile();
-      File upgradeDirectory = new File(configDirectory, "upgrade");
+      File upgradeDirectory = getUpgradeDirectory();
       upgradeDirectory.mkdir();
       File concatFile = new File(upgradeDirectory, SCHEMA_CONCAT_FILE_NAME);
       concatFilePath = concatFile.getAbsolutePath();
@@ -362,6 +360,15 @@
     }
   }
 
+  /** Returns the upgrade directory of the server. */
+  private File getUpgradeDirectory()
+  {
+    File configFile = serverContext.getEnvironment().getConfigFile();
+    File configDirectory = configFile.getParentFile();
+    File upgradeDirectory = new File(configDirectory, "upgrade");
+    return upgradeDirectory;
+  }
+
   private static void addModification(List<Modification> mods, ModificationType modType, Set<String> included,
       Set<String> excluded, AttributeBuilder builder)
   {
@@ -385,10 +392,9 @@
     definitions.add(getSchemaDefinition(line.substring(attrName.length()), fileName));
   }
 
-  private static File getConcatenatedSchemaFile() throws InitializationException
+  private File getConcatenatedSchemaFile() throws InitializationException
   {
-    File configDirectory = new File(DirectoryServer.getConfigFile()).getParentFile();
-    File upgradeDirectory = new File(configDirectory, "upgrade");
+    File upgradeDirectory = getUpgradeDirectory();
     File concatFile = new File(upgradeDirectory, SCHEMA_CONCAT_FILE_NAME);
     if (concatFile.exists())
     {
@@ -477,7 +483,7 @@
     }
   }
 
-  private static List<StringBuilder> readSchemaElementsFromLdif(File f) throws IOException, FileNotFoundException
+  private List<StringBuilder> readSchemaElementsFromLdif(File f) throws IOException, FileNotFoundException
   {
     final LinkedList<StringBuilder> lines = new LinkedList<>();
 

--
Gitblit v1.10.0