ServerController.java revision 2f6d798e90520dd1b83ac30e53838ae6fd41a150
/*
* 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
*/
/**
* Class used to manipulate an OpenDS server.
*/
public class ServerController {
private Application application;
private Installation installation;
/**
* Creates a new instance that will operate on <code>application</code>'s
* installation.
* @param application to use for notifications
*/
}
/**
* Creates a new instance that will operate on <code>application</code>'s
* installation.
* @param installation representing the server instance to control
*/
this(null, installation);
}
/**
* Creates a new instance that will operate on <code>installation</code>
* and use <code>application</code> for notifications.
* @param application to use for notifications
* @param installation representing the server instance to control
*/
if (installation == null) {
throw new NullPointerException("installation cannot be null");
}
this.application = application;
this.installation = installation;
}
/**
* This methods stops the server.
*
* @throws org.opends.quicksetup.ApplicationException if something goes wrong.
*/
public void stopServer() throws ApplicationException {
stopServer(false);
}
/**
* This methods stops the server.
*
* @param suppressOutput boolean indicating that ouput to standard output
* streams from the server should be suppressed.
* @throws org.opends.quicksetup.ApplicationException
* if something goes wrong.
*/
stopServer(suppressOutput,false);
}
/**
* This methods stops the server.
*
* @param suppressOutput boolean indicating that ouput to standard output
* streams from the server should be suppressed.
* @param noPropertiesFile boolean indicating if the stopServer should
* be called without taking into account the
* properties file.
* @throws org.opends.quicksetup.ApplicationException
* if something goes wrong.
*/
throws ApplicationException {
}
{
application.setNotifyListeners(false);
}
try {
if (application != null) {
}
if (noPropertiesFile)
{
size++;
}
if (noPropertiesFile)
{
}
int stopTries = 3;
while (stopTries > 0)
{
stopTries --;
stopTries));
try
{
new BufferedReader(
new BufferedReader(
/* Create these objects to resend the stop process output to the
* details area.
*/
new StopReader(err, true);
new StopReader(out, false);
int clientSideError =
if (isWindows()
/*
* Sometimes the server keeps some locks on the files.
* TODO: remove this code once stop-ds returns properly when
* server is stopped.
*/
int nTries = 10;
boolean stopped = false;
try {
}
{
// do nothing
}
"After calling stop-ds. Is server running? " + !stopped));
if (stopped) {
break;
}
if (application != null) {
}
}
if (!stopped) {
returnValue = -1;
}
}
if (returnValue == clientSideError) {
if (application != null) {
}
break;
} else if (returnValue != 0) {
if (stopTries <= 0)
{
/*
* The return code is not the one expected, assume the server
* could not be stopped.
*/
throw new ApplicationException(
null);
}
} else {
if (application != null) {
}
break;
}
} catch (Exception e) {
throw new ApplicationException(
INFO_ERROR_STOPPING_SERVER.get(), e), e);
}
}
}
finally {
if (suppressOutput)
{
{
}
if (application != null)
{
application.setNotifyListeners(true);
}
}
}
}
/**
* This methods starts the server.
*
*@throws org.opends.quicksetup.ApplicationException if something goes wrong.
*/
public void startServer() throws ApplicationException {
startServer(true, false);
}
/**
* This methods starts the server.
* @param suppressOutput boolean indicating that ouput to standard output
* streams from the server should be suppressed.
* @throws org.opends.quicksetup.ApplicationException if something goes wrong.
*/
public void startServer(boolean suppressOutput)
throws ApplicationException
{
startServer(true, suppressOutput);
}
/**
* This methods starts the server.
* @param verify boolean indicating whether this method will attempt to
* connect to the server after starting to verify that it is listening.
* @param suppressOutput indicating that ouput to standard output streams
* from the server should be suppressed.
* @throws org.opends.quicksetup.ApplicationException if something goes wrong.
*/
throws ApplicationException
{
}
{
application.setNotifyListeners(false);
}
try {
if (application != null) {
}
// Upgrader's classpath contains jars located in the temporary
// directory that we don't want locked by the directory server
// when it starts. Since we're just calling the start-ds script
// it will figure out the correct classpath for the server.
try
{
if (returnValue != 0)
{
null);
}
if (outputReader.isFinished())
{
}
if (errReader.isFinished())
{
}
{
}
// Check if something wrong occurred reading the starting of the server
{
}
{
// This is meaningless right now since we throw
// the exception below, but in case we change out
// minds later or add the ability to return exceptions
// in the output only instead of throwing...
throw ex;
} else if (verify)
{
/*
* There are no exceptions from the readers and they are marked as
* finished. So it seems that everything went fine.
*
* However we can have issues with the firewalls or do not have rights
* to connect or since the startup process is asynchronous we will
* have to wait for the databases and the listeners to initialize.
* Just check if we can connect to the server.
* Try 30 times with an interval of 3 seconds between try.
*/
boolean connected = false;
// See if the application has prompted for credentials. If
// not we'll just try to connect anonymously.
if (application != null) {
}
}
{
if (application != null)
{
}
{
hostName = "localhost";
}
int dig = i % 10;
{
// Try with local host. This might be necessary in certain
// network configurations.
hostName = "localhost";
}
{
// Try with 0.0.0.0. This might be necessary in certain
// network configurations.
hostName = "0.0.0.0";
}
try
{
{
}
connected = true;
}
catch (NamingException ne)
{
}
finally
{
}
if (!connected)
{
try
{
}
catch (Throwable t)
{
// do nothing
}
}
}
if (!connected)
{
}
}
{
throw new ApplicationException(
}
} finally {
if (suppressOutput)
{
{
}
if (application != null)
{
application.setNotifyListeners(true);
}
}
}
}
/**
* This class is used to read the standard error and standard output of the
* Stop process.
* <p/>
* When a new log message is found notifies the
* UninstallProgressUpdateListeners of it. If an error occurs it also
* notifies the listeners.
*/
private class StopReader {
private boolean isFirstLine;
/**
* The protected constructor.
*
* @param reader the BufferedReader of the stop process.
* @param isError a boolean indicating whether the BufferedReader
* corresponds to the standard error or to the standard output.
*/
final boolean isError) {
final LocalizableMessage errorTag =
isError ?
isFirstLine = true;
public void run() {
try {
if (application != null) {
if (!isFirstLine) {
getLineBreak());
}
if (isError) {
} else {
}
isFirstLine = false;
}
}
} catch (Throwable t) {
if (application != null) {
}
}
}
});
t.start();
}
}
/**
* Returns the LocalizableMessage ID indicating that the server has started.
* @return the LocalizableMessage ID indicating that the server has started.
*/
private String getStartedId()
{
return helper.getStartedId();
}
/**
* This class is used to read the standard error and standard output of the
* Start process.
*
* When a new log message is found notifies the ProgressUpdateListeners
* of it. If an error occurs it also notifies the listeners.
*
*/
private class StartReader
{
private ApplicationException ex;
private boolean isFinished;
private boolean startedIdFound;
private boolean isFirstLine;
/**
* The protected constructor.
* @param reader the BufferedReader of the start process.
* @param startedId the message ID that this class can use to know whether
* the start is over or not.
* @param isError a boolean indicating whether the BufferedReader
* corresponds to the standard error or to the standard output.
*/
final boolean isError)
{
final LocalizableMessage errorTag =
isError ?
isFirstLine = true;
{
public void run()
{
try
{
{
if (application != null) {
if (!isFirstLine)
{
getLineBreak());
}
if (isError)
{
} else
{
}
isFirstLine = false;
}
{
isFinished = true;
startedIdFound = true;
}
}
} catch (Throwable t)
{
ex = new ApplicationException(
getThrowableMsg(errorTag, t), t);
}
isFinished = true;
}
});
t.start();
}
/**
* Returns the ApplicationException that occurred reading the Start error
* and output or <CODE>null</CODE> if no exception occurred.
* @return the exception that occurred reading or <CODE>null</CODE> if
* no exception occurred.
*/
public ApplicationException getException()
{
return ex;
}
/**
* Returns <CODE>true</CODE> if the server starting process finished
* (successfully or not) and <CODE>false</CODE> otherwise.
* @return <CODE>true</CODE> if the server starting process finished
* (successfully or not) and <CODE>false</CODE> otherwise.
*/
public boolean isFinished()
{
return isFinished;
}
/**
* Returns <CODE>true</CODE> if the server start Id was found and
* <CODE>false</CODE> otherwise.
* @return <CODE>true</CODE> if the server start Id was found and
* <CODE>false</CODE> otherwise.
*/
public boolean startedIdFound()
{
return startedIdFound;
}
}
}