From 5b42ca5a24a7b30dff7ed246ba104c64389607c9 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 14 Jun 2016 08:47:39 +0000
Subject: [PATCH] Schema: code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/schema/ServerSchemaElement.java |   11 -
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java     |  104 ++++++-----------
 opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java |    9 -
 opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java               |  197 ++++++++++++++------------------
 4 files changed, 126 insertions(+), 195 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 d62ad64..0d5c8ff 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,6 +22,8 @@
 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.types.CommonSchemaElements.*;
 import static org.opends.server.util.CollectionUtils.*;
 import static org.opends.server.util.ServerConstants.*;
@@ -84,7 +86,6 @@
 import org.opends.server.core.SearchOperation;
 import org.opends.server.core.ServerContext;
 import org.opends.server.schema.AttributeTypeSyntax;
-import org.opends.server.schema.GeneralizedTimeSyntax;
 import org.opends.server.types.Attribute;
 import org.opends.server.types.AttributeBuilder;
 import org.opends.server.types.Attributes;
@@ -218,41 +219,21 @@
     creatorsName = newBaseDN;
     modifiersName = newBaseDN;
 
-    long createTime = DirectoryServer.getSchema().getOldestModificationTime();
-    createTimestamp =
-         GeneralizedTimeSyntax.createGeneralizedTimeValue(createTime);
-
-    long newModifyTime =
-        DirectoryServer.getSchema().getYoungestModificationTime();
-    modifyTimestamp =
-         GeneralizedTimeSyntax.createGeneralizedTimeValue(newModifyTime);
+    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
     // schema entry.
     userDefinedAttributes = new ArrayList<>();
-    addAll(configEntry.getUserAttributes().values());
-    addAll(configEntry.getOperationalAttributes().values());
+    addAllNonSchemaConfigAttributes(userDefinedAttributes, configEntry.getUserAttributes().values());
+    addAllNonSchemaConfigAttributes(userDefinedAttributes, configEntry.getOperationalAttributes().values());
 
     showAllAttributes = cfg.isShowAllAttributes();
 
     currentConfig = cfg;
   }
 
-  private void addAll(Collection<List<Attribute>> attrsList)
-  {
-    for (List<Attribute> attrs : attrsList)
-    {
-      for (Attribute a : attrs)
-      {
-        if (! isSchemaConfigAttribute(a))
-        {
-          userDefinedAttributes.add(a);
-        }
-      }
-    }
-  }
-
   @Override
   public void openBackend() throws ConfigException, InitializationException
   {
@@ -290,42 +271,7 @@
       // Next, generate lists of elements from the previous concatenated schema.
       // If there isn't a previous concatenated schema, then use the base
       // schema for the current revision.
-      String concatFilePath;
-      File configFile       = new File(DirectoryServer.getConfigFile());
-      File configDirectory  = configFile.getParentFile();
-      File upgradeDirectory = new File(configDirectory, "upgrade");
-      File concatFile       = new File(upgradeDirectory, SCHEMA_CONCAT_FILE_NAME);
-      if (concatFile.exists())
-      {
-        concatFilePath = concatFile.getAbsolutePath();
-      }
-      else
-      {
-        concatFile = new File(upgradeDirectory, SCHEMA_BASE_FILE_NAME_WITHOUT_REVISION +
-            BuildVersion.instanceVersion().getRevision());
-        if (concatFile.exists())
-        {
-          concatFilePath = concatFile.getAbsolutePath();
-        }
-        else
-        {
-          String runningUnitTestsStr =
-               System.getProperty(PROPERTY_RUNNING_UNIT_TESTS);
-          if ("true".equalsIgnoreCase(runningUnitTestsStr))
-          {
-            Schema.writeConcatenatedSchema();
-            concatFile = new File(upgradeDirectory, SCHEMA_CONCAT_FILE_NAME);
-            concatFilePath = concatFile.getAbsolutePath();
-          }
-          else
-          {
-            LocalizableMessage message = ERR_SCHEMA_CANNOT_FIND_CONCAT_FILE.
-                get(upgradeDirectory.getAbsolutePath(), SCHEMA_CONCAT_FILE_NAME,
-                    concatFile.getName());
-            throw new InitializationException(message);
-          }
-        }
-      }
+      File concatFile = getConcatFile();
 
       Set<String> oldATs  = new LinkedHashSet<>();
       Set<String> oldOCs  = new LinkedHashSet<>();
@@ -334,7 +280,7 @@
       Set<String> oldDSRs = new LinkedHashSet<>();
       Set<String> oldMRUs = new LinkedHashSet<>();
       Set<String> oldLSs = new LinkedHashSet<>();
-      Schema.readConcatenatedSchema(concatFilePath, oldATs, oldOCs, oldNFs,
+      Schema.readConcatenatedSchema(concatFile, oldATs, oldOCs, oldNFs,
                                     oldDCRs, oldDSRs, oldMRUs,oldLSs);
 
       // Create a list of modifications and add any differences between the old
@@ -373,6 +319,34 @@
     currentConfig.addSchemaChangeListener(this);
   }
 
+  private File getConcatFile() throws InitializationException
+  {
+    File configDirectory  = new File(DirectoryServer.getConfigFile()).getParentFile();
+    File upgradeDirectory = new File(configDirectory, "upgrade");
+    File concatFile       = new File(upgradeDirectory, SCHEMA_CONCAT_FILE_NAME);
+    if (concatFile.exists())
+    {
+      return concatFile.getAbsoluteFile();
+    }
+
+    String fileName = SCHEMA_BASE_FILE_NAME_WITHOUT_REVISION + BuildVersion.instanceVersion().getRevision();
+    concatFile = new File(upgradeDirectory, fileName);
+    if (concatFile.exists())
+    {
+      return concatFile.getAbsoluteFile();
+    }
+
+    String runningUnitTestsStr = System.getProperty(PROPERTY_RUNNING_UNIT_TESTS);
+    if ("true".equalsIgnoreCase(runningUnitTestsStr))
+    {
+      Schema.writeConcatenatedSchema();
+      concatFile = new File(upgradeDirectory, SCHEMA_CONCAT_FILE_NAME);
+      return concatFile.getAbsoluteFile();
+    }
+    throw new InitializationException(ERR_SCHEMA_CANNOT_FIND_CONCAT_FILE.get(
+        upgradeDirectory.getAbsolutePath(), SCHEMA_CONCAT_FILE_NAME, concatFile.getName()));
+  }
+
   @Override
   public void closeBackend()
   {
@@ -556,8 +530,7 @@
       synchronized (this)
       {
         modifyTime = DirectoryServer.getSchema().getYoungestModificationTime();
-        modifyTimestamp = GeneralizedTimeSyntax
-            .createGeneralizedTimeValue(modifyTime);
+        modifyTimestamp = createGeneralizedTimeValue(modifyTime);
       }
     }
     addAttributeToSchemaEntry(
@@ -749,8 +722,7 @@
     }
 
     modifiersName = ByteString.valueOfUtf8(authzDN.toString());
-    modifyTimestamp = GeneralizedTimeSyntax.createGeneralizedTimeValue(
-                           System.currentTimeMillis());
+    modifyTimestamp = createGeneralizedTimeValue(System.currentTimeMillis());
   }
 
   private void addAttribute(Schema newSchema, Attribute a, Set<String> modifiedSchemaFiles) throws DirectoryException
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/schema/ServerSchemaElement.java b/opendj-server-legacy/src/main/java/org/opends/server/schema/ServerSchemaElement.java
index 326b892..0dcef32 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/schema/ServerSchemaElement.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/schema/ServerSchemaElement.java
@@ -15,7 +15,7 @@
  */
 package org.opends.server.schema;
 
-import static org.opends.server.util.ServerConstants.SCHEMA_PROPERTY_FILENAME;
+import static org.opends.server.types.Schema.*;
 
 import java.util.List;
 import java.util.Map;
@@ -56,15 +56,8 @@
    */
   public String getDefinitionWithFileName()
   {
-    final String schemaFile = getSchemaFile();
     final String definition = element.toString();
-    if (schemaFile != null)
-    {
-      int pos = definition.lastIndexOf(')');
-      return definition.substring(0, pos).trim() + " "
-          + SCHEMA_PROPERTY_FILENAME + " '" + schemaFile + "' )";
-    }
-    return definition;
+    return addSchemaFileToElementDefinitionIfAbsent(definition, getSchemaFile());
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java b/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java
index 98162f1..acb30f3 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/CommonSchemaElements.java
@@ -156,14 +156,7 @@
    */
   public static String getDefinitionWithFileName(SchemaElement elem)
   {
-    final String schemaFile = getSchemaFile(elem);
     final String definition = elem.toString();
-    if (schemaFile != null)
-    {
-      int pos = definition.lastIndexOf(')');
-      return definition.substring(0, pos).trim() + " "
-          + SCHEMA_PROPERTY_FILENAME + " '" + schemaFile + "' )";
-    }
-    return definition;
+    return Schema.addSchemaFileToElementDefinitionIfAbsent(definition, getSchemaFile(elem));
   }
 }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java b/opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java
index 79104b0..7a56b59 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java
@@ -16,6 +16,7 @@
  */
 package org.opends.server.types;
 
+import static org.forgerock.opendj.ldap.ModificationType.*;
 import static org.forgerock.opendj.ldap.schema.CoreSchema.*;
 import static org.forgerock.opendj.ldap.schema.SchemaOptions.*;
 import static org.opends.messages.BackendMessages.*;
@@ -27,6 +28,7 @@
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.FilenameFilter;
@@ -1680,8 +1682,7 @@
   {
     // Get a sorted list of the files in the schema directory.
     TreeSet<File> schemaFiles = new TreeSet<>();
-    String schemaDirectory =
-      SchemaConfigManager.getSchemaDirectoryPath();
+    String schemaDirectory = SchemaConfigManager.getSchemaDirectoryPath();
 
     final FilenameFilter filter = new SchemaConfigManager.SchemaFileFilter();
     for (File f : new File(schemaDirectory).listFiles(filter))
@@ -1697,34 +1698,7 @@
     // contain, appending them to the appropriate lists.
     for (File f : schemaFiles)
     {
-      // Read the contents of the file into a list with one schema
-      // element per list element.
-      LinkedList<StringBuilder> lines = new LinkedList<>();
-      BufferedReader reader = new BufferedReader(new FileReader(f));
-
-      while (true)
-      {
-        String line = reader.readLine();
-        if (line == null)
-        {
-          break;
-        }
-        else if (line.startsWith("#") || line.length() == 0)
-        {
-          continue;
-        }
-        else if (line.startsWith(" "))
-        {
-          lines.getLast().append(line.substring(1));
-        }
-        else
-        {
-          lines.add(new StringBuilder(line));
-        }
-      }
-
-      reader.close();
-
+      List<StringBuilder> lines = readSchemaElementsFromLdif(f);
 
       // Iterate through each line in the list.  Find the colon and
       // get the attribute name at the beginning.  If it's something
@@ -1741,40 +1715,58 @@
     }
   }
 
+  private static List<StringBuilder> readSchemaElementsFromLdif(File f) throws IOException, FileNotFoundException
+  {
+    final LinkedList<StringBuilder> lines = new LinkedList<>();
+
+    try (BufferedReader reader = new BufferedReader(new FileReader(f)))
+    {
+      String line;
+      while ((line = reader.readLine()) != null)
+      {
+        if (line.startsWith("#") || line.length() == 0)
+        {
+          continue;
+        }
+        else if (line.startsWith(" "))
+        {
+          lines.getLast().append(line.substring(1));
+        }
+        else
+        {
+          lines.add(new StringBuilder(line));
+        }
+      }
+    }
+    return lines;
+  }
+
 
 
   /**
    * Reads data from the specified concatenated schema file into the
    * provided sets.
    *
-   * @param  concatSchemaFile   The path to the concatenated schema
-   *                            file to be read.
-   * @param  attributeTypes     The set into which to place the
-   *                            attribute types read from the
-   *                            concatenated schema file.
-   * @param  objectClasses      The set into which to place the object
-   *                            classes read from the concatenated
-   *                            schema file.
-   * @param  nameForms          The set into which to place the name
-   *                            forms read from the concatenated
-   *                            schema file.
-   * @param  ditContentRules    The set into which to place the DIT
-   *                            content rules read from the
-   *                            concatenated schema file.
-   * @param  ditStructureRules  The set into which to place the DIT
-   *                            structure rules read from the
-   *                            concatenated schema file.
-   * @param  matchingRuleUses   The set into which to place the
-   *                            matching rule uses read from the
-   *                            concatenated schema file.
-   * @param ldapSyntaxes The set into which to place the
-   *                            ldap syntaxes read from the
-   *                            concatenated schema file.
+   * @param  concatSchemaFile   The concatenated schema file to be read.
+   * @param  attributeTypes     The set into which to place the attribute types
+   *                            read from the concatenated schema file.
+   * @param  objectClasses      The set into which to place the object classes
+   *                            read from the concatenated schema file.
+   * @param  nameForms          The set into which to place the name forms
+   *                            read from the concatenated schema file.
+   * @param  ditContentRules    The set into which to place the DIT content rules
+   *                            read from the concatenated schema file.
+   * @param  ditStructureRules  The set into which to place the DIT structure rules
+   *                            read from the concatenated schema file.
+   * @param  matchingRuleUses   The set into which to place the matching rule
+   *                            uses read from the concatenated schema file.
+   * @param ldapSyntaxes        The set into which to place the ldap syntaxes
+   *                            read from the concatenated schema file.
    *
    * @throws  IOException  If a problem occurs while reading the
    *                       schema file elements.
    */
-  public static void readConcatenatedSchema(String concatSchemaFile,
+  public static void readConcatenatedSchema(File concatSchemaFile,
                           Set<String> attributeTypes,
                           Set<String> objectClasses,
                           Set<String> nameForms,
@@ -1784,24 +1776,19 @@
                           Set<String> ldapSyntaxes)
           throws IOException
   {
-    BufferedReader reader =
-         new BufferedReader(new FileReader(concatSchemaFile));
-    while (true)
+    try (BufferedReader reader = new BufferedReader(new FileReader(concatSchemaFile)))
     {
-      String line = reader.readLine();
-      if (line == null)
+      String line;
+      while ((line = reader.readLine()) != null)
       {
-        break;
+        parseSchemaLine(line, null, attributeTypes, objectClasses,
+            nameForms, ditContentRules, ditStructureRules, matchingRuleUses,
+            ldapSyntaxes);
       }
-      parseSchemaLine(line, null, attributeTypes, objectClasses,
-          nameForms, ditContentRules, ditStructureRules, matchingRuleUses,
-          ldapSyntaxes);
     }
-
-    reader.close();
   }
 
-  private static void parseSchemaLine(String line, String fileName,
+  private static void parseSchemaLine(String definition, String fileName,
       Set<String> attributeTypes,
       Set<String> objectClasses,
       Set<String> nameForms,
@@ -1810,71 +1797,66 @@
       Set<String> matchingRuleUses,
       Set<String> ldapSyntaxes)
   {
-    String lowerLine = toLowerCase(line);
+    String lowerLine = toLowerCase(definition);
 
     try
     {
       if (lowerLine.startsWith(ATTR_ATTRIBUTE_TYPES_LC))
       {
-        attributeTypes.add(getSchemaDefinition(line.substring(ATTR_ATTRIBUTE_TYPES_LC.length()), fileName));
+        addSchemaDefinition(attributeTypes, definition, ATTR_ATTRIBUTE_TYPES_LC, fileName);
       }
       else if (lowerLine.startsWith(ATTR_OBJECTCLASSES_LC))
       {
-        objectClasses.add(getSchemaDefinition(line.substring(ATTR_OBJECTCLASSES_LC.length()), fileName));
+        addSchemaDefinition(objectClasses, definition, ATTR_OBJECTCLASSES_LC, fileName);
       }
       else if (lowerLine.startsWith(ATTR_NAME_FORMS_LC))
       {
-        nameForms.add(getSchemaDefinition(line.substring(ATTR_NAME_FORMS_LC.length()), fileName));
+        addSchemaDefinition(nameForms, definition, ATTR_NAME_FORMS_LC, fileName);
       }
       else if (lowerLine.startsWith(ATTR_DIT_CONTENT_RULES_LC))
       {
-        ditContentRules.add(getSchemaDefinition(line.substring(ATTR_DIT_CONTENT_RULES_LC.length()), fileName));
+        addSchemaDefinition(ditContentRules, definition, ATTR_DIT_CONTENT_RULES_LC, fileName);
       }
       else if (lowerLine.startsWith(ATTR_DIT_STRUCTURE_RULES_LC))
       {
-        ditStructureRules.add(getSchemaDefinition(line.substring(ATTR_DIT_STRUCTURE_RULES_LC.length()), fileName));
+        addSchemaDefinition(ditStructureRules, definition, ATTR_DIT_STRUCTURE_RULES_LC, fileName);
       }
       else if (lowerLine.startsWith(ATTR_MATCHING_RULE_USE_LC))
       {
-        matchingRuleUses.add(getSchemaDefinition(line.substring(ATTR_MATCHING_RULE_USE_LC.length()), fileName));
+        addSchemaDefinition(matchingRuleUses, definition, ATTR_MATCHING_RULE_USE_LC, fileName);
       }
       else if (lowerLine.startsWith(ATTR_LDAP_SYNTAXES_LC))
       {
-        ldapSyntaxes.add(getSchemaDefinition(line.substring(ATTR_LDAP_SYNTAXES_LC.length()), fileName));
+        addSchemaDefinition(ldapSyntaxes, definition, ATTR_LDAP_SYNTAXES_LC, fileName);
       }
     } catch (ParseException pe)
     {
-      logger.error(ERR_SCHEMA_PARSE_LINE.get(line, pe.getLocalizedMessage()));
+      logger.error(ERR_SCHEMA_PARSE_LINE.get(definition, pe.getLocalizedMessage()));
     }
   }
 
-  private static String getSchemaDefinition(String line, String fileName) throws ParseException
+  private static void addSchemaDefinition(Set<String> definitions, String line, String attrName, String fileName)
+      throws ParseException
   {
-    if (line.startsWith("::"))
+    definitions.add(getSchemaDefinition(line.substring(attrName.length()), fileName));
+  }
+
+  private static String getSchemaDefinition(String definition, String schemaFile) throws ParseException
+  {
+    if (definition.startsWith("::"))
     {
-      line = ByteString.wrap(Base64.decode(line.substring(2).trim())).toString();
+      definition = ByteString.wrap(Base64.decode(definition.substring(2).trim())).toString();
     }
-    else if (line.startsWith(":"))
+    else if (definition.startsWith(":"))
     {
-      line = line.substring(1).trim();
+      definition = definition.substring(1).trim();
     }
     else
     {
       throw new ParseException(ERR_SCHEMA_COULD_NOT_PARSE_DEFINITION.get().toString(), 0);
     }
 
-    if (fileName != null)
-    {
-      if (line.endsWith(" )"))
-      {
-        line = line.substring(0, line.length() - 1) + SCHEMA_PROPERTY_FILENAME + " '" + fileName + "' )";
-      }
-      else if (line.endsWith(")"))
-      {
-        line = line.substring(0, line.length() - 1) + " " + SCHEMA_PROPERTY_FILENAME + " '" + fileName + "' )";
-      }
-    }
-    return line;
+    return addSchemaFileToElementDefinitionIfAbsent(definition, schemaFile);
   }
 
   /**
@@ -1899,38 +1881,29 @@
                           List<Modification> mods)
   {
     AttributeBuilder builder = new AttributeBuilder(elementType);
-    for (String s : oldElements)
-    {
-      if (!newElements.contains(s))
-      {
-        builder.add(s);
-      }
-    }
-
-    if (!builder.isEmpty())
-    {
-      mods.add(new Modification(ModificationType.DELETE,
-                                builder.toAttribute()));
-    }
+    addModification(mods, DELETE, oldElements, newElements, builder);
 
     builder.setAttributeDescription(AttributeDescription.create(elementType));
-    for (String s : newElements)
+    addModification(mods, ADD, newElements, oldElements, builder);
+  }
+
+  private static void addModification(List<Modification> mods, ModificationType modType, Set<String> included,
+      Set<String> excluded, AttributeBuilder builder)
+  {
+    for (String val : included)
     {
-      if (!oldElements.contains(s))
+      if (!excluded.contains(val))
       {
-        builder.add(s);
+        builder.add(val);
       }
     }
 
     if (!builder.isEmpty())
     {
-      mods.add(new Modification(ModificationType.ADD,
-                                builder.toAttribute()));
+      mods.add(new Modification(modType, builder.toAttribute()));
     }
   }
 
-
-
   /**
    * Destroys the structures maintained by the schema so that they are
    * no longer usable. This should only be called at the end of the

--
Gitblit v1.10.0