Author: andreasmyth
Date: Wed Feb 28 06:52:55 2007
New Revision: 512783
URL: http://svn.apache.org/viewvc?view=rev&rev=512783
Log:
[JIRA CXF-259] JSR250: support for @PreDestroy
Minor change to @PreDestroy annoated method shutdown of RMManager (cancel timer
to fix hang on shutdown).
Added:
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
(with props)
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
(with props)
Modified:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/Messages.properties
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Jsr250BeanPostProcessor.java
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/SpringBusFactoryTest.java
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/policy-bus.xml
incubator/cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/RMManager.java
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/RMManagerTest.java
Modified:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/Messages.properties
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/Messages.properties?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/Messages.properties
(original)
+++
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/Messages.properties
Wed Feb 28 06:52:55 2007
@@ -25,6 +25,8 @@
PRIVATE_FIELD_INJECTION_NYI=resource injection for private fields is not yet
implemented: {0}
INJECTION_COMPLETE_NOT_VISIBLE=method annotated by @PostConstruct is not
visible: {0}
INJECTION_COMPLETE_THREW_EXCEPTION=method annotated by @PostConstruct throws
exception when invoked
+PRE_DESTROY_NOT_VISIBLE=Method annotated by @PreDestroy is not visible: {0}
+PRE_DESTROY_THREW_EXCEPTION=Method annotated by @PreDestroy throws exception
when invoked
SETTER_INJECTION_WITH_INCORRECT_TYPE=setter for resource found but with wrong
signature: {0}
RESOURCE_NAME_NOT_SPECIFIED=Resource annotation on {0} must specify resource
name
NO_SETTER_OR_FIELD_FOR_RESOURCE=Resource annotation {0} on {0} but no field or
setter found.
Modified:
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
(original)
+++
incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
Wed Feb 28 06:52:55 2007
@@ -31,6 +31,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.Resources;
@@ -84,6 +85,11 @@
}
+ public void destroy(Object o) {
+ setTarget(o);
+ invokePreDestroy();
+ }
+
// Implementation of org.apache.cxf.common.annotation.AnnotationVisitor
@@ -291,19 +297,49 @@
}
}
+ public void invokePreDestroy() {
+
+ boolean accessible = false;
+ for (Method method : getPreDestroyMethods()) {
+ PreDestroy pd = method.getAnnotation(PreDestroy.class);
+ if (pd != null) {
+ try {
+ method.setAccessible(true);
+ method.invoke(target);
+ } catch (IllegalAccessException e) {
+ LOG.log(Level.WARNING, "PRE_DESTROY_NOT_VISIBLE", method);
+ } catch (InvocationTargetException e) {
+ LOG.log(Level.WARNING, "PRE_DESTROY_THREW_EXCEPTION", e);
+ } finally {
+ method.setAccessible(accessible);
+ }
+ }
+ }
+ }
+
+
private Collection<Method> getPostConstructMethods() {
+ return getAnnotatedMethods(PostConstruct.class);
+ }
+
+ private Collection<Method> getPreDestroyMethods() {
+ return getAnnotatedMethods(PreDestroy.class);
+ }
+
+ private Collection<Method> getAnnotatedMethods(Class<? extends Annotation>
acls) {
Collection<Method> methods = new LinkedList<Method>();
- addPostConstructMethods(getTarget().getClass().getMethods(), methods);
- addPostConstructMethods(getTarget().getClass().getDeclaredMethods(),
methods);
+ addAnnotatedMethods(acls, getTarget().getClass().getMethods(),
methods);
+ addAnnotatedMethods(acls, getTarget().getClass().getDeclaredMethods(),
methods);
return methods;
}
- private void addPostConstructMethods(Method[] methods, Collection<Method>
postConstructMethods) {
+ private void addAnnotatedMethods(Class<? extends Annotation> acls,
Method[] methods,
+ Collection<Method> annotatedMethods) {
for (Method method : methods) {
- if (method.getAnnotation(PostConstruct.class) != null
- && !postConstructMethods.contains(method)) {
- postConstructMethods.add(method);
+ if (method.getAnnotation(acls) != null
+ && !annotatedMethods.contains(method)) {
+ annotatedMethods.add(method);
}
}
}
Modified:
incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
(original)
+++
incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
Wed Feb 28 06:52:55 2007
@@ -25,6 +25,7 @@
import java.util.List;
import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.Resources;
import junit.framework.TestCase;
@@ -84,6 +85,12 @@
assertTrue(target.injectionCompleteCalled());
}
+ public void testPreDestroy() {
+ injector = new ResourceInjector(null, null);
+ SetterTarget target = new SetterTarget();
+ injector.destroy(target);
+ assertTrue(target.preDestroyCalled());
+ }
protected void doInjectTest(Target target) {
@@ -132,6 +139,8 @@
private String resource2;
private boolean injectionCompletePublic;
private boolean injectionCompletePrivate;
+ private boolean preDestroy;
+ private boolean preDestroyPrivate;
public final String getResource1() {
return this.resource1;
@@ -164,8 +173,22 @@
injectionCompletePrivate = true;
}
+ @PreDestroy
+ public void preDestroyMethod() {
+ preDestroy = true;
+ }
+
+ @PreDestroy
+ private void preDestroyMethodPrivate() {
+ preDestroyPrivate = true;
+ }
+
public boolean injectionCompleteCalled() {
return injectionCompletePrivate && injectionCompletePublic;
+ }
+
+ public boolean preDestroyCalled() {
+ return preDestroy && preDestroyPrivate;
}
}
Modified:
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Jsr250BeanPostProcessor.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Jsr250BeanPostProcessor.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Jsr250BeanPostProcessor.java
(original)
+++
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/Jsr250BeanPostProcessor.java
Wed Feb 28 06:52:55 2007
@@ -21,10 +21,10 @@
import org.apache.cxf.common.injection.ResourceInjector;
import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
+import
org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.core.Ordered;
-public class Jsr250BeanPostProcessor implements BeanPostProcessor, Ordered {
+public class Jsr250BeanPostProcessor implements
DestructionAwareBeanPostProcessor, Ordered {
private ResourceInjector injector;
@@ -43,6 +43,10 @@
public Object postProcessBeforeInitialization(Object bean, String beanId)
throws BeansException {
injector.construct(bean);
return bean;
+ }
+
+ public void postProcessBeforeDestruction(Object bean, String beanId) {
+ injector.destroy(bean);
}
}
Modified:
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java
(original)
+++
incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/bus/spring/SpringBusFactory.java
Wed Feb 28 06:52:55 2007
@@ -25,6 +25,8 @@
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
+import org.apache.cxf.buslifecycle.BusLifeCycleListener;
+import org.apache.cxf.buslifecycle.BusLifeCycleManager;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.configuration.Configurer;
import org.apache.cxf.configuration.spring.ConfigurerImpl;
@@ -75,6 +77,7 @@
bus.setExtension(configurer, Configurer.class);
possiblySetDefaultBus(bus);
+ registerApplicationContextLifeCycleListener(bus, bac);
return bus;
}
@@ -101,7 +104,34 @@
bus.setExtension(configurer, Configurer.class);
possiblySetDefaultBus(bus);
+ registerApplicationContextLifeCycleListener(bus, bac);
return bus;
}
+
+ void registerApplicationContextLifeCycleListener(Bus bus,
BusApplicationContext bac) {
+ BusLifeCycleManager lm = bus.getExtension(BusLifeCycleManager.class);
+ if (null != lm) {
+ lm.registerLifeCycleListener(new
BusApplicationContextLifeCycleListener(bac));
+ }
+ }
+
+ static class BusApplicationContextLifeCycleListener implements
BusLifeCycleListener {
+ private BusApplicationContext bac;
+
+ BusApplicationContextLifeCycleListener(BusApplicationContext b) {
+ bac = b;
+ }
+
+ public void initComplete() {
+ }
+
+ public void preShutdown() {
+ }
+
+ public void postShutdown() {
+ bac.close();
+ }
+
+ }
}
Modified:
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/SpringBusFactoryTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/SpringBusFactoryTest.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/SpringBusFactoryTest.java
(original)
+++
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/SpringBusFactoryTest.java
Wed Feb 28 06:52:55 2007
@@ -22,6 +22,9 @@
import java.net.URL;
import java.util.List;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+
import junit.framework.TestCase;
import org.apache.cxf.Bus;
@@ -145,6 +148,17 @@
assertEquals(0, cxfPhases.get(i).compareTo(defaultPhases.get(i)));
}
}
+
+ public void testJsr250() {
+ Bus bus = new
SpringBusFactory().createBus("org/apache/cxf/bus/spring/testjsr250.xml");
+ TestExtension te = bus.getExtension(TestExtension.class);
+ assertTrue("@PostConstruct annotated method has not been called.",
te.postConstructMethodCalled);
+ assertTrue("@PreDestroy annoated method has been called already.",
!te.preDestroyMethodCalled);
+ bus.shutdown(true);
+ assertTrue("@PreDestroy annoated method has not been called.",
te.preDestroyMethodCalled);
+
+ }
+
static class TestInterceptor implements Interceptor {
@@ -171,6 +185,26 @@
public void postHandleMessage(Message message) throws Fault {
}
+ }
+
+ static class TestExtension {
+
+ boolean postConstructMethodCalled;
+ boolean preDestroyMethodCalled;
+
+ public TestExtension(Bus bus) {
+ bus.setExtension(this, TestExtension.class);
+ }
+
+ @PostConstruct
+ void postConstructMethod() {
+ postConstructMethodCalled = true;
+ }
+
+ @PreDestroy
+ void preDestroyMethod() {
+ preDestroyMethodCalled = true;
+ }
}
Added:
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml?view=auto&rev=512783
==============================================================================
---
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
(added)
+++
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
Wed Feb 28 06:52:55 2007
@@ -0,0 +1,29 @@
+<?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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <bean id="testExtension"
class="org.apache.cxf.bus.spring.SpringBusFactoryTest$TestExtension">
+ <constructor-arg ref="cxf"/>
+ </bean>
+
+</beans>
\ No newline at end of file
Propchange:
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/bus/spring/testjsr250.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Modified:
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/policy-bus.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/policy-bus.xml?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/policy-bus.xml
(original)
+++
incubator/cxf/trunk/rt/ws/policy/src/test/java/org/apache/cxf/ws/policy/policy-bus.xml
Wed Feb 28 06:52:55 2007
@@ -30,6 +30,9 @@
<bean
class="org.apache.cxf.ws.policy.PolicyExtensionsTest$TestPolicyInterceptorProvider"/>
<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl"/>
+ <bean id="org.apache.cxf.buslifecycle.BusLifeCycleManager"
class="org.apache.cxf.buslifecycle.CXFBusLifeCycleManager">
+ <property name="bus" ref="cxf"/>
+ </bean>
<bean class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>
<bean class="org.apache.cxf.bus.spring.BusExtensionPostProcessor"/>
<import resource="../../../../../META-INF/cxf/cxf-extension-policy.xml"/>
Modified:
incubator/cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/RMManager.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/RMManager.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/RMManager.java
(original)
+++
incubator/cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/RMManager.java
Wed Feb 28 06:52:55 2007
@@ -203,11 +203,9 @@
}
}
- // TODO: cancel outstanding timer tasks (acknowledgment requests) for
all source sequences
-
// remove references to timer tasks cancelled above to make them
eligible for garbage collection
timer.purge();
-
+ timer.cancel();
}
@PostConstruct
Modified:
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/RMManagerTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/RMManagerTest.java?view=diff&rev=512783&r1=512782&r2=512783
==============================================================================
---
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/RMManagerTest.java
(original)
+++
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/RMManagerTest.java
Wed Feb 28 06:52:55 2007
@@ -23,10 +23,12 @@
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.Date;
+import java.util.TimerTask;
import junit.framework.TestCase;
import org.apache.cxf.Bus;
+import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
@@ -223,4 +225,23 @@
assertSame(sseq, manager.getSequence(inSid, message, maps));
control.verify();
}
-}
+
+ public void testShutdown() {
+
+ Bus bus = new
SpringBusFactory().createBus("org/apache/cxf/ws/rm/rmmanager.xml", false);
+ RMManager manager = bus.getExtension(RMManager.class);
+ assertNotNull(manager);
+ class TestTask extends TimerTask {
+ public void run() {
+ }
+ }
+ bus.shutdown(true);
+ try {
+ manager.getTimer().schedule(new TestTask(), 5000);
+ fail("Timer has not been cancelled.");
+ } catch (IllegalStateException ex) {
+ // expected
+ }
+ }
+
+}
Added:
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml?view=auto&rev=512783
==============================================================================
---
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
(added)
+++
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
Wed Feb 28 06:52:55 2007
@@ -0,0 +1,36 @@
+<?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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:wsrm-mgmt="http://cxf.apache.org/ws/rm/manager"
+ xmlns:wsrm-policy="http://schemas.xmlsoap.org/ws/2005/02/rm/policy"
+ xmlns:http="http://cxf.apache.org/transports/http/configuration"
+ xsi:schemaLocation="
+http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schema/transports/http.xsd
+http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl"/>
+ <bean id="org.apache.cxf.buslifecycle.BusLifeCycleManager"
class="org.apache.cxf.buslifecycle.CXFBusLifeCycleManager">
+ <property name="bus" ref="cxf"/>
+ </bean>
+ <bean class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>
+ <import resource="../../../../../META-INF/cxf/cxf-extension-rm.xml"/>
+
+</beans>
Propchange:
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
incubator/cxf/trunk/rt/ws/rm/src/test/java/org/apache/cxf/ws/rm/rmmanager.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml