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

Gaetan Boismal
14.25.2016 0ae99d533bc817133ea38bababa380a30fc6a082
Code cleanup

Create a separate class to check the JVM version before launching an
OpenDJ tool.
This will make the check faster by preventing creation of an unused temp
log file.
2 files added
4 files modified
247 ■■■■ changed files
opendj-server-legacy/resource/bin/_script-util.bat 2 ●●● patch | view | raw | blame | history
opendj-server-legacy/resource/bin/_script-util.sh 12 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/quicksetup/TempLogFile.java 128 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/CheckJVMVersion.java 46 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/InstallDS.java 51 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/InstallDSArgumentParser.java 8 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/resource/bin/_script-util.bat
@@ -170,7 +170,7 @@
:testJava
if "%OPENDJ_JAVA_ARGS%" == "" goto checkLegacyArgs
:continueTestJava
"%OPENDJ_JAVA_BIN%" %OPENDJ_JAVA_ARGS% org.opends.server.tools.InstallDS --testonly > NUL 2>&1
"%OPENDJ_JAVA_BIN%" %OPENDJ_JAVA_ARGS% org.opends.server.tools.CheckJVMVersion > NUL 2>&1
set RESULT_CODE=%errorlevel%
if %RESULT_CODE% == 13 goto notSupportedJavaHome
if not %RESULT_CODE% == 0 goto noValidJavaHome
opendj-server-legacy/resource/bin/_script-util.sh
@@ -167,13 +167,13 @@
test_java() {
  if test -z "${OPENDJ_JAVA_ARGS}"
  then
    "${OPENDJ_JAVA_BIN}" org.opends.server.tools.InstallDS --testonly 2> /dev/null
    "${OPENDJ_JAVA_BIN}" org.opends.server.tools.CheckJVMVersion 2> /dev/null
    RESULT_CODE=${?}
    if test ${RESULT_CODE} -eq 13
    then
      # This is a particular error code that means that the Java version is 6
      # but not supported.  Let InstallDS to display the localized error message
      "${OPENDJ_JAVA_BIN}" org.opends.server.tools.InstallDS --testonly
      # but not supported.  Let TestJVM to display the localized error message
      "${OPENDJ_JAVA_BIN}" org.opends.server.tools.CheckJVMVersion
      exit 1
    elif test ${RESULT_CODE} -ne 0
    then
@@ -194,13 +194,13 @@
      exit 1
    fi
  else
    "${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} org.opends.server.tools.InstallDS --testonly 2> /dev/null
    "${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} org.opends.server.tools.CheckJVMVersion 2> /dev/null
    RESULT_CODE=${?}
    if test ${RESULT_CODE} -eq 13
    then
      # This is a particular error code that means that the Java version is 7
      # but not supported.  Let InstallDS to display the localized error message
      "${OPENDJ_JAVA_BIN}" org.opends.server.tools.InstallDS --testonly
      # but not supported.  Let TestJVM to display the localized error message
      "${OPENDJ_JAVA_BIN}" org.opends.server.tools.CheckJVMVersion
      exit 1
    elif test ${RESULT_CODE} -ne 0
    then
opendj-server-legacy/src/main/java/org/opends/quicksetup/TempLogFile.java
New file
@@ -0,0 +1,128 @@
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */
package org.opends.quicksetup;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.Date;
import java.text.DateFormat;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.server.loggers.JDKLogging;
/** This class represents a temporary log file which should be usually deleted if linked operation succeeded. */
public class TempLogFile
{
  private static final LocalizedLogger localizedLogger = LocalizedLogger.getLoggerForThisClass();
  private static final String OPENDS_LOGGER_NAME = "org.opends";
  /**
   * Creates a new temporary log file.
   * <p>
   * Log file will be generated in the OS temporary directory and its name will have
   * the following pattern: prefix-[RANDOM_NUMBER_STRING].log
   *
   * @param prefix
   *          log file prefix to which log messages will be written.
   * @return a new temporary log file.
   */
  public static TempLogFile newTempLogFile(final String prefix)
  {
    try
    {
      return new TempLogFile(File.createTempFile(prefix, ".log"));
    }
    catch (final IOException e)
    {
      localizedLogger.error(LocalizableMessage.raw("Unable to create temp log file because: " + e.getMessage()), e);
      return new TempLogFile();
    }
  }
  /** Prevents messages written to loggers from appearing in the console output. */
  private static void disableConsoleLogging(final Logger logger)
  {
    if (!"true".equalsIgnoreCase(System.getenv("OPENDJ_LOG_TO_STDOUT")))
    {
      logger.setUseParentHandlers(false);
    }
  }
  private final File logFile;
  private final FileHandler fileHandler;
  private TempLogFile()
  {
    this.logFile = null;
    this.fileHandler = null;
  }
  private TempLogFile(final File file) throws IOException
  {
    logFile = file;
    fileHandler = new FileHandler(logFile.getCanonicalPath());
    fileHandler.setFormatter(JDKLogging.getFormatter());
    final Logger parentLogger = Logger.getLogger(OPENDS_LOGGER_NAME);
    parentLogger.addHandler(fileHandler);
    disableConsoleLogging(parentLogger);
    final Logger logger = Logger.getLogger(getClass().getPackage().getName());
    logger.info("QuickSetup application launched " + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)
          .format(new Date()));
  }
  /**
   * Gets the name of the log file.
   *
   * @return File representing the log file
   */
  public File getLogFile()
  {
    return logFile;
  }
  /** Closes the log file handler and delete the temp log file . */
  public void deleteLogFileAfterSuccess()
  {
    if (isEnabled())
    {
      fileHandler.close();
      logFile.delete();
    }
  }
  /**
   * Return {@code true} if a temp log file has been created and could be used to log messages.
   * @return {@code true} if a temp log file has been created and could be used to log messages.
   */
  public boolean isEnabled()
  {
    return logFile != null;
  }
  /**
   * Return the absolute path of the temp log file.
   * @return the absolute path of the temp log file.
   */
  public String getPath()
  {
    return logFile.getAbsolutePath();
  }
}
opendj-server-legacy/src/main/java/org/opends/server/tools/CheckJVMVersion.java
New file
@@ -0,0 +1,46 @@
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 *  Copyright 2016 ForgeRock AS.
 */
package org.opends.server.tools;
import org.opends.quicksetup.util.IncompatibleVersionException;
import org.opends.quicksetup.util.Utils;
/** Class used by script to ensure the running java version is compatible with OpenDJ software. */
public class CheckJVMVersion
{
  /** Incompatible java version return code. */
  private static final int JAVA_VERSION_INCOMPATIBLE = 8;
  /**
   * Ensure that running Java version is supported.
   *
   * @param args
   *         unused
   */
  public static void main(final String[] args)
  {
    try
    {
      Utils.checkJavaVersion();
      System.exit(0);
    }
    catch (final IncompatibleVersionException ive)
    {
      System.out.println(ive.getMessageObject());
      System.exit(JAVA_VERSION_INCOMPATIBLE);
    }
  }
}
opendj-server-legacy/src/main/java/org/opends/server/tools/InstallDS.java
@@ -140,10 +140,7 @@
    ERROR_USER_CANCELLED(6),
    /** The user doesn't accept the license. */
    ERROR_LICENSE_NOT_ACCEPTED(7),
    /** Incompatible java version. */
    JAVA_VERSION_INCOMPATIBLE(8);
    ERROR_LICENSE_NOT_ACCEPTED(7);
    private int returnCode;
    private InstallReturnCode(int returnCode)
@@ -243,19 +240,6 @@
  }
  /**
   * The main method for the InstallDS CLI tool.
   *
   * @param args
   *          the command-line arguments provided to this program.
   */
  public static void main(String[] args)
  {
    final int retCode = mainCLI(args, System.out, System.err, System.in);
    System.exit(retCode);
  }
  /**
   * Parses the provided command-line arguments and uses that information to run
   * the setup tool.
   *
@@ -349,12 +333,7 @@
      return InstallReturnCode.ERROR_USER_DATA.getReturnCode();
    }
    if (argParser.testOnlyArg.isPresent() && !testJVM())
    {
        return InstallReturnCode.JAVA_VERSION_INCOMPATIBLE.getReturnCode();
    }
    if (argParser.usageOrVersionDisplayed() || argParser.testOnlyArg.isPresent())
    if (argParser.usageOrVersionDisplayed())
    {
      return InstallReturnCode.SUCCESSFUL_NOP.getReturnCode();
    }
@@ -478,32 +457,6 @@
    return InstallReturnCode.SUCCESSFUL;
  }
  private boolean testJVM()
  {
    // Delete the log file that does not contain any information.  The test only
    // mode is called several times by the setup script and if we do not remove
    // it we have a lot of empty log files.
    try
    {
      QuickSetupLog.getLogFile().deleteOnExit();
    }
    catch (final Throwable t)
    {
      logger.warn(LocalizableMessage.raw("Error while trying to update the contents of "
          + "the set-java-home file in test only mode: " + t, t));
    }
    try
    {
      Utils.checkJavaVersion();
      return true;
    }
    catch (final IncompatibleVersionException ive)
    {
      println(ive.getMessageObject());
      return false;
    }
  }
  private boolean checkLicense()
  {
    if (!LicenseFile.exists()) {
opendj-server-legacy/src/main/java/org/opends/server/tools/InstallDSArgumentParser.java
@@ -63,7 +63,6 @@
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
  BooleanArgument   testOnlyArg;
  BooleanArgument   cliArg;
  BooleanArgument   addBaseEntryArg;
  BooleanArgument   showUsageArg;
@@ -121,13 +120,6 @@
   */
  public void initializeArguments() throws ArgumentException
  {
    testOnlyArg =
            BooleanArgument.builder(OPTION_LONG_TESTONLY_ARGUMENT)
                    .description(INFO_ARGUMENT_DESCRIPTION_TESTONLY.get())
                    .hidden()
                    .buildArgument();
    addArgument(testOnlyArg);
    cliArg = cliArgument();
    addArgument(cliArg);