From 04f17d173c9945abfc931d0794fa8d69f821a264 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Wed, 29 Aug 2007 22:55:03 +0000
Subject: [PATCH] Update the CreateRCScript tool so that it provides the ability to specify the user that the server should run as (invoked via "su"), and also lets the user specify the JAVA_HOME and JAVA_ARGS settings that should be used.

---
 opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java |   27 ++++++
 opendj-sdk/opends/resource/bin/stop-ds                                   |   26 +++++-
 opendj-sdk/opends/src/messages/messages/tools.properties                 |    8 ++
 opendj-sdk/opends/src/server/org/opends/server/tools/CreateRCScript.java |   71 +++++++++++++++--
 opendj-sdk/opends/src/server/org/opends/server/tools/StopDS.java         |   68 +++++++++++++++--
 opendj-sdk/opends/resource/bin/start-ds                                  |   12 ++
 6 files changed, 184 insertions(+), 28 deletions(-)

diff --git a/opendj-sdk/opends/resource/bin/start-ds b/opendj-sdk/opends/resource/bin/start-ds
index a2f69a7..4e314cd 100755
--- a/opendj-sdk/opends/resource/bin/start-ds
+++ b/opendj-sdk/opends/resource/bin/start-ds
@@ -139,7 +139,7 @@
             --configClass org.opends.server.extensions.ConfigFileHandler \
             --configFile "${CONFIG_FILE}" --checkStartability "${@}"
 EC=${?}
-if test ${EC} -eq 99
+if test ${EC} -eq 99 -o ${EC} -eq 103
 then
   #
   # run detach
@@ -150,8 +150,14 @@
        --configClass org.opends.server.extensions.ConfigFileHandler \
        --configFile "${CONFIG_FILE}" "${@}" > "${LOG_FILE}" 2>&1 &
   echo $! > "${PID_FILE}"
-  "${JAVA_BIN}" -Xms8M -Xmx8M org.opends.server.tools.WaitForFileDelete \
-       --targetFile "${STARTING_FILE}" --logFile "${LOG_FILE}"
+  if test ${EC} -eq 99
+  then
+    "${JAVA_BIN}" -Xms8M -Xmx8M org.opends.server.tools.WaitForFileDelete \
+         --targetFile "${STARTING_FILE}" --logFile "${LOG_FILE}"
+  else
+    "${JAVA_BIN}" -Xms8M -Xmx8M org.opends.server.tools.WaitForFileDelete \
+         --targetFile "${STARTING_FILE}"
+  fi
   exit ${?}
 else
   if test ${EC} -eq 100
diff --git a/opendj-sdk/opends/resource/bin/stop-ds b/opendj-sdk/opends/resource/bin/stop-ds
index 3a8aab4..02ee4df 100755
--- a/opendj-sdk/opends/resource/bin/stop-ds
+++ b/opendj-sdk/opends/resource/bin/stop-ds
@@ -132,6 +132,7 @@
 EXIT_CODE=1
 MUST_START_USING_SYSTEM_CALL=1
 MUST_STOP_USING_SYSTEM_CALL=1
+QUIET_MODE=1
 
 if test ${EC} -eq 98
 #
@@ -140,13 +141,17 @@
 then
   STOPPED=0
 else
-  if test ${EC} -eq 99
+  if test ${EC} -eq 99 -o ${EC} -eq 105
   #
   # Already stopped and must start locally.
   #
   then
     STOPPED=0
     MUST_START_USING_SYSTEM_CALL=0
+    if test ${EC} -eq 105
+    then
+      QUIET_MODE=0
+    fi
   else
     if test ${EC} -eq 100
     then
@@ -155,13 +160,17 @@
       #
       MUST_STOP_USING_SYSTEM_CALL=0
     else
-      if test ${EC} -eq 101
+      if test ${EC} -eq 101 -o ${EC} -eq 106
       then
         #
         # Restart using system call
         #
         MUST_STOP_USING_SYSTEM_CALL=0
         MUST_START_USING_SYSTEM_CALL=0
+        if test ${EC} -eq 106
+        then
+          QUIET_MODE=0
+        fi
       else
         if test ${EC} -ne 102
         then
@@ -212,9 +221,16 @@
 then
   if test ${STOPPED} -eq 0
   then
-    "${INSTANCE_ROOT}/bin/start-ds"
-    EXIT_CODE=${?}
-    exit ${EXIT_CODE}
+    if test ${QUIET_MODE} -eq 0
+    then
+      "${INSTANCE_ROOT}/bin/start-ds" --quiet
+      EXIT_CODE=${?}
+      exit ${EXIT_CODE}
+    else
+      "${INSTANCE_ROOT}/bin/start-ds"
+      EXIT_CODE=${?}
+      exit ${EXIT_CODE}
+    fi
   fi
 #
 # The user does not want to start the server locally and it is already stopped.
diff --git a/opendj-sdk/opends/src/messages/messages/tools.properties b/opendj-sdk/opends/src/messages/messages/tools.properties
index 958efa6..ae5f24b 100644
--- a/opendj-sdk/opends/src/messages/messages/tools.properties
+++ b/opendj-sdk/opends/src/messages/messages/tools.properties
@@ -1999,3 +1999,11 @@
 SEVERE_ERR_CONFIGDS_CANNOT_ENABLE_ADS_TRUST_STORE_1373=An error occurred while \
  attempting to enable the ADS trust store: %s
 SEVERE_ERR_DBTEST_MISSING_SUBCOMMAND_1374=A sub-command must be specified
+INFO_CREATERC_USER_DESCRIPTION_1375=The name of the user account under which \
+ the server should run
+INFO_CREATERC_JAVA_HOME_DESCRIPTION_1376=The path to the Java installation \
+ that should be used to run the server
+INFO_CREATERC_JAVA_ARGS_DESCRIPTION_1377=A set of arguments that should be \
+ passed to the JVM when running the server
+SEVERE_ERR_CREATERC_JAVA_HOME_DOESNT_EXIST_1378=The directory %s specified \
+ as the JAVA_HOME path does not exist or is not a directory
diff --git a/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java b/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java
index d5662b3..bde7bd0 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java
@@ -98,6 +98,7 @@
 import org.opends.messages.MessageDescriptor;
 import org.opends.messages.Message;
 import static org.opends.messages.CoreMessages.*;
+import static org.opends.messages.ToolMessages.*;
 import org.opends.server.monitors.BackendMonitor;
 import org.opends.server.monitors.ConnectionHandlerMonitor;
 import org.opends.server.schema.AttributeTypeSyntax;
@@ -282,6 +283,11 @@
    * Windows Service.
    */
   private static int START_AS_DETACH_CALLED_FROM_WINDOWS_SERVICE = 102;
+  /**
+   * The server must be started as detached process and should not produce any
+   * output.
+   */
+  private static int START_AS_DETACH_QUIET = 103;
 
   // The policy to use regarding single structural objectclass enforcement.
   private AcceptRejectWarn singleStructuralClassPolicy;
@@ -8990,6 +8996,7 @@
   {
     // Define the arguments that may be provided to the server.
     BooleanArgument checkStartability = null;
+    BooleanArgument quietMode         = null;
     BooleanArgument windowsNetStart   = null;
     BooleanArgument displayUsage      = null;
     BooleanArgument fullVersion       = null;
@@ -9058,6 +9065,11 @@
       argParser.addArgument(noDetach);
 
 
+      quietMode = new BooleanArgument("quiet", 'Q', "quiet",
+                                      INFO_DESCRIPTION_QUIET.get());
+      argParser.addArgument(quietMode);
+
+
       displayUsage = new BooleanArgument("help", 'H', "help",
                                          INFO_DSCORE_DESCRIPTION_USAGE.get());
       argParser.addArgument(displayUsage);
@@ -9317,9 +9329,12 @@
 
           if (noDetach.isPresent())
           {
-            MultiOutputStream multiStream =
-                 new MultiOutputStream(System.out, serverOutStream);
-            serverOutStream = new PrintStream(multiStream);
+            if (! quietMode.isPresent())
+            {
+              MultiOutputStream multiStream =
+                   new MultiOutputStream(System.out, serverOutStream);
+              serverOutStream = new PrintStream(multiStream);
+            }
           }
 
           System.setOut(serverOutStream);
@@ -9533,6 +9548,8 @@
 
     BooleanArgument noDetach =
       (BooleanArgument)argParser.getArgumentForLongID("nodetach");
+    BooleanArgument quietMode =
+      (BooleanArgument)argParser.getArgumentForLongID("quiet");
     BooleanArgument windowsNetStart =
       (BooleanArgument)argParser.getArgumentForLongID("windowsnetstart");
 
@@ -9618,6 +9635,10 @@
         {
           returnValue = START_AS_NON_DETACH;
         }
+        else if (quietMode.isPresent())
+        {
+          returnValue = START_AS_DETACH_QUIET;
+        }
         else
         {
           returnValue = START_AS_DETACH;
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/CreateRCScript.java b/opendj-sdk/opends/src/server/org/opends/server/tools/CreateRCScript.java
index afb0f1d..b1689cf 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/CreateRCScript.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/CreateRCScript.java
@@ -138,7 +138,10 @@
          new ArgumentParser(CreateRCScript.class.getName(), description, false);
 
     BooleanArgument showUsage  = null;
+    StringArgument  javaArgs   = null;
+    StringArgument  javaHome   = null;
     StringArgument  outputFile = null;
+    StringArgument  userName   = null;
 
     try
     {
@@ -148,6 +151,24 @@
       argParser.addArgument(outputFile);
 
 
+      userName = new StringArgument("username", 'u', "userName", false, false,
+                                    true, "{username}", null, null,
+                                    INFO_CREATERC_USER_DESCRIPTION.get());
+      argParser.addArgument(userName);
+
+
+      javaHome = new StringArgument("javahome", 'j', "javaHome", false, false,
+                                    true, "{path}", null, null,
+                                    INFO_CREATERC_JAVA_HOME_DESCRIPTION.get());
+      argParser.addArgument(javaHome);
+
+
+      javaArgs = new StringArgument("javaargs", 'J', "javaArgs", false, false,
+                                    true, "{args}", null,null,
+                                    INFO_CREATERC_JAVA_ARGS_DESCRIPTION.get());
+      argParser.addArgument(javaArgs);
+
+
       showUsage = new BooleanArgument("help", 'H', "help",
                                       INFO_DESCRIPTION_SHOWUSAGE.get());
       argParser.addArgument(showUsage);
@@ -169,17 +190,40 @@
       return 1;
     }
 
-    if (showUsage.isPresent())
+    if (argParser.usageOrVersionDisplayed())
     {
       return 0;
     }
 
 
     // Determine the path to the Java installation that should be used.
-    String javaHomeDir = System.getenv("JAVA_HOME");
-    if (javaHomeDir == null)
+    String javaHomeDir;
+    if (javaHome.isPresent())
     {
-      javaHomeDir = System.getProperty("java.home");
+      File f = new File(javaHome.getValue());
+      if (! (f.exists() && f.isDirectory()))
+      {
+        err.println(ERR_CREATERC_JAVA_HOME_DOESNT_EXIST.get(
+                         javaHome.getValue()).toString());
+        return 1;
+      }
+
+      javaHomeDir = f.getAbsolutePath();
+    }
+    else
+    {
+      javaHomeDir = System.getenv("JAVA_HOME");
+      if (javaHomeDir == null)
+      {
+        javaHomeDir = System.getProperty("java.home");
+      }
+    }
+
+
+    String suString = "";
+    if (userName.isPresent())
+    {
+      suString = "/bin/su " + userName.getValue() + " ";
     }
 
 
@@ -201,27 +245,36 @@
       w.println();
 
       w.println("# Set the path to the OpenDS instance to manage");
-      w.println("INSTANCE_ROOT=" + serverRoot.getAbsolutePath());
+      w.println("INSTANCE_ROOT=\"" + serverRoot.getAbsolutePath() + "\"");
       w.println("export INSTANCE_ROOT");
       w.println();
 
       w.println("# Specify the path to the Java installation to use");
-      w.println("JAVA_HOME=" + javaHomeDir);
+      w.println("JAVA_HOME=\"" + javaHomeDir + "\"");
       w.println("export JAVA_HOME");
       w.println();
 
+      if (javaArgs.isPresent())
+      {
+        w.println("# Specify arguments that should be provided to the JVM");
+        w.println("JAVA_ARGS=\"" + javaArgs.getValue() + "\"");
+        w.println("export JAVA_ARGS");
+        w.println();
+      }
+
       w.println("# Determine what action should be performed on the server");
       w.println("case \"${1}\" in");
       w.println("start)");
-      w.println("  ${INSTANCE_ROOT}/bin/start-ds");
+      w.println("  " + suString + "\"${INSTANCE_ROOT}/bin/start-ds\" --quiet");
       w.println("  exit ${?}");
       w.println("  ;;");
       w.println("stop)");
-      w.println("  ${INSTANCE_ROOT}/bin/stop-ds");
+      w.println("  " + suString + "\"${INSTANCE_ROOT}/bin/stop-ds\" --quiet");
       w.println("  exit ${?}");
       w.println("  ;;");
       w.println("restart)");
-      w.println("  ${INSTANCE_ROOT}/bin/stop-ds --restart");
+      w.println("  " + suString + "\"${INSTANCE_ROOT}/bin/stop-ds\" " +
+                "--restart --quiet");
       w.println("  exit ${?}");
       w.println("  ;;");
       w.println("*)");
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/StopDS.java b/opendj-sdk/opends/src/server/org/opends/server/tools/StopDS.java
index 7f034f3..687b44d 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/StopDS.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/StopDS.java
@@ -119,6 +119,15 @@
    * The server must be restarted as a window service.
    */
   private static int RESTART_AS_WINDOW_SERVICE = 104;
+  /**
+   * The server must be started and it should use quiet mode.
+   */
+  private static int START_SERVER_QUIET = 105;
+  /**
+   * The server must be restarted using system calls and it should use quiet
+   * mode.
+   */
+  private static int RESTART_USING_SYSTEM_CALL_QUIET = 106;
 
   /**
    * Invokes the <CODE>stopDS</CODE> method, passing it the provided command
@@ -199,6 +208,7 @@
     ArgumentParser    argParser = new ArgumentParser(CLASS_NAME,
                                                      toolDescription, false);
     BooleanArgument   checkStoppability;
+    BooleanArgument   quietMode;
     BooleanArgument   windowsNetStop;
     BooleanArgument   restart;
     BooleanArgument   showUsage;
@@ -377,6 +387,11 @@
                                   INFO_STOPDS_DESCRIPTION_TSPWFILE.get());
       argParser.addArgument(trustStorePWFile);
 
+      quietMode = new BooleanArgument("quiet", OPTION_SHORT_QUIET,
+                                      OPTION_LONG_QUIET,
+                                      INFO_DESCRIPTION_QUIET.get());
+      argParser.addArgument(quietMode);
+
       showUsage = new BooleanArgument("showusage", OPTION_SHORT_HELP,
                                       OPTION_LONG_HELP,
                                       INFO_STOPDS_DESCRIPTION_SHOWUSAGE.get());
@@ -414,9 +429,14 @@
       return LDAPResultCode.SUCCESS;
     }
 
+    if (quietMode.isPresent())
+    {
+      out = NullOutputStream.printStream();
+    }
+
     if (checkStoppability.isPresent())
     {
-      System.exit(checkStoppability(argParser));
+      System.exit(checkStoppability(argParser, out, err));
     }
 
     // If both a bind password and bind password file were provided, then return
@@ -785,14 +805,24 @@
    * of the server.  This basically tells the invoker what must be done based
    * on the different parameters passed.
    * @param argParser the ArgumentParser with the arguments already parsed.
+   * @param out the print stream to use for standard output.
+   * @param err the print stream to use for standard error.
    * @return the error code that we return when we are checking the stoppability
    * of the server.
    */
-  private static int checkStoppability(ArgumentParser argParser)
+  private static int checkStoppability(ArgumentParser argParser,
+                                       PrintStream out, PrintStream err)
   {
     int returnValue;
     boolean isServerRunning;
 
+    boolean quietMode = false;
+    Argument quietArg = argParser.getArgumentForLongID("quiet");
+    if ((quietArg != null) && quietArg.isPresent())
+    {
+      quietMode = true;
+    }
+
     BooleanArgument restart =
       (BooleanArgument)argParser.getArgumentForLongID("restart");
     boolean restartPresent = restart.isPresent();
@@ -806,6 +836,7 @@
     for (Argument arg: list)
     {
       if (!"restart".equals(arg.getName()) &&
+          !"quiet".equals(arg.getName()) &&
           !"showusage".equals(arg.getName()) &&
           !"checkstoppability".equals(arg.getName()) &&
           !"windowsnetstop".equals(arg.getName()))
@@ -830,7 +861,7 @@
           // The server is not running: write a message informing of that
           // in the standard out (this is not an error message).
           Message message = INFO_STOPDS_SERVER_ALREADY_STOPPED.get();
-          System.out.println(message);
+          out.println(message);
           LockFileManager.releaseLock(lockFile, failureReason);
           isServerRunning = false;
         }
@@ -864,7 +895,14 @@
         }
         else if (restartPresent)
         {
-          returnValue = START_SERVER;
+          if (quietMode)
+          {
+            returnValue = START_SERVER_QUIET;
+          }
+          else
+          {
+            returnValue = START_SERVER;
+          }
         }
         else
         {
@@ -882,7 +920,14 @@
             // batch file actually stops the server.
             if (restartPresent)
             {
-              returnValue = RESTART_USING_SYSTEM_CALL;
+              if (quietMode)
+              {
+                returnValue = RESTART_USING_SYSTEM_CALL_QUIET;
+              }
+              else
+              {
+                returnValue = RESTART_USING_SYSTEM_CALL;
+              }
             }
             else
             {
@@ -902,7 +947,7 @@
             // Display a message informing that we are going to the server.
 
             Message message = INFO_STOPDS_GOING_TO_STOP.get();
-            System.out.println(message);
+            out.println(message);
           }
         }
         else
@@ -910,11 +955,18 @@
           // Display a message informing that we are going to the server.
 
           Message message = INFO_STOPDS_GOING_TO_STOP.get();
-          System.out.println(message);
+          out.println(message);
 
           if (restartPresent)
           {
-            returnValue = RESTART_USING_SYSTEM_CALL;
+            if (quietMode)
+            {
+              returnValue = RESTART_USING_SYSTEM_CALL_QUIET;
+            }
+            else
+            {
+              returnValue = RESTART_USING_SYSTEM_CALL;
+            }
           }
           else
           {

--
Gitblit v1.10.0