From 906862fa0eb1af63206dbff8ed92474706359458 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Tue, 04 Oct 2016 15:20:46 +0000
Subject: [PATCH] OPENDJ-2413 Update EmbeddedDirectoryServer class for usage by OpenAM

---
 opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java |  117 ++++++++++++++++++++++++--------------
 opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/LocalPurgeHistorical.java   |    4 
 opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java     |    4 
 opendj-server-legacy/src/messages/org/opends/messages/utility.properties                             |   14 ++--
 4 files changed, 86 insertions(+), 53 deletions(-)

diff --git a/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java
index 5dcfc68..d835d25 100644
--- a/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java
+++ b/opendj-embedded-server-examples/src/main/java/org/forgerock/opendj/examples/StartStopServer.java
@@ -17,7 +17,7 @@
 package org.forgerock.opendj.examples;
 
 import static org.forgerock.opendj.server.embedded.ConfigParameters.configParams;
-import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.manageEmbeddedDirectoryServerForStartStop;
+import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.*;
 
 import java.nio.file.Paths;
 
@@ -48,7 +48,7 @@
         final String serverRootDir = args[0];
 
         final EmbeddedDirectoryServer server =
-                manageEmbeddedDirectoryServerForStartStop(
+               manageEmbeddedDirectoryServerForRestrictedOps(
                         configParams()
                             .serverRootDirectory(serverRootDir)
                             .configurationFile(Paths.get(serverRootDir, "config", "config.ldif").toString()),
diff --git a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java
index bfaa684..37ae50f 100644
--- a/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java
+++ b/opendj-server-legacy/src/main/java/org/forgerock/opendj/server/embedded/EmbeddedDirectoryServer.java
@@ -33,10 +33,6 @@
 import org.forgerock.opendj.config.server.ConfigException;
 import org.forgerock.opendj.ldap.Connection;
 import org.forgerock.opendj.ldap.DN;
-import org.forgerock.opendj.ldap.LDAPConnectionFactory;
-import org.forgerock.opendj.ldap.requests.Requests;
-import org.forgerock.opendj.ldap.requests.SimpleBindRequest;
-import org.forgerock.util.Options;
 import org.forgerock.util.Reject;
 import org.opends.quicksetup.TempLogFile;
 import org.opends.server.config.ConfigConstants;
@@ -50,6 +46,7 @@
 import org.opends.server.types.DirectoryEnvironmentConfig;
 import org.opends.server.types.DirectoryException;
 import org.opends.server.types.InitializationException;
+import org.opends.server.util.BuildVersion;
 import org.opends.server.util.DynamicConstants;
 import org.opends.server.util.StaticUtils;
 
@@ -70,9 +67,6 @@
   /** The connection parameters for the server. */
   private final ConnectionParameters connectionParams;
 
-  /** The connection factory to get connections to the server. */
-  private final LDAPConnectionFactory ldapConnectionFactory;
-
   /** The output stream used for feedback during operations on server. */
   private final OutputStream outStream;
 
@@ -100,19 +94,14 @@
     this.connectionParams = connParams;
     this.outStream = out;
     this.errStream = err;
-    if (connectionParams != null)
-    {
-      SimpleBindRequest authRequest = Requests.newSimpleBindRequest(
-          connectionParams.getBindDn(), connectionParams.getBindPassword().toCharArray());
-      ldapConnectionFactory = new LDAPConnectionFactory(
-          connectionParams.getHostName(),
-          connectionParams.getLdapPort(),
-          Options.defaultOptions().set(LDAPConnectionFactory.AUTHN_BIND_REQUEST, authRequest));
-    }
-    else
-    {
-      ldapConnectionFactory = null;
-    }
+    System.setProperty(PROPERTY_SERVER_ROOT, configParams.getServerRootDirectory());
+    System.setProperty(QUICKSETUP_ROOT_PROPERTY, configParams.getServerRootDirectory());
+    String instanceDir = configParams.getServerInstanceDirectory() != null ?
+        configParams.getServerInstanceDirectory() :
+        configParams.getServerRootDirectory();
+    System.setProperty(QUICKSETUP_INSTANCE_PROPERTY, instanceDir);
+    System.setProperty(PROPERTY_INSTANCE_ROOT, instanceDir);
+
   }
 
   private EmbeddedDirectoryServer(ConfigParameters configParams, ConnectionParameters connParams)
@@ -148,9 +137,15 @@
   }
 
   /**
-   * Creates an instance of an embedded directory server for start/stop operation.
+   * Creates an instance of an embedded directory server to perform a restricted set of operations that
+   * do not need connection parameters.
    * <p>
-   * To be able to perform any operation on the server, use the alternative {@code defineServer()}
+   * The following operations won't be allowed because they require the connection parameters: setup,
+   * configureReplication, initializeReplication, isReplicationRunning, importLDIF
+   * and getInternalConnection without argument
+   * ({@code getInternalConnection(DN)} method is allowed).
+   * <p>
+   * To be able to perform any operation on the server, use the alternative {@code manageEmbeddedDirectoryServer()}
    * method.
    *
    * @param configParams
@@ -161,23 +156,31 @@
    *          Error stream used for feedback during operations on server
    * @return the directory server
    */
-  public static EmbeddedDirectoryServer manageEmbeddedDirectoryServerForStartStop(ConfigParameters configParams,
+  public static EmbeddedDirectoryServer manageEmbeddedDirectoryServerForRestrictedOps(ConfigParameters configParams,
       OutputStream out, OutputStream err)
   {
     return new EmbeddedDirectoryServer(configParams, null, out, err);
   }
 
   /**
-   * Creates an instance of an embedded directory server for start/stop operation.
+   * Creates an instance of an embedded directory server to perform a restricted set of operations that
+   * do not need connection parameters.
    * <p>
-   * To be able to perform any operation on the server, use the alternative {@code defineServer()}
+   * The following operations won't be allowed because they require the connection parameters: setup,
+   * configureReplication, initializeReplication, isReplicationRunning, importLDIF
+   * and getInternalConnection without argument
+   * ({@code getInternalConnection(DN)} method is allowed).
+   * <p>
+   * To be able to perform any operation on the server, use the alternative {@code manageEmbeddedDirectoryServer()}
    * method.
+   * <p>
+   * Standard out and error streams will be used for any output during operations on the embedded server.
    *
    * @param configParams
    *          The basic configuration parameters for the server.
    * @return the directory server
    */
-  public static EmbeddedDirectoryServer manageEmbeddedDirectoryServerForStartStop(
+  public static EmbeddedDirectoryServer manageEmbeddedDirectoryServerForRestrictedOps(
       ConfigParameters configParams)
   {
     return new EmbeddedDirectoryServer(configParams, null, System.out, System.err);
@@ -211,6 +214,26 @@
   }
 
   /**
+   * Returns the build version of the directory server as a String.
+   * <p>
+   * This version corresponds to the instance version, as written in the "config/buildinfo" file.
+   *
+   * @return the version
+   * @throws EmbeddedDirectoryServerException
+   *            If an error occurs while retrieving the version
+   */
+  public String getBuildVersion() throws EmbeddedDirectoryServerException
+  {
+    try
+    {
+      return BuildVersion.instanceVersion().toString();
+    }
+    catch (InitializationException e) {
+      throw new EmbeddedDirectoryServerException(ERR_EMBEDDED_SERVER_BUILD_VERSION.get(e.getLocalizedMessage()), e);
+    }
+  }
+
+  /**
    * Returns the configuration of this server, which can be read or updated.
    * <p>
    * The returned object is an instance of {@code ManagementContext} class, which allow access to the
@@ -243,14 +266,13 @@
    * @throws EmbeddedDirectoryServerException
    *            If the retrieval of the configuration fails
    */
-  @SuppressWarnings("resource")
   public ManagementContext getConfiguration() throws EmbeddedDirectoryServerException
   {
     try
     {
       if (isRunning())
       {
-        Connection ldapConnection = ldapConnectionFactory.getConnection();
+        Connection ldapConnection = getInternalConnection();
         return newManagementContext(ldapConnection, LDAPProfile.getInstance());
       }
       return newLDIFManagementContext(new File(configParams.getConfigurationFile()));
@@ -263,6 +285,24 @@
   }
 
   /**
+   * Returns an internal connection to the server that will be authenticated automatically with
+   * the bind DN defined for this server.
+   * <p>
+   * This method is available only if connection parameters are provided for the server.
+   * If the connection must be authenticated with another DN or if no connection parameters have been
+   * provided, use the alternate method {@code getInternalConnection(DN userDn)}.
+   *
+   * @return the connection
+   * @throws EmbeddedDirectoryServerException
+   *            If the connection can't be returned
+   */
+  public Connection getInternalConnection() throws EmbeddedDirectoryServerException
+  {
+    Reject.checkNotNull(connectionParams);
+    return getInternalConnection(DN.valueOf(connectionParams.getBindDn()));
+  }
+
+  /**
    * Returns an internal connection to the server that will be authenticated as the specified user.
    *
    * @param userDn
@@ -296,7 +336,8 @@
   public void importLDIF(ImportParameters parameters) throws EmbeddedDirectoryServerException
   {
     checkServerIsRunning();
-    Reject.checkNotNull(connectionParams);    int returnCode = ImportLDIF.mainImportLDIF(
+    Reject.checkNotNull(connectionParams);
+    int returnCode = ImportLDIF.mainImportLDIF(
         parameters.toCommandLineArguments(configParams.getConfigurationFile(), connectionParams),
         !isRunning(), outStream, errStream);
 
@@ -333,15 +374,14 @@
   /**
    * Indicates whether replication is currently running for the embedded server.
    *
-   * @param parameters
-   *            The parameters for the replication.
    * @return {@code true} if replication is running, {@code false} otherwise
    */
-  public boolean isReplicationRunning(ReplicationParameters parameters)
+  public boolean isReplicationRunning()
   {
     Reject.checkNotNull(connectionParams);
     int returnCode = ReplicationCliMain.mainCLI(
-        parameters.toCommandLineArgumentsStatus(configParams.getConfigurationFile(), connectionParams),
+        ReplicationParameters.replicationParams()
+          .toCommandLineArgumentsStatus(configParams.getConfigurationFile(), connectionParams),
         !isRunning(), outStream, errStream);
     return returnCode == 0;
   }
@@ -360,7 +400,7 @@
    * Set this server up from the root directory.
    * <p>
    * As a pre-requisite, the OpenDJ archive must have been previously extracted to some
-   * directory. To perform a setup directly from an archive, see {@code setupFromArchive()}.
+   * directory. To extract an archive for setup, see {@code extractArchiveForSetup()}.
    *
    * @param parameters
    *            The setup parameters.
@@ -370,14 +410,6 @@
   public void setup(SetupParameters parameters) throws EmbeddedDirectoryServerException
   {
     Reject.checkNotNull(connectionParams);
-    System.setProperty(PROPERTY_SERVER_ROOT, configParams.getServerRootDirectory());
-    System.setProperty(QUICKSETUP_ROOT_PROPERTY, configParams.getServerRootDirectory());
-    String instanceDir = configParams.getServerInstanceDirectory() != null ?
-        configParams.getServerInstanceDirectory() :
-        configParams.getServerRootDirectory();
-    System.setProperty(QUICKSETUP_INSTANCE_PROPERTY, instanceDir);
-    System.setProperty(PROPERTY_INSTANCE_ROOT, instanceDir);
-
     int returnCode = InstallDS.mainCLI(parameters.toCommandLineArguments(connectionParams), outStream, errStream,
         TempLogFile.newTempLogFile(EMBEDDED_OPEN_DJ_PREFIX));
 
@@ -401,7 +433,6 @@
    */
   public void extractArchiveForSetup(File openDJZipFile) throws EmbeddedDirectoryServerException
   {
-    Reject.checkNotNull(connectionParams);
     try
     {
       File serverRoot = new File(configParams.getServerRootDirectory());
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/LocalPurgeHistorical.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/LocalPurgeHistorical.java
index 6b27802..13552e3 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/LocalPurgeHistorical.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/LocalPurgeHistorical.java
@@ -17,7 +17,7 @@
 package org.opends.server.tools.dsreplication;
 
 import static org.forgerock.opendj.server.embedded.ConfigParameters.configParams;
-import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.manageEmbeddedDirectoryServerForStartStop;
+import static org.forgerock.opendj.server.embedded.EmbeddedDirectoryServer.*;
 
 import static org.opends.messages.AdminToolMessages.*;
 
@@ -90,7 +90,7 @@
 
     try
     {
-      EmbeddedDirectoryServer server = manageEmbeddedDirectoryServerForStartStop(
+      EmbeddedDirectoryServer server = manageEmbeddedDirectoryServerForRestrictedOps(
           configParams()
             .configurationFile(configFile)
             .disableConnectionHandlers(true));
diff --git a/opendj-server-legacy/src/messages/org/opends/messages/utility.properties b/opendj-server-legacy/src/messages/org/opends/messages/utility.properties
index 8a75da5..029fa06 100644
--- a/opendj-server-legacy/src/messages/org/opends/messages/utility.properties
+++ b/opendj-server-legacy/src/messages/org/opends/messages/utility.properties
@@ -428,14 +428,16 @@
  to start the embedded server with server root '%s' : %s
 ERR_EMBEDDED_SERVER_UPGRADE_337=An error occurred while attempting \
  to upgrade the embedded server with server root '%s' : %s
-ERR_EMBEDDED_SERVER_INTERNAL_CONNECTION=An error occurred while attempting to retrieve \
+ERR_EMBEDDED_SERVER_INTERNAL_CONNECTION_338=An error occurred while attempting to retrieve \
  an internal connection to the server with the user DN '%s'
-ERR_EMBEDDED_SERVER_ARCHIVE_SETUP_WRONG_ROOT_DIRECTORY=The setup from an archive \
+ERR_EMBEDDED_SERVER_ARCHIVE_SETUP_WRONG_ROOT_DIRECTORY_339=The setup from an archive \
  can only be done with a server root directory named after the root directory contained \
  in the archive: '%s'. The provided server root was: '%s'
-ERR_EMBEDDED_SERVER_IMPORT_DATA_SERVER_IS_NOT_RUNNING=The import data operation could \
+ERR_EMBEDDED_SERVER_IMPORT_DATA_SERVER_IS_NOT_RUNNING_340=The import data operation could \
  not be performed on the embedded server because it is not running
-ERR_EMBEDDED_SERVER_REBUILD_INDEX_SERVER_IS_RUNNING=The rebuild index operation could \
+ERR_EMBEDDED_SERVER_REBUILD_INDEX_SERVER_IS_RUNNING_341=The rebuild index operation could \
  not be performed on the embedded server because it is running
-ERR_EMBEDDED_SERVER_LDIF_MANAGEMENT_CONTEXT=An error occurred while attempting to \
- read the configuration file '%s'
\ No newline at end of file
+ERR_EMBEDDED_SERVER_LDIF_MANAGEMENT_CONTEXT_342=An error occurred while attempting to \
+ read the configuration file '%s'
+ERR_EMBEDDED_SERVER_BUILD_VERSION_343=An error occurred while attempting to \
+ retrieve the build version of the directory server: '%s'
\ No newline at end of file

--
Gitblit v1.10.0