ConfigureWindowsService.java revision 82d394e493e7a22528477b2aa47467140a0d5929
/*
* 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 2008-2010 Sun Microsystems, Inc.
* Portions Copyright 2011-2015 ForgeRock AS.
*/
/**
* This class is used to configure 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.
*
* Some comments about Vista:
* In Vista, when we launch the subcommands that require administrator
* privileges (enable, disable and cleanup) we cannot use the administrator
* launcher binary directly from Java (see
* winlauncher.exe.
* When we launch subcommands that required administrator privileges
* we must launch a binary containing the manifest that specifies that we
* require administrator privileges (requireAdministrator value): if UAC is
* enabled, the user will be asked for confirmation.
* To minimize the number of confirmation that the user must provide when
* launching the state subcommand we will use a binary whose manifest does
* not contain the requireAdministrator value.
*
* See the files under src/build-tools/windows for more details.
*/
public class ConfigureWindowsService
{
/** The fully-qualified name of this class. */
/** Option to be used when calling the launchers. */
private static final int SUCCESS = 0;
private static final int ERROR = 1;
/** Return codes for the method enableService. */
/** The service was successfully enabled. */
public static final int SERVICE_ENABLE_SUCCESS = 0;
/** The service was already enabled. */
public static final int SERVICE_ALREADY_ENABLED = 1;
/** The service name was already in use. */
public static final int SERVICE_NAME_ALREADY_IN_USE = 2;
/** 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. */
public static final int SERVICE_DISABLE_SUCCESS = 0;
/** The service was already disabled. */
public static final int SERVICE_ALREADY_DISABLED = 1;
/** The service is marked for deletion. */
public static final int SERVICE_MARKED_FOR_DELETION = 2;
/** An error occurred disabling the service. */
public static final int SERVICE_DISABLE_ERROR = 3;
/** Return codes for the method serviceState. */
/** The service is enabled. */
public static final int SERVICE_STATE_ENABLED = 0;
/** The service is disabled. */
public static final int SERVICE_STATE_DISABLED = 1;
/** 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. */
public static final int SERVICE_CLEANUP_SUCCESS = 0;
/** The service could not be found. */
public static final int SERVICE_NOT_FOUND = 1;
/** An error occurred cleaning up the service. */
public static final int SERVICE_CLEANUP_ERROR = 2;
/** 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.
*
* @param args
* The command-line arguments provided to this program.
*/
{
}
/**
* 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.
* @return the integer code describing if the operation could be completed or
* not.
*/
public static int configureWindowsService(String[] args, OutputStream outStream, OutputStream errStream)
{
// Define all the arguments that may be used with this program.
try
{
}
catch (ArgumentException ae)
{
return ERROR;
}
try
{
}
catch (ArgumentException ae)
{
return ERROR;
}
// If we should just display usage or version information,
// then it is already done
if (!argParser.usageOrVersionDisplayed())
{
/* Check that the user only asked for one argument */
int nArgs = 0;
if (enableService.isPresent())
{
nArgs++;
}
if (disableService.isPresent())
{
nArgs++;
}
if (serviceState.isPresent())
{
nArgs++;
}
if (cleanupService.isPresent())
{
nArgs++;
}
if (nArgs != 1)
{
return ERROR;
}
}
if (argParser.usageOrVersionDisplayed())
{
return SUCCESS;
}
if (enableService.isPresent())
{
}
else if (disableService.isPresent())
{
}
else if (serviceState.isPresent())
{
}
}
/**
* 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.
*/
static String getServiceName()
{
try
{
boolean processDone = false;
String s;
while (!processDone)
{
try
{
p.exitValue();
processDone = true;
}
catch (Throwable t)
{
}
{
serviceName = s;
{
serviceName = null;
}
}
}
return serviceName;
}
catch (Throwable t)
{
return null;
}
}
/**
* 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.
* @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.
*/
{
"INFO_WINDOWS_SERVICE_NAME",
LocalizableMessage.class);
"INFO_WINDOWS_SERVICE_DESCRIPTION",
}
/**
* 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.
* @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.
*/
public static int enableService(PrintStream out, PrintStream err, String serviceName, String serviceDescription)
{
if (hasUAC())
{
"create",
};
}
else
{
"create",
};
}
try
{
switch (resultCode)
{
case 0:
if (isServerRunning)
{
// We have to launch the windows service. The service code already
// handles this case (the service binary is executed when the server
// already runs).
if (returnValue == 0)
{
return SERVICE_ENABLE_SUCCESS;
}
else
{
return SERVICE_ENABLE_ERROR;
}
}
else
{
return SERVICE_ENABLE_SUCCESS;
}
case 1:
return SERVICE_ALREADY_ENABLED;
case 2:
return SERVICE_NAME_ALREADY_IN_USE;
case 3:
return SERVICE_ENABLE_ERROR;
default:
return SERVICE_ENABLE_ERROR;
}
}
catch (Throwable t)
{
t.printStackTrace();
return SERVICE_ENABLE_ERROR;
}
}
/**
* 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.
* @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.
*/
{
if (hasUAC())
{
"remove",
};
}
else
{
"remove",
};
}
try
{
switch (resultCode)
{
case 0:
return SERVICE_DISABLE_SUCCESS;
case 1:
return SERVICE_ALREADY_DISABLED;
case 2:
return SERVICE_MARKED_FOR_DELETION;
case 3:
return SERVICE_DISABLE_ERROR;
default:
return SERVICE_DISABLE_ERROR;
}
}
catch (Throwable t)
{
t.printStackTrace();
return SERVICE_DISABLE_ERROR;
}
}
/**
* 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.
* @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.
*/
{
if (hasUAC())
{
"cleanup",
};
}
else
{
"cleanup",
};
}
try
{
switch (resultCode)
{
case 0:
return SERVICE_CLEANUP_SUCCESS;
case 1:
return SERVICE_NOT_FOUND;
case 2:
case 3:
return SERVICE_CLEANUP_ERROR;
default:
return SERVICE_CLEANUP_ERROR;
}
}
catch (Throwable t)
{
return SERVICE_CLEANUP_ERROR;
}
}
/**
* 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()
{
}
/**
* 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.
*/
{
"state",
};
try
{
int resultCode = -1;
boolean processDone = false;
String s;
while (!processDone)
{
try
{
processDone = true;
}
catch (Throwable t)
{
}
{
{
serviceName = s;
}
}
}
switch (resultCode)
{
case 0:
return SERVICE_STATE_ENABLED;
case 1:
return SERVICE_STATE_DISABLED;
case 2:
return SERVICE_STATE_ERROR;
default:
return SERVICE_STATE_ERROR;
}
}
catch (Throwable t)
{
return SERVICE_STATE_ERROR;
}
}
/**
* Returns the Directory Server installation path in a user friendly
* representation.
*
* @return the Directory Server installation path in a user friendly
* representation.
*/
private static String getServerRoot()
{
try
{
/*
* Do a best effort to avoid having a relative representation (for
* instance to avoid having ../../../).
*/
f = f.getCanonicalFile();
}
catch (IOException ioe)
{
/* This is a best effort to get the best possible representation of the
* file: reporting the error is not necessary.
*/
}
serverRoot = f.toString();
{
}
return serverRoot;
}
/**
* Returns the full path of the executable used by this class to perform
* 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.
*/
private static String getBinaryFullPath()
{
}
/**
* 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.
*
* @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.
*/
public static String getLauncherAdministratorBinaryFullPath()
{
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.
*
* @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.
*/
public static String getLauncherBinaryFullPath()
{
return getServerRoot() + "\\lib\\winlauncher.exe";
}
}