From 0149a25c5a0aea9d63e409cb7a9492eaaa09d956 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Mon, 30 May 2016 10:43:13 +0000
Subject: [PATCH] OPENDJ-2987 Replace an attribute type in Schema in one step to prevent error when validating schema

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java |    5 --
 opendj-server-legacy/src/main/java/org/opends/server/types/Schema.java           |   69 ++++++++++++++++++++++++++++------
 2 files changed, 57 insertions(+), 17 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 242eada..849d69c 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
@@ -1161,12 +1161,9 @@
     }
     else
     {
-      schema.deregisterAttributeType(existingType);
-
       String schemaFile = replaceExistingSchemaElement(
           modifiedSchemaFiles, new SomeSchemaElement(attributeType), new SomeSchemaElement(existingType));
-      schema.registerAttributeType(attributeType, schemaFile, false);
-      schema.rebuildDependentElements(existingType);
+      schema.replaceAttributeType(attributeType, existingType, schemaFile);
     }
   }
 
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 c3995fb..d1abb5f 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
@@ -478,19 +478,7 @@
     try
     {
       SchemaBuilder builder = new SchemaBuilder(schemaNG);
-      AttributeType.Builder b = builder.buildAttributeType(attributeType);
-      if (schemaFile != null)
-      {
-        b.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
-      }
-      if (overwriteExisting)
-      {
-        b.addToSchemaOverwrite();
-      }
-      else
-      {
-        b.addToSchema();
-      }
+      registerAttributeType0(builder, attributeType, schemaFile, overwriteExisting);
       switchSchema(builder.toSchema());
 
       updateSubordinateTypes(attributeType);
@@ -505,6 +493,61 @@
     }
   }
 
+  private void registerAttributeType0(SchemaBuilder builder, final AttributeType attributeType,
+      final String schemaFile, final boolean overwriteExisting)
+  {
+    AttributeType.Builder b = builder.buildAttributeType(attributeType);
+    if (schemaFile != null)
+    {
+      b.removeExtraProperty(SCHEMA_PROPERTY_FILENAME).extraProperties(SCHEMA_PROPERTY_FILENAME, schemaFile);
+    }
+    if (overwriteExisting)
+    {
+      b.addToSchemaOverwrite();
+    }
+    else
+    {
+      b.addToSchema();
+    }
+  }
+
+  /**
+   * Replaces an existing attribute type by the provided new attribute type.
+   *
+   * @param newAttributeType
+   *          Attribute type to register to the schema.
+   * @param existingAttributeType
+   *          Attribute type to remove from the schema.
+   * @param schemaFile
+   *          The schema file which the new object class belongs to.
+   * @throws DirectoryException
+   *            If an errors occurs.
+   */
+  public void replaceAttributeType(AttributeType newAttributeType, AttributeType existingAttributeType,
+      String schemaFile) throws DirectoryException
+  {
+    exclusiveLock.lock();
+    try
+    {
+      SchemaBuilder builder = new SchemaBuilder(schemaNG);
+      builder.removeAttributeType(existingAttributeType.getNameOrOID());
+      registerAttributeType0(builder, newAttributeType, schemaFile, false);
+      switchSchema(builder.toSchema());
+
+      AttributeType superiorType = existingAttributeType.getSuperiorType();
+      if (superiorType != null)
+      {
+        deregisterSubordinateType(existingAttributeType, superiorType);
+      }
+      updateSubordinateTypes(newAttributeType);
+    }
+    finally
+    {
+      exclusiveLock.unlock();
+    }
+    rebuildDependentElements(existingAttributeType);
+  }
+
   /**
    * Retrieves the OID of the provided object class definition.
    *

--
Gitblit v1.10.0