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

jvergara
30.49.2009 b593e7ae07a38e372ec12b717fcdca3211509942
opendj-sdk/opends/src/server/org/opends/server/tools/dsconfig/DSConfig.java
@@ -22,7 +22,7 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2007-2008 Sun Microsystems, Inc.
 *      Copyright 2007-2009 Sun Microsystems, Inc.
 */
package org.opends.server.tools.dsconfig;
@@ -48,6 +48,9 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.opends.messages.Message;
import org.opends.quicksetup.util.Utils;
@@ -90,6 +93,12 @@
 */
public final class DSConfig extends ConsoleApplication {
  /** Prefix for log files. */
  static public final String LOG_FILE_PREFIX = "dsconfig";
  /** Suffix for log files. */
  static public final String LOG_FILE_SUFFIX = ".log";
  /**
   * A menu call-back which runs a sub-command interactively.
   */
@@ -344,6 +353,13 @@
        return 1;
      }
    }
    try {
      DSConfigLog.initLogFileHandler(
          File.createTempFile(LOG_FILE_PREFIX, LOG_FILE_SUFFIX));
    } catch (Throwable t) {
      System.err.println("Unable to initialize log");
      t.printStackTrace();
    }
    // Run the application.
    return app.run(args);
@@ -1128,3 +1144,62 @@
    }
  }
}
/**
 * Utilities for setting up DSConfig application log. This is required since
 * DSConfig is using classes (such as ApplicationTrustManager) than can log
 * messages.
 */
class DSConfigLog
{
  private static String[] packages = {
    "org.opends"
  };
  static private File logFile = null;
  static private FileHandler fileHandler;
  /**
   * Creates a new file handler for writing log messages to the file indicated
   * by <code>file</code>.
   * @param file log file to which log messages will be written
   * @throws IOException if something goes wrong
   */
  static public void initLogFileHandler(File file) throws IOException {
    if (!isInitialized())
    {
      logFile = file;
      fileHandler = new FileHandler(logFile.getCanonicalPath());
      fileHandler.setFormatter(new SimpleFormatter());
      for (String packageName : packages)
      {
        Logger logger = Logger.getLogger(packageName);
        logger.setUseParentHandlers(false); // disable logging to console
        logger.addHandler(fileHandler);
      }
    }
  }
  /**
   * Writes messages under a given package in the file handler defined when
   * calling initLogFileHandler.  Note that initLogFileHandler should be called
   * before calling this method.
   * @param packageName the package name.
   * @throws IOException if something goes wrong
   */
  static public void initPackage(String packageName) throws IOException {
    Logger logger = Logger.getLogger(packageName);
    logger.setUseParentHandlers(false); // disable logging to console
    logger.addHandler(fileHandler);
  }
  /**
   * Indicates whether or not the log file has been initialized.
   * @return true when the log file has been initialized
   */
  static private boolean isInitialized() {
    return logFile != null;
  }
}