cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5964] The injection of contexts into Application needs to be done earlier
Date Thu, 21 Aug 2014 15:42:29 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes d781d9212 -> deca47d1f


[CXF-5964] The injection of contexts into Application needs to be done earlier


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/deca47d1
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/deca47d1
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/deca47d1

Branch: refs/heads/3.0.x-fixes
Commit: deca47d1ff49f43ecd137f3a1a04e771fe1a7712
Parents: d781d92
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Thu Aug 21 16:38:27 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Thu Aug 21 16:41:42 2014 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/JAXRSServerFactoryBean.java       |  9 +++--
 .../apache/cxf/jaxrs/model/ProviderInfo.java    | 15 ++++++--
 .../cxf/jaxrs/provider/ProviderFactory.java     | 12 ++++---
 .../jaxrs/servlet/CXFNonSpringJaxrsServlet.java | 38 +++++++++++---------
 .../cxf/systest/jaxrs/BookApplication.java      |  5 +--
 .../org/apache/cxf/systest/jaxrs/BookStore.java | 12 +++++++
 .../JAXRSClientServerNonSpringBookTest.java     |  9 +++++
 7 files changed, 73 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
index a49e0a3..1f6bc1e 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
@@ -97,8 +97,13 @@ public class JAXRSServerFactoryBean extends AbstractJAXRSFactoryBean {
      * @param app
      */
     public void setApplication(Application app) {
-        appProvider = new ProviderInfo<Application>(app, getBus());
-        Set<String> appNameBindings = AnnotationUtils.getNameBindings(app.getClass().getAnnotations());
+        setApplication(new ProviderInfo<Application>(app, getBus()));
+    }
+    
+    public void setApplication(ProviderInfo<Application> provider) {
+        appProvider = provider;
+        Set<String> appNameBindings = AnnotationUtils.getNameBindings(provider.getProvider()
+                                                                      .getClass().getAnnotations());
         for (ClassResourceInfo cri : getServiceFactory().getClassResourceInfo()) {
             Set<String> clsNameBindings = new LinkedHashSet<String>(appNameBindings);
             clsNameBindings.addAll(AnnotationUtils.getNameBindings(cri.getServiceClass().getAnnotations()));

http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java
index 0070367..afabd3c 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ProviderInfo.java
@@ -29,13 +29,24 @@ public class ProviderInfo<T> extends AbstractResourceInfo {
     private T provider;
     
     public ProviderInfo(T provider, Bus bus) {
-        this(provider, null, bus);
+        this(provider, bus, true);
+    }
+    
+    public ProviderInfo(T provider, Bus bus, boolean checkContexts) {
+        this(provider, null, bus, checkContexts);
     }
     
     public ProviderInfo(T provider, 
                         Map<Class<?>, ThreadLocalProxy<?>> constructorProxies,

                         Bus bus) {
-        super(provider.getClass(), provider.getClass(), true, true, constructorProxies, bus,
provider);
+        this(provider, constructorProxies, bus, true);
+    }
+    
+    public ProviderInfo(T provider, 
+                        Map<Class<?>, ThreadLocalProxy<?>> constructorProxies,

+                        Bus bus,
+                        boolean checkContexts) {
+        super(provider.getClass(), provider.getClass(), true, checkContexts, constructorProxies,
bus, provider);
         this.provider = provider;
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 2a0fed8..ce47262 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -1056,12 +1056,14 @@ public abstract class ProviderFactory {
         }
     }
     
-    protected ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?>
c, 
-                                                                 Map<Class<?>, Object>
values) {
+    public static ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?>
c, 
+                                                                 Map<Class<?>, Object>
values,
+                                                                 Bus theBus,
+                                                                 boolean checkContexts) {
         
         
         Map<Class<?>, Map<Class<?>, ThreadLocalProxy<?>>> proxiesMap
= 
-            CastUtils.cast((Map<?, ?>)getBus().getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
+            CastUtils.cast((Map<?, ?>)theBus.getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
         Map<Class<?>, ThreadLocalProxy<?>> existingProxies = null; 
         if (proxiesMap != null) {
             existingProxies = proxiesMap.get(c.getDeclaringClass());
@@ -1091,7 +1093,7 @@ public abstract class ProviderFactory {
                 proxies.put(paramTypes[i], proxy);
             }
         }
-        return new ProviderInfo<Object>(instance, proxies, getBus()); 
+        return new ProviderInfo<Object>(instance, proxies, theBus, checkContexts);

     }
     
     protected static class NameKey { 
@@ -1223,7 +1225,7 @@ public abstract class ProviderFactory {
             if (o instanceof Constructor) {
                 Map<Class<?>, Object> values = CastUtils.cast(application ==
null ? null 
                     : Collections.singletonMap(Application.class, application.getProvider()));
-                theProviders.add(createProviderFromConstructor((Constructor<?>)o, values));
+                theProviders.add(createProviderFromConstructor((Constructor<?>)o, values,
getBus(), true));
             } else if (o instanceof ProviderInfo) {
                 theProviders.add((ProviderInfo<?>)o);
             } else {    

http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
index 0b3e516..312c383 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java
@@ -44,6 +44,8 @@ import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider;
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.jaxrs.model.ProviderInfo;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
 import org.apache.cxf.message.Message;
@@ -369,20 +371,21 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
         if (c == null) {
             throw new ServletException("No valid constructor found for " + cls.getName());
         }
-        boolean isDefault = c.getParameterTypes().length == 0; 
-        if (!isDefault && (c.getParameterTypes().length != 1 
-            || c.getParameterTypes()[0] != ServletConfig.class
-            && c.getParameterTypes()[0] != ServletContext.class)) {
-            throw new ServletException("Resource classes with singleton scope can only have
"
-                + "ServletConfig or ServletContext instances injected through their constructors");
-        }
-        Object[] values = isDefault ? new Object[]{} 
-            : new Object[]{c.getParameterTypes()[0] == ServletConfig.class ? sc : sc.getServletContext()};

+        boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
         try {
-            Object instance = c.newInstance(values);
+            ProviderInfo<? extends Object> provider = null;
+            if (c.getParameterTypes().length == 0) {
+                provider = new ProviderInfo<Object>(c.newInstance(), getBus(), isApplication);
+            } else {
+                Map<Class<?>, Object> values = new HashMap<Class<?>,
Object>();
+                values.put(ServletContext.class, sc.getServletContext());
+                values.put(ServletConfig.class, sc);
+                provider = ProviderFactory.createProviderFromConstructor(c, values, getBus(),
isApplication);
+            }
+            Object instance = provider.getProvider();
             injectProperties(instance, props);
             configureSingleton(instance);
-            return instance;
+            return isApplication ? provider : instance;
         } catch (InstantiationException ex) {
             ex.printStackTrace();
             throw new ServletException("Resource class " + cls.getName()
@@ -441,9 +444,9 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
         }
         
         for (String cName : classNames) {
-            Application app = createApplicationInstance(cName, servletConfig);
+            ProviderInfo<Application> providerApp = createApplicationInstance(cName,
servletConfig);
             
-            JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, 
+            JAXRSServerFactoryBean bean = ResourceUtils.createApplication(providerApp.getProvider(),

                                                 ignoreApplicationPath,
                                                 getStaticSubResolutionValue(servletConfig));
             String splitChar = getParameterSplitChar(servletConfig);
@@ -452,18 +455,21 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
             setExtensions(bean, servletConfig);
             setDocLocation(bean, servletConfig);
             setSchemasLocations(bean, servletConfig);
-            
             bean.setBus(getBus());
+            bean.setApplication(providerApp);
             bean.create();
         }
     }
     
-    protected Application createApplicationInstance(String appClassName, ServletConfig servletConfig)

+    protected ProviderInfo<Application> createApplicationInstance(String appClassName,
ServletConfig servletConfig) 
         throws ServletException {
         Map<String, List<String>> props = new HashMap<String, List<String>>();
         appClassName = getClassNameAndProperties(appClassName, props);
         Class<?> appClass = loadApplicationClass(appClassName);
-        return (Application)createSingletonInstance(appClass, props, servletConfig);
+        @SuppressWarnings("unchecked")
+        ProviderInfo<Application> provider = 
+            (ProviderInfo<Application>)createSingletonInstance(appClass, props, servletConfig);
+        return provider;
     }
     
     protected Class<?> loadApplicationClass(String appClassName) throws ServletException
{

http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
index d27f97b..6772913 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
@@ -38,7 +38,8 @@ public class BookApplication extends Application {
 
     private String defaultName;
     private long defaultId;
-    
+    @Context
+    private UriInfo uriInfo;
     
     public BookApplication(@Context ServletContext sc) {
         if (sc == null) {
@@ -64,7 +65,7 @@ public class BookApplication extends Application {
     public Set<Object> getSingletons() {
         Set<Object> classes = new HashSet<Object>();
         org.apache.cxf.systest.jaxrs.BookStore store = 
-            new org.apache.cxf.systest.jaxrs.BookStore();
+            new org.apache.cxf.systest.jaxrs.BookStore(uriInfo);
         store.setDefaultNameAndId(defaultName, defaultId);
         classes.add(store);
         BookExceptionMapper mapper = new org.apache.cxf.systest.jaxrs.BookExceptionMapper();

http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index 23f2518..f1331d5 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -120,6 +120,7 @@ public class BookStore {
     private SecurityContext securityContext;
     @Context 
     private UriInfo ui;
+    private UriInfo uiFromConstructor;
     @Context 
     private ResourceContext resourceContext;
     
@@ -131,6 +132,10 @@ public class BookStore {
     public BookStore() {
         init();
     }
+    public BookStore(UriInfo ui) {
+        this.uiFromConstructor = ui;
+        init();
+    }
     
     @PostConstruct
     public void postConstruct() {
@@ -167,6 +172,13 @@ public class BookStore {
     public double[] getBookIndexAsDoubleArray() {
         return new double[]{1, 2, 3};
     }
+    
+    @GET
+    @Path("/uifromconstructor")
+    @Produces("text/plain")
+    public String getPathFromUriInfo() {
+        return uiFromConstructor.getAbsolutePath().toString();
+    }
         
     @GET
     @Path("/redirect")

http://git-wip-us.apache.org/repos/asf/cxf/blob/deca47d1/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
index 8c262cb..7a4e384 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
@@ -69,6 +69,15 @@ public class JAXRSClientServerNonSpringBookTest extends AbstractBusClientServerT
         assertEquals("application/xml+model", wc.getResponse().getMetadata().getFirst("Content-Type"));
         
     }
+    @Test
+    public void testGetPathFromUriInfo() throws Exception {
+        String address = "http://localhost:" + PORT + "/application/bookstore/uifromconstructor";
+        WebClient wc = WebClient.create(address);
+        wc.accept("text/plain");
+        String response = wc.get(String.class);
+        assertEquals(address, response);
+        
+    }
     
     @Test
     public void testGetBook123UserModel() throws Exception {


Mime
View raw message