cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5710] Updating JAX-RS servlet to support loading multiple applications and fixing the issue with the constructor injection of thread local proxies along the way
Date Thu, 24 Apr 2014 12:33:33 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 5e8329447 -> dc4f09b76


[CXF-5710] Updating JAX-RS servlet to support loading multiple applications and fixing the
issue with the constructor injection of thread local proxies along the way


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

Branch: refs/heads/master
Commit: dc4f09b76afa9c5ac2d06d113fb0811f9e4c3594
Parents: 5e83294
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Thu Apr 24 13:33:04 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Thu Apr 24 13:33:04 2014 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/model/AbstractResourceInfo.java   |  2 +-
 .../cxf/jaxrs/provider/ProviderFactory.java     | 22 ++++++++++--
 .../jaxrs/servlet/CXFNonSpringJaxrsServlet.java | 38 +++++++++++++-------
 .../cxf/systest/jaxrs/BookApplication.java      |  8 +++--
 .../cxf/systest/jaxrs/BookApplication2.java     | 30 ++++++++++++++++
 .../JAXRSClientServerNonSpringBookTest.java     | 12 ++++++-
 .../resources/jaxrs_non_spring/WEB-INF/web.xml  | 23 ++++++++++++
 7 files changed, 115 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
index df345cd..2770ef2 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/AbstractResourceInfo.java
@@ -38,9 +38,9 @@ import org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 
 public abstract class AbstractResourceInfo {
+    public static final String CONSTRUCTOR_PROXY_MAP = "jaxrs-constructor-proxy-map";
     private static final String FIELD_PROXY_MAP = "jaxrs-field-proxy-map";
     private static final String SETTER_PROXY_MAP = "jaxrs-setter-proxy-map";
-    private static final String CONSTRUCTOR_PROXY_MAP = "jaxrs-constructor-proxy-map";
     
     protected boolean root;
     protected Class<?> resourceClass;

http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/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 1810743..641f4db 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
@@ -29,8 +29,8 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
@@ -66,6 +66,7 @@ import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.impl.ReaderInterceptorMBR;
 import org.apache.cxf.jaxrs.impl.WriterInterceptorMBW;
 import org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.FilterProviderInfo;
 import org.apache.cxf.jaxrs.model.ProviderInfo;
@@ -1069,7 +1070,23 @@ public abstract class ProviderFactory {
     
     protected ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?>
c, 
                                                                  Map<Class<?>, Object>
values) {
+        
+        
+        Map<Class<?>, Map<Class<?>, ThreadLocalProxy<?>>> proxiesMap
= 
+            CastUtils.cast((Map<?, ?>)getBus().getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
+        Map<Class<?>, ThreadLocalProxy<?>> existingProxies = null; 
+        if (proxiesMap != null) {
+            existingProxies = proxiesMap.get(c.getDeclaringClass());
+        }
+        Class<?>[] paramTypes = c.getParameterTypes();
         Object[] cArgs = ResourceUtils.createConstructorArguments(c, null, false, values);
+        if (existingProxies != null && existingProxies.size() <= paramTypes.length)
{
+            for (int i = 0; i < paramTypes.length; i++) {
+                if (cArgs[i] instanceof ThreadLocalProxy) {
+                    cArgs[i] = existingProxies.get(paramTypes[i]);
+                }
+            }
+        } 
         Object instance = null;
         try {
             instance = c.newInstance(cArgs);
@@ -1078,8 +1095,7 @@ public abstract class ProviderFactory {
                                        + " can not be instantiated"); 
         }
         Map<Class<?>, ThreadLocalProxy<?>> proxies = 
-            new HashMap<Class<?>, ThreadLocalProxy<?>>();
-        Class<?>[] paramTypes = c.getParameterTypes();
+            new LinkedHashMap<Class<?>, ThreadLocalProxy<?>>();
         for (int i = 0; i < paramTypes.length; i++) {
             if (cArgs[i] instanceof ThreadLocalProxy) {
                 @SuppressWarnings("unchecked")

http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/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 b1b0026..0b3e516 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
@@ -427,23 +427,35 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
         
     }
     
-    protected void createServerFromApplication(String cName, ServletConfig servletConfig)

+    protected void createServerFromApplication(String applicationNames, ServletConfig servletConfig)

         throws ServletException {
-        Application app = createApplicationInstance(cName, servletConfig);
         
         String ignoreParam = servletConfig.getInitParameter(IGNORE_APP_PATH_PARAM);
-        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, 
-                                            ignoreParam == null || MessageUtils.isTrue(ignoreParam),
-                                            getStaticSubResolutionValue(servletConfig));
-        String splitChar = getParameterSplitChar(servletConfig);
-        setAllInterceptors(bean, servletConfig, splitChar);
-        setInvoker(bean, servletConfig);
-        setExtensions(bean, servletConfig);
-        setDocLocation(bean, servletConfig);
-        setSchemasLocations(bean, servletConfig);
+        boolean ignoreApplicationPath = ignoreParam == null || MessageUtils.isTrue(ignoreParam);
         
-        bean.setBus(getBus());
-        bean.create();
+        String[] classNames = StringUtils.split(applicationNames, getParameterSplitChar(servletConfig));
+        
+        if (classNames.length > 1 && ignoreApplicationPath) {
+            throw new ServletException("\"" + IGNORE_APP_PATH_PARAM 
+                + "\" parameter must be set to false for multiple Applications be supported");
+        }
+        
+        for (String cName : classNames) {
+            Application app = createApplicationInstance(cName, servletConfig);
+            
+            JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, 
+                                                ignoreApplicationPath,
+                                                getStaticSubResolutionValue(servletConfig));
+            String splitChar = getParameterSplitChar(servletConfig);
+            setAllInterceptors(bean, servletConfig, splitChar);
+            setInvoker(bean, servletConfig);
+            setExtensions(bean, servletConfig);
+            setDocLocation(bean, servletConfig);
+            setSchemasLocations(bean, servletConfig);
+            
+            bean.setBus(getBus());
+            bean.create();
+        }
     }
     
     protected Application createApplicationInstance(String appClassName, ServletConfig servletConfig)


http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/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 67e4d9c..d27f97b 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
@@ -100,7 +100,9 @@ public class BookApplication extends Application {
             if (ap == null) {
                 throw new RuntimeException();
             }
-            if (ui.getRequestUri().toString().endsWith("/application11/thebooks/bookstore2/bookheaders"))
{
+            String uri = ui.getRequestUri().toString();
+            if (uri.endsWith("/application11/thebooks/bookstore2/bookheaders")
+                || uri.contains("/application6")) {
                 context.getHeaders().put("BOOK", Arrays.asList("1", "2"));    
             }
             
@@ -124,7 +126,9 @@ public class BookApplication extends Application {
             if (ap == null) {
                 throw new RuntimeException();
             }
-            if (ui.getRequestUri().toString().endsWith("/application11/thebooks/bookstore2/bookheaders"))
{
+            String uri = ui.getRequestUri().toString();
+            if (uri.endsWith("/application11/thebooks/bookstore2/bookheaders")
+                || uri.contains("/application6")) {
                 context.getHeaders().add("BOOK", "3");    
             }
             

http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication2.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication2.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication2.java
new file mode 100644
index 0000000..c89bed2
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication2.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.systest.jaxrs;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.ApplicationPath;
+import javax.ws.rs.core.Context;
+
+@ApplicationPath("/thebooks2")
+public class BookApplication2 extends BookApplication {
+    public BookApplication2(@Context ServletContext sc) {
+        super(sc);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/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 2c17694..8c262cb 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
@@ -165,7 +165,17 @@ public class JAXRSClientServerNonSpringBookTest extends AbstractBusClientServerT
     
     @Test
     public void testGetBook123Application11PerRequest() throws Exception {
-        WebClient wc = WebClient.create("http://localhost:" + PORT + "/application11/thebooks/bookstore2/bookheaders");
+        doTestPerRequest("http://localhost:" + PORT + "/application11/thebooks/bookstore2/bookheaders");
+    }
+    
+    @Test
+    public void testGetBook123TwoApplications() throws Exception {
+        doTestPerRequest("http://localhost:" + PORT + "/application6/thebooks/bookstore2/bookheaders");
+        doTestPerRequest("http://localhost:" + PORT + "/application6/thebooks2/bookstore2/bookheaders");
+    }
+    
+    private void doTestPerRequest(String address) throws Exception {
+        WebClient wc = WebClient.create(address);
         WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
         wc.accept("application/xml");
         Book book = wc.get(Book.class);

http://git-wip-us.apache.org/repos/asf/cxf/blob/dc4f09b7/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml b/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml
index 8982f15..c861065 100644
--- a/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs_non_spring/WEB-INF/web.xml
@@ -135,6 +135,25 @@
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
+    <servlet>
+        <servlet-name>CXFServlet6</servlet-name>
+        <display-name>CXF Servlet6</display-name>
+        <servlet-class>
+            org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
+        </servlet-class>
+        <init-param>
+            <param-name>javax.ws.rs.Application</param-name>
+            <param-value>
+               org.apache.cxf.systest.jaxrs.BookApplication,
+               org.apache.cxf.systest.jaxrs.BookApplication2
+            </param-value>
+        </init-param>
+        <init-param>
+            <param-name>jaxrs.application.address.ignore</param-name>
+            <param-value>false</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
     <servlet-mapping>
         <servlet-name>CXFServlet</servlet-name>
         <url-pattern>/singleton/*</url-pattern>
@@ -155,5 +174,9 @@
         <servlet-name>CXFServlet5</servlet-name>
         <url-pattern>/application11/*</url-pattern>
     </servlet-mapping>
+    <servlet-mapping>
+        <servlet-name>CXFServlet6</servlet-name>
+        <url-pattern>/application6/*</url-pattern>
+    </servlet-mapping>
 </web-app>
 <!-- END SNIPPET: webxml -->


Mime
View raw message