* @throws CommandException
*/
@Override
protected int executeCommand()
throws CommandException, CommandValidationException {
final CLIProcessExecutor cpe = new CLIProcessExecutor();
String dbLog = "";
int exitCode = 0;
try {
prepareProcessExecutor();
dbHome = getDatabaseHomeDir();
if (dbHome != null)
dbLog = dbHome + File.separator + DerbyControl.DB_LOG_FILENAME;
logger.finer("Ping Database");
cpe.execute("pingDatabaseCmd", pingDatabaseCmd(true), true);
// if ping is unsuccesfull then database is not up and running
if (cpe.exitValue() > 0) {
logger.finer("Start Database");
cpe.execute("startDatabaseCmd", startDatabaseCmd(), false);
if (cpe.exitValue() != 0) {
throw new CommandException(strings.get("UnableToStartDatabase", dbLog));
}
} else if (cpe.exitValue() < 0) {
// Something terribly wrong!
throw new CommandException(strings.get("CommandUnSuccessful", name));
} else {
// database already started
logger.info(strings.get("StartDatabaseStatus", dbHost, dbPort));
}
} catch (IllegalThreadStateException ite) {
// IllegalThreadStateException is thrown if the
// process has not yet teminated and is still running.
// see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html#exitValue()
// This is good since that means the database is up and running.
CLIProcessExecutor cpePing = new CLIProcessExecutor();
CLIProcessExecutor cpeSysInfo = new CLIProcessExecutor();
try {
if (!programOpts.isTerse()) {
// try getting sysinfo
logger.fine(strings.get("database.info.msg", dbHost, dbPort));
}
cpePing.execute("pingDatabaseCmd", pingDatabaseCmd(true), true);
int counter = 0;
//give time for the database to be started
while (cpePing.exitValue() != 0 && counter < 10) {
cpePing.execute("pingDatabaseCmd", pingDatabaseCmd(true), true);
Thread.sleep(500);
counter++;
//break out if start-database failed
try {
cpe.exitValue();
break;
} catch (IllegalThreadStateException itse) {
continue;
}
}
if (!programOpts.isTerse()) {
logger.finer("Database SysInfo");
if (cpePing.exitValue() == 0) {
cpeSysInfo.execute("sysinfoCmd", sysinfoCmd(), true);
if (cpeSysInfo.exitValue() != 0) {
logger.info(strings.get("CouldNotGetSysInfo"));
}
}
}
} catch (Exception e) {