From 5ff46475f706645bc4767faa2efcf295564d8f81 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Wed, 15 Jul 2015 09:42:55 +0000
Subject: [PATCH] OPENDJ-2027 Code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/tools/ConfigureWindowsService.java |  493 +++++++++++++++++++++++-------------------------------
 1 files changed, 209 insertions(+), 284 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/ConfigureWindowsService.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/ConfigureWindowsService.java
index e48dd50..9436525 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/ConfigureWindowsService.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/ConfigureWindowsService.java
@@ -73,103 +73,61 @@
   */
 public class ConfigureWindowsService
 {
-  /**
-   * The fully-qualified name of this class.
-   */
-  private static final String CLASS_NAME =
-    "org.opends.server.tools.ConfigureWindowsService";
+  /** The fully-qualified name of this class. */
+  private static final String CLASS_NAME = "org.opends.server.tools.ConfigureWindowsService";
 
   private static final String DEBUG_OPTION = "--debug";
-  /**
-   * Option to be used when calling the launchers.
-   */
+  /** Option to be used when calling the launchers. */
   public static final String LAUNCHER_OPTION = "run";
 
-  private static int ERROR = 1;
+  private static final int SUCCESS = 0;
+  private static final int ERROR = 1;
 
-  /**
-   * Return codes for the method enableService.
-   */
-  /**
-   * The service was successfully enabled.
-   */
+  /** Return codes for the method enableService. */
+  /** The service was successfully enabled. */
   public static final int SERVICE_ENABLE_SUCCESS = 0;
-  /**
-   * The service was already enabled.
-   */
+  /** The service was already enabled. */
   public static final int SERVICE_ALREADY_ENABLED = 1;
-  /**
-   * The service name was already in use.
-   */
+  /** The service name was already in use. */
   public static final int SERVICE_NAME_ALREADY_IN_USE = 2;
-  /**
-   * An error occurred enabling the service.
-   */
+  /** An error occurred enabling the service. */
   public static final int SERVICE_ENABLE_ERROR = 3;
 
-  /**
-   * Return codes for the method disableService.
-   */
-  /**
-   * The service was successfully disabled.
-   */
+  /** Return codes for the method disableService. */
+  /** The service was successfully disabled. */
   public static final int SERVICE_DISABLE_SUCCESS = 0;
-  /**
-   * The service was already disabled.
-   */
+  /** The service was already disabled. */
   public static final int SERVICE_ALREADY_DISABLED = 1;
-  /**
-   * The service is marked for deletion.
-   */
+  /** The service is marked for deletion. */
   public static final int SERVICE_MARKED_FOR_DELETION = 2;
-  /**
-   * An error occurred disabling the service.
-   */
+  /** An error occurred disabling the service. */
   public static final int SERVICE_DISABLE_ERROR = 3;
 
-  /**
-   * Return codes for the method serviceState.
-   */
-  /**
-   * The service is enabled.
-   */
+  /** Return codes for the method serviceState. */
+  /** The service is enabled. */
   public static final int SERVICE_STATE_ENABLED = 0;
-  /**
-   * The service is disabled.
-   */
+  /** The service is disabled. */
   public static final int SERVICE_STATE_DISABLED = 1;
-  /**
-   * An error occurred checking the service state.
-   */
+  /** An error occurred checking the service state. */
   public static final int SERVICE_STATE_ERROR = 2;
 
-  /**
-   * Return codes for the method cleanupService.
-   */
-  /**
-   * The service cleanup worked.
-   */
+  /** Return codes for the method cleanupService. */
+  /** The service cleanup worked. */
   public static final int SERVICE_CLEANUP_SUCCESS = 0;
-  /**
-   * The service could not be found.
-   */
+  /** The service could not be found. */
   public static final int SERVICE_NOT_FOUND = 1;
-  /**
-   * An error occurred cleaning up the service.
-   */
+  /** An error occurred cleaning up the service. */
   public static final int SERVICE_CLEANUP_ERROR = 2;
-  /**
-   * The service is marked for deletion.
-   */
+  /** The service is marked for deletion. */
   public static final int SERVICE_CLEANUP_MARKED_FOR_DELETION = 3;
 
-
   /**
-   * Configures the Windows service for this instance on this machine.
-   * This tool allows to enable and disable OpenDJ to run as a Windows service
-   * and allows to know if OpenDJ is running as a Windows service or not.
+   * Configures the Windows service for this instance on this machine. This tool
+   * allows to enable and disable OpenDJ to run as a Windows service and allows
+   * to know if OpenDJ is running as a Windows service or not.
    *
-   * @param  args  The command-line arguments provided to this program.
+   * @param args
+   *          The command-line arguments provided to this program.
    */
   public static void main(String[] args)
   {
@@ -179,29 +137,28 @@
   }
 
   /**
-   * Configures the Windows service for this instance on this machine.
-   * This tool allows to enable and disable OpenDJ to run as a Windows service
-   * and allows to know if OpenDJ is running as a Windows service or not.
+   * Configures the Windows service for this instance on this machine. This tool
+   * allows to enable and disable OpenDJ to run as a Windows service and allows
+   * to know if OpenDJ is running as a Windows service or not.
    *
-   * @param  args  The command-line arguments provided to this program.
-   * @param outStream the stream used to write the standard output.
-   * @param errStream the stream used to write the error output.
+   * @param args
+   *          The command-line arguments provided to this program.
+   * @param outStream
+   *          the stream used to write the standard output.
+   * @param errStream
+   *          the stream used to write the error output.
    * @return the integer code describing if the operation could be completed or
-   * not.
+   *         not.
    */
-  public static int configureWindowsService(String[] args,
-      OutputStream outStream, OutputStream errStream)
+  public static int configureWindowsService(String[] args, OutputStream outStream, OutputStream errStream)
   {
-    int returnValue = 0;
     PrintStream out = NullOutputStream.wrapOrNullStream(outStream);
     PrintStream err = NullOutputStream.wrapOrNullStream(errStream);
     JDKLogging.disableLogging();
 
-//  Define all the arguments that may be used with this program.
-    LocalizableMessage toolDescription =
-        INFO_CONFIGURE_WINDOWS_SERVICE_TOOL_DESCRIPTION.get();
-    ArgumentParser argParser = new ArgumentParser(CLASS_NAME,
-        toolDescription, false);
+    //  Define all the arguments that may be used with this program.
+    LocalizableMessage toolDescription = INFO_CONFIGURE_WINDOWS_SERVICE_TOOL_DESCRIPTION.get();
+    ArgumentParser argParser = new ArgumentParser(CLASS_NAME, toolDescription, false);
     argParser.setShortToolDescription(REF_SHORT_DESC_WINDOWS_SERVICE.get());
     BooleanArgument enableService = null;
     BooleanArgument disableService = null;
@@ -215,18 +172,15 @@
           INFO_CONFIGURE_WINDOWS_SERVICE_DESCRIPTION_ENABLE.get());
       argParser.addArgument(enableService);
 
-      disableService = new BooleanArgument("disableservice", 'd',
-          "disableService",
+      disableService = new BooleanArgument("disableservice", 'd', "disableService",
           INFO_CONFIGURE_WINDOWS_SERVICE_DESCRIPTION_DISABLE.get());
       argParser.addArgument(disableService);
 
-      serviceState = new BooleanArgument("servicestate", 's',
-          "serviceState",
+      serviceState = new BooleanArgument("servicestate", 's', "serviceState",
           INFO_CONFIGURE_WINDOWS_SERVICE_DESCRIPTION_STATE.get());
       argParser.addArgument(serviceState);
 
-      cleanupService = new StringArgument("cleanupservice", 'c',
-          "cleanupService", false, false, true,
+      cleanupService = new StringArgument("cleanupservice", 'c', "cleanupService", false, false, true,
           INFO_SERVICE_NAME_PLACEHOLDER.get(), null, null,
           INFO_CONFIGURE_WINDOWS_SERVICE_DESCRIPTION_CLEANUP.get());
       argParser.addArgument(cleanupService);
@@ -240,29 +194,25 @@
       LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
 
       err.println(wrapText(message, MAX_LINE_WIDTH));
-      returnValue = ERROR;
+      return ERROR;
     }
 
-    // Parse the command-line arguments provided to this program.
-    if (returnValue == 0)
+    try
     {
-      try
-      {
-        argParser.parseArguments(args);
-      }
-      catch (ArgumentException ae)
-      {
-        LocalizableMessage message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
+      argParser.parseArguments(args);
+    }
+    catch (ArgumentException ae)
+    {
+      LocalizableMessage message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
 
-        err.println(wrapText(message, MAX_LINE_WIDTH));
-        err.println(argParser.getUsage());
-        returnValue = ERROR;
-      }
+      err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(argParser.getUsage());
+      return ERROR;
     }
 
     // If we should just display usage or version information,
     // then it is already done
-    if ((returnValue == 0) && !argParser.usageOrVersionDisplayed())
+    if (!argParser.usageOrVersionDisplayed())
     {
       /* Check that the user only asked for one argument */
       int nArgs = 0;
@@ -282,65 +232,53 @@
       {
         nArgs++;
       }
-      if (nArgs > 1)
+      if (nArgs != 1)
       {
-        LocalizableMessage message = ERR_CONFIGURE_WINDOWS_SERVICE_TOO_MANY_ARGS.get();
+        LocalizableMessage message = nArgs == 0 ? ERR_CONFIGURE_WINDOWS_SERVICE_TOO_FEW_ARGS.get()
+                                                : ERR_CONFIGURE_WINDOWS_SERVICE_TOO_MANY_ARGS.get();
         err.println(wrapText(message, MAX_LINE_WIDTH));
         err.println(argParser.getUsage());
-        returnValue = ERROR;
-      }
-      if (nArgs == 0)
-      {
-        LocalizableMessage message = ERR_CONFIGURE_WINDOWS_SERVICE_TOO_FEW_ARGS.get();
-        err.println(wrapText(message, MAX_LINE_WIDTH));
-        err.println(argParser.getUsage());
-        returnValue = ERROR;
+        return ERROR;
       }
     }
 
-    if ((returnValue == 0) && !argParser.usageOrVersionDisplayed())
+    if (argParser.usageOrVersionDisplayed())
     {
-      if (enableService.isPresent())
-      {
-        returnValue = enableService(out, err);
-      }
-      else if (disableService.isPresent())
-      {
-        returnValue = disableService(out, err);
-      }
-      else if (serviceState.isPresent())
-      {
-        returnValue = serviceState(out, err);
-      }
-      else
-      {
-        returnValue = cleanupService(cleanupService.getValue(), out, err);
-      }
+      return SUCCESS;
     }
 
-    return returnValue;
+    if (enableService.isPresent())
+    {
+      return enableService(out, err);
+    }
+    else if (disableService.isPresent())
+    {
+      return disableService(out, err);
+    }
+    else if (serviceState.isPresent())
+    {
+      return serviceState(out, err);
+    }
+
+    return cleanupService(cleanupService.getValue(), out, err);
   }
 
   /**
    * Returns the service name associated with OpenDJ or null if no service name
    * could be found.
+   *
    * @return the service name associated with OpenDJ or null if no service name
-   * could be found.
+   *         could be found.
    */
   static String getServiceName()
   {
     String serviceName = null;
     String serverRoot = getServerRoot();
-    String[] cmd = {
-        getBinaryFullPath(),
-        "state",
-        serverRoot
-    };
+    String[] cmd = { getBinaryFullPath(), "state", serverRoot };
     try
     {
       Process p = Runtime.getRuntime().exec(cmd);
-      BufferedReader stdout = new BufferedReader(
-          new InputStreamReader(p.getInputStream()));
+      BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
       boolean processDone = false;
       String s;
       while (!processDone)
@@ -373,13 +311,16 @@
 
   /**
    * Enables OpenDJ to run as a windows service.
-   * @param out the stream used to write the standard output.
-   * @param err the stream used to write the error output.
+   *
+   * @param out
+   *          the stream used to write the standard output.
+   * @param err
+   *          the stream used to write the error output.
    * @return <CODE>SERVICE_ENABLE_SUCCESS</CODE>,
-   * <CODE>SERVICE_ENABLE_ERROR</CODE>,
-   * <CODE>SERVICE_NAME_ALREADY_IN_USE</CODE> or
-   * <CODE>SERVICE_ALREADY_ENABLED</CODE> depending on whether the service could
-   * be enabled or not.
+   *         <CODE>SERVICE_ENABLE_ERROR</CODE>,
+   *         <CODE>SERVICE_NAME_ALREADY_IN_USE</CODE> or
+   *         <CODE>SERVICE_ALREADY_ENABLED</CODE> depending on whether the
+   *         service could be enabled or not.
    */
   public static int enableService(PrintStream out, PrintStream err)
   {
@@ -390,28 +331,28 @@
     LocalizableMessage serviceDescription = Utils.getCustomizedObject(
         "INFO_WINDOWS_SERVICE_DESCRIPTION",
         INFO_WINDOWS_SERVICE_DESCRIPTION.get(getServerRoot()), LocalizableMessage.class);
-    return enableService(out, err, serviceName.toString(),
-        serviceDescription.toString());
+    return enableService(out, err, serviceName.toString(), serviceDescription.toString());
   }
 
   /**
    * Enables OpenDJ to run as a windows service.
-   * @param out the stream used to write the standard output.
-   * @param err the stream used to write the error output.
-   * @param serviceName the name of the service as it will appear in the
-   * registry.
-   * @param serviceDescription the description of the service as it will appear
-   * in the registry.
+   *
+   * @param out
+   *          the stream used to write the standard output.
+   * @param err
+   *          the stream used to write the error output.
+   * @param serviceName
+   *          the name of the service as it will appear in the registry.
+   * @param serviceDescription
+   *          the description of the service as it will appear in the registry.
    * @return <CODE>SERVICE_ENABLE_SUCCESS</CODE>,
-   * <CODE>SERVICE_ENABLE_ERROR</CODE>,
-   * <CODE>SERVICE_NAME_ALREADY_IN_USE</CODE> or
-   * <CODE>SERVICE_ALREADY_ENABLED</CODE> depending on whether the service could
-   * be enabled or not.
+   *         <CODE>SERVICE_ENABLE_ERROR</CODE>,
+   *         <CODE>SERVICE_NAME_ALREADY_IN_USE</CODE> or
+   *         <CODE>SERVICE_ALREADY_ENABLED</CODE> depending on whether the
+   *         service could be enabled or not.
    */
-  public static int enableService(PrintStream out, PrintStream err,
-      String serviceName, String serviceDescription)
+  public static int enableService(PrintStream out, PrintStream err, String serviceName, String serviceDescription)
   {
-    int returnValue;
     LocalizableMessage msg;
     String serverRoot = getServerRoot();
 
@@ -446,8 +387,7 @@
 
     try
     {
-      boolean isServerRunning = Utilities.isServerRunning(
-          new File(serverRoot));
+      boolean isServerRunning = Utilities.isServerRunning(new File(serverRoot));
 
       int resultCode = Runtime.getRuntime().exec(cmd).waitFor();
       switch (resultCode)
@@ -458,73 +398,69 @@
           // We have to launch the windows service.  The service code already
           // handles this case (the service binary is executed when the server
           // already runs).
-          returnValue = StartWindowsService.startWindowsService(out, err);
+          final int returnValue = StartWindowsService.startWindowsService(out, err);
           if (returnValue == 0)
           {
-            returnValue = SERVICE_ENABLE_SUCCESS;
             msg = INFO_WINDOWS_SERVICE_SUCCESSULLY_ENABLED.get();
             out.println(wrapText(msg, MAX_LINE_WIDTH));
+            return SERVICE_ENABLE_SUCCESS;
           }
           else
           {
-            msg = ERR_WINDOWS_SERVICE_ENABLING_ERROR_STARTING_SERVER.get(
-                returnValue);
-            returnValue = SERVICE_ENABLE_ERROR;
+            msg = ERR_WINDOWS_SERVICE_ENABLING_ERROR_STARTING_SERVER.get(returnValue);
             err.println(wrapText(msg, MAX_LINE_WIDTH));
+            return SERVICE_ENABLE_ERROR;
           }
         }
         else
         {
-          returnValue = SERVICE_ENABLE_SUCCESS;
           msg = INFO_WINDOWS_SERVICE_SUCCESSULLY_ENABLED.get();
           out.println(wrapText(msg, MAX_LINE_WIDTH));
+          return SERVICE_ENABLE_SUCCESS;
         }
-        break;
       case 1:
-        returnValue = SERVICE_ALREADY_ENABLED;
         msg = INFO_WINDOWS_SERVICE_ALREADY_ENABLED.get();
         out.println(wrapText(msg, MAX_LINE_WIDTH));
-        break;
+        return SERVICE_ALREADY_ENABLED;
       case 2:
-        returnValue = SERVICE_NAME_ALREADY_IN_USE;
         msg = ERR_WINDOWS_SERVICE_NAME_ALREADY_IN_USE.get();
         err.println(wrapText(msg, MAX_LINE_WIDTH));
-        break;
+        return SERVICE_NAME_ALREADY_IN_USE;
       case 3:
-        returnValue = SERVICE_ENABLE_ERROR;
         msg = ERR_WINDOWS_SERVICE_ENABLE_ERROR.get();
         err.println(wrapText(msg, MAX_LINE_WIDTH));
-        break;
+        return SERVICE_ENABLE_ERROR;
       default:
-        returnValue = SERVICE_ENABLE_ERROR;
         msg = ERR_WINDOWS_SERVICE_ENABLE_ERROR.get();
         err.println(wrapText(msg, MAX_LINE_WIDTH));
+        return SERVICE_ENABLE_ERROR;
       }
     }
     catch (Throwable t)
     {
       err.println("Unexpected throwable: "+t);
       t.printStackTrace();
-      returnValue = SERVICE_ENABLE_ERROR;
       msg = ERR_WINDOWS_SERVICE_ENABLE_ERROR.get();
       err.println(wrapText(msg, MAX_LINE_WIDTH));
+      return SERVICE_ENABLE_ERROR;
     }
-    return returnValue;
   }
 
   /**
    * Disables OpenDJ to run as a windows service.
-   * @param out the stream used to write the standard output.
-   * @param err the stream used to write the error output.
+   *
+   * @param out
+   *          the stream used to write the standard output.
+   * @param err
+   *          the stream used to write the error output.
    * @return <CODE>SERVICE_DISABLE_SUCCESS</CODE>,
-   * <CODE>SERVICE_DISABLE_ERROR</CODE>,
-   * <CODE>SERVICE_MARKED_FOR_DELETION</CODE> or
-   * <CODE>SERVICE_ALREADY_DISABLED</CODE> depending on whether the service
-   * could be disabled or not.
+   *         <CODE>SERVICE_DISABLE_ERROR</CODE>,
+   *         <CODE>SERVICE_MARKED_FOR_DELETION</CODE> or
+   *         <CODE>SERVICE_ALREADY_DISABLED</CODE> depending on whether the
+   *         service could be disabled or not.
    */
   public static int disableService(PrintStream out, PrintStream err)
   {
-    int returnValue;
     LocalizableMessage msg;
     String serverRoot = getServerRoot();
     String[] cmd;
@@ -556,56 +492,54 @@
       switch (resultCode)
       {
       case 0:
-        returnValue = SERVICE_DISABLE_SUCCESS;
         msg = INFO_WINDOWS_SERVICE_SUCCESSULLY_DISABLED.get();
         out.println(msg);
-        break;
+        return SERVICE_DISABLE_SUCCESS;
       case 1:
-        returnValue = SERVICE_ALREADY_DISABLED;
         msg = INFO_WINDOWS_SERVICE_ALREADY_DISABLED.get();
         out.println(msg);
-        break;
+        return SERVICE_ALREADY_DISABLED;
       case 2:
-        returnValue = SERVICE_MARKED_FOR_DELETION;
         msg = WARN_WINDOWS_SERVICE_MARKED_FOR_DELETION.get();
         out.println(msg);
-        break;
+        return SERVICE_MARKED_FOR_DELETION;
       case 3:
-        returnValue = SERVICE_DISABLE_ERROR;
         msg = ERR_WINDOWS_SERVICE_DISABLE_ERROR.get();
         err.println(msg);
-        break;
+        return SERVICE_DISABLE_ERROR;
       default:
-        returnValue = SERVICE_DISABLE_ERROR;
         msg = ERR_WINDOWS_SERVICE_DISABLE_ERROR.get();
         err.println(msg);
+        return SERVICE_DISABLE_ERROR;
       }
     }
     catch (Throwable t)
     {
       t.printStackTrace();
-      returnValue = SERVICE_DISABLE_ERROR;
       msg = ERR_WINDOWS_SERVICE_DISABLE_ERROR.get();
       err.println(msg);
+      return SERVICE_DISABLE_ERROR;
     }
-    return returnValue;
   }
 
   /**
    * Cleans up a service for a given service name.
-   * @param serviceName the service name to be cleaned up.
-   * @param out the stream used to write the standard output.
-   * @param err the stream used to write the error output.
+   *
+   * @param serviceName
+   *          the service name to be cleaned up.
+   * @param out
+   *          the stream used to write the standard output.
+   * @param err
+   *          the stream used to write the error output.
    * @return <CODE>SERVICE_CLEANUP_SUCCESS</CODE>,
-   * <CODE>SERVICE_NOT_FOUND</CODE>,
-   * <CODE>SERVICE_MARKED_FOR_DELETION</CODE> or
-   * <CODE>SERVICE_CLEANUP_ERROR</CODE> depending on whether the service
-   * could be found or not.
+   *         <CODE>SERVICE_NOT_FOUND</CODE>,
+   *         <CODE>SERVICE_MARKED_FOR_DELETION</CODE> or
+   *         <CODE>SERVICE_CLEANUP_ERROR</CODE> depending on whether the service
+   *         could be found or not.
    */
   public static int cleanupService(String serviceName, PrintStream out,
       PrintStream err)
   {
-    int returnValue;
     LocalizableMessage msg;
     String[] cmd;
     if (hasUAC())
@@ -636,53 +570,64 @@
       switch (resultCode)
       {
       case 0:
-        returnValue = SERVICE_CLEANUP_SUCCESS;
         msg = INFO_WINDOWS_SERVICE_CLEANUP_SUCCESS.get(serviceName);
         out.println(msg);
-        break;
+        return SERVICE_CLEANUP_SUCCESS;
       case 1:
-        returnValue = SERVICE_NOT_FOUND;
         msg = ERR_WINDOWS_SERVICE_CLEANUP_NOT_FOUND.get(serviceName);
         err.println(msg);
-        break;
+        return SERVICE_NOT_FOUND;
       case 2:
-        returnValue = SERVICE_CLEANUP_MARKED_FOR_DELETION;
         msg = WARN_WINDOWS_SERVICE_CLEANUP_MARKED_FOR_DELETION.get(serviceName);
         out.println(msg);
-        break;
+        return SERVICE_CLEANUP_MARKED_FOR_DELETION;
       case 3:
-        returnValue = SERVICE_CLEANUP_ERROR;
         msg = ERR_WINDOWS_SERVICE_CLEANUP_ERROR.get(serviceName);
         err.println(msg);
-        break;
+        return SERVICE_CLEANUP_ERROR;
       default:
-        returnValue = SERVICE_CLEANUP_ERROR;
         msg = ERR_WINDOWS_SERVICE_CLEANUP_ERROR.get(serviceName);
         err.println(msg);
+        return SERVICE_CLEANUP_ERROR;
       }
     }
     catch (Throwable t)
     {
-      returnValue = SERVICE_CLEANUP_ERROR;
       msg = ERR_WINDOWS_SERVICE_CLEANUP_ERROR.get(serviceName);
       err.println(msg);
       err.println("Exception:" + t);
+      return SERVICE_CLEANUP_ERROR;
     }
-    return returnValue;
   }
 
   /**
-    * Checks if OpenDJ is enabled as a windows service and if it is
-    * write the serviceName in the output stream (if it is not null).
-    * @param out the stream used to write the standard output.
-    * @param err the stream used to write the error output.
-    * @return <CODE>SERVICE_STATE_ENABLED</CODE>,
-    * <CODE>SERVICE_STATE_DISABLED</CODE> or <CODE>SERVICE_STATE_ERROR</CODE>
-    * depending on the state of the service.
-    */
+   * Checks if OpenDJ is enabled as a windows service.
+   *
+   * @return <CODE>SERVICE_STATE_ENABLED</CODE>,
+   *         <CODE>SERVICE_STATE_DISABLED</CODE> or
+   *         <CODE>SERVICE_STATE_ERROR</CODE> depending on the state of the
+   *         service.
+   */
+  public static int serviceState()
+  {
+    return serviceState(NullOutputStream.printStream(), NullOutputStream.printStream());
+  }
+
+  /**
+   * Checks if OpenDJ is enabled as a windows service and if it is write the
+   * serviceName in the output stream (if it is not null).
+   *
+   * @param out
+   *          the stream used to write the standard output.
+   * @param err
+   *          the stream used to write the error output.
+   * @return <CODE>SERVICE_STATE_ENABLED</CODE>,
+   *         <CODE>SERVICE_STATE_DISABLED</CODE> or
+   *         <CODE>SERVICE_STATE_ERROR</CODE> depending on the state of the
+   *         service.
+   */
   public static int serviceState(PrintStream out, PrintStream err)
   {
-    int returnValue;
     LocalizableMessage msg;
     String serviceName = null;
 
@@ -697,11 +642,8 @@
     try
     {
       int resultCode = -1;
-      ProcessBuilder pb = new ProcessBuilder(cmd);
-      Process process = pb.start();
-
-      BufferedReader stdout =
-          new BufferedReader(new InputStreamReader(process.getInputStream()));
+      Process process = new ProcessBuilder(cmd).start();
+      BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
 
       boolean processDone = false;
       String s;
@@ -723,60 +665,41 @@
           }
         }
       }
-
       switch (resultCode)
       {
       case 0:
-        returnValue = SERVICE_STATE_ENABLED;
-        if (out != null)
-        {
-          msg = INFO_WINDOWS_SERVICE_ENABLED.get(serviceName);
-          out.println(msg);
-        }
-        break;
+        msg = INFO_WINDOWS_SERVICE_ENABLED.get(serviceName);
+        out.println(msg);
+        return SERVICE_STATE_ENABLED;
       case 1:
-        returnValue = SERVICE_STATE_DISABLED;
-        if (out != null)
-        {
-          msg = INFO_WINDOWS_SERVICE_DISABLED.get();
-          out.println(msg);
-        }
-        break;
+        msg = INFO_WINDOWS_SERVICE_DISABLED.get();
+        out.println(msg);
+        return SERVICE_STATE_DISABLED;
       case 2:
-        returnValue = SERVICE_STATE_ERROR;
-        if (err != null)
-        {
-          msg = ERR_WINDOWS_SERVICE_STATE_ERROR.get();
-          err.println(msg);
-        }
-        break;
+        msg = ERR_WINDOWS_SERVICE_STATE_ERROR.get();
+        err.println(msg);
+        return SERVICE_STATE_ERROR;
       default:
-        returnValue = SERVICE_STATE_ERROR;
-        if (err != null)
-        {
-          msg = ERR_WINDOWS_SERVICE_STATE_ERROR.get();
-          err.println(msg);
-        }
+        msg = ERR_WINDOWS_SERVICE_STATE_ERROR.get();
+        err.println(msg);
+        return SERVICE_STATE_ERROR;
       }
     }
     catch (Throwable t)
     {
-      returnValue = SERVICE_STATE_ERROR;
-      if (err != null)
-      {
-        msg = ERR_WINDOWS_SERVICE_STATE_ERROR.get();
-        err.println(msg);
-        err.println(wrapText(t.toString(), MAX_LINE_WIDTH));
-      }
+      msg = ERR_WINDOWS_SERVICE_STATE_ERROR.get();
+      err.println(msg);
+      err.println(wrapText(t.toString(), MAX_LINE_WIDTH));
+      return SERVICE_STATE_ERROR;
     }
-    return returnValue;
   }
 
   /**
    * Returns the Directory Server installation path in a user friendly
    * representation.
+   *
    * @return the Directory Server installation path in a user friendly
-   * representation.
+   *         representation.
    */
   private static String getServerRoot()
   {
@@ -806,40 +729,42 @@
 
   /**
    * Returns the full path of the executable used by this class to perform
-   * operations related to the service.  This binaries file has the asInvoker
+   * operations related to the service. This binaries file has the asInvoker
    * value in its manifest.
+   *
    * @return the full path of the executable used by this class to perform
-   * operations related to the service.
+   *         operations related to the service.
    */
   private static String getBinaryFullPath()
   {
-    return SetupUtils.getScriptPath(
-        getServerRoot()+"\\lib\\opendj_service.exe");
+    return SetupUtils.getScriptPath(getServerRoot() + "\\lib\\opendj_service.exe");
   }
 
   /**
    * Returns the full path of the executable that has a manifest requiring
-   * administrator privileges used by this class to perform
-   * operations related to the service.
+   * administrator privileges used by this class to perform operations related
+   * to the service.
+   *
    * @return the full path of the executable that has a manifest requiring
-   * administrator privileges used by this class to perform
-   * operations related to the service.
+   *         administrator privileges used by this class to perform operations
+   *         related to the service.
    */
   public static String getLauncherAdministratorBinaryFullPath()
   {
-    return getServerRoot()+"\\lib\\launcher_administrator.exe";
+    return getServerRoot() + "\\lib\\launcher_administrator.exe";
   }
 
   /**
    * Returns the full path of the executable that has a manifest requiring
-   * administrator privileges used by this class to perform
-   * operations related to the service.
+   * administrator privileges used by this class to perform operations related
+   * to the service.
+   *
    * @return the full path of the executable that has a manifest requiring
-   * administrator privileges used by this class to perform
-   * operations related to the service.
+   *         administrator privileges used by this class to perform operations
+   *         related to the service.
    */
   public static String getLauncherBinaryFullPath()
   {
-    return getServerRoot()+"\\lib\\winlauncher.exe";
+    return getServerRoot() + "\\lib\\winlauncher.exe";
   }
 }

--
Gitblit v1.10.0