Repository: tomee Updated Branches: refs/heads/master cb6baa010 -> d25943021
Ensure @PreDestroy is called for application resources Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/cce266a4 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/cce266a4 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/cce266a4 Branch: refs/heads/master Commit: cce266a4f2ceca41ed88702cd71aa9f487c6eb4a Parents: 8ed192c Author: Jonathan Gallimore <[email protected]> Authored: Wed Jul 12 18:16:48 2017 +0100 Committer: Jonathan Gallimore <[email protected]> Committed: Wed Jul 12 20:46:25 2017 +0100 ---------------------------------------------------------------------- .../openejb/assembler/classic/Assembler.java | 7 ++ .../ApplicationResourceLifecycleTest.java | 99 ++++++++++++++++++++ .../src/test/resources/app-resources.xml | 21 +++++ 3 files changed, 127 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/cce266a4/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java index 18cc218..d0c0958 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java @@ -2484,6 +2484,13 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A if (!binding.getName().equals(objName)) { continue; } + if (DestroyableResource.class.isInstance(binding.getObject())) { + final DestroyableResource destroyableResource = DestroyableResource.class.cast(binding.getObject()); + destroyableResource.destroyResource(); + globalContext.unbind(name); + return; + } + if (!LazyObjectReference.class.isInstance(binding.getObject())) { continue; } http://git-wip-us.apache.org/repos/asf/tomee/blob/cce266a4/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ApplicationResourceLifecycleTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ApplicationResourceLifecycleTest.java b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ApplicationResourceLifecycleTest.java new file mode 100644 index 0000000..1b44277 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ApplicationResourceLifecycleTest.java @@ -0,0 +1,99 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.assembler.classic; + +import org.apache.openejb.OpenEJB; +import org.apache.openejb.OpenEJBException; +import org.apache.openejb.config.AppModule; +import org.apache.openejb.config.ConfigurationFactory; +import org.apache.openejb.config.EjbModule; +import org.apache.openejb.core.LocalInitialContextFactory; +import org.apache.openejb.jee.EjbJar; +import org.apache.openejb.jee.SingletonBean; +import org.junit.Test; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import java.io.IOException; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class ApplicationResourceLifecycleTest { + + private static final AtomicBoolean POST_CONSTRUCT = new AtomicBoolean(false); + private static final AtomicBoolean PRE_DESTROY = new AtomicBoolean(false); + + + @Test + public void test() throws OpenEJBException, NamingException, IOException { + final ConfigurationFactory config = new ConfigurationFactory(); + final Assembler assembler = new Assembler(); + + assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class)); + assembler.createSecurityService(config.configureService(SecurityServiceInfo.class)); + + final AppModule app = new AppModule(ApplicationResourceLifecycleTest.class.getClassLoader(), ApplicationResourceLifecycleTest.class.getSimpleName()); + + final EjbJar ejbJar = new EjbJar(); + ejbJar.addEnterpriseBean(new SingletonBean(ApplicationResource.class)); + app.getEjbModules().add(new EjbModule(ejbJar)); + app.getEjbModules().iterator().next().getAltDDs().put("resources.xml", getClass().getClassLoader().getResource("app-resources.xml")); + + assembler.createApplication(config.configureApplication(app)); + + final Properties properties = new Properties(); + properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName()); + properties.setProperty("openejb.embedded.initialcontext.close", "destroy"); + + // some hack to be sure to call destroy() + InitialContext context = new InitialContext(properties); + assertNotNull(context); + + assertTrue(POST_CONSTRUCT.getAndSet(false)); + assertFalse(PRE_DESTROY.get()); + + ApplicationResource bean = (ApplicationResource) context.lookup("ApplicationResourceLocalBean"); + + assertNotNull(bean); + context.close(); + + OpenEJB.destroy(); + assertFalse(POST_CONSTRUCT.get()); + assertTrue(PRE_DESTROY.get()); + } + + public static class ApplicationResource { + + @PostConstruct + public void start() { + POST_CONSTRUCT.set(true); + } + + @PreDestroy + public void stop() { + PRE_DESTROY.set(true); + } + + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/cce266a4/container/openejb-core/src/test/resources/app-resources.xml ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/resources/app-resources.xml b/container/openejb-core/src/test/resources/app-resources.xml new file mode 100644 index 0000000..60a42c2 --- /dev/null +++ b/container/openejb-core/src/test/resources/app-resources.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<resources> + <Resource id="ApplicationResource" class-name="org.apache.openejb.assembler.classic.ApplicationResourceLifecycleTest$ApplicationResource"> + </Resource> +</resources> \ No newline at end of file
