Author: aparfonov
Date: 2011-02-15 09:14:38 -0500 (Tue, 15 Feb 2011)
New Revision: 3978
Modified:
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java
Log:
EXOJCR-1181
Modified:
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java 2011-02-15
13:39:35 UTC (rev 3977)
+++
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java 2011-02-15
14:14:38 UTC (rev 3978)
@@ -26,10 +26,13 @@
import org.exoplatform.services.rest.resource.ResourceDescriptorVisitor;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import java.util.List;
import javax.ws.rs.DefaultValue;
@@ -69,6 +72,8 @@
/** See {@link java.lang.reflect.Field} . */
private final java.lang.reflect.Field jfield;
+ private final Method setter;
+
/**
* @param resourceClass class that contains field <tt>jfield</tt>
* @param jfield java.lang.reflect.Field
@@ -77,6 +82,7 @@
{
this.jfield = jfield;
this.annotations = jfield.getDeclaredAnnotations();
+ this.setter = getSetter(resourceClass, jfield);
Annotation annotation = null;
String defaultValue = null;
@@ -130,6 +136,27 @@
this.encoded = encoded || resourceClass.getAnnotation(Encoded.class) != null;
}
+ private static Method getSetter(final Class<?> clazz, final
java.lang.reflect.Field jfield)
+ {
+ Method setter = null;
+ try
+ {
+ setter = AccessController.doPrivileged(new
PrivilegedExceptionAction<Method>() {
+ public Method run() throws NoSuchMethodException
+ {
+ String name = jfield.getName();
+ String setterName = "set" +
Character.toUpperCase(name.charAt(0)) + name.substring(1);
+ return clazz.getMethod(setterName, jfield.getType());
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ // Ignore NoSuchMethodException.
+ }
+ return setter;
+ }
+
/**
* {@inheritDoc}
*/
@@ -196,17 +223,24 @@
ParameterResolver<?> pr =
ParameterResolverFactory.createParameterResolver(annotation);
try
{
- if (!Modifier.isPublic(jfield.getModifiers()))
+ if (setter != null)
{
- AccessController.doPrivileged(new PrivilegedAction<Void>() {
- public Void run()
- {
- jfield.setAccessible(true);
- return null;
- }
- });
+ setter.invoke(resource, pr.resolve(this, context));
}
- jfield.set(resource, pr.resolve(this, context));
+ else
+ {
+ if (!Modifier.isPublic(jfield.getModifiers()))
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ public Void run()
+ {
+ jfield.setAccessible(true);
+ return null;
+ }
+ });
+ }
+ jfield.set(resource, pr.resolve(this, context));
+ }
}
catch (Throwable e)
{
@@ -223,17 +257,24 @@
{
try
{
- if (!Modifier.isPublic(jfield.getModifiers()))
+ if (setter != null)
{
- AccessController.doPrivileged(new PrivilegedAction<Void>() {
- public Void run()
- {
- jfield.setAccessible(true);
- return null;
- }
- });
+ setter.invoke(resource, tmp);
}
- jfield.set(resource, tmp);
+ else
+ {
+ if (!Modifier.isPublic(jfield.getModifiers()))
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ public Void run()
+ {
+ jfield.setAccessible(true);
+ return null;
+ }
+ });
+ }
+ jfield.set(resource, tmp);
+ }
}
catch (Throwable e)
{
Modified:
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java
===================================================================
---
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java 2011-02-15
13:39:35 UTC (rev 3977)
+++
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java 2011-02-15
14:14:38 UTC (rev 3978)
@@ -80,6 +80,28 @@
}
}
+ @Path("a")
+ public static class Resource3
+ {
+ @Inject
+ private GenericIngectable<String> injected;
+ private boolean injectedThroughSetter = false;
+
+ @GET
+ public String m()
+ {
+ assertNotNull(injected);
+ assertTrue(injectedThroughSetter);
+ return ((InjectableComponent)injected).message;
+ }
+
+ public void setInjected(GenericIngectable<String> injected)
+ {
+ this.injectedThroughSetter = true;
+ this.injected = injected;
+ }
+ }
+
public void testInjectFromContainer() throws Exception
{
container.registerComponentInstance(InjectableComponent.class.getName(), new
InjectableComponent());
@@ -112,4 +134,15 @@
unregistry(Resource2.class);
container.unregisterComponent(Provider90.class.getName());
}
+
+ public void testInjectWithSetter() throws Exception
+ {
+ container.registerComponentInstance(InjectableComponent.class.getName(), new
InjectableComponent());
+ registry(Resource3.class);
+ ContainerResponse response = launcher.service("GET", "/a",
"", null, null, null);
+ assertEquals(200, response.getStatus());
+ assertEquals("injected from container", response.getEntity());
+ unregistry(Resource3.class);
+ container.unregisterComponent(InjectableComponent.class.getName());
+ }
}
Show replies by date