cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1082225 - in /cxf/branches/2.3.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/src/...
Date Wed, 16 Mar 2011 17:23:00 GMT
Author: sergeyb
Date: Wed Mar 16 17:22:59 2011
New Revision: 1082225

URL: http://svn.apache.org/viewvc?rev=1082225&view=rev
Log:
Merged revisions 1082223 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1082223 | sergeyb | 2011-03-16 17:18:05 +0000 (Wed, 16 Mar 2011) | 1 line
  
  [CXF-3379] Support for the injection of JAX-RS Applications
........

Added:
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerApplication.java
      - copied unchanged from r1082223, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/CustomerApplication.java
Modified:
    cxf/branches/2.3.x-fixes/   (props changed)
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
    svn:mergeinfo = /cxf/trunk:1082223

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
Wed Mar 16 17:22:59 2011
@@ -25,6 +25,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.core.Application;
+
 import org.apache.cxf.BusException;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.endpoint.EndpointException;
@@ -64,6 +66,7 @@ public class JAXRSServerFactoryBean exte
     private Map<Object, Object> languageMappings;
     private Map<Object, Object> extensionMappings;
     private ResourceComparator rc;
+    private Application application;
     
     public JAXRSServerFactoryBean() {
         this(new JAXRSServiceFactoryBean());
@@ -74,6 +77,14 @@ public class JAXRSServerFactoryBean exte
     }
     
     /**
+     * Saves the reference to the JAX-RS {@link Application}
+     * @param app
+     */
+    public void setApplication(Application app) {
+        application = app;    
+    }
+    
+    /**
      * Resource comparator which may be used to customize the way 
      * a root resource or resource method is selected 
      * @param rcomp comparator
@@ -121,6 +132,7 @@ public class JAXRSServerFactoryBean exte
                 ep.getService().setInvoker(invoker);
             }
             
+            ep.put(Application.class.getName(), application);
             ProviderFactory factory = setupFactory(ep);
             factory.setRequestPreprocessor(
                 new RequestPreprocessor(languageMappings, extensionMappings));
@@ -305,8 +317,9 @@ public class JAXRSServerFactoryBean exte
     private void injectContexts() {
         for (ClassResourceInfo cri : serviceFactory.getClassResourceInfo()) {
             if (cri.isSingleton()) {
-                InjectionUtils.injectContextProxies(cri, 
-                                                    cri.getResourceProvider().getInstance(null));
+                InjectionUtils.injectContextProxiesAndApplication(cri, 
+                                                    cri.getResourceProvider().getInstance(null),
+                                                    application);
             }
         }
     }

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
Wed Mar 16 17:22:59 2011
@@ -29,6 +29,7 @@ import java.util.List;
 import java.util.Map;
 
 import javax.annotation.Resource;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Context;
 
 import org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy;
@@ -81,23 +82,15 @@ public abstract class AbstractResourceIn
         for (Field f : cls.getDeclaredFields()) {
             for (Annotation a : f.getAnnotations()) {
                 if (a.annotationType() == Context.class) {
-                    if (contextFields == null) {
-                        contextFields = new HashMap<Class<?>, List<Field>>();
+                    contextFields = addContextField(contextFields, f);
+                    if (f.getType() != Application.class) {
+                        fieldProxyMap = getProxyMap(Field.class, fieldProxyMap);
+                        addToMap(fieldProxyMap, f, InjectionUtils.createThreadLocalProxy(f.getType()));
                     }
-                    addContextField(contextFields, f);
-                    if (fieldProxyMap == null) {
-                        fieldProxyMap = new HashMap<Class<?>, Map<Field, ThreadLocalProxy>>();
-                    }
-                    addToMap(fieldProxyMap, f, InjectionUtils.createThreadLocalProxy(f.getType()));
                 } else if (a.annotationType() == Resource.class 
                            && AnnotationUtils.isContextClass(f.getType())) {
-                    if (resourceFields == null) {
-                        resourceFields = new HashMap<Class<?>, List<Field>>();
-                    }
-                    addContextField(resourceFields, f);
-                    if (resourceProxyMap == null) {
-                        resourceProxyMap = new HashMap<Class<?>, Map<Field, ThreadLocalProxy>>();
-                    }
+                    resourceFields = addContextField(resourceFields, f);
+                    resourceProxyMap = getProxyMap(Field.class, resourceProxyMap);
                     addToMap(resourceProxyMap, f, InjectionUtils.createThreadLocalProxy(f.getType()));
                 }
             }
@@ -105,6 +98,11 @@ public abstract class AbstractResourceIn
         findContextFields(cls.getSuperclass());
     }
     
+    private <T> Map<Class<?>, Map<T, ThreadLocalProxy>> getProxyMap(Class<T>
keyClass,
+        Map<Class<?>, Map<T, ThreadLocalProxy>> map) {
+        return map == null ? new HashMap<Class<?>, Map<T, ThreadLocalProxy>>()
: map;
+    }
+    
     private void findContextSetterMethods(Class<?> cls) {
         
         for (Method m : cls.getMethods()) {
@@ -145,11 +143,11 @@ public abstract class AbstractResourceIn
             contextMethods = new HashMap<Class<?>, Map<Class<?>, Method>>();
         }
         addToMap(contextMethods, contextClass, m);
-        if (setterProxyMap == null) {
-            setterProxyMap = new HashMap<Class<?>, Map<Method, ThreadLocalProxy>>();
+        if (m.getParameterTypes()[0] != Application.class) {
+            setterProxyMap = getProxyMap(Method.class, setterProxyMap);
+            addToMap(setterProxyMap, m, 
+                     InjectionUtils.createThreadLocalProxy(m.getParameterTypes()[0]));
         }
-        addToMap(setterProxyMap, m, 
-                 InjectionUtils.createThreadLocalProxy(m.getParameterTypes()[0]));
     }
     
     public boolean isRoot() {
@@ -200,7 +198,10 @@ public abstract class AbstractResourceIn
         }
     }
     
-    private void addContextField(Map<Class<?>, List<Field>> theFields,
Field f) {
+    private Map<Class<?>, List<Field>> addContextField(Map<Class<?>,
List<Field>> theFields, Field f) {
+        
+        theFields = theFields == null ? new HashMap<Class<?>, List<Field>>()
: theFields;
+        
         List<Field> fields = theFields.get(serviceClass);
         if (fields == null) {
             fields = new ArrayList<Field>();
@@ -209,6 +210,7 @@ public abstract class AbstractResourceIn
         if (!fields.contains(f)) {
             fields.add(f);
         }
+        return theFields;
     }
     
     private <T, V> void addToMap(Map<Class<?>, Map<T, V>> theFields,


Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
Wed Mar 16 17:22:59 2011
@@ -41,6 +41,7 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Request;
@@ -80,6 +81,7 @@ public final class AnnotationUtils {
         classes.add(ContextResolver.class);
         classes.add(Providers.class);
         classes.add(Request.class);
+        classes.add(Application.class);
         // Servlet API
         try {
             classes.add(HttpServletRequest.class);

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
Wed Mar 16 17:22:59 2011
@@ -48,6 +48,7 @@ import java.util.TreeSet;
 import java.util.logging.Logger;
 
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
@@ -769,19 +770,23 @@ public final class InjectionUtils {
         }
     }
     
-    public static void injectContextProxies(AbstractResourceInfo cri, Object instance) {
+    public static void injectContextProxiesAndApplication(AbstractResourceInfo cri, 
+                                                          Object instance,
+                                                          Application app) {
         if (!cri.isSingleton()) {
             return;
         }
         
         for (Map.Entry<Class<?>, Method> entry : cri.getContextMethods().entrySet())
{
-            ThreadLocalProxy proxy = cri.getContextSetterProxy(entry.getValue());
-            InjectionUtils.injectThroughMethod(instance, entry.getValue(), proxy);
+            Method method = entry.getValue();
+            Object value = method.getParameterTypes()[0] == Application.class 
+                ? app : cri.getContextSetterProxy(method);
+            InjectionUtils.injectThroughMethod(instance, method, value);
         }
         
         for (Field f : cri.getContextFields()) {
-            ThreadLocalProxy proxy = cri.getContextFieldProxy(f);
-            InjectionUtils.injectFieldValue(f, instance, proxy);
+            Object value = f.getType() == Application.class ? app : cri.getContextFieldProxy(f);
+            InjectionUtils.injectFieldValue(f, instance, value);
         }
         
         for (Field f : cri.getResourceFields()) {
@@ -790,6 +795,10 @@ public final class InjectionUtils {
         }
     }
     
+    public static void injectContextProxies(AbstractResourceInfo cri, Object instance) {
+        injectContextProxiesAndApplication(cri, instance, null);
+    }
+    
     @SuppressWarnings("unchecked")
     public static void injectContextField(AbstractResourceInfo cri, 
                                           Field f, Object o, Object value,
@@ -809,16 +818,21 @@ public final class InjectionUtils {
     public static void injectContextMethods(Object requestObject,
                                             AbstractResourceInfo cri,
                                             Message message) {
+        
         for (Map.Entry<Class<?>, Method> entry : cri.getContextMethods().entrySet())
{
+            Method method = entry.getValue();
+            if (method.getParameterTypes()[0] == Application.class && cri.isSingleton())
{
+                continue;
+            }
             Object o = JAXRSUtils.createContextValue(message, 
-                                              entry.getValue().getGenericParameterTypes()[0],
+                                              method.getGenericParameterTypes()[0],
                                               entry.getKey());
             
             if (o != null) {
                 if (!cri.isSingleton()) {
-                    InjectionUtils.injectThroughMethod(requestObject, entry.getValue(), o);
+                    InjectionUtils.injectThroughMethod(requestObject, method, o);
                 } else {
-                    ThreadLocalProxy proxy = cri.getContextSetterProxy(entry.getValue());
+                    ThreadLocalProxy proxy = cri.getContextSetterProxy(method);
                     if (proxy != null) {
                         proxy.set(o);
                     }
@@ -835,6 +849,9 @@ public final class InjectionUtils {
                                            Message m) {
         
         for (Field f : cri.getContextFields()) {
+            if (f.getType() == Application.class && cri.isSingleton()) {
+                continue;
+            }
             Object value = JAXRSUtils.createContextValue(m, f.getGenericType(), f.getType());
             InjectionUtils.injectContextField(cri, f, o, value, false);
         }

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
Wed Mar 16 17:22:59 2011
@@ -49,6 +49,7 @@ import javax.servlet.http.HttpServletRes
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -812,6 +813,8 @@ public final class JAXRSUtils {
             o = new MessageContextImpl(m);
         } else if (SearchContext.class.isAssignableFrom(clazz)) {
             o = new SearchContextImpl(m);
+        } else if (Application.class.isAssignableFrom(clazz)) {
+            o = contextMessage.getExchange().getEndpoint().get(Application.class.getName());
         }
         if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage))
{
             o = createServletResourceValue(contextMessage, clazz);
@@ -861,8 +864,8 @@ public final class JAXRSUtils {
             value = m.get(AbstractHTTPDestination.HTTP_REQUEST);
         }
         if (HttpServletResponse.class.isAssignableFrom(clazz)) {
-            value = new HttpServletResponseFilter(
-                            (HttpServletResponse)m.get(AbstractHTTPDestination.HTTP_RESPONSE),
m);
+            HttpServletResponse response = (HttpServletResponse)m.get(AbstractHTTPDestination.HTTP_RESPONSE);
+            value = response != null ? new HttpServletResponseFilter(response, m) : null;
         }
         if (ServletContext.class.isAssignableFrom(clazz)) {
             value = m.get(AbstractHTTPDestination.HTTP_CONTEXT);

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
Wed Mar 16 17:22:59 2011
@@ -592,6 +592,7 @@ public final class ResourceUtils {
         for (Map.Entry<Class, ResourceProvider> entry : map.entrySet()) {
             bean.setResourceProvider(entry.getKey(), entry.getValue());
         }
+        bean.setApplication(app);
         
         return bean;
     }

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
Wed Mar 16 17:22:59 2011
@@ -34,12 +34,14 @@ import javax.ws.rs.Consumes;
 import javax.ws.rs.CookieParam;
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
 import javax.ws.rs.MatrixParam;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
@@ -100,6 +102,9 @@ public class Customer extends AbstractCu
     @Context private Request request;
     @Context private SecurityContext sContext;
     @Context private Providers bodyWorkers;
+    @Context private Application application1;
+    private Application application2;
+    private int applicationInjections;
     
     @Resource private HttpServletRequest servletRequest;
     @Resource private HttpServletResponse servletResponse;
@@ -176,6 +181,23 @@ public class Customer extends AbstractCu
         
     }
     
+    public Application getApplication1() {
+        return application1;
+    }
+    
+    public Application getApplication2() {
+        if (applicationInjections > 1) {
+            throw new RuntimeException();
+        }
+        return application2;
+    }
+    
+    @Context
+    public void setApplication(Application app) {
+        applicationInjections++;
+        application2 = app;
+    }
+    
     public UriInfo getUriInfo() {
         return uriInfo;
     }
@@ -255,6 +277,7 @@ public class Customer extends AbstractCu
 
     @Produces("text/xml")
     @Consumes("text/xml")
+    @GET
     public void test() {
         // complete
     }

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java?rev=1082225&r1=1082224&r2=1082225&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
Wed Mar 16 17:22:59 2011
@@ -33,6 +33,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -46,11 +47,15 @@ import javax.ws.rs.ext.Providers;
 import javax.xml.bind.JAXBContext;
 
 import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxrs.Customer;
 import org.apache.cxf.jaxrs.Customer2;
+import org.apache.cxf.jaxrs.CustomerApplication;
 import org.apache.cxf.jaxrs.CustomerGender;
 import org.apache.cxf.jaxrs.CustomerParameterHandler;
 import org.apache.cxf.jaxrs.JAXBContextProvider;
+import org.apache.cxf.jaxrs.JAXRSInvoker;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
 import org.apache.cxf.jaxrs.JAXRSServiceImpl;
 import org.apache.cxf.jaxrs.SimpleFactory;
@@ -113,6 +118,57 @@ public class JAXRSUtilsTest extends Asse
     }
     
     @Test
+    public void testInjectApplicationInSingleton() throws Exception {
+        Application app = new CustomerApplication();
+        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        Customer customer = new Customer();
+        sf.setServiceBeanObjects(customer);
+        sf.setApplication(app);
+        sf.setStart(false);
+        Server server = sf.create();  
+        assertSame(app, customer.getApplication1());
+        assertSame(app, customer.getApplication2());
+        invokeCustomerMethod(sf.getServiceFactory().getClassResourceInfo().get(0),
+                             customer, server);
+        assertSame(app, customer.getApplication2());
+    }
+    
+    @Test
+    public void testInjectApplicationInPerRequestResource() throws Exception {
+        Application app = new CustomerApplication();
+        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        sf.setServiceClass(Customer.class);
+        sf.setApplication(app);
+        sf.setStart(false);
+        Server server = sf.create();  
+        
+        ClassResourceInfo cri = sf.getServiceFactory().getClassResourceInfo().get(0);
+        
+        Customer customer = (Customer)cri.getResourceProvider().getInstance(
+             new MessageImpl());
+        
+        assertNull(customer.getApplication1());
+        assertNull(customer.getApplication2());
+        
+        invokeCustomerMethod(cri, customer, server);
+        assertSame(app, customer.getApplication1());
+        assertSame(app, customer.getApplication2());
+    }
+    
+    private void invokeCustomerMethod(ClassResourceInfo cri, 
+        Customer customer, Server server) throws Exception {
+        OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(
+            Customer.class.getMethod("test", new Class[]{}));
+        JAXRSInvoker invoker = new JAXRSInvoker();
+        Exchange exc = new ExchangeImpl();
+        exc.put(Endpoint.class, server.getEndpoint());
+        Message inMessage = new MessageImpl();
+        exc.setInMessage(inMessage);
+        exc.put(OperationResourceInfo.class, ori);
+        invoker.invoke(exc, Collections.emptyList(), customer);
+    }
+    
+    @Test
     public void testSelectBetweenMultipleResourceClasses2() throws Exception {
         JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
         sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class,
@@ -1385,7 +1441,7 @@ public class JAXRSUtilsTest extends Asse
         Customer c = new Customer();
         OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0],
                                                               cri);
-        Message m = new MessageImpl();
+        Message m = createMessage();
         MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
         headers.add("AHeader2", "theAHeader2");
         m.put(Message.PROTOCOL_HEADERS, headers);
@@ -1402,7 +1458,8 @@ public class JAXRSUtilsTest extends Asse
         Customer c = new Customer();
         OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0],
                                                               cri);
-        Message m = new MessageImpl();
+        Message m = createMessage();
+        
         MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
         headers.add("AHeader", "theAHeader");
         m.put(Message.PROTOCOL_HEADERS, headers);
@@ -1411,7 +1468,7 @@ public class JAXRSUtilsTest extends Asse
         assertEquals("bValue", c.getB());
         assertEquals("theAHeader", c.getAHeader());
     }
-
+    
     @Test
     public void testContextResolverParam() throws Exception {
         
@@ -1554,6 +1611,8 @@ public class JAXRSUtilsTest extends Asse
         Endpoint endpoint = EasyMock.createMock(Endpoint.class);
         endpoint.getEndpointInfo();
         EasyMock.expectLastCall().andReturn(null).anyTimes();
+        endpoint.get(Application.class.getName());
+        EasyMock.expectLastCall().andReturn(null);
         endpoint.size();
         EasyMock.expectLastCall().andReturn(0).anyTimes();
         endpoint.isEmpty();



Mime
View raw message