Author: sergeyb
Date: Thu Aug 29 16:49:10 2013
New Revision: 1518703
URL: http://svn.apache.org/r1518703
Log:
[CXF-5245] Improving the way SpringResourceFactory deals with lifecycle methods
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreStorage.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface2.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/LifecycleInterface.java
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
Thu Aug 29 16:49:10 2013
@@ -108,8 +108,9 @@ public class JAXRSInvoker extends Abstra
}
if (!suspended &&
!isServiceObjectRequestScope(exchange.getInMessage())) {
provider.releaseInstance(exchange.getInMessage(),
rootInstance);
+ } else {
+ persistRoots(exchange, rootInstance, provider);
}
- persistRoots(exchange, rootInstance, provider);
}
}
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringResourceFactory.java
Thu Aug 29 16:49:10 2013
@@ -49,7 +49,11 @@ public class SpringResourceFactory imple
private Method postConstructMethod;
private Method preDestroyMethod;
private boolean isSingleton;
- private boolean callLifecycleMethods = true;
+ private boolean isPrototype;
+ private boolean callPostConstruct;
+ private boolean callPreDestroy = true;
+ private String postConstructMethodName;
+ private String preDestroyMethodName;
public SpringResourceFactory() {
@@ -69,9 +73,12 @@ public class SpringResourceFactory imple
throw new RuntimeException("Resource class " + type
+ " has no valid constructor");
}
- postConstructMethod = ResourceUtils.findPostConstructMethod(type);
- preDestroyMethod = ResourceUtils.findPreDestroyMethod(type);
+ postConstructMethod = ResourceUtils.findPostConstructMethod(type,
postConstructMethodName);
+ preDestroyMethod = ResourceUtils.findPreDestroyMethod(type,
preDestroyMethodName);
isSingleton = ac.isSingleton(beanId);
+ if (!isSingleton) {
+ isPrototype = ac.isPrototype(beanId);
+ }
}
/**
@@ -89,8 +96,8 @@ public class SpringResourceFactory imple
}
protected void initInstance(Message m, Object instance) {
- if (callLifecycleMethods && (!isSingleton() || m == null)) {
- InjectionUtils.invokeLifeCycleMethod(instance,
postConstructMethod);
+ if (callPostConstruct) {
+
InjectionUtils.invokeLifeCycleMethod(ClassHelper.getRealObject(instance),
postConstructMethod);
}
}
@@ -107,7 +114,7 @@ public class SpringResourceFactory imple
* {@inheritDoc}
*/
public void releaseInstance(Message m, Object o) {
- if (callLifecycleMethods && !isSingleton()) {
+ if (callPreDestroy && isPrototype) {
InjectionUtils.invokeLifeCycleMethod(o, preDestroyMethod);
}
}
@@ -136,7 +143,19 @@ public class SpringResourceFactory imple
return c.getDeclaringClass();
}
- public void setCallLifecycleMethods(boolean callLifecycleMethods) {
- this.callLifecycleMethods = callLifecycleMethods;
+ public void setCallPostConstruct(boolean callPostConstruct) {
+ this.callPostConstruct = callPostConstruct;
+ }
+
+ public void setCallPreDestroy(boolean callPreDestroy) {
+ this.callPreDestroy = callPreDestroy;
+ }
+
+ public void setPreDestroyMethodName(String preDestroyMethodName) {
+ this.preDestroyMethodName = preDestroyMethodName;
+ }
+
+ public void setPostConstructMethodName(String postConstructMethodName) {
+ this.postConstructMethodName = postConstructMethodName;
}
}
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
Thu Aug 29 16:49:10 2013
@@ -117,22 +117,28 @@ public final class ResourceUtils {
private ResourceUtils() {
}
-
public static Method findPostConstructMethod(Class<?> c) {
+ return findPostConstructMethod(c, null);
+ }
+ public static Method findPostConstructMethod(Class<?> c, String name) {
if (Object.class == c || null == c) {
return null;
}
for (Method m : c.getDeclaredMethods()) {
- if (m.getAnnotation(PostConstruct.class) != null) {
+ if (name != null) {
+ if (m.getName().equals(name)) {
+ return m;
+ }
+ } else if (m.getAnnotation(PostConstruct.class) != null) {
return m;
}
}
- Method m = findPostConstructMethod(c.getSuperclass());
+ Method m = findPostConstructMethod(c.getSuperclass(), name);
if (m != null) {
return m;
}
for (Class<?> i : c.getInterfaces()) {
- m = findPostConstructMethod(i);
+ m = findPostConstructMethod(i, name);
if (m != null) {
return m;
}
@@ -141,20 +147,28 @@ public final class ResourceUtils {
}
public static Method findPreDestroyMethod(Class<?> c) {
+ return findPreDestroyMethod(c, null);
+ }
+
+ public static Method findPreDestroyMethod(Class<?> c, String name) {
if (Object.class == c || null == c) {
return null;
}
for (Method m : c.getDeclaredMethods()) {
- if (m.getAnnotation(PreDestroy.class) != null) {
+ if (name != null) {
+ if (m.getName().equals(name)) {
+ return m;
+ }
+ } else if (m.getAnnotation(PreDestroy.class) != null) {
return m;
}
}
- Method m = findPreDestroyMethod(c.getSuperclass());
+ Method m = findPreDestroyMethod(c.getSuperclass(), name);
if (m != null) {
return m;
}
for (Class<?> i : c.getInterfaces()) {
- m = findPreDestroyMethod(i);
+ m = findPreDestroyMethod(i, name);
if (m != null) {
return m;
}
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreStorage.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreStorage.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreStorage.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreStorage.java
Thu Aug 29 16:49:10 2013
@@ -22,10 +22,25 @@ package org.apache.cxf.systest.jaxrs;
import java.util.HashMap;
import java.util.Map;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.ws.rs.Path;
@Path("/bookstorestorage/")
-public abstract class BookStoreStorage {
+public abstract class BookStoreStorage implements LifecycleInterface {
protected Map<Long, Book> books = new HashMap<Long, Book>();
protected long bookId = 123;
+ protected boolean postConstructCalled;
+
+ @PostConstruct
+ public void postConstruct() {
+ if (postConstructCalled) {
+ throw new RuntimeException();
+ }
+ postConstructCalled = true;
+ }
+ @PreDestroy
+ public void preDestroy() {
+ // System.out.println("PreDestroy called");
+ }
}
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface.java
Thu Aug 29 16:49:10 2013
@@ -42,11 +42,8 @@ package org.apache.cxf.systest.jaxrs;
-public class BookStoreWithInterface extends BookStoreStorage
- implements BookInterface, LifecycleInterface {
+public class BookStoreWithInterface extends BookStoreStorage implements
BookInterface {
- private boolean postConstructCalled;
-
public BookStoreWithInterface() {
Book book = new Book();
book.setId(bookId);
@@ -54,14 +51,6 @@ public class BookStoreWithInterface exte
books.put(book.getId(), book);
}
- public void postConstruct() {
- postConstructCalled = true;
- }
-
- public void preDestroy() {
- //System.out.println("PreDestroy called");
- }
-
public Book getThatBook(Long id, String s) throws BookNotFoundFault {
if (!postConstructCalled) {
throw new RuntimeException();
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface2.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface2.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface2.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreWithInterface2.java
Thu Aug 29 16:49:10 2013
@@ -38,6 +38,7 @@
package org.apache.cxf.systest.jaxrs;
+import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
@@ -58,6 +59,11 @@ public class BookStoreWithInterface2 ext
this.servletContext = scontext;
}
+ @PreDestroy
+ public void preDestroy() {
+ System.out.println("PreDestroy called");
+ }
+
public Book getThatBook(Long id, String s) throws BookNotFoundFault {
if (servletContext == null) {
throw new RuntimeException();
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/LifecycleInterface.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/LifecycleInterface.java?rev=1518703&r1=1518702&r2=1518703&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/LifecycleInterface.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/LifecycleInterface.java
Thu Aug 29 16:49:10 2013
@@ -18,12 +18,8 @@
*/
package org.apache.cxf.systest.jaxrs;
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
public interface LifecycleInterface {
- @PostConstruct
void postConstruct();
- @PreDestroy
void preDestroy();
}