From cd3a41a0bd6762b1f63e58c6a65743f9800f5300 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Wed, 21 Sep 2016 15:15:44 +0000
Subject: [PATCH] OPENDJ-2413 Remove uses of EmbeddedUtils class and deprecate it

---
 opendj-server-legacy/src/main/java/org/opends/server/util/embedded/EmbeddedDirectoryServer.java |   54 +++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/embedded/EmbeddedDirectoryServer.java b/opendj-server-legacy/src/main/java/org/opends/server/util/embedded/EmbeddedDirectoryServer.java
index 355d7f4..e10a13e 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/embedded/EmbeddedDirectoryServer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/embedded/EmbeddedDirectoryServer.java
@@ -44,7 +44,6 @@
 import org.opends.server.types.DirectoryEnvironmentConfig;
 import org.opends.server.types.DirectoryException;
 import org.opends.server.types.InitializationException;
-import org.opends.server.util.ServerConstants;
 import org.opends.server.util.StaticUtils;
 
 /**
@@ -94,11 +93,11 @@
         new LDAPConnectionFactory(connectionParams.getHostname(), connectionParams.getLdapPort())
         : null;
 
-    System.setProperty("org.opends.quicksetup.Root", configParams.getServerRootDirectory());
-    System.setProperty(ServerConstants.PROPERTY_SERVER_ROOT, configParams.getServerRootDirectory());
-    System.setProperty(ServerConstants.PROPERTY_INSTANCE_ROOT, configParams.getServerInstanceDirectory());
+    //System.setProperty("org.opends.quicksetup.Root", configParams.getServerRootDirectory());
+    //System.setProperty(ServerConstants.PROPERTY_SERVER_ROOT, configParams.getServerRootDirectory());
+    //System.setProperty(ServerConstants.PROPERTY_INSTANCE_ROOT, configParams.getServerInstanceDirectory());
     // from LicenseFile.java - provided by AM OpenDJUpgrader.java
-    System.setProperty("INSTALL_ROOT", configParams.getServerInstanceDirectory());
+    //System.setProperty("INSTALL_ROOT", configParams.getServerInstanceDirectory());
   }
 
   /**
@@ -156,6 +155,21 @@
   }
 
   /**
+   * Defines an embedded directory server for start/stop operation.
+   * <p>
+   * To be able to perform any operation on the server, use the alternative {@code defineServer()}
+   * method.
+   *
+   * @param configParams
+   *          The basic configuration parameters for the server.
+   * @return the directory server
+   */
+  public static EmbeddedDirectoryServer defineServerForStartStopOperations(ConfigParameters configParams)
+  {
+    return new EmbeddedDirectoryServer(configParams, null, System.out, System.err);
+  }
+
+  /**
    * Displays the replication status on the output stream defined for this server.
    * <p>
    * Displays a list with the basic replication configuration of all base DNs of
@@ -403,7 +417,7 @@
    * @param reason
    *          A message explaining the reason for the restart.
    */
-  public void restartServer(String className, LocalizableMessage reason)
+  public void restart(String className, LocalizableMessage reason)
   {
     DirectoryServer.restart(className, reason, DirectoryServer.getEnvironmentConfig());
   }
@@ -425,13 +439,7 @@
 
     try
     {
-      DirectoryEnvironmentConfig env = new DirectoryEnvironmentConfig();
-      env.setServerRoot(new File(configParams.getServerRootDirectory()));
-      env.setInstanceRoot(new File(configParams.getServerInstanceDirectory()));
-      env.setForceDaemonThreads(true);
-      env.setConfigFile(new File(configParams.getConfigurationFile()));
-
-      DirectoryServer directoryServer = DirectoryServer.reinitialize(env);
+      DirectoryServer directoryServer = DirectoryServer.reinitialize(createEnvironmentConfig());
       directoryServer.startServer();
     }
     catch (InitializationException | ConfigException e)
@@ -441,6 +449,26 @@
     }
   }
 
+  private DirectoryEnvironmentConfig createEnvironmentConfig() throws InitializationException
+  {
+    // If server root directory or instance directory are not defined,
+    // the DirectoryEnvironmentConfig class has several ways to find the values,
+    // including using system properties.
+    DirectoryEnvironmentConfig env = new DirectoryEnvironmentConfig();
+    if (configParams.getServerRootDirectory() != null)
+    {
+      env.setServerRoot(new File(configParams.getServerRootDirectory()));
+    }
+    if (configParams.getServerInstanceDirectory() != null)
+    {
+      env.setInstanceRoot(new File(configParams.getServerInstanceDirectory()));
+    }
+    env.setForceDaemonThreads(true);
+    env.setDisableConnectionHandlers(configParams.isDisableConnectionHandlers());
+    env.setConfigFile(new File(configParams.getConfigurationFile()));
+    return env;
+  }
+
   /**
    * Stops this server.
    *

--
Gitblit v1.10.0