Example usage for javax.naming ServiceUnavailableException getCause

List of usage examples for javax.naming ServiceUnavailableException getCause

Introduction

In this page you can find the example usage for javax.naming ServiceUnavailableException getCause.

Prototype

public Throwable getCause() 

Source Link

Document

Returns the cause of this exception.

Usage

From source file:org.apache.openejb.assembler.classic.cmd.Info2Properties.java

public static void main(final String[] args) {

    final CommandLineParser parser = new PosixParser();

    // create the Options
    final Options options = new Options();
    options.addOption(option("v", "version", "cmd.properties.opt.version"));
    options.addOption(option("h", "help", "cmd.properties.opt.help"));
    options.addOption(option("s", "server-url", "url", "cmd.properties.opt.server"));

    CommandLine line = null;/*  ww  w.  j av  a2s  . c  o  m*/
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        System.exit(-1);
    }

    if (line.hasOption("help")) {
        help(options);
        System.exit(0);
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        System.exit(0);
    }

    final Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");

    final String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
    p.put(Context.PROVIDER_URL, serverUrl);

    ConfigurationInfo configInfo = null;
    try {
        final InitialContext ctx = new InitialContext(p);
        configInfo = (ConfigurationInfo) ctx.lookup("openejb/ConfigurationInfoBusinessRemote");
    } catch (final ServiceUnavailableException e) {
        System.out.println(e.getCause().getMessage());
        System.out.println(messages.format("cmd.deploy.serverOffline"));
        System.exit(1);
    } catch (final NamingException e) {
        System.out.println("ConfigurationInfo does not exist in server '" + serverUrl
                + "', check the server logs to ensure it exists and has not been removed.");
        System.exit(2);
    }

    File tempFile = null;
    try {
        try {
            tempFile = File.createTempFile("configrequest", "txt");
        } catch (final Throwable e) {
            final File tmp = new File("tmp");
            if (!tmp.exists() && !tmp.mkdirs()) {
                throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
            }

            tempFile = File.createTempFile("configrequest", "txt", tmp);

        }
        if (!tempFile.exists()) {
            throw new IllegalStateException("Failed to create tmp file: " + tempFile.getAbsolutePath());
        }
    } catch (final Exception e) {
        System.err.println("Temp file creation failed.");
        e.printStackTrace();
        System.exit(1);
    }

    OpenEjbConfiguration configuration = null;
    try {
        configuration = configInfo.getOpenEjbConfiguration(tempFile);
    } catch (final ConfigurationInfo.UnauthorizedException e) {
        System.err.println(
                "This tool is currently crippled to only work with server's on the same physical machine.  See this JIRA issue for details: http://issues.apache.org/jira/browse/OPENEJB-621");
        System.exit(10);
    }

    printConfig(configuration);
}

From source file:org.apache.openejb.config.Undeploy.java

public static void main(final String[] args) throws SystemExitException {

    final CommandLineParser parser = new PosixParser();

    // create the Options
    final Options options = new Options();
    options.addOption(Undeploy.option("v", "version", "cmd.deploy.opt.version"));
    options.addOption(Undeploy.option("h", "help", "cmd.undeploy.opt.help")); // TODO this message doesn't exist
    options.addOption(Undeploy.option("s", "server-url", "url", "cmd.deploy.opt.server"));

    CommandLine line = null;/*ww w.  jav a 2 s  . c o  m*/
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        Undeploy.help(options);
        throw new SystemExitException(-1);
    }

    if (line.hasOption("help")) {
        Undeploy.help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    }

    if (line.getArgList().size() == 0) {
        System.out.println("Must specify an module id.");
        help(options);
        return;
    }

    final Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");

    final String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
    p.put(Context.PROVIDER_URL, serverUrl);

    Deployer deployer = null;
    try {
        final InitialContext ctx = new InitialContext(p);
        deployer = (Deployer) ctx.lookup("openejb/DeployerBusinessRemote");
    } catch (final ServiceUnavailableException e) {
        System.out.println(e.getCause().getMessage());
        System.out.println(Undeploy.messages.format("cmd.deploy.serverOffline"));
        throw new SystemExitException(-1);
    } catch (final NamingException e) {
        System.out.println("DeployerEjb does not exist in server '" + serverUrl
                + "', check the server logs to ensure it exists and has not been removed.");
        throw new SystemExitException(-2);
    }

    int exitCode = 0;
    for (final Object obj : line.getArgList()) {
        final String moduleId = (String) obj;

        try {
            undeploy(moduleId, deployer);
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            exitCode++;
        } catch (final UndeployException e) {
            System.out.println(messages.format("cmd.undeploy.failed", moduleId));
            e.printStackTrace(System.out);
            exitCode++;
        } catch (final NoSuchApplicationException e) {
            // TODO make this message
            System.out.println(messages.format("cmd.undeploy.noSuchModule", moduleId));
            exitCode++;
        }
    }

    if (exitCode != 0) {
        throw new SystemExitException(exitCode);
    }
}