Search in sources :

Example 1 with ContainerSystemPreDestroy

use of org.apache.openejb.assembler.classic.event.ContainerSystemPreDestroy 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

IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 Binding (javax.naming.Binding)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 NamingException (javax.naming.NamingException)1 ResourceAdapterInternalException (javax.resource.spi.ResourceAdapterInternalException)1