You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ni...@apache.org on 2007/10/04 16:41:00 UTC
svn commit: r581930 - in /incubator/cxf/trunk/common/common/src:
main/java/org/apache/cxf/common/injection/
test/java/org/apache/cxf/common/injection/
Author: ningjiang
Date: Thu Oct 4 07:40:59 2007
New Revision: 581930
URL: http://svn.apache.org/viewvc?rev=581930&view=rev
Log:
CXF-1074 now it should work for the proxy setter resource injection
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
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?rev=581930&r1=581929&r2=581930&view=diff
==============================================================================
--- 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 Thu Oct 4 07:40:59 2007
@@ -30,3 +30,4 @@
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.
+INJECTION_SETTER_METHOD_NOT_FOUND = Can't find the injection setter method: {0}
\ No newline at end of file
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?rev=581930&r1=581929&r2=581930&view=diff
==============================================================================
--- 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 Thu Oct 4 07:40:59 2007
@@ -237,12 +237,18 @@
private void invokeSetter(Method method, Object resource) {
try {
- method.setAccessible(true);
- method.invoke(getTarget(), resource);
+ method.setAccessible(true);
+ Method targetMethod = getTarget().getClass().
+ getMethod(method.getName(), new Class[]{resource.getClass()});
+ targetMethod.invoke(getTarget(), resource);
} catch (IllegalAccessException e) {
LOG.log(Level.SEVERE, "INJECTION_SETTER_NOT_VISIBLE", method);
} catch (InvocationTargetException e) {
LogUtils.log(LOG, Level.SEVERE, "INJECTION_SETTER_RAISED_EXCEPTION", e, method);
+ } catch (SecurityException e) {
+ LogUtils.log(LOG, Level.SEVERE, "INJECTION_SETTER_RAISED_EXCEPTION", e, method);
+ } catch (NoSuchMethodException e) {
+ LOG.log(Level.SEVERE, "INJECTION_SETTER_METHOD_NOT_FOUND", 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?rev=581930&r1=581929&r2=581930&view=diff
==============================================================================
--- 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 Thu Oct 4 07:40:59 2007
@@ -19,8 +19,11 @@
package org.apache.cxf.common.injection;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
@@ -90,8 +93,14 @@
@Test
public void testProxyInjection() {
+ setUpResourceManager(SetterTarget.class.getCanonicalName() + "/");
+ doInjectTest(getProxyObject(), SetterTarget.class);
+ }
+
+ @Test
+ public void testEnhancedInjection() {
setUpResourceManager(FieldTarget.class.getCanonicalName() + "/");
- doInjectTest(getProxyObject());
+ doInjectTest(getEnhancedObject());
}
@Test
@@ -123,9 +132,13 @@
assertTrue(target.preDestroyCalled());
}
- protected void doInjectTest(Target target) {
+ private void doInjectTest(Target target) {
+ doInjectTest(target, target.getClass());
+ }
+
+ private void doInjectTest(Target target, Class<?> clazz) {
- injector.inject(target);
+ injector.inject(target, clazz);
assertNotNull(target.getResource1());
assertEquals(RESOURCE_ONE, target.getResource1());
@@ -134,8 +147,14 @@
}
+ private Target getProxyObject() {
+ Target t = (Target)Proxy.newProxyInstance(ISetterTarget.class.getClassLoader(),
+ new Class[] {ISetterTarget.class},
+ new ProxyClass(new SetterTarget()));
+ return t;
+ }
- private FieldTarget getProxyObject() {
+ private FieldTarget getEnhancedObject() {
Enhancer e = new Enhancer();
e.setSuperclass(FieldTarget.class);
e.setCallback(new CallInterceptor());
@@ -191,6 +210,40 @@
}
+interface ISetterTarget extends Target {
+ void setResource1(final String argResource1);
+ void setResource2(final String argResource2);
+}
+
+class ProxyClass implements InvocationHandler {
+ Object obj;
+
+ public ProxyClass(Object o) {
+ obj = o;
+ }
+
+ public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
+ Object result = null;
+ try {
+ Class[] types = new Class[0];
+ if (args != null) {
+ types = new Class[args.length];
+ for (int i = 0; i < args.length; i++) {
+ types[i] = args[i].getClass();
+ }
+ }
+ Method target = obj.getClass().getMethod(m.getName(), types);
+ result = target.invoke(obj, args);
+ } catch (InvocationTargetException e) {
+ // Do nothing here
+ } catch (Exception eBj) {
+ eBj.printStackTrace();
+ } finally {
+ // Do something after the method is called ...
+ }
+ return result;
+ }
+}
class SetterTarget implements Target {
private String resource1;
@@ -214,7 +267,7 @@
}
@Resource(name = "resource2")
- private void setResource2(final String argResource2) {
+ public void setResource2(final String argResource2) {
this.resource2 = argResource2;
}