From 57e2f84980341ddcbcea0baf337f276365a986be Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Tue, 27 Feb 2007 16:50:09 +0000
Subject: [PATCH] The proposed fix is to do a similar thing that what has been done by Neil for issue #1266: use a hidden option to check whether the server is already running or not. If the server is not running the message 'Server already stopped' is displayed, if the server is running the message 'Stopping Server... is displayed'.
---
opendj-sdk/opends/resource/bin/stop-ds | 67 +++++++++++++++++-----
opendj-sdk/opends/src/server/org/opends/server/tools/StopDS.java | 49 ++++++++++++++++
opendj-sdk/opends/resource/bin/stop-ds.bat | 36 ++++++++++++
opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java | 27 ++++++++
4 files changed, 161 insertions(+), 18 deletions(-)
diff --git a/opendj-sdk/opends/resource/bin/stop-ds b/opendj-sdk/opends/resource/bin/stop-ds
index bf19ebd..b343b0c 100755
--- a/opendj-sdk/opends/resource/bin/stop-ds
+++ b/opendj-sdk/opends/resource/bin/stop-ds
@@ -105,9 +105,11 @@
export SCRIPT_NAME_ARG
-# See if any arguments were provided and if a local PID file exists. If there
-# were no arguments (or only the -R argument was passed) and there is a PID
-# file, then try to stop the server with a kill command.
+# If there were no arguments (or only the -R argument was passed) we assume that
+# the server to be stopped is the local server. If this is the case we check if
+# the server is running or not.
+# If the server is running then try to stop the server with a kill command. If
+# there are more arguments, try to stop the server using LDAP protocol.
NO_ARG_OR_ONLY_RESTART=1
RESTART=0
for ARG in "${@}"
@@ -127,27 +129,60 @@
if test ${NO_ARG_OR_ONLY_RESTART} -eq 1
then
- if test -f "${INSTANCE_ROOT}/logs/server.pid"
+ # Use the code in StopDS class to know if the server is already
+ # stopped. An exit code of 99 means that the server is stopped.
+ STOPPED=0
+ "${JAVA_BIN}" ${JAVA_ARGS} ${SCRIPT_NAME_ARG} \
+ org.opends.server.tools.StopDS --checkStoppability
+ EC=${?}
+ if test ${EC} -ne 99
then
- kill `cat "${INSTANCE_ROOT}/logs/server.pid"`
- EXIT_CODE=${?}
- if test "${EXIT_CODE}" -eq 0
+ STOPPED=1
+ fi
+
+ EXIT_CODE=0
+ if test "${STOPPED}" -eq 1
+ then
+ if test -f "${INSTANCE_ROOT}/logs/server.pid"
then
- "${JAVA_BIN}" -Xms8M -Xmx8M org.opends.server.tools.WaitForFileDelete \
- --targetFile "${INSTANCE_ROOT}/logs/server.pid" \
- --logFile "${INSTANCE_ROOT}/logs/errors"
- EXIT_CODE=${?}
- if test "${RESTART}" -eq 1
- then
+ kill `cat "${INSTANCE_ROOT}/logs/server.pid"`
+ EXIT_CODE=${?}
if test "${EXIT_CODE}" -eq 0
then
- "${INSTANCE_ROOT}/bin/start-ds"
+ "${JAVA_BIN}" -Xms8M -Xmx8M \
+ org.opends.server.tools.WaitForFileDelete \
+ --targetFile "${INSTANCE_ROOT}/logs/server.pid" \
+ --logFile "${INSTANCE_ROOT}/logs/errors"
EXIT_CODE=${?}
+ if test "${EXIT_CODE}" -eq 0
+ then
+ STOPPED=0
+ fi
fi
- fi
fi
- exit ${EXIT_CODE}
fi
+
+ # Delete the pid file if the server is stopped (this can occur if the process has
+ # been killed using kill -9).
+ if test "{$STOPPED}" -eq 0
+ then
+ if test -f "${INSTANCE_ROOT}/logs/server.pid"
+ then
+ rm "${INSTANCE_ROOT}/logs/server.pid"
+ fi
+ fi
+
+ # Now if the user wants to restart the server, try to restart it if the server
+ # is stopped.
+ if test "${RESTART}" -eq 1
+ then
+ if test "${STOPPED}" -eq 0
+ then
+ "${INSTANCE_ROOT}/bin/start-ds"
+ EXIT_CODE=${?}
+ fi
+ fi
+ exit ${EXIT_CODE}
fi
diff --git a/opendj-sdk/opends/resource/bin/stop-ds.bat b/opendj-sdk/opends/resource/bin/stop-ds.bat
index 91b4f84..fb456e0 100644
--- a/opendj-sdk/opends/resource/bin/stop-ds.bat
+++ b/opendj-sdk/opends/resource/bin/stop-ds.bat
@@ -27,10 +27,37 @@
setlocal
+set PATH=%SystemRoot%
+
set OPENDS_INVOKE_CLASS="org.opends.server.tools.StopDS"
set SCRIPT_NAME_ARG="-Dorg.opends.server.scriptName=stop-ds"
set DIR_HOME=%~dP0..
+:checkJavaBin
+if "%JAVA_BIN%" == "" goto noJavaBin
+goto setClassPath
+
+:noJavaBin
+if "%JAVA_HOME%" == "" goto noJavaHome
+if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
+goto setClassPath
+
+:noJavaHome
+if not exist "%DIR_HOME%\bin\set-java-home.bat" goto noSetJavaHome
+call "%DIR_HOME%\bin\set-java-home.bat"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
+goto setClassPath
+
+:noSetJavaHome
+echo Error: JAVA_HOME environment variable is not set.
+echo Please set it to a valid Java 5 installation.
+goto end
+
+:setClassPath
+FOR %%x in ("%DIR_HOME%\lib\*.jar") DO call "%DIR_HOME%\bin\setcp.bat" %%x
+
+
set RESTART=0
set NO_ARG_OR_ONLY_RESTART=1
@@ -67,9 +94,18 @@
:execute
if "%NO_ARG_OR_ONLY_RESTART%" == "0" goto stopWithLDAP
+
+rem Use the code in StopDS class to know if the server is already
+rem stopped. An exit code of 99 means that the server is stopped.
+"%JAVA_BIN%" %JAVA_ARGS% %SCRIPT_NAME_ARG% org.opends.server.tools.StopDS --checkStoppability
+if %errorlevel% == 99 goto serverStopped
+
if not exist "%DIR_HOME%\logs\server.pid" goto stopWithLDAP
"%DIR_HOME%\lib\winlauncher.exe" stop "%DIR_HOME%"
if not %errorlevel% == 0 goto end
+
+:serverStopped
+if exist "%DIR_HOME%\logs\server.pid" erase "%DIR_HOME%\logs\server.pid"
if "%RESTART%" == "1" "%DIR_HOME%\bin\start-ds.bat"
goto end
diff --git a/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java b/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java
index da025b4..ae6eefb 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java
@@ -7567,7 +7567,6 @@
CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 794;
-
/**
* The message ID for the message that will be used if the prompt trust
* manager is asked about trusting a client certificate. This does not take
@@ -7635,6 +7634,26 @@
CATEGORY_MASK_TOOLS | SEVERITY_MASK_SEVERE_ERROR | 801;
+ /**
+ * The message ID for the message that will be used when the server is
+ * already stopped. This does not take any arguments.
+ */
+ public static final int MSGID_STOPDS_SERVER_ALREADY_STOPPED =
+ CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 802;
+
+ /**
+ * The message ID for the message that will be used when the server is
+ * going to stopped. This does not take any arguments.
+ */
+ public static final int MSGID_STOPDS_GOING_TO_STOP =
+ CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 803;
+
+ /**
+ * The message ID for the message that will be used as the description for the
+ * checkStoppability command-line argument. This does not take any arguments.
+ */
+ public static final int MSGID_STOPDS_CHECK_STOPPABILITY =
+ CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 804;
/**
* Associates a set of generic messages with the message IDs defined in this
@@ -8846,6 +8865,8 @@
registerMessage(MSGID_STOPDS_DESCRIPTION_RESTART,
"Attempt to automatically restart the server once it has " +
"stopped");
+ registerMessage(MSGID_STOPDS_CHECK_STOPPABILITY,
+ "Used to determine whether the server is stopped or not.");
registerMessage(MSGID_STOPDS_DESCRIPTION_STOP_TIME,
"Time to begin the shutdown in YYYYMMDDhhmmss format " +
"(local time)");
@@ -8907,6 +8928,10 @@
registerMessage(MSGID_STOPDS_INVALID_RESPONSE_TYPE,
"ERROR: Expected an add response message but got a %s " +
"message instead.");
+ registerMessage(MSGID_STOPDS_SERVER_ALREADY_STOPPED,
+ "Server already stopped.");
+ registerMessage(MSGID_STOPDS_GOING_TO_STOP,
+ "Stopping Server...\n");
registerMessage(MSGID_LDIFSEARCH_DESCRIPTION_LDIF_FILE,
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 7b92c0e..6c1bc93 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
@@ -40,6 +40,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import org.opends.server.controls.ProxiedAuthV2Control;
+import org.opends.server.core.LockFileManager;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1Exception;
import org.opends.server.protocols.asn1.ASN1OctetString;
@@ -163,6 +164,7 @@
String toolDescription = getMessage(MSGID_STOPDS_TOOL_DESCRIPTION);
ArgumentParser argParser = new ArgumentParser(CLASS_NAME,
toolDescription, false);
+ BooleanArgument checkStoppability;
BooleanArgument restart;
BooleanArgument showUsage;
BooleanArgument trustAll;
@@ -236,6 +238,12 @@
MSGID_STOPDS_DESCRIPTION_STOP_REASON);
argParser.addArgument(stopReason);
+ checkStoppability = new BooleanArgument("checkstoppability", null,
+ "checkStoppability",
+ MSGID_STOPDS_CHECK_STOPPABILITY);
+ checkStoppability.setHidden(true);
+ argParser.addArgument(checkStoppability);
+
restart = new BooleanArgument("restart", 'R', "restart",
MSGID_STOPDS_DESCRIPTION_RESTART);
argParser.addArgument(restart);
@@ -324,6 +332,46 @@
return LDAPResultCode.SUCCESS;
}
+ if (checkStoppability.isPresent())
+ {
+ // This option should only be used if we want to check if the local
+ // server is running or not. If the server is running result code is 98.
+ // If the server is stopped the return code is 99.
+ String lockFile = LockFileManager.getServerLockFileName();
+ try
+ {
+ StringBuilder failureReason = new StringBuilder();
+ if (LockFileManager.acquireExclusiveLock(lockFile, failureReason))
+ {
+ // The server is not running: write a message informing of that
+ // in the standard out (this is not an error message).
+ int msgID = MSGID_STOPDS_SERVER_ALREADY_STOPPED;
+ String message = getMessage(msgID, null, null);
+ System.out.println(message);
+ LockFileManager.releaseLock(lockFile, failureReason);
+ System.exit(99);
+ }
+ else
+ {
+ // Display a message informing that we are going to the server.
+ int msgID = MSGID_STOPDS_GOING_TO_STOP;
+ String message = getMessage(msgID, null, null);
+ System.out.println(message);
+ // The server is running.
+ System.exit(98);
+ }
+ }
+ catch (Exception e)
+ {
+ // Display a message informing that we are going to the server.
+ int msgID = MSGID_STOPDS_GOING_TO_STOP;
+ String message = getMessage(msgID, null, null);
+ System.out.println(message);
+ // Assume that if we cannot acquire the lock file the server is
+ // running.
+ System.exit(98);
+ }
+ }
// If both a bind password and bind password file were provided, then return
// an error.
@@ -480,7 +528,6 @@
else
{
String name = s.substring(0, equalPos);
- String value = s.substring(equalPos+1);
if (name.equalsIgnoreCase("mech"))
{
--
Gitblit v1.10.0