Search in sources :

Example 1 with CLIProcessExecutor

use of com.sun.enterprise.admin.cli.CLIProcessExecutor in project Payara by payara.

the class StartDatabaseCommand method executeCommand.

/**
 * Execute the command
 *
 * @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 + dbManager.getLogFileName();
        }
        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) {
            throw new CommandException(strings.get("CommandUnSuccessful", name), e);
        }
        if (cpePing.exitValue() == 0) {
            logger.info(strings.get("DatabaseStartMsg"));
            if ((new File(dbLog)).canWrite()) {
                logger.info(strings.get("LogRedirectedTo", dbLog));
            }
        } else {
            throw new CommandException(strings.get("UnableToStartDatabase", dbLog));
        }
    } catch (CommandException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CommandException(strings.get("CommandUnSuccessful", name), e);
    }
    return exitCode;
}
Also used : CLIProcessExecutor(com.sun.enterprise.admin.cli.CLIProcessExecutor) CommandException(org.glassfish.api.admin.CommandException) File(java.io.File) IOException(java.io.IOException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) CommandException(org.glassfish.api.admin.CommandException)

Example 2 with CLIProcessExecutor

use of com.sun.enterprise.admin.cli.CLIProcessExecutor in project Payara by payara.

the class StopDatabaseCommand method executeCommand.

/**
 * Executes the command
 *
 * @throws CommandException
 */
@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    try {
        prepareProcessExecutor();
        CLIProcessExecutor cpe = new CLIProcessExecutor();
        cpe.execute("pingDatabaseCmd", pingDatabaseCmd(false), true);
        if (cpe.exitValue() > 0) {
            // if ping is unsuccesfull then database is not up and running
            throw new CommandException(strings.get("StopDatabaseStatus", dbHost, dbPort));
        } else if (cpe.exitValue() < 0) {
            // Something terribly wrong!
            throw new CommandException(strings.get("UnableToStopDatabase", dbManager.getLogFileName()));
        } else {
            // Database is running so go ahead and stop the database
            cpe.execute("stopDatabaseCmd", stopDatabaseCmd(), true);
            if (cpe.exitValue() > 0) {
                throw new CommandException(strings.get("UnableToStopDatabase", dbManager.getLogFileName()));
            }
        }
    } catch (Exception e) {
        throw new CommandException(strings.get("UnableToStopDatabase", dbManager.getLogFileName()), e);
    }
    return 0;
}
Also used : CLIProcessExecutor(com.sun.enterprise.admin.cli.CLIProcessExecutor) CommandException(org.glassfish.api.admin.CommandException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) CommandException(org.glassfish.api.admin.CommandException)

Aggregations

CLIProcessExecutor (com.sun.enterprise.admin.cli.CLIProcessExecutor)2 CommandException (org.glassfish.api.admin.CommandException)2 CommandValidationException (org.glassfish.api.admin.CommandValidationException)2 File (java.io.File)1 IOException (java.io.IOException)1