From 2c7d26f5b94a5e4e964dbafd56f30837e53db6d3 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Fri, 09 Sep 2016 09:45:23 +0000
Subject: [PATCH] OPENDJ-3089 OPENDJ-1237 Remove management of schema files modification times from SchemaBackend

---
 opendj-server-legacy/src/main/java/org/opends/server/core/SchemaHandler.java     |  140 ++++++++++++++++++++++++++++++----------------
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java |   34 +++-------
 2 files changed, 101 insertions(+), 73 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 efa3d17..573058e 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
@@ -22,7 +22,6 @@
 import static org.opends.messages.ConfigMessages.*;
 import static org.opends.messages.SchemaMessages.*;
 import static org.opends.server.config.ConfigConstants.*;
-import static org.opends.server.core.DirectoryServer.*;
 import static org.opends.server.schema.GeneralizedTimeSyntax.*;
 import static org.opends.server.util.CollectionUtils.*;
 import static org.opends.server.util.SchemaUtils.*;
@@ -136,12 +135,6 @@
   private ByteString creatorsName;
   /** The value containing the DN of the last user to modify the configuration. */
   private ByteString modifiersName;
-  /** The timestamp that will be used for the schema creation time. */
-  private ByteString createTimestamp;
-  /** The timestamp that will be used for the latest schema modification time. */
-  private ByteString modifyTimestamp;
-  /** The time that the schema was last modified. */
-  private long modifyTime;
 
   /** The DN of the configuration entry for this backend. */
   private DN configEntryDN;
@@ -179,6 +172,7 @@
   public void configureBackend(SchemaBackendCfg cfg, ServerContext serverContext) throws ConfigException
   {
     this.serverContext = serverContext;
+    this.schemaHandler = serverContext.getSchemaHandler();
 
     // Make sure that a configuration entry was provided.  If not, then we will
     // not be able to complete initialization.
@@ -203,8 +197,6 @@
     ByteString newBaseDN = ByteString.valueOfUtf8(baseDNs.iterator().next().toString());
     creatorsName = newBaseDN;
     modifiersName = newBaseDN;
-    createTimestamp = createGeneralizedTimeValue(getSchema().getOldestModificationTime());
-    modifyTimestamp = createGeneralizedTimeValue(getSchema().getYoungestModificationTime());
 
     // Get the set of user-defined attributes for the configuration entry.  Any
     // attributes that we don't recognize will be included directly in the
@@ -212,7 +204,6 @@
     userDefinedAttributes = new ArrayList<>();
     addAllNonSchemaConfigAttributes(userDefinedAttributes, configEntry.getAllAttributes());
 
-    schemaHandler = serverContext.getSchemaHandler();
 
     currentConfig = cfg;
   }
@@ -415,22 +406,20 @@
         operationalAttrs, matchingRuleUsesType, includeSchemaFile, false, true);
 
     // Add the lastmod attributes.
-    if (DirectoryServer.getSchema().getYoungestModificationTime() != modifyTime)
-    {
-      synchronized (this)
-      {
-        modifyTime = DirectoryServer.getSchema().getYoungestModificationTime();
-        modifyTimestamp = createGeneralizedTimeValue(modifyTime);
-      }
-    }
     addAttributeToSchemaEntry(
-        Attributes.create(getCreatorsNameAttributeType(), creatorsName), userAttrs, operationalAttrs);
+        Attributes.create(getCreatorsNameAttributeType(), creatorsName),
+        userAttrs, operationalAttrs);
     addAttributeToSchemaEntry(
-        Attributes.create(getCreateTimestampAttributeType(), createTimestamp), userAttrs, operationalAttrs);
+        Attributes.create(getCreateTimestampAttributeType(),
+            createGeneralizedTimeValue(schemaHandler.getOldestModificationTime())),
+        userAttrs, operationalAttrs);
     addAttributeToSchemaEntry(
-        Attributes.create(getModifiersNameAttributeType(), modifiersName), userAttrs, operationalAttrs);
+        Attributes.create(getModifiersNameAttributeType(), modifiersName),
+        userAttrs, operationalAttrs);
     addAttributeToSchemaEntry(
-        Attributes.create(getModifyTimestampAttributeType(), modifyTimestamp), userAttrs, operationalAttrs);
+        Attributes.create(getModifyTimestampAttributeType(),
+            createGeneralizedTimeValue(schemaHandler.getYoungestModificationTime())),
+        userAttrs, operationalAttrs);
 
     // Add the extra attributes.
     for (Attribute attribute : DirectoryServer.getSchema().getExtraAttributes())
@@ -558,7 +547,6 @@
     }
 
     modifiersName = ByteString.valueOfUtf8(authzDN.toString());
-    modifyTimestamp = createGeneralizedTimeValue(System.currentTimeMillis());
   }
 
   private void applyModifications(SchemaBuilder newSchemaBuilder, List<Modification> mods,
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 b6a70f1..94e0264 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
@@ -15,12 +15,12 @@
  */
 package org.opends.server.core;
 
-import static org.opends.messages.ConfigMessages.WARN_CONFIG_SCHEMA_CANNOT_OPEN_FILE;
-import static org.opends.server.util.ServerConstants.SCHEMA_PROPERTY_FILENAME;
-import static org.opends.messages.SchemaMessages.ERR_SCHEMA_HAS_WARNINGS;
-import static org.forgerock.util.Utils.*;
+import static org.forgerock.util.Utils.closeSilently;
 import static org.opends.messages.ConfigMessages.*;
-import static org.opends.server.util.StaticUtils.*;
+import static org.opends.messages.SchemaMessages.ERR_SCHEMA_HAS_WARNINGS;
+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;
@@ -114,16 +114,40 @@
   /** Guards updates to the schema. */
   private final Lock exclusiveLock = new ReentrantLock();
 
-  private long oldestModificationTime = -1L;
+  /** The oldest modification time for any schema configuration file, as a byte string. */
+  private long oldestModificationTime;
 
-  private long youngestModificationTime = -1L;
+  /** The youngest modification time for any schema configuration file. */
+  private long youngestModificationTime;
+
 
   /**
    * Creates a new instance.
    */
   public SchemaHandler()
   {
-    // no implementation.
+    oldestModificationTime = System.currentTimeMillis();
+    youngestModificationTime = oldestModificationTime;
+  }
+
+  /**
+   * 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;
   }
 
   /**
@@ -257,6 +281,7 @@
     {
       switchSchema(schema);
       new SchemaWriter().updateSchemaFiles(schemaNG, getExtraAttributes(), modifiedSchemaFiles, alertGenerator);
+      youngestModificationTime = System.currentTimeMillis();
     }
     finally
     {
@@ -429,7 +454,7 @@
   }
 
   /**
-   * Complete the schema with schema files.
+   * Completes the schema with schema files.
    *
    * @param schemaBuilder
    *          The schema builder to update with the content of the schema files.
@@ -444,58 +469,73 @@
       throws ConfigException, InitializationException
   {
     final File schemaDirectory = getSchemaDirectoryPath();
-    for (String schemaFile : getSchemaFileNames(schemaDirectory))
+    final File[] schemaFiles = getSchemaFiles(schemaDirectory);
+    final List<String> schemaFileNames = getSchemaFileNames(schemaFiles);
+    updateModificationTimes(schemaFiles);
+
+    for (String schemaFile : schemaFileNames)
     {
       loadSchemaFile(new File(schemaDirectory, schemaFile), schemaBuilder, Schema.getDefaultSchema());
     }
   }
 
-  /** Returns the list of names of schema files contained in the provided directory. */
-  private List<String> getSchemaFileNames(final File schemaDirectory) throws InitializationException {
+  private File[] getSchemaFiles(File schemaDirectory) throws InitializationException
+  {
     try
     {
-      final File[] schemaFiles = schemaDirectory.listFiles(new SchemaFileFilter());
-      final List<String> schemaFileNames = new ArrayList<>(schemaFiles.length);
-
-      for (final File f : schemaFiles)
-      {
-        if (f.isFile())
-        {
-          schemaFileNames.add(f.getName());
-        }
-
-        final long modificationTime = f.lastModified();
-        if (oldestModificationTime <= 0L
-            || modificationTime < oldestModificationTime)
-        {
-          oldestModificationTime = modificationTime;
-        }
-
-        if (youngestModificationTime <= 0
-            || modificationTime > youngestModificationTime)
-        {
-          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;
-      }
-      Collections.sort(schemaFileNames);
-      return schemaFileNames;
+      return schemaDirectory.listFiles(new SchemaFileFilter());
     }
     catch (Exception e)
     {
       logger.traceException(e);
-      throw new InitializationException(ERR_CONFIG_SCHEMA_CANNOT_LIST_FILES
-          .get(schemaDirectory, getExceptionMessage(e)), e);
+      throw new InitializationException(
+          ERR_CONFIG_SCHEMA_CANNOT_LIST_FILES.get(schemaDirectory, getExceptionMessage(e)), e);
+    }
+  }
+
+  /** Returns the sorted list of names of schema files contained in the provided directory. */
+  private List<String> getSchemaFileNames(final File[] schemaFiles)
+  {
+    final List<String> schemaFileNames = new ArrayList<>(schemaFiles.length);
+    for (final File f : schemaFiles)
+    {
+      if (f.isFile())
+      {
+        schemaFileNames.add(f.getName());
+      }
+    }
+    Collections.sort(schemaFileNames);
+    return schemaFileNames;
+  }
+
+  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;
     }
   }
 

--
Gitblit v1.10.0