Search in sources :

Example 1 with UndeployException

use of org.apache.openejb.UndeployException 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 2 with UndeployException

use of org.apache.openejb.UndeployException 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 UndeployException

use of org.apache.openejb.UndeployException in project tomee by apache.

the class WebappDeployer method undeploy.

@Override
public void undeploy(final String moduleId) throws UndeployException, NoSuchApplicationException {
    try {
        final AppInfo appInfo = findAppInfo(moduleId);
        if (appInfo != null) {
            webappBuilder.undeployWebApps(appInfo);
        }
        assembler.destroyApplication(moduleId);
        final File moduleFile = new File(moduleId);
        if (moduleFile.getName().contains(".")) {
            // delete matching directory
            final File dir = new File(moduleFile.getAbsolutePath().substring(0, moduleFile.getAbsolutePath().lastIndexOf('.')));
            if (dir.exists() && dir.isDirectory()) {
                delete(dir);
            }
        } else {
            delete(new File(moduleFile + ".war"));
            delete(new File(moduleFile + ".ear"));
            delete(new File(moduleFile + ".rar"));
        }
        delete(moduleFile);
    } catch (Exception e) {
        throw new UndeployException(e);
    }
}
Also used : File(java.io.File) UndeployException(org.apache.openejb.UndeployException) OpenEJBException(org.apache.openejb.OpenEJBException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) AppInfo(org.apache.openejb.assembler.classic.AppInfo) UndeployException(org.apache.openejb.UndeployException)

Example 4 with UndeployException

use of org.apache.openejb.UndeployException in project tomee by apache.

the class TomEEEmbeddedMojo method reload.

protected synchronized void reload(final Thread thread, final ClassLoader loader, final Container container) throws OpenEJBException, NamingException, IOException {
    getLog().info("Redeploying " + (deployedName == null ? '/' : deployedName));
    try {
        final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
        if (classpathAsWar) {
            // this doesn't track module names so no need to go through container.undeploy()
            assembler.destroyApplication(assembler.getDeployedApplications().iterator().next().path);
        } else {
            container.undeploy(deployedName);
        }
    } catch (final UndeployException e) {
        throw new IllegalStateException(e);
    }
    doDeploy(thread, loader, container, false);
    getLog().info("Redeployed " + (deployedName == null ? '/' : deployedName));
}
Also used : Assembler(org.apache.openejb.assembler.classic.Assembler) UndeployException(org.apache.openejb.UndeployException)

Example 5 with UndeployException

use of org.apache.openejb.UndeployException in project tomee by apache.

the class Assembler method destroy.

@Override
public void destroy() {
    final ReentrantLock l = lock;
    l.lock();
    try {
        final SystemInstance systemInstance = SystemInstance.get();
        systemInstance.fireEvent(new ContainerSystemPreDestroy());
        try {
            EjbTimerServiceImpl.shutdown();
        } catch (final Exception e) {
            logger.warning("Unable to shutdown scheduler", e);
        } catch (final NoClassDefFoundError ncdfe) {
        // no-op
        }
        logger.debug("Undeploying Applications");
        final Assembler assembler = this;
        final List<AppInfo> deployedApps = new ArrayList<>(assembler.getDeployedApplications());
        // if an app relies on the previous one it surely relies on it too at undeploy time
        Collections.reverse(deployedApps);
        for (final AppInfo appInfo : deployedApps) {
            try {
                assembler.destroyApplication(appInfo.path);
            } catch (final UndeployException e) {
                logger.error("Undeployment failed: " + appInfo.path, e);
            } catch (final NoSuchApplicationException e) {
            // Ignore
            }
        }
        final Iterator<ObjectName> it = containerObjectNames.iterator();
        final MBeanServer server = LocalMBeanServer.get();
        while (it.hasNext()) {
            try {
                server.unregisterMBean(it.next());
            } catch (final Exception ignored) {
            // no-op
            }
            it.remove();
        }
        try {
            remoteResourceMonitor.unregister();
        } catch (final Exception ignored) {
        // no-op
        }
        NamingEnumeration<Binding> namingEnumeration = null;
        try {
            namingEnumeration = containerSystem.getJNDIContext().listBindings("openejb/Resource");
        } catch (final NamingException ignored) {
        // no resource adapters were created
        }
        destroyResourceTree("", namingEnumeration);
        try {
            containerSystem.getJNDIContext().unbind("java:global");
        } catch (final NamingException ignored) {
        // no-op
        }
        systemInstance.removeComponent(OpenEjbConfiguration.class);
        systemInstance.removeComponent(JtaEntityManagerRegistry.class);
        systemInstance.removeComponent(TransactionSynchronizationRegistry.class);
        systemInstance.removeComponent(EjbResolver.class);
        systemInstance.removeComponent(ThreadSingletonService.class);
        systemInstance.fireEvent(new AssemblerDestroyed());
        systemInstance.removeObservers();
        if (DestroyableResource.class.isInstance(this.securityService)) {
            DestroyableResource.class.cast(this.securityService).destroyResource();
        }
        if (DestroyableResource.class.isInstance(this.transactionManager)) {
            DestroyableResource.class.cast(this.transactionManager).destroyResource();
        }
        for (final Container c : this.containerSystem.containers()) {
            if (DestroyableResource.class.isInstance(c)) {
                // TODO: should we use auto closeable there?
                DestroyableResource.class.cast(c).destroyResource();
            }
        }
        SystemInstance.reset();
    } finally {
        l.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Binding(javax.naming.Binding) DestroyableResource(org.apache.openejb.api.resource.DestroyableResource) ArrayList(java.util.ArrayList) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ObjectName(javax.management.ObjectName) JMXContainer(org.apache.openejb.assembler.monitoring.JMXContainer) Container(org.apache.openejb.Container) SystemInstance(org.apache.openejb.loader.SystemInstance) ContainerSystemPreDestroy(org.apache.openejb.assembler.classic.event.ContainerSystemPreDestroy) NamingException(javax.naming.NamingException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) UndeployException(org.apache.openejb.UndeployException) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) MBeanServer(javax.management.MBeanServer) AssemblerDestroyed(org.apache.openejb.assembler.classic.event.AssemblerDestroyed)

Aggregations

UndeployException (org.apache.openejb.UndeployException)7 NoSuchApplicationException (org.apache.openejb.NoSuchApplicationException)5 NamingException (javax.naming.NamingException)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 File (java.io.File)3 IOException (java.io.IOException)3 InitialContext (javax.naming.InitialContext)3 InvalidObjectException (java.io.InvalidObjectException)2 ObjectStreamException (java.io.ObjectStreamException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 DefinitionException (javax.enterprise.inject.spi.DefinitionException)2 DeploymentException (javax.enterprise.inject.spi.DeploymentException)2 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 MBeanRegistrationException (javax.management.MBeanRegistrationException)2