mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
21.01.2016 d8e799bd818c9f26198a2d586bc8c647068f82dd
tools: push initialization of loggers to the InitializationBuilder

All tools (but one) setting up Error and Debug loggers have been migrated.
However ImportLDIF has not been migrated since it does not initialize the debug logger.
5 files modified
165 ■■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java 62 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/BackUpDB.java 20 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/ExportLDIF.java 20 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/RebuildIndex.java 44 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/RestoreDB.java 19 ●●●●● patch | view | raw | blame | history
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));
      }
    }
  }
  /**
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));
      }
    }
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
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;
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));
      }
    }