/*
* 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 legal-notices/CDDLv1_0.txt
* 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 legal-notices/CDDLv1_0.txt.
* 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
*
*
* Copyright 2006-2010 Sun Microsystems, Inc.
* Portions Copyright 2011-2015 ForgeRock AS.
*/
/**
* This class provides a tool that can send a request to the Directory Server
* that will cause it to shut down.
*/
public class StopDS
{
/** The fully-qualified name of this class. */
/**
* Return codes used when the hidden option --checkStoppability is used.
* NOTE: when checkStoppability is specified is recommended not to allocate
* a lot of memory for the JVM (Using -Xms and -Xmx options) as there might
* be calls to Runtime.exec.
*/
/** The server is already stopped. */
/** The server must be started. */
/** The server must be stopped using a system call. */
/** The server must be restarted using system calls. */
/** The server must be stopped using protocol. */
/** The server must be stopped as a window service. */
/** The server must be restarted as a window service. */
/** The server must be started and it should use quiet mode. */
/** The server must be restarted using system calls and it should use quiet mode. */
/**
* Invokes the <CODE>stopDS</CODE> method, passing it the provided command
* line arguments. If the call to <CODE>stopDS</CODE> returns a nonzero
* value, then that will be used as the exit code for this program.
*
* @param args The command-line arguments provided to this program.
*/
{
{
}
}
/**
* Parses the provided set of command-line arguments and attempts to contact
* the Directory Server in order to send it the shutdown request.
*
* @param args The command-line arguments provided to this program.
*
* @return An integer value that indicates whether the shutdown request was
* accepted by the Directory Server. A nonzero value should be
* interpreted as a failure of some kind.
*/
{
}
/**
* Parses the provided set of command-line arguments and attempts to contact
* the Directory Server in order to send it the shutdown request.
*
* @param args The command-line arguments provided to this program.
* @param outStream The output stream to use for standard output, or
* <CODE>null</CODE> if standard output is not needed.
* @param errStream The output stream to use for standard error, or
* <CODE>null</CODE> if standard error is not needed.
*
* @return An integer value that indicates whether the shutdown request was
* accepted by the Directory Server. A nonzero value should be
* interpreted as a failure of some kind.
*/
{
// Define all the arguments that may be used with this program.
toolDescription, false);
try
{
OPTION_LONG_HOST, false, false, true,
port = new IntegerArgument(
"port", OPTION_SHORT_PORT,
OPTION_LONG_PORT, false, false, true,
null, true, 1,
OPTION_LONG_BINDDN, false, false, true,
OPTION_LONG_BINDPWD, false, false,
true,
bindPWFile = new FileBasedArgument(
"bindpwfile",
false, false,
saslOption = new StringArgument(
"sasloption", OPTION_SHORT_SASLOPTION,
OPTION_LONG_SASLOPTION, false,
true, true,
proxyAuthzID = new StringArgument(
"proxyauthzid",
OPTION_LONG_PROXYAUTHID, false,
false, true,
null,
stopReason = new StringArgument(
"stopreason", 'r', "stopReason", false,
"checkStoppability",
checkStoppability.setHidden(true);
windowsNetStop.setHidden(true);
false, true,
null,
false, false, true,
false, false, true,
keyStorePWFile = new FileBasedArgument(
"keystorepwfile",
false, false,
certNickname = new StringArgument(
"certnickname", 'N', "certNickname",
false, false, true,
trustStorePW = new StringArgument(
"truststorepw", 'T',
false, false,
false, false,
}
catch (ArgumentException ae)
{
return CLIENT_SIDE_PARAM_ERROR;
}
// Parse the command-line arguments provided to the program.
try
{
}
catch (ArgumentException ae)
{
return CLIENT_SIDE_PARAM_ERROR;
}
// If we should just display usage or version information,
// then exit because it will have already been done.
if (argParser.usageOrVersionDisplayed())
{
return LDAPResultCode.SUCCESS;
}
{
}
if (checkStoppability.isPresent())
{
}
// If both a bind password and bind password file were provided, then return
// an error.
{
ERR_STOPDS_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(bindPW.getLongIdentifier(), bindPWFile.getLongIdentifier()));
return CLIENT_SIDE_PARAM_ERROR;
}
// If both a key store password and key store password file were provided,
// then return an error.
{
return CLIENT_SIDE_PARAM_ERROR;
}
// If both a trust store password and trust store password file were
// provided, then return an error.
{
return CLIENT_SIDE_PARAM_ERROR;
}
// Make sure that we can decode the stop time string if one was provided.
if (stopTimeStr.isPresent())
{
{
try
{
}
catch (Exception e)
{
return CLIENT_SIDE_PARAM_ERROR;
}
// Check that the provided date is not previous to the current date.
{
return CLIENT_SIDE_PARAM_ERROR;
}
}
}
// Create the LDAP connection options object, which will be used to
// customize the way that we connect to the server and specify a set of
// basic defaults.
try {
if (certNickname.isPresent()) {
} else {
clientAlias = null;
}
trustStorePW.getValue());
} catch (SSLConnectionException sce) {
return CLIENT_SIDE_LOCAL_ERROR;
}
// If one or more SASL options were provided, then make sure that one of
// them was "mech" and specified a valid SASL mechanism.
if (saslOption.isPresent())
{
{
if (equalPos <= 0)
{
return CLIENT_SIDE_PARAM_ERROR;
}
else
{
{
mechanism = s;
}
else
{
}
}
}
{
return CLIENT_SIDE_PARAM_ERROR;
}
{
}
}
// Attempt to connect and authenticate to the Directory Server.
try
{
}
catch (ArgumentException ae)
{
return CLIENT_SIDE_PARAM_ERROR;
}
catch (LDAPConnectionException lce)
{
} else {
lce.getMessage());
}
return CLIENT_SIDE_CONNECT_ERROR;
}
// Construct the add request to send to the server.
attributes.add(new LDAPAttribute(ATTR_OBJECTCLASS, newArrayList("top", "ds-task", "ds-task-shutdown")));
{
}
if (stopReason.isPresent())
{
}
{
}
if (proxyAuthzID.isPresent())
{
}
// Send the request to the server and read the response.
try
{
if (responseMessage == null)
{
return CLIENT_SIDE_SERVER_DOWN;
}
}
catch (DecodeException | LDAPException e)
{
return CLIENT_SIDE_DECODING_ERROR;
}
catch (IOException ioe)
{
}
if (responseMessage.getProtocolOpType() !=
{
if (responseMessage.getProtocolOpType() ==
{
// It's possible that this is a notice of disconnection, which we can
// probably interpret as a "success" in this case.
{
return extendedResponse.getResultCode();
}
}
return CLIENT_SIDE_LOCAL_ERROR;
}
return addResponse.getResultCode();
}
/**
* Returns the error code that we return when we are checking the stoppability
* 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.
*/
{
int returnValue;
boolean isServerRunning;
boolean quietMode = false;
{
quietMode = true;
}
// Check if this is a stop through protocol.
boolean stopThroughProtocol = false;
{
{
}
}
if (stopThroughProtocol)
{
// Assume that this is done on a remote server and do no more checks.
}
else
{
try
{
{
// The server is not running: write a message informing of that
// in the standard out (this is not an error message).
isServerRunning = false;
}
else
{
isServerRunning = true;
}
}
catch (Exception e)
{
// Assume that if we cannot acquire the lock file the server is
// running.
isServerRunning = true;
}
boolean configuredAsService =
if (!isServerRunning)
{
if (configuredAsService && !windowsNetStopPresent)
{
if (restartPresent)
{
}
else
{
}
}
else if (restartPresent)
{
if (quietMode)
{
}
else
{
}
}
else
{
}
}
else
{
if (configuredAsService)
{
{
// stop-ds.bat is being called through net stop, so return
// STOP_USING_SYSTEM_CALL or RESTART_USING_SYSTEM_CALL so that the
// batch file actually stops the server.
if (restartPresent)
{
if (quietMode)
{
}
else
{
}
}
else
{
}
}
else
{
if (restartPresent)
{
}
else
{
}
// Display a message informing that we are going to the server.
}
}
else
{
// Display a message informing that we are going to the server.
if (restartPresent)
{
if (quietMode)
{
}
else
{
}
}
else
{
}
}
}
}
return returnValue;
}
}