From 4700a31d399d3e3fe5594cce350744ea71ede5ee Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Fri, 24 Aug 2007 23:00:23 +0000
Subject: [PATCH] Add a mechanism for generating an RC script that can be used on UNIX systems to configure the server to automatically start when the system boots.

---
 opends/resource/bin/create-rc-script                          |   37 +++++
 opends/resource/bin/stop-ds                                   |    4 
 opends/src/messages/messages/tools.properties                 |   59 +++++---
 opends/src/server/org/opends/server/tools/CreateRCScript.java |  253 ++++++++++++++++++++++++++++++++++++
 opends/src/server/org/opends/server/util/ServerConstants.java |   31 ++++
 5 files changed, 360 insertions(+), 24 deletions(-)

diff --git a/opends/resource/bin/create-rc-script b/opends/resource/bin/create-rc-script
new file mode 100644
index 0000000..b958bab
--- /dev/null
+++ b/opends/resource/bin/create-rc-script
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License, Version 1.0 only
+# (the "License").  You may not use this file except in compliance
+# with the License.
+#
+# You can obtain a copy of the license at
+# trunk/opends/resource/legal-notices/OpenDS.LICENSE
+# or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at
+# trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+# add the following below this CDDL HEADER, with the fields enclosed
+# by brackets "[]" replaced with your own identifying information:
+#      Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#
+#      Portions Copyright 2007 Sun Microsystems, Inc.
+
+
+# This script may be used to display the status panel.
+OPENDS_INVOKE_CLASS="org.opends.server.tools.CreateRCScript"
+export OPENDS_INVOKE_CLASS
+
+SCRIPT_NAME_ARG="-Dorg.opends.server.scriptName=create-rc-script"
+export SCRIPT_NAME_ARG
+
+SCRIPT_DIR=`dirname "${0}"`
+"${SCRIPT_DIR}/../lib/_client-script.sh" "${@}"
diff --git a/opends/resource/bin/stop-ds b/opends/resource/bin/stop-ds
index b424df1..3a8aab4 100755
--- a/opends/resource/bin/stop-ds
+++ b/opends/resource/bin/stop-ds
@@ -189,6 +189,10 @@
         STOPPED=0
       fi
     fi
+  else
+    echo "ERROR:  Unable to find the server.pid file to determine the process"
+    echo "        ID of the OpenDS process to terminate."
+    exit 1
   fi
 fi
 
diff --git a/opends/src/messages/messages/tools.properties b/opends/src/messages/messages/tools.properties
index fc7ff36..62d63aa 100644
--- a/opends/src/messages/messages/tools.properties
+++ b/opends/src/messages/messages/tools.properties
@@ -2164,23 +2164,23 @@
  initialization:  %s
 SEVERE_ERR_LDAP_CONN_CANNOT_PARSE_SASL_OPTION_1296=ERROR:  The provided SASL \
  option string "%s" could not be parsed in the form "name=value"
-SEVERE_ERR_LDAP_CONN_NO_SASL_MECHANISM_1297=ERROR:  One or more SASL options were \
- provided, but none of them were the "mech" option to specify which SASL \
+SEVERE_ERR_LDAP_CONN_NO_SASL_MECHANISM_1297=ERROR:  One or more SASL options \
+ were provided, but none of them were the "mech" option to specify which SASL \
  mechanism should be used
-SEVERE_ERR_LDAP_CONN_CANNOT_DETERMINE_PORT_1298=ERROR:  Cannot parse the value of \
- the %s argument as an integer value between 1 and 65535:  %s
-SEVERE_ERR_LDAP_CONN_CANNOT_CONNECT_1299=ERROR:  Cannot establish a connection to \
- the Directory Server:  %s
+SEVERE_ERR_LDAP_CONN_CANNOT_DETERMINE_PORT_1298=ERROR:  Cannot parse the value \
+ of the %s argument as an integer value between 1 and 65535:  %s
+SEVERE_ERR_LDAP_CONN_CANNOT_CONNECT_1299=ERROR:  Cannot establish a connection \
+ to the Directory Server:  %s
 INFO_LDAP_CONN_DESCRIPTION_HOST_1300=Directory server hostname or IP address
 INFO_LDAP_CONN_DESCRIPTION_PORT_1301=Directory server port number
-INFO_LDAP_CONN_DESCRIPTION_USESSL_1302=Use SSL for secure communication with the \
- server
-INFO_LDAP_CONN_DESCRIPTION_USESTARTTLS_1303=Use StartTLS for secure communication \
- with the server
+INFO_LDAP_CONN_DESCRIPTION_USESSL_1302=Use SSL for secure communication with \
+ the server
+INFO_LDAP_CONN_DESCRIPTION_USESTARTTLS_1303=Use StartTLS for secure \
+ communication with the server
 INFO_LDAP_CONN_DESCRIPTION_BINDDN_1304=Specifies the DN to use to bind to the \
  server
-INFO_LDAP_CONN_DESCRIPTION_BINDPW_1305=Specifies the password to use to bind to \
- the server
+INFO_LDAP_CONN_DESCRIPTION_BINDPW_1305=Specifies the password to use to bind \
+ to the server
 INFO_LDAP_CONN_DESCRIPTION_BINDPWFILE_1306=Bind password file
 INFO_LDAP_CONN_DESCRIPTION_SASLOPTIONS_1307=SASL bind options
 INFO_LDAP_CONN_DESCRIPTION_TRUST_ALL_1308=Trust all server SSL certificates
@@ -2190,17 +2190,28 @@
 INFO_LDAP_CONN_DESCRIPTION_TSFILE_1312=Certificate trust store path
 INFO_LDAP_CONN_DESCRIPTION_TSPW_1313=Certificate trust store PIN
 INFO_LDAP_CONN_DESCRIPTION_TSPWFILE_1314=Certificate trust store PIN file
-SEVERE_ERR_TASK_CLIENT_UNEXPECTED_CONNECTION_CLOSURE_1315=NOTICE:  The connection \
- to the Directory Server was closed while waiting for a response to the \
- shutdown request.  This likely means that the server has started the shutdown \
- process
-SEVERE_ERR_TASK_CLIENT_IO_ERROR_1316=ERROR:  An I/O error occurred while attempting \
- to communicate with the Directory Server:  %s
-SEVERE_ERR_TASK_CLIENT_DECODE_ERROR_1317=ERROR:  An error occurred while trying to \
- decode the response from the server:  %s
-SEVERE_ERR_TASK_CLIENT_INVALID_RESPONSE_TYPE_1318=ERROR:  Expected an add response \
- message but got a %s message instead
+SEVERE_ERR_TASK_CLIENT_UNEXPECTED_CONNECTION_CLOSURE_1315=NOTICE:  The \
+ connection to the Directory Server was closed while waiting for a response to \
+ the shutdown request.  This likely means that the server has started the \
+ shutdown process
+SEVERE_ERR_TASK_CLIENT_IO_ERROR_1316=ERROR:  An I/O error occurred while \
+ attempting to communicate with the Directory Server:  %s
+SEVERE_ERR_TASK_CLIENT_DECODE_ERROR_1317=ERROR:  An error occurred while \
+ trying to decode the response from the server:  %s
+SEVERE_ERR_TASK_CLIENT_INVALID_RESPONSE_TYPE_1318=ERROR:  Expected an add \
+ response message but got a %s message instead
 INFO_TASK_CLIENT_TASK_SCHEDULED_1319=Scheduled task %s
-SEVERE_ERR_LDAP_CONN_INCOMPATIBLE_ARGS_1320=ERROR:  argument %s is incompatible \
- with use of this tool to interact with the directory as a client
+SEVERE_ERR_LDAP_CONN_INCOMPATIBLE_ARGS_1320=ERROR:  argument %s is \
+ incompatible with use of this tool to interact with the directory as a client
+SEVERE_ERR_CREATERC_ONLY_RUNS_ON_UNIX_1321=This tool may only be used on \
+ UNIX-based systems
+INFO_CREATERC_TOOL_DESCRIPTION_1322=Create an RC script that may be used to \
+ start, stop, and restart the Directory Server on UNIX-based systems
+INFO_CREATERC_OUTFILE_DESCRIPTION_1323=The path to the output file to create
+SEVERE_ERR_CREATERC_UNABLE_TO_DETERMINE_SERVER_ROOT_1324=Unable to determine \
+ the path to the server root directory.  Please ensure that the %s system \
+ property or the %s environment variable is set to the path of the server \
+ root directory
+SEVERE_ERR_CREATERC_CANNOT_WRITE_1325=An error occurred while attempting to \
+ generate the RC script:  %s
 
diff --git a/opends/src/server/org/opends/server/tools/CreateRCScript.java b/opends/src/server/org/opends/server/tools/CreateRCScript.java
new file mode 100644
index 0000000..afb0f1d
--- /dev/null
+++ b/opends/src/server/org/opends/server/tools/CreateRCScript.java
@@ -0,0 +1,253 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Portions Copyright 2007 Sun Microsystems, Inc.
+ */
+package org.opends.server.tools;
+
+
+
+import java.io.File;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+
+import org.opends.messages.Message;
+import org.opends.server.core.DirectoryServer;
+import org.opends.server.types.FilePermission;
+import org.opends.server.types.NullOutputStream;
+import org.opends.server.types.OperatingSystem;
+import org.opends.server.util.EmbeddedUtils;
+import org.opends.server.util.args.ArgumentParser;
+import org.opends.server.util.args.ArgumentException;
+import org.opends.server.util.args.BooleanArgument;
+import org.opends.server.util.args.StringArgument;
+
+import static org.opends.messages.ToolMessages.*;
+import static org.opends.server.config.ConfigConstants.*;
+import static org.opends.server.util.ServerConstants.*;
+import static org.opends.server.util.StaticUtils.*;
+
+
+
+/**
+ * This program provides a tool that may be used to generate an RC script that
+ * can be used to start, stop, and restart the Directory Server, as well as to
+ * display its current status.  It is only intended for use on UNIX-based
+ * systems that support the use of RC scripts in a location like /etc/init.d.
+ */
+public class CreateRCScript
+{
+  /**
+   * Parse the command line arguments and create an RC script that can be used
+   * to control the server.
+   *
+   * @param  args  The command-line arguments provided to this program.
+   */
+  public static void main(String[] args)
+  {
+    int exitCode = main(args, System.out, System.err);
+    if (exitCode != 0)
+    {
+      System.exit(exitCode);
+    }
+  }
+
+
+
+  /**
+   * Parse the command line arguments and create an RC script that can be used
+   * to control the server.
+   *
+   * @param  args  The command-line arguments provided to this program.
+   * @param  outStream  The output stream to which standard output should be
+   *                    directed, or {@code null} if standard output should be
+   *                    suppressed.
+   * @param  errStream  The output stream to which standard error should be
+   *                    directed, or {@code null} if standard error should be
+   *                    suppressed.
+   *
+   * @return  Zero if all processing completed successfully, or nonzero if an
+   *          error occurred.
+   */
+  public static int main(String[] args, OutputStream outStream,
+                         OutputStream errStream)
+  {
+    PrintStream out;
+    if (outStream == null)
+    {
+      out = NullOutputStream.printStream();
+    }
+    else
+    {
+      out = new PrintStream(outStream);
+    }
+
+    PrintStream err;
+    if (errStream == null)
+    {
+      err = NullOutputStream.printStream();
+    }
+    else
+    {
+      err = new PrintStream(errStream);
+    }
+
+
+    EmbeddedUtils.initializeForClientUse();
+
+    OperatingSystem operatingSystem = DirectoryServer.getOperatingSystem();
+    if (! OperatingSystem.isUNIXBased(operatingSystem))
+    {
+      err.println(ERR_CREATERC_ONLY_RUNS_ON_UNIX.get().toString());
+      return 1;
+    }
+
+    File serverRoot = DirectoryServer.getEnvironmentConfig().getServerRoot();
+    if (serverRoot == null)
+    {
+      err.println(ERR_CREATERC_UNABLE_TO_DETERMINE_SERVER_ROOT.get(
+                       PROPERTY_SERVER_ROOT, ENV_VAR_INSTANCE_ROOT).toString());
+      return 1;
+    }
+
+
+    Message description = INFO_CREATERC_TOOL_DESCRIPTION.get();
+    ArgumentParser argParser =
+         new ArgumentParser(CreateRCScript.class.getName(), description, false);
+
+    BooleanArgument showUsage  = null;
+    StringArgument  outputFile = null;
+
+    try
+    {
+      outputFile = new StringArgument("outputfile", 'f', "outputFile", true,
+                                      false, true, "{path}", null, null,
+                                      INFO_CREATERC_OUTFILE_DESCRIPTION.get());
+      argParser.addArgument(outputFile);
+
+
+      showUsage = new BooleanArgument("help", 'H', "help",
+                                      INFO_DESCRIPTION_SHOWUSAGE.get());
+      argParser.addArgument(showUsage);
+      argParser.setUsageArgument(showUsage);
+    }
+    catch (ArgumentException ae)
+    {
+      err.println(ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage()));
+      return 1;
+    }
+
+    try
+    {
+      argParser.parseArguments(args);
+    }
+    catch (ArgumentException ae)
+    {
+      err.println(ERR_ERROR_PARSING_ARGS.get(ae.getMessage()).toString());
+      return 1;
+    }
+
+    if (showUsage.isPresent())
+    {
+      return 0;
+    }
+
+
+    // Determine the path to the Java installation that should be used.
+    String javaHomeDir = System.getenv("JAVA_HOME");
+    if (javaHomeDir == null)
+    {
+      javaHomeDir = System.getProperty("java.home");
+    }
+
+
+    // Start writing the output file.
+    try
+    {
+      File f = new File(outputFile.getValue());
+      PrintWriter w = new PrintWriter(f);
+
+      w.println("#!/bin/sh");
+      w.println("#");
+
+      for (String headerLine : CDDL_HEADER_LINES)
+      {
+        w.println("# " + headerLine);
+      }
+
+      w.println();
+      w.println();
+
+      w.println("# Set the path to the OpenDS instance to manage");
+      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("export JAVA_HOME");
+      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("  exit ${?}");
+      w.println("  ;;");
+      w.println("stop)");
+      w.println("  ${INSTANCE_ROOT}/bin/stop-ds");
+      w.println("  exit ${?}");
+      w.println("  ;;");
+      w.println("restart)");
+      w.println("  ${INSTANCE_ROOT}/bin/stop-ds --restart");
+      w.println("  exit ${?}");
+      w.println("  ;;");
+      w.println("*)");
+      w.println("  echo \"Usage:  $0 { start | stop | restart }\"");
+      w.println("  exit 1");
+      w.println("  ;;");
+      w.println("esac");
+      w.println();
+
+      w.close();
+
+      if (FilePermission.canSetPermissions())
+      {
+        FilePermission.setPermissions(f, FilePermission.decodeUNIXMode("755"));
+      }
+    }
+    catch (Exception e)
+    {
+      err.println(ERR_CREATERC_CANNOT_WRITE.get(
+                       getExceptionMessage(e)).toString());
+      return 1;
+    }
+
+
+    // If we've gotten here, then everything has completed successfully.
+    return 0;
+  }
+}
+
diff --git a/opends/src/server/org/opends/server/util/ServerConstants.java b/opends/src/server/org/opends/server/util/ServerConstants.java
index 0e2a5ed..b1cab30 100644
--- a/opends/src/server/org/opends/server/util/ServerConstants.java
+++ b/opends/src/server/org/opends/server/util/ServerConstants.java
@@ -2660,5 +2660,36 @@
    */
   public static final String ALERT_TYPE_REPLICATION_UNRESOLVED_CONFLICT =
           "org.opends.server.replication.UnresolvedConflict";
+
+
+
+  /**
+   * The lines that make up the CDDL header.  They will not have any prefix, so
+   * an appropriate prefix may need to be added for some cases (e.g., "# " for
+   * shell scripts, "rem " for batch files, etc.).
+   */
+  public static final String[] CDDL_HEADER_LINES =
+  {
+    "CDDL HEADER START",
+    "",
+    "The contents of this file are subject to the terms of the",
+    "Common Development and Distribution License, Version 1.0 only",
+    "(the \"License\").  You may not use this file except in compliance",
+    "with the License.",
+    "",
+    "You can obtain a copy of the license at",
+    "https://OpenDS.dev.java.net/OpenDS.LICENSE.",
+    "See the License for the specific language governing permissions",
+    "and limitations under the License.",
+    "",
+    "When distributing Covered Code, include this CDDL HEADER in each",
+    "file and include the License file at",
+    "trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,",
+    "add the following below this CDDL HEADER, with the fields enclosed",
+    "by brackets \"[]\" replaced with your own identifying information:",
+    "     Portions Copyright [yyyy] [name of copyright owner]",
+    "",
+    "CDDL HEADER END"
+  };
 }
 

--
Gitblit v1.10.0