[exo-jcr-commits] exo-jcr SVN: r3922 - in ws/trunk: exo.ws.rest.core and 6 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Feb 4 10:02:54 EST 2011
Author: aparfonov
Date: 2011-02-04 10:02:54 -0500 (Fri, 04 Feb 2011)
New Revision: 3922
Added:
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/DependencySupplier.java
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/InjectionProvider.java
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java
Modified:
ws/trunk/exo.ws.rest.core/pom.xml
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ApplicationContext.java
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationContextImpl.java
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationRegistry.java
ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ConstructorDescriptorImpl.java
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/RequestHandlerImpl.java
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/BaseTest.java
ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java
ws/trunk/exo.ws.rest.core/src/test/resources/conf/standalone/test-configuration.xml
ws/trunk/pom.xml
Log:
EXOJCR-1181
Modified: ws/trunk/exo.ws.rest.core/pom.xml
===================================================================
--- ws/trunk/exo.ws.rest.core/pom.xml 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/pom.xml 2011-02-04 15:02:54 UTC (rev 3922)
@@ -104,6 +104,10 @@
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ </dependency>
</dependencies>
<build>
Modified: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ApplicationContext.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ApplicationContext.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ApplicationContext.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -18,6 +18,7 @@
*/
package org.exoplatform.services.rest;
+import org.exoplatform.services.rest.impl.DependencySupplier;
import org.exoplatform.services.rest.impl.ProviderBinder;
import org.exoplatform.services.rest.uri.UriPattern;
@@ -33,7 +34,7 @@
/**
* Provides access to ContainerRequest, ContainerResponse and other context
* information information.
- *
+ *
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id: $
*/
@@ -45,14 +46,14 @@
* in matching at
* {@link org.exoplatform.services.rest.uri.UriPattern#match(String, List)} .
* List will be cleared during matching.
- *
+ *
* @return the list for template values
*/
List<String> getParameterValues();
/**
* Pass in context list of path template parameters .
- *
+ *
* @param parameterNames list of templates parameters
* @see UriPattern
*/
@@ -65,9 +66,9 @@
* the root resource last.
* </p>
* So add each new resource at the begin of list.
- *
+ *
* @param resource the resource e. g. resource class, sub-resource method or
- * sub-resource locator
+ * sub-resource locator
*/
void addMatchedResource(Object resource);
@@ -78,9 +79,9 @@
* resource URI last.
* </p>
* So add each new URI at the begin of list.
- *
+ *
* @param uri the partial part of that matched to resource class,
- * sub-resource method or sub-resource locator
+ * sub-resource method or sub-resource locator
*/
void addMatchedURI(String uri);
@@ -132,6 +133,13 @@
GenericContainerResponse getContainerResponse();
/**
+ * @return instance of actual dependency resolver. It will be used for
+ * injection fields annotated with 'inject' annotation and
+ * constructor parameters not annotated with JAX-RS annotations
+ */
+ DependencySupplier getDependencySupplier();
+
+ /**
* @return set of providers
* @see Providers
*/
Modified: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/ContainerObjectFactory.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -28,7 +28,6 @@
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id: $
*/
-
public class ContainerObjectFactory<T extends ObjectModel> implements ObjectFactory<T>
{
Modified: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationContextImpl.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationContextImpl.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationContextImpl.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -60,7 +60,7 @@
/**
* Set ApplicationContext for current thread.
- *
+ *
* @param context the ApplicationContext.
*/
public static void setCurrent(ApplicationContext context)
@@ -158,21 +158,30 @@
*/
private MultivaluedMap<String, String> queryParameters;
+ private DependencySupplier dependencySupplier;
+
/**
* Constructs new instance of ApplicationContext.
- *
+ *
* @param request See {@link GenricContainerRequest}
* @param response See {@link GenericContainerResponse}
* @param providerBinder
*/
public ApplicationContextImpl(GenericContainerRequest request, GenericContainerResponse response,
- ProviderBinder providers)
+ ProviderBinder providers, DependencySupplier dependencySupplier)
{
this.request = request;
this.response = response;
this.providers = providers;
+ this.dependencySupplier = dependencySupplier;
}
+ public ApplicationContextImpl(GenericContainerRequest request, GenericContainerResponse response,
+ ProviderBinder providers)
+ {
+ this(request, response, providers, new DependencySupplier());
+ }
+
/**
* {@inheritDoc}
*/
@@ -253,6 +262,19 @@
/**
* {@inheritDoc}
*/
+ public DependencySupplier getDependencySupplier()
+ {
+ return dependencySupplier;
+ }
+
+ public void setDependencySupplier(DependencySupplier dependencySupplier)
+ {
+ this.dependencySupplier = dependencySupplier;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public HttpHeaders getHttpHeaders()
{
return request;
@@ -355,8 +377,8 @@
{
if (!pathParameters.containsKey(key))
{
- pathParameters.putSingle(UriComponent.decode(key, UriComponent.PATH_SEGMENT), UriComponent.decode(
- encodedPathParameters.getFirst(key), UriComponent.PATH));
+ pathParameters.putSingle(UriComponent.decode(key, UriComponent.PATH_SEGMENT),
+ UriComponent.decode(encodedPathParameters.getFirst(key), UriComponent.PATH));
}
}
}
Modified: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationRegistry.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationRegistry.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ApplicationRegistry.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -86,7 +86,7 @@
}
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked", "rawtypes"})
public void addApplication(Application app)
{
String applicationId = app.getClass().getName();
@@ -167,5 +167,4 @@
public void stop()
{
}
-
}
Modified: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ConstructorDescriptorImpl.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ConstructorDescriptorImpl.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/ConstructorDescriptorImpl.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -18,8 +18,6 @@
*/
package org.exoplatform.services.rest.impl;
-import org.exoplatform.container.ExoContainer;
-import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.ApplicationContext;
@@ -54,26 +52,15 @@
*/
public class ConstructorDescriptorImpl implements ConstructorDescriptor
{
-
- /**
- * Logger.
- */
+ /** Logger. */
private static final Log LOG = ExoLogger.getLogger("exo.ws.rest.core.ConstructorDescriptorImpl");
- /**
- * ConstructorDescriptor comparator.
- */
+ /** ConstructorDescriptor comparator. */
public static final Comparator<ConstructorDescriptor> CONSTRUCTOR_COMPARATOR = new ConstructorComparator();
- /**
- * Compare two ConstructorDescriptor in number parameters order.
- */
+ /** Compare two ConstructorDescriptor in number parameters order. */
private static class ConstructorComparator implements Comparator<ConstructorDescriptor>
{
-
- /**
- * {@inheritDoc}
- */
public int compare(ConstructorDescriptor o1, ConstructorDescriptor o2)
{
int r = o2.getParameters().size() - o1.getParameters().size();
@@ -83,19 +70,13 @@
}
}
- /**
- * Constructor.
- */
+ /** Constructor. */
private final Constructor<?> constructor;
- /**
- * Collection of constructor's parameters.
- */
+ /** Collection of constructor's parameters. */
private final List<ConstructorParameter> parameters;
- /**
- * Resource class.
- */
+ /** Resource class. */
private final Class<?> resourceClass;
/**
@@ -111,20 +92,16 @@
if (paramTypes.length == 0)
{
-
parameters = java.util.Collections.emptyList();
-
}
else
{
-
Type[] getParamTypes = constructor.getGenericParameterTypes();
Annotation[][] annotations = constructor.getParameterAnnotations();
List<ConstructorParameter> params = new ArrayList<ConstructorParameter>(paramTypes.length);
for (int i = 0; i < paramTypes.length; i++)
{
-
String defaultValue = null;
Annotation annotation = null;
boolean encoded = false;
@@ -142,7 +119,6 @@
Class<?> ac = a.annotationType();
if (allowedAnnotation.contains(ac.getName()))
{
-
if (annotation == null)
{
annotation = a;
@@ -154,15 +130,12 @@
+ annotation + " and " + a + " can't be applied to one parameter.";
throw new RuntimeException(msg);
}
-
- // @Encoded has not sense for Provider. Provider may use only
- // @Context annotation for constructor parameters
+ // @Encoded has not sense for Provider. Provider may use only @Context annotation for constructor parameters
}
else if (ac == Encoded.class && !provider)
{
encoded = true;
- // @Default has not sense for Provider. Provider may use only
- // @Context annotation for constructor parameters
+ // @Default has not sense for Provider. Provider may use only @Context annotation for constructor parameters
}
else if (ac == DefaultValue.class && !provider)
{
@@ -186,7 +159,6 @@
parameters = java.util.Collections.unmodifiableList(params);
}
-
}
/**
@@ -218,7 +190,6 @@
*/
public Object createInstance(ApplicationContext context)
{
- ExoContainer container = ExoContainerContext.getCurrentContainer();
Object[] p = new Object[parameters.size()];
int i = 0;
for (ConstructorParameter cp : parameters)
@@ -236,18 +207,18 @@
String msg = "Not able resolve constructor parameter " + cp;
Class<?> ac = a.annotationType();
if (ac == MatrixParam.class || ac == QueryParam.class || ac == PathParam.class)
- throw new WebApplicationException(e, Response.status(Response.Status.NOT_FOUND).entity(msg).type(
- MediaType.TEXT_PLAIN).build());
+ throw new WebApplicationException(e, Response.status(Response.Status.NOT_FOUND).entity(msg)
+ .type(MediaType.TEXT_PLAIN).build());
- throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity(msg).type(
- MediaType.TEXT_PLAIN).build());
+ throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity(msg)
+ .type(MediaType.TEXT_PLAIN).build());
}
}
else
{
// If parameter not has not annotation then get constructor parameter
- // from container, this is out of scope JAX-RS specification.
- Object tmp = container.getComponentInstanceOfType(cp.getParameterClass());
+ // try to find it via DependencySupplier, this is out of scope JAX-RS specification.
+ Object tmp = context.getDependencySupplier().getComponent(cp);
if (tmp == null)
{
@@ -314,5 +285,4 @@
sb.append(" ]");
return sb.toString();
}
-
}
Added: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/DependencySupplier.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/DependencySupplier.java (rev 0)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/DependencySupplier.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import org.exoplatform.container.ExoContainer;
+import org.exoplatform.container.ExoContainerContext;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.ValueParam;
+import org.exoplatform.services.rest.FieldInjector;
+import org.exoplatform.services.rest.Parameter;
+
+import java.lang.annotation.Annotation;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Provide objects that required for constructors or fields of Resource or
+ * Provider.
+ *
+ * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class DependencySupplier
+{
+ protected final Class<? extends Annotation> injectAnnotationClass;
+
+ protected DependencySupplier(Class<? extends Annotation> injectAnnotationClass)
+ {
+ this.injectAnnotationClass = injectAnnotationClass;
+ }
+
+ // Default.
+ public DependencySupplier()
+ {
+ this(javax.inject.Inject.class);
+ }
+
+ // For eXo Container usage.
+ public DependencySupplier(InitParams params)
+ {
+ this(findInjectAnnotationClass(params));
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private static Class<? extends Annotation> findInjectAnnotationClass(InitParams params)
+ {
+ Class<? extends Annotation> injectAnnotationClass = null;
+ if (params != null)
+ {
+ final ValueParam injectAnnotationParameter = params.getValueParam("inject.annotation.class");
+ try
+ {
+ injectAnnotationClass = AccessController.doPrivileged(new PrivilegedExceptionAction<Class>() {
+ public Class run() throws ClassNotFoundException
+ {
+ return Thread.currentThread().getContextClassLoader().loadClass(injectAnnotationParameter.getValue());
+ }
+ });
+ }
+ catch (PrivilegedActionException pe)
+ {
+ ClassNotFoundException c = (ClassNotFoundException)pe.getCause();
+ throw new RuntimeException(c.getMessage());
+ }
+ }
+ if (injectAnnotationClass == null)
+ injectAnnotationClass = javax.inject.Inject.class;
+ return injectAnnotationClass;
+ }
+
+ /**
+ * Instance for initialization <code>parameter</code>.
+ *
+ * @param parameter parameter
+ * @return instance or <code>null</code> if required instance can't be
+ * provided
+ */
+ public final Object getComponent(Parameter parameter)
+ {
+ if (parameter instanceof FieldInjector)
+ {
+ for (Annotation a : parameter.getAnnotations())
+ if (injectAnnotationClass.isInstance(a))
+ return getComponent(parameter.getParameterClass());
+ return null;
+ }
+ // Annotation required for fields only.
+ return getComponent(parameter.getParameterClass());
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ protected Object getComponent(Class<?> parameterClass)
+ {
+ ExoContainer container = ExoContainerContext.getCurrentContainer();
+ List injectionProviders = container.getComponentInstancesOfType(InjectionProvider.class);
+ if (injectionProviders != null && injectionProviders.size() > 0)
+ {
+ for (Iterator i = injectionProviders.iterator(); i.hasNext();)
+ {
+ InjectionProvider provider = (InjectionProvider)i.next();
+ if (provider.isSupported(parameterClass))
+ return javax.inject.Provider.class.isAssignableFrom(parameterClass) ? provider : provider.get();
+ }
+ }
+ // Directly look up component in container by class,
+ return container.getComponentInstanceOfType(parameterClass);
+ }
+}
Property changes on: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/DependencySupplier.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
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-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -49,20 +49,13 @@
*/
public class FieldInjectorImpl implements FieldInjector
{
-
- /**
- * Logger.
- */
+ /** Logger. */
private static final Log LOG = ExoLogger.getLogger("exo.ws.rest.core.FieldInjectorImpl");
- /**
- * All annotations including JAX-RS annotation.
- */
+ /** All annotations including JAX-RS annotation. */
private final Annotation[] annotations;
- /**
- * JAX-RS annotation.
- */
+ /** JAX-RS annotation. */
private final Annotation annotation;
/**
@@ -72,9 +65,7 @@
*/
private final String defaultValue;
- /**
- * See {@link javax.ws.rs.Encoded}.
- */
+ /** See {@link javax.ws.rs.Encoded}. */
private final boolean encoded;
/** See {@link java.lang.reflect.Field} . */
@@ -86,7 +77,6 @@
*/
public FieldInjectorImpl(Class<?> resourceClass, java.lang.reflect.Field jfield)
{
-
this.jfield = jfield;
this.annotations = jfield.getDeclaredAnnotations();
@@ -119,15 +109,12 @@
+ annotation.toString() + " and " + a.toString() + " can't be applied to one field.";
throw new RuntimeException(msg);
}
-
- // @Encoded has not sense for Provider. Provider may use only @Context
- // annotation for fields
+ // @Encoded has not sense for Provider. Provider may use only @Context annotation for fields
}
else if (ac == Encoded.class && !provider)
{
encoded = true;
- // @Default has not sense for Provider. Provider may use only @Context
- // annotation for fields
+ // @Default has not sense for Provider. Provider may use only @Context annotation for fields
}
else if (ac == DefaultValue.class && !provider)
{
@@ -143,7 +130,6 @@
this.defaultValue = defaultValue;
this.annotation = annotation;
this.encoded = encoded || resourceClass.getAnnotation(Encoded.class) != null;
-
}
/**
@@ -221,22 +207,43 @@
return null;
}
});
-
}
-
jfield.set(resource, pr.resolve(this, context));
}
catch (Throwable e)
{
-
Class<?> ac = annotation.annotationType();
if (ac == MatrixParam.class || ac == QueryParam.class || ac == PathParam.class)
throw new WebApplicationException(e, Response.status(Response.Status.NOT_FOUND).build());
-
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).build());
}
}
-
+ else
+ {
+ Object tmp = context.getDependencySupplier().getComponent(this);
+ if (tmp != null)
+ {
+ try
+ {
+ 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)
+ {
+ throw new WebApplicationException(e, Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
+ }
+ // TODO Need to throw exception ?????
+ }
+ }
}
/**
@@ -259,5 +266,4 @@
.append("; encoded: " + isEncoded()).append(" ]");
return sb.toString();
}
-
}
Added: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/InjectionProvider.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/InjectionProvider.java (rev 0)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/InjectionProvider.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import javax.inject.Provider;
+
+/**
+ * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
+ * @version $Id$
+ * @see Provider
+ */
+public interface InjectionProvider<O> extends Provider<O>
+{
+ /**
+ * Check is InjectionProvider able to produce instance of class
+ * <code>clazz</code>.
+ *
+ * @param clazz class to be checked
+ * @return <code>true</code> if able to produce class and <code>false</code>
+ * otherwise
+ * @see #get()
+ */
+ boolean isSupported(Class<O> clazz);
+}
Property changes on: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/InjectionProvider.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -80,6 +80,8 @@
*/
private final RequestDispatcher dispatcher;
+ private final DependencySupplier dependencySupplier;
+
public static final String getProperty(String name)
{
return properties.get(name);
@@ -88,23 +90,15 @@
public static final void setProperty(String name, String value)
{
if (value == null)
- {
properties.remove(name);
- }
else
- {
properties.put(name, value);
- }
}
- /**
- * Constructs new instance of {@link RequestHandler}.
- *
- * @param dispatcher See {@link RequestDispatcher}
- * @param params init parameters
- */
- public RequestHandlerImpl(RequestDispatcher dispatcher, InitParams params)
+ public RequestHandlerImpl(RequestDispatcher dispatcher, DependencySupplier dependencySupplier, InitParams params)
{
+ this.dispatcher = dispatcher;
+ this.dependencySupplier = dependencySupplier;
if (params != null)
{
for (Iterator<ValueParam> i = params.getValueParamIterator(); i.hasNext();)
@@ -113,21 +107,32 @@
properties.put(vp.getName(), vp.getValue());
}
}
- this.dispatcher = dispatcher;
}
+ /**
+ * Constructs new instance of {@link RequestHandler}.
+ *
+ * @param dispatcher See {@link RequestDispatcher}
+ * @param params init parameters
+ */
+ public RequestHandlerImpl(RequestDispatcher dispatcher, InitParams params)
+ {
+ this(dispatcher, new DependencySupplier(), params);
+ }
+
// RequestHandler
/**
* {@inheritDoc}
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked", "rawtypes"})
public void handleRequest(GenericContainerRequest request, GenericContainerResponse response) throws Exception
{
try
{
ProviderBinder defaultProviders = ProviderBinder.getInstance();
- ApplicationContextImpl context = new ApplicationContextImpl(request, response, defaultProviders);
+ ApplicationContextImpl context =
+ new ApplicationContextImpl(request, response, defaultProviders, dependencySupplier);
context.getProperties().putAll(properties);
ApplicationContextImpl.setCurrent(context);
@@ -252,7 +257,7 @@
/**
* Create error response with specified status and body message.
- *
+ *
* @param status response status
* @param message response message
* @return response
@@ -271,7 +276,7 @@
/**
* Get JAXR header for response status.
- *
+ *
* @param status response status
* @return JAXRS header or null.
*/
@@ -361,12 +366,10 @@
}
// Register Shutdown Hook for cleaning temporary files.
- SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>()
- {
+ SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
public Void run()
{
- Runtime.getRuntime().addShutdownHook(new Thread()
- {
+ Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run()
{
@@ -388,10 +391,10 @@
/**
* Processing {@link ComponentPlugin} for injection external components.
- *
+ *
* @param plugin See {@link ComponentPlugin}
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"rawtypes"})
public void addPlugin(ComponentPlugin plugin)
{
// NOTE!!! ProviderBinder should be already initialized by ResourceBinder
@@ -426,7 +429,8 @@
}
else if (ExceptionMapperComponentPlugin.class.isAssignableFrom(plugin.getClass()))
{
- Set<Class<? extends ExceptionMapper<?>>> emaps = ((ExceptionMapperComponentPlugin)plugin).getExceptionMappers();
+ Set<Class<? extends ExceptionMapper<?>>> emaps =
+ ((ExceptionMapperComponentPlugin)plugin).getExceptionMappers();
for (Class<? extends ExceptionMapper<?>> mapper : emaps)
providers.addExceptionMapper(mapper);
}
Modified: ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/BaseTest.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/BaseTest.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/BaseTest.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -23,6 +23,7 @@
import org.exoplatform.container.StandaloneContainer;
import org.exoplatform.services.rest.impl.ApplicationContextImpl;
import org.exoplatform.services.rest.impl.ApplicationRegistry;
+import org.exoplatform.services.rest.impl.DependencySupplier;
import org.exoplatform.services.rest.impl.ProviderBinder;
import org.exoplatform.services.rest.impl.ProvidersRegistry;
import org.exoplatform.services.rest.impl.RequestHandlerImpl;
@@ -61,6 +62,8 @@
binder = (ResourceBinder)container.getComponentInstanceOfType(ResourceBinder.class);
requestHandler = (RequestHandlerImpl)container.getComponentInstanceOfType(RequestHandlerImpl.class);
providersRegistry = (ProvidersRegistry)container.getComponentInstanceOfType(ProvidersRegistry.class);
+ DependencySupplier dependencySupplier =
+ (DependencySupplier)container.getComponentInstanceOfType(DependencySupplier.class);
// reset default providers to be sure it is clean.
ProviderBinder.setInstance(new ProviderBinder());
@@ -68,7 +71,8 @@
binder.clear();
- ApplicationContextImpl.setCurrent(new ApplicationContextImpl(null, null, providers));
+ ApplicationContextImpl.setCurrent(new ApplicationContextImpl(null, null, providers, dependencySupplier));
+
launcher = new ResourceLauncher(requestHandler);
}
Added: 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 (rev 0)
+++ ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import org.exoplatform.services.rest.BaseTest;
+
+import javax.inject.Inject;
+import javax.inject.Provider;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+/**
+ * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class InjectAnnotationTest extends BaseTest
+{
+ public static class InjectableComponent
+ {
+ public String message = "injected from container";
+ }
+
+ public static class InjectionProvider0 implements InjectionProvider<InjectableComponent>
+ {
+ public InjectableComponent get()
+ {
+ InjectableComponent injectable = new InjectableComponent();
+ injectable.message = "injected from provider";
+ return injectable;
+ }
+
+ public boolean isSupported(Class<InjectableComponent> clazz)
+ {
+ return InjectableComponent.class.isAssignableFrom(clazz);
+ }
+ }
+
+ @Path("a")
+ public static class Resource1
+ {
+ @Inject
+ private InjectableComponent injected;
+
+ @GET
+ public String m()
+ {
+ assertNotNull(injected);
+ return injected.message;
+ }
+ }
+
+ @Path("b")
+ public static class Resource2
+ {
+ @Inject
+ private Provider<InjectableComponent> injected;
+
+ @GET
+ public String m()
+ {
+ assertNotNull(injected);
+ InjectableComponent inst = injected.get();
+ return inst.message;
+ }
+ }
+
+ public void testInjectFromContainer() throws Exception
+ {
+ container.registerComponentInstance(InjectableComponent.class.getName(), new InjectableComponent());
+ registry(Resource1.class);
+ ContainerResponse response = launcher.service("GET", "/a", "", null, null, null);
+ assertEquals(200, response.getStatus());
+ assertEquals("injected from container", response.getEntity());
+ unregistry(Resource1.class);
+ container.unregisterComponent(InjectableComponent.class.getName());
+ }
+
+ public void testInjectFromProvider() throws Exception
+ {
+ container.registerComponentInstance(InjectionProvider0.class.getName(), new InjectionProvider0());
+ registry(Resource1.class);
+ ContainerResponse response = launcher.service("GET", "/a", "", null, null, null);
+ assertEquals(200, response.getStatus());
+ assertEquals("injected from provider", response.getEntity());
+ unregistry(Resource1.class);
+ container.unregisterComponent(InjectionProvider0.class.getName());
+ }
+
+ public void testInjectProvider() throws Exception
+ {
+ container.registerComponentInstance(InjectionProvider0.class.getName(), new InjectionProvider0());
+ registry(Resource2.class);
+ ContainerResponse response = launcher.service("GET", "/b", "", null, null, null);
+ assertEquals(200, response.getStatus());
+ assertEquals("injected from provider", response.getEntity());
+ unregistry(Resource2.class);
+ container.unregisterComponent(InjectionProvider0.class.getName());
+ }
+}
Property changes on: ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/InjectAnnotationTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java
===================================================================
--- ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/test/java/org/exoplatform/services/rest/impl/provider/JsonEntityProviderTest.java 2011-02-04 15:02:54 UTC (rev 3922)
@@ -19,7 +19,6 @@
package org.exoplatform.services.rest.impl.provider;
import org.exoplatform.services.rest.BaseTest;
-import org.exoplatform.services.rest.RequestHandler;
import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
import java.io.ByteArrayInputStream;
@@ -40,14 +39,14 @@
private static final String DATA = "{\"name\":\"andrew\", \"password\":\"hello\"}";
- private RequestHandler requestHandler;
+ //private RequestHandler requestHandler;
private MediaType mediaType;
public void setUp() throws Exception
{
super.setUp();
- requestHandler = (RequestHandler)container.getComponentInstanceOfType(RequestHandler.class);
+ //requestHandler = (RequestHandler)container.getComponentInstanceOfType(RequestHandler.class);
assertNotNull(requestHandler);
mediaType = new MediaType("application", "json");
}
Modified: ws/trunk/exo.ws.rest.core/src/test/resources/conf/standalone/test-configuration.xml
===================================================================
--- ws/trunk/exo.ws.rest.core/src/test/resources/conf/standalone/test-configuration.xml 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/exo.ws.rest.core/src/test/resources/conf/standalone/test-configuration.xml 2011-02-04 15:02:54 UTC (rev 3922)
@@ -64,6 +64,9 @@
<type>org.exoplatform.services.rest.impl.RequestHandlerImpl</type>
</component>
<component>
+ <type>org.exoplatform.services.rest.impl.DependencySupplier</type>
+ </component>
+ <component>
<type>org.exoplatform.services.rest.impl.RequestDispatcher</type>
</component>
<component>
Modified: ws/trunk/pom.xml
===================================================================
--- ws/trunk/pom.xml 2011-02-04 13:39:49 UTC (rev 3921)
+++ ws/trunk/pom.xml 2011-02-04 15:02:54 UTC (rev 3922)
@@ -184,6 +184,11 @@
<artifactId>groovy-all</artifactId>
<version>1.6.5</version>
</dependency>
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <version>1</version>
+ </dependency>
</dependencies>
</dependencyManagement>
<dependencies>
More information about the exo-jcr-commits
mailing list