Search in sources :

Example 1 with SystemExitException

use of org.apache.openejb.cli.SystemExitException in project tomee by apache.

the class DontStartServerException method main.

public static void main(String[] args) throws SystemExitException {
    CommandLineParser parser = new PosixParser();
    // create the Options
    Options options = new Options();
    options.addOption(option("v", "version", "cmd.start.opt.version"));
    options.addOption(option("h", "help", "cmd.start.opt.help"));
    options.addOption(option(null, "conf", "file", "cmd.start.opt.conf"));
    options.addOption(option(null, "local-copy", "boolean", "cmd.start.opt.localCopy"));
    options.addOption(option(null, "examples", "cmd.start.opt.examples"));
    ResourceFinder finder = new ResourceFinder("META-INF/");
    Set<String> services = null;
    try {
        Map<String, Properties> serviceEntries = finder.mapAvailableProperties(ServerService.class.getName());
        services = serviceEntries.keySet();
        for (String service : services) {
            options.addOption(option(null, service + "-port", "int", "cmd.start.opt.port", service));
            options.addOption(option(null, service + "-bind", "host", "cmd.start.opt.bind", service));
        }
    } catch (Exception e) {
        services = Collections.EMPTY_SET;
    }
    CommandLine line = null;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    } else if (line.hasOption("examples")) {
        try {
            String text = finder.findString("org.apache.openejb.cli/start.examples");
            System.out.println(text);
            return;
        } catch (Exception e) {
            System.err.println("Unable to print examples:");
            e.printStackTrace(System.err);
            throw new SystemExitException(-2);
        }
    }
    Properties props = SystemInstance.get().getProperties();
    if (line.hasOption("conf")) {
        props.setProperty("openejb.configuration", line.getOptionValue("conf"));
    } else if (line.hasOption("local-copy")) {
        String value = line.getOptionValue("local-copy");
        if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) {
            props.setProperty("openejb.localcopy", "false");
        } else {
            props.setProperty("openejb.localcopy", "true");
        }
    }
    for (String service : services) {
        String[] opts = { "port", "bind" };
        for (String opt : opts) {
            String option = service + "-" + opt;
            if (line.hasOption(option)) {
                props.setProperty(service + "." + opt, line.getOptionValue(option));
            }
        }
    }
    try {
        final SystemInstance system = SystemInstance.get();
        File libs = system.getHome().getDirectory("lib");
        system.getClassPath().addJarsToPath(libs);
        initServer(system);
    } catch (DontStartServerException ignored) {
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Options(org.apache.commons.cli.Options) ResourceFinder(org.apache.xbean.finder.ResourceFinder) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) Properties(java.util.Properties) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) SystemExitException(org.apache.openejb.cli.SystemExitException) CommandLine(org.apache.commons.cli.CommandLine) SystemInstance(org.apache.openejb.loader.SystemInstance) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 2 with SystemExitException

use of org.apache.openejb.cli.SystemExitException in project tomee by apache.

the class Undeploy method main.

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"));
    // TODO this message doesn't exist
    options.addOption(Undeploy.option("h", "help", "cmd.undeploy.opt.help"));
    options.addOption(Undeploy.option("s", "server-url", "url", "cmd.deploy.opt.server"));
    CommandLine line = null;
    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");
    String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
    if ("auto".equalsIgnoreCase(serverUrl.trim())) {
        try {
            final File sXml = new File(JavaSecurityManagers.getSystemProperty("openejb.base", "conf/server.xml"));
            if (sXml.exists()) {
                final QuickServerXmlParser result = QuickServerXmlParser.parse(sXml);
                serverUrl = "http://" + result.host() + ":" + result.http() + "/tomee/ejb";
            }
        } catch (final Throwable e) {
        // no-op
        }
    }
    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);
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) CommandLine(org.apache.commons.cli.CommandLine) NamingException(javax.naming.NamingException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) File(java.io.File) Deployer(org.apache.openejb.assembler.Deployer) UndeployException(org.apache.openejb.UndeployException)

Example 3 with SystemExitException

use of org.apache.openejb.cli.SystemExitException in project tomee by apache.

the class Deploy method main.

public static void main(final String... args) throws SystemExitException {
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(option("v", "version", "cmd.deploy.opt.version"));
    options.addOption(option("h", "help", "cmd.deploy.opt.help"));
    options.addOption(option("o", "offline", "cmd.deploy.opt.offline"));
    options.addOption(option("s", "server-url", "url", "cmd.deploy.opt.server"));
    options.addOption(option("d", "debug", "cmd.deploy.opt.debug"));
    options.addOption(option("q", "quiet", "cmd.deploy.opt.quiet"));
    options.addOption(option("u", "undeploy", "cmd.deploy.opt.undeploy"));
    options.addOption(option(null, "dir", "cmd.deploy.opt.dir"));
    final CommandLine line;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        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 archive to deploy.");
        help(options);
        return;
    }
    // make sure that the modules given on the command line are accessible
    final List<?> modules = line.getArgList();
    for (final Object module : modules) {
        final String path = (String) module;
        final File file = new File(path);
        try {
            checkSource(file);
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            // TODO: What is it for?
            throw new SystemExitException(-100);
        }
    }
    final boolean offline = line.hasOption("offline");
    final File apps;
    try {
        final String dir = line.getOptionValue("dir", "apps");
        apps = SystemInstance.get().getBase().getDirectory(dir);
    } catch (final IOException e) {
        throw new SystemExitException(-1);
    }
    if (!apps.exists()) {
        System.out.println("Directory does not exist: " + apps.getAbsolutePath());
    }
    Deployer deployer = null;
    if (!offline) {
        final Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
        if ("auto".equalsIgnoreCase(serverUrl.trim())) {
            try {
                final File sXml = new File(JavaSecurityManagers.getSystemProperty("openejb.base", "conf/server.xml"));
                if (sXml.exists()) {
                    final QuickServerXmlParser result = QuickServerXmlParser.parse(sXml);
                    serverUrl = "http://" + result.host() + ":" + result.http() + "/tomee/ejb";
                }
            } catch (final Throwable e) {
            // no-op
            }
        }
        p.put(Context.PROVIDER_URL, serverUrl);
        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(messages.format("cmd.deploy.serverOffline"));
            throw new SystemExitException(-1);
        } catch (final NamingException e) {
            System.out.println("openejb/DeployerBusinessRemote does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed.");
            throw new SystemExitException(-2);
        }
    }
    final boolean undeploy = line.hasOption("undeploy");
    // We increment the exit code once for every failed deploy
    int exitCode = 0;
    for (final Object obj : line.getArgList()) {
        final String path = (String) obj;
        final File file = new File(path);
        File destFile = new File(apps, file.getName());
        try {
            if (shouldUnpack(file)) {
                final File unpacked = unpackedLocation(file, apps);
                if (undeploy) {
                    undeploy(offline, unpacked, path, deployer);
                }
                destFile = unpack(file, unpacked);
            } else {
                if (undeploy) {
                    undeploy(offline, destFile, path, deployer);
                }
                checkDest(destFile, file);
                copyFile(file, destFile);
            }
            if (offline) {
                System.out.println(messages.format("cmd.deploy.offline", path, apps.getAbsolutePath()));
                continue;
            }
            final String location;
            try {
                location = destFile.getCanonicalPath();
            } catch (final IOException e) {
                throw new OpenEJBException(messages.format("cmd.deploy.fileNotFound", path));
            }
            final AppInfo appInfo = deployer.deploy(location);
            System.out.println(messages.format("cmd.deploy.successful", path, appInfo.path));
            if (line.hasOption("quiet")) {
                continue;
            }
            print(appInfo);
        } catch (final UndeployException e) {
            System.out.println(messages.format("cmd.undeploy.failed", path));
            e.printStackTrace(System.out);
            exitCode++;
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            exitCode++;
        } catch (final ValidationFailedException e) {
            System.out.println(messages.format("cmd.deploy.validationFailed", path));
            int level = 2;
            if (line.hasOption("debug")) {
                level = 3;
            }
            final AppValidator appValidator = new AppValidator(level, false, true, false);
            appValidator.printResults(e);
            exitCode++;
            if (!delete(destFile)) {
                System.out.println(messages.format("cmd.deploy.cantDelete.deploy", destFile.getAbsolutePath()));
            }
        } catch (final Throwable e) {
            System.out.println(messages.format("cmd.deploy.failed", path));
            e.printStackTrace(System.out);
            exitCode++;
            if (!delete(destFile)) {
                System.out.println(messages.format("cmd.deploy.cantDelete.deploy", destFile.getAbsolutePath()));
            }
        }
    }
    if (exitCode != 0) {
        throw new SystemExitException(exitCode);
    }
}
Also used : Options(org.apache.commons.cli.Options) OpenEJBException(org.apache.openejb.OpenEJBException) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) Properties(java.util.Properties) NamingException(javax.naming.NamingException) CommandLineParser(org.apache.commons.cli.CommandLineParser) Deployer(org.apache.openejb.assembler.Deployer) IOException(java.io.IOException) InitialContext(javax.naming.InitialContext) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) JarFile(java.util.jar.JarFile) File(java.io.File) UndeployException(org.apache.openejb.UndeployException)

Example 4 with SystemExitException

use of org.apache.openejb.cli.SystemExitException in project tomee by apache.

the class ListSetters method main.

public static void main(final String[] args) throws Exception {
    final Options options = new Options();
    options.addOption(OptionBuilder.isRequired(true).hasArg(true).withLongOpt("class").withDescription("the class to introspect").create("c"));
    final CommandLine line;
    try {
        line = new PosixParser().parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    }
    final String clazz = line.getOptionValue("class");
    final Collection<Class<?>> ancestors = Classes.ancestors(Thread.currentThread().getContextClassLoader().loadClass(clazz));
    final List<String> list = new LinkedList<>();
    for (final Class<?> c : ancestors) {
        for (final Method m : c.getDeclaredMethods()) {
            if (!Modifier.isPublic(m.getModifiers())) {
                continue;
            }
            if (!m.getName().startsWith("set")) {
                continue;
            }
            if (m.getGenericParameterTypes().length != 1) {
                continue;
            }
            list.add(m.getName().substring(3));
        }
    }
    Collections.sort(list);
    for (final String s : list) {
        System.out.println("- " + s);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) ParseException(org.apache.commons.cli.ParseException) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList)

Example 5 with SystemExitException

use of org.apache.openejb.cli.SystemExitException in project tomee by apache.

the class EffectiveTomEEXml method parseCommand.

private static CommandLine parseCommand(final String[] args) throws SystemExitException {
    final Options options = new Options();
    options.addOption(OptionBuilder.hasArg(true).withLongOpt("path").withDescription("[openejb|tomee].xml path").create("p"));
    final CommandLine line;
    try {
        line = new PosixParser().parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return null;
    }
    return line;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) ParseException(org.apache.commons.cli.ParseException)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)7 Options (org.apache.commons.cli.Options)7 ParseException (org.apache.commons.cli.ParseException)7 PosixParser (org.apache.commons.cli.PosixParser)7 SystemExitException (org.apache.openejb.cli.SystemExitException)7 CommandLineParser (org.apache.commons.cli.CommandLineParser)5 File (java.io.File)4 Properties (java.util.Properties)3 IOException (java.io.IOException)2 InitialContext (javax.naming.InitialContext)2 NamingException (javax.naming.NamingException)2 ServiceUnavailableException (javax.naming.ServiceUnavailableException)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 UndeployException (org.apache.openejb.UndeployException)2 Deployer (org.apache.openejb.assembler.Deployer)2 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 JarFile (java.util.jar.JarFile)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 NoSuchApplicationException (org.apache.openejb.NoSuchApplicationException)1