From d8e799bd818c9f26198a2d586bc8c647068f82dd Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 26 Apr 2016 07:54:21 +0000
Subject: [PATCH] tools: push initialization of loggers to the InitializationBuilder

---
 opendj-server-legacy/src/main/java/org/opends/server/tools/RebuildIndex.java   |   44 +-------------
 opendj-server-legacy/src/main/java/org/opends/server/tools/BackUpDB.java       |   20 ------
 opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java |   62 ++++++++++++++++----
 opendj-server-legacy/src/main/java/org/opends/server/tools/ExportLDIF.java     |   20 ------
 opendj-server-legacy/src/main/java/org/opends/server/tools/RestoreDB.java      |   19 ------
 5 files changed, 57 insertions(+), 108 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java b/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
index f932415..ad573c3 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
@@ -690,20 +690,22 @@
       ADMIN_USERS,
       START_CRYPTO,
       PASSWORD_STORAGE_SCHEME,
-      USER_PLUGINS;
+      USER_PLUGINS,
+      ERROR_DEBUG_LOGGERS;
     }
 
     private String configFile;
     private Set<PluginType> pluginTypes = new HashSet<>();
     private static EnumSet<SubSystem> subSystemsToInitialize = EnumSet.noneOf(SubSystem.class);
+    private PrintStream loggingOut;
+    private PrintStream errConfiguringLogging;
 
     /**
      * Initialize the client side of DirectoryServer and the Core Configuration.
      *
      * @param configFile the configuration file
-     * @throws InitializationException if client initialization or core Config fails
      */
-    public InitializationBuilder(String configFile) throws InitializationException
+    public InitializationBuilder(String configFile)
     {
       this.configFile = configFile;
       subSystemsToInitialize.add(SubSystem.CLIENT_INIT);
@@ -714,10 +716,9 @@
      * Require to setup and start everything necessary for Crypto Services.
      * Core config should already be initialized through the constructor.
      *
-     * @return the initialization object
-     * @throws InitializationException if Core Config is not initialized
+     * @return this initialization builder
      */
-    public InitializationBuilder requireCryptoServices() throws InitializationException
+    public InitializationBuilder requireCryptoServices()
     {
       Collections.addAll(subSystemsToInitialize,
           SubSystem.INIT_CRYPTO,
@@ -731,10 +732,9 @@
      * Requires to setup and start Password Storage Schemes.
      * Crypto services are needed for Password Storage, so it will also set them up if not already done.
      *
-     * @return the initialization object
-     * @throws InitializationException if Core Config is not initialized
+     * @return this initialization builder
      */
-    public InitializationBuilder requirePasswordStorageSchemes() throws InitializationException
+    public InitializationBuilder requirePasswordStorageSchemes()
     {
       requireCryptoServices();
       Collections.addAll(subSystemsToInitialize, SubSystem.PASSWORD_STORAGE_SCHEME);
@@ -745,10 +745,9 @@
      * Requires to start specified user plugins.
      *
      * @param plugins the plugins to start
-     * @return the initialization object
-     * @throws InitializationException if Core Config is not initialized
+     * @return this initialization builder
      */
-    public InitializationBuilder requireUserPlugins(PluginType... plugins) throws InitializationException
+    public InitializationBuilder requireUserPlugins(PluginType... plugins)
     {
       Collections.addAll(subSystemsToInitialize, SubSystem.USER_PLUGINS);
       this.pluginTypes.addAll(Arrays.asList(plugins));
@@ -756,9 +755,28 @@
     }
 
     /**
+     * Requires to start the error and debug log publishers for tools.
+     *
+     * @param loggingOut
+     *          The output stream where to write error and debug logging.
+     * @param errConfiguringLogging
+     *          The output stream where to write errors occurring when configuring logging.
+     * @return this initialization builder
+     */
+    public InitializationBuilder requireErrorAndDebugLogPublisher(
+        final PrintStream loggingOut, final PrintStream errConfiguringLogging)
+    {
+      subSystemsToInitialize.add(SubSystem.ERROR_DEBUG_LOGGERS);
+      this.loggingOut = loggingOut;
+      this.errConfiguringLogging = errConfiguringLogging;
+      return this;
+    }
+
+    /**
      * Run all Initialization blocks as configured.
      *
-     * @throws InitializationException if one of the initialization steps fails
+     * @throws InitializationException
+     *           if one of the initialization steps fails
      */
     public void initialize() throws InitializationException
     {
@@ -790,6 +808,9 @@
         case USER_PLUGINS:
           startUserPlugin();
           break;
+        case ERROR_DEBUG_LOGGERS:
+          startErrorAndDebugLoggers();
+          break;
         }
       }
     }
@@ -948,6 +969,21 @@
         throw new InitializationException(ERR_CANNOT_INITIALIZE_STORAGE_SCHEMES.get(getExceptionMessage(e)));
       }
     }
+
+    private void startErrorAndDebugLoggers()
+    {
+      try
+      {
+        final ErrorLogPublisher errorLogPublisher =
+            TextErrorLogPublisher.getToolStartupTextErrorPublisher(new TextWriter.STREAM(loggingOut));
+        ErrorLogger.getInstance().addLogPublisher(errorLogPublisher);
+        DebugLogger.getInstance().addPublisherIfRequired(new TextWriter.STREAM(loggingOut));
+      }
+      catch (Exception e)
+      {
+        errConfiguringLogging.println("Error installing the custom error logger: " + stackTraceToSingleLineString(e));
+      }
+    }
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/BackUpDB.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/BackUpDB.java
index 25b5177..88bb917 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/BackUpDB.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/BackUpDB.java
@@ -45,12 +45,7 @@
 import org.opends.server.api.Backend.BackendOperation;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.LockFileManager;
-import org.opends.server.loggers.DebugLogger;
-import org.opends.server.loggers.ErrorLogPublisher;
-import org.opends.server.loggers.ErrorLogger;
 import org.opends.server.loggers.JDKLogging;
-import org.opends.server.loggers.TextErrorLogPublisher;
-import org.opends.server.loggers.TextWriter;
 import org.opends.server.protocols.ldap.LDAPAttribute;
 import org.opends.server.tasks.BackupTask;
 import org.opends.server.tools.tasks.TaskTool;
@@ -433,6 +428,7 @@
       try
       {
         new DirectoryServer.InitializationBuilder(configFile.getValue())
+            .requireErrorAndDebugLogPublisher(out, err)
             .initialize();
       }
       catch (InitializationException ie)
@@ -440,20 +436,6 @@
         printWrappedText(err, ERR_CANNOT_INITIALIZE_SERVER_COMPONENTS.get(getExceptionMessage(ie)));
         return 1;
       }
-
-      try
-      {
-        ErrorLogPublisher errorLogPublisher =
-            TextErrorLogPublisher.getToolStartupTextErrorPublisher(
-            new TextWriter.STREAM(out));
-        ErrorLogger.getInstance().addLogPublisher(errorLogPublisher);
-        DebugLogger.getInstance().addPublisherIfRequired(new TextWriter.STREAM(out));
-      }
-      catch(Exception e)
-      {
-        err.println("Error installing the custom error logger: " +
-                    stackTraceToSingleLineString(e));
-      }
     }
 
 
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/ExportLDIF.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/ExportLDIF.java
index 3fcaa1a..4181bc2 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/ExportLDIF.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/ExportLDIF.java
@@ -39,12 +39,7 @@
 import org.opends.server.api.plugin.PluginType;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.LockFileManager;
-import org.opends.server.loggers.DebugLogger;
-import org.opends.server.loggers.ErrorLogPublisher;
-import org.opends.server.loggers.ErrorLogger;
 import org.opends.server.loggers.JDKLogging;
-import org.opends.server.loggers.TextErrorLogPublisher;
-import org.opends.server.loggers.TextWriter;
 import org.opends.server.protocols.ldap.LDAPAttribute;
 import org.opends.server.tasks.ExportTask;
 import org.opends.server.tools.tasks.TaskTool;
@@ -375,6 +370,7 @@
         new DirectoryServer.InitializationBuilder(configFile.getValue())
             .requireCryptoServices()
             .requireUserPlugins(PluginType.LDIF_EXPORT)
+            .requireErrorAndDebugLogPublisher(out, err)
             .initialize();
       }
       catch (InitializationException ie)
@@ -382,20 +378,6 @@
         printWrappedText(err, ERR_CANNOT_INITIALIZE_SERVER_COMPONENTS.get(getExceptionMessage(ie)));
         return 1;
       }
-
-      try
-      {
-        ErrorLogPublisher errorLogPublisher = TextErrorLogPublisher.getToolStartupTextErrorPublisher(
-            new TextWriter.STREAM(out));
-        ErrorLogger.getInstance().addLogPublisher(errorLogPublisher);
-
-        DebugLogger.getInstance().addPublisherIfRequired(new TextWriter.STREAM(out));
-      }
-      catch (Exception e)
-      {
-        err.println("Error installing the custom error logger: " + stackTraceToSingleLineString(e));
-        return 1;
-      }
     }
 
     // See if there were any user-defined sets of include/exclude attributes or
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/RebuildIndex.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/RebuildIndex.java
index b286f5e..7f82579 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/RebuildIndex.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/RebuildIndex.java
@@ -39,12 +39,7 @@
 import org.opends.server.backends.RebuildConfig.RebuildMode;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.LockFileManager;
-import org.opends.server.loggers.DebugLogger;
-import org.opends.server.loggers.ErrorLogPublisher;
-import org.opends.server.loggers.ErrorLogger;
 import org.opends.server.loggers.JDKLogging;
-import org.opends.server.loggers.TextErrorLogPublisher;
-import org.opends.server.loggers.TextWriter;
 import org.opends.server.protocols.ldap.LDAPAttribute;
 import org.opends.server.tasks.RebuildTask;
 import org.opends.server.tools.tasks.TaskTool;
@@ -282,12 +277,11 @@
   {
     if (initializeServer)
     {
-      final int init = initializeServer(err);
+      final int init = initializeServer(out, err);
       if (init != 0)
       {
         return init;
       }
-      setErrorAndDebugLogPublisher(out, err);
     }
 
     if (!configureRebuildProcess(baseDNString.getValue()))
@@ -337,47 +331,21 @@
   }
 
   /**
-   * Defines the error and the debug log publisher used in this tool.
-   *
-   * @param out
-   *          The output stream to use for standard output, or {@code null} if
-   *          standard output is not needed.
-   * @param err
-   *          The output stream to use for standard error, or {@code null} if
-   *          standard error is not needed.
-   */
-  private void setErrorAndDebugLogPublisher(final PrintStream out,
-      final PrintStream err)
-  {
-    try
-    {
-      final ErrorLogPublisher errorLogPublisher =
-          TextErrorLogPublisher
-              .getToolStartupTextErrorPublisher(new TextWriter.STREAM(out));
-      ErrorLogger.getInstance().addLogPublisher(errorLogPublisher);
-      DebugLogger.getInstance().addPublisherIfRequired(new TextWriter.STREAM(out));
-    }
-    catch (Exception e)
-    {
-      err.println("Error installing the custom error logger: "
-          + stackTraceToSingleLineString(e));
-    }
-  }
-
-  /**
    * Initializes the directory server.
    *
+   * @param out stream to write messages; may be null
    * @param err
    *          The output stream to use for standard error, or {@code null} if
    *          standard error is not needed.
    * @return The result code.
    */
-  private int initializeServer(final PrintStream err)
+  private int initializeServer(final PrintStream out, final PrintStream err)
   {
     try
     {
       new DirectoryServer.InitializationBuilder(configFile.getValue())
           .requireCryptoServices()
+          .requireErrorAndDebugLogPublisher(out, err)
           .initialize();
       return 0;
     }
@@ -557,8 +525,6 @@
   {
     try
     {
-      setErrorAndDebugLogPublisher(out, out);
-
       try
       {
         initializeArguments(true);
@@ -581,7 +547,7 @@
 
       if (initializeServer)
       {
-        final int init = initializeServer(out);
+        final int init = initializeServer(out, out);
         if (init != 0)
         {
           return init;
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/RestoreDB.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/RestoreDB.java
index 65d36a2..ed78ab1 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/RestoreDB.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/RestoreDB.java
@@ -40,12 +40,7 @@
 import org.opends.server.api.Backend.BackendOperation;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.LockFileManager;
-import org.opends.server.loggers.DebugLogger;
-import org.opends.server.loggers.ErrorLogPublisher;
-import org.opends.server.loggers.ErrorLogger;
 import org.opends.server.loggers.JDKLogging;
-import org.opends.server.loggers.TextErrorLogPublisher;
-import org.opends.server.loggers.TextWriter;
 import org.opends.server.protocols.ldap.LDAPAttribute;
 import org.opends.server.tasks.RestoreTask;
 import org.opends.server.tools.tasks.TaskTool;
@@ -285,6 +280,7 @@
       try
       {
         new DirectoryServer.InitializationBuilder(configFile.getValue())
+            .requireErrorAndDebugLogPublisher(out, err)
             .initialize();
       }
       catch (InitializationException ie)
@@ -292,19 +288,6 @@
         printWrappedText(err, ERR_CANNOT_INITIALIZE_SERVER_COMPONENTS.get(ie.getLocalizedMessage()));
         return 1;
       }
-
-      try
-      {
-        ErrorLogPublisher errorLogPublisher =
-            TextErrorLogPublisher.getToolStartupTextErrorPublisher(new TextWriter.STREAM(out));
-        ErrorLogger.getInstance().addLogPublisher(errorLogPublisher);
-        DebugLogger.getInstance().addPublisherIfRequired(new TextWriter.STREAM(out));
-      }
-      catch(Exception e)
-      {
-        err.println("Error installing the custom error logger: " +
-                    stackTraceToSingleLineString(e));
-      }
     }
 
 

--
Gitblit v1.10.0