cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Support for the constructor injection into the singleton resources starting from Application.getClasses
Date Mon, 13 Mar 2017 13:56:51 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes f5b75c7a0 -> d0fb61f79


Support for the constructor injection into the singleton resources starting from Application.getClasses


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

Branch: refs/heads/3.1.x-fixes
Commit: d0fb61f798561c4e24e283672e317e7eaaec0ede
Parents: f5b75c7
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Feb 17 13:58:53 2017 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Mar 13 13:56:32 2017 +0000

----------------------------------------------------------------------
 .../cxf/cdi/JAXRSCdiResourceExtension.java      |  6 ++--
 .../cxf/jaxrs/impl/RuntimeDelegateImpl.java     |  2 +-
 .../lifecycle/SingletonResourceProvider.java    | 19 +++++++++--
 .../jaxrs/servlet/CXFNonSpringJaxrsServlet.java | 30 +++++++++++------
 .../apache/cxf/jaxrs/utils/ResourceUtils.java   | 34 +++++++++++---------
 .../cxf/jaxrs/model/ClassResourceInfoTest.java  |  2 +-
 .../cxf/jaxrs/utils/ResourceUtilsTest.java      |  6 ++--
 .../test/resources/jaxrs_sse/WEB-INF/web.xml    | 28 ++++++++++++++++
 8 files changed, 93 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
index 68cd164..498dabf 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
@@ -182,7 +182,8 @@ public class JAXRSCdiResourceExtension implements Extension {
     private JAXRSServerFactoryBean createFactoryInstance(final Application application, final
List< ? > services,
             final List< ? > providers, final List< ? extends Feature > features)
{
 
-        final JAXRSServerFactoryBean instance = ResourceUtils.createApplication(application,
false, false, bus);
+        final JAXRSServerFactoryBean instance = 
+            ResourceUtils.createApplication(application, false, false, false, bus);
         instance.setServiceBeans(new ArrayList<>(services));
         instance.setProviders(providers);
         instance.setProviders(loadExternalProviders());
@@ -198,7 +199,8 @@ public class JAXRSCdiResourceExtension implements Extension {
      */
     private JAXRSServerFactoryBean createFactoryInstance(final Application application, final
BeanManager beanManager) {
 
-        final JAXRSServerFactoryBean instance = ResourceUtils.createApplication(application,
false, false, bus);
+        final JAXRSServerFactoryBean instance = 
+            ResourceUtils.createApplication(application, false, false, false, bus);
         final ClassifiedClasses classified = classes2singletons(application, beanManager);
         
         instance.setProviders(classified.getProviders());

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java
index dcd5946..ef1af45 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RuntimeDelegateImpl.java
@@ -110,7 +110,7 @@ public class RuntimeDelegateImpl extends RuntimeDelegate {
             && !JAXRSServerFactoryBean.class.isAssignableFrom(endpointType))) {
             throw new IllegalArgumentException();
         }
-        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, false);
+        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, false, false,
false, null);
         if (JAXRSServerFactoryBean.class.isAssignableFrom(endpointType)) {
             return endpointType.cast(bean);
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/lifecycle/SingletonResourceProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/lifecycle/SingletonResourceProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/lifecycle/SingletonResourceProvider.java
index a3f2469..c0dc39b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/lifecycle/SingletonResourceProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/lifecycle/SingletonResourceProvider.java
@@ -19,10 +19,14 @@
 
 package org.apache.cxf.jaxrs.lifecycle;
 
+import java.lang.reflect.Constructor;
+import java.util.Collections;
+
 import org.apache.cxf.common.util.ClassHelper;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.service.factory.ServiceConstructionException;
 
 /**
  * The default singleton resource provider which returns 
@@ -32,10 +36,21 @@ public class SingletonResourceProvider implements ResourceProvider {
     private Object resourceInstance;
     
     public SingletonResourceProvider(Object o, boolean callPostConstruct) {
-        resourceInstance = o;
+        if (o instanceof Constructor) {
+            Constructor<?> c = (Constructor<?>)o;
+            Object[] values = 
+                ResourceUtils.createConstructorArguments(c, null, false, Collections.<Class<?>,
Object>emptyMap());
+            try {
+                resourceInstance = values.length > 0 ? c.newInstance(values) : c.newInstance(new
Object[]{});
+            } catch (Exception ex) {
+                throw new ServiceConstructionException(ex);
+            }
+        } else {
+            resourceInstance = o;
+        }   
         if (callPostConstruct) {
             InjectionUtils.invokeLifeCycleMethod(o, 
-                ResourceUtils.findPostConstructMethod(ClassHelper.getRealClass(o)));
+                ResourceUtils.findPostConstructMethod(ClassHelper.getRealClass(resourceInstance)));
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/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 8332ab8..9c1154d 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
@@ -413,10 +413,14 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
                                          true));
         }
         return map;
-    }    
-    
+    }
+
+    protected boolean isResourceLifecycleASingleton(ServletConfig servletConfig) {
+        String scope = servletConfig.getInitParameter(SERVICE_SCOPE_PARAM);
+        return SERVICE_SCOPE_SINGLETON.equals(scope);
+    }
     
-    protected Object createSingletonInstance(Class<?> cls, Map<String, List<String>>
props, ServletConfig sc) 
+    protected Object createSingletonInstance(Class<?> cls, Map<String, List<String>>
props, ServletConfig sc)
         throws ServletException {
         Constructor<?> c = ResourceUtils.findResourceConstructor(cls, false);
         if (c == null) {
@@ -499,10 +503,13 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
         
         for (String cName : classNames) {
             ApplicationInfo providerApp = createApplicationInfo(cName, servletConfig);
-            
-            JAXRSServerFactoryBean bean = ResourceUtils.createApplication(providerApp.getProvider(),

+
+            JAXRSServerFactoryBean bean = ResourceUtils.createApplication(
+                                                providerApp.getProvider(),
                                                 ignoreApplicationPath,
-                                                getStaticSubResolutionValue(servletConfig));
+                                                getStaticSubResolutionValue(servletConfig),
+                                                isResourceLifecycleASingleton(servletConfig),
+                                                getBus());
             String splitChar = getParameterSplitChar(servletConfig);
             setAllInterceptors(bean, servletConfig, splitChar);
             setInvoker(bean, servletConfig);
@@ -522,10 +529,13 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
     
     protected void createServerFromApplication(ServletConfig servletConfig) 
         throws ServletException {
-        
-        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(getApplication(), 
-                                                                      isIgnoreApplicationPath(servletConfig),
-                                                                      getStaticSubResolutionValue(servletConfig));
+
+        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(
+                                          getApplication(),
+                                          isIgnoreApplicationPath(servletConfig),
+                                          getStaticSubResolutionValue(servletConfig),
+                                          isResourceLifecycleASingleton(servletConfig),
+                                          getBus());
         bean.setBus(getBus());
         bean.setApplication(getApplication());
         bean.create();

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
index ced9fc1..74d8d6f 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
@@ -100,6 +100,7 @@ import org.apache.cxf.jaxrs.model.UserOperation;
 import org.apache.cxf.jaxrs.model.UserResource;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.resource.ResourceManager;
 import org.apache.cxf.staxutils.StaxUtils;
 
@@ -124,8 +125,7 @@ public final class ResourceUtils {
         SERVER_PROVIDER_CLASS_NAMES.add("javax.ws.rs.container.ContainerRequestFilter");
         SERVER_PROVIDER_CLASS_NAMES.add("javax.ws.rs.container.ContainerResponseFilter");
         SERVER_PROVIDER_CLASS_NAMES.add("javax.ws.rs.container.DynamicFeature");
-        SERVER_PROVIDER_CLASS_NAMES.add("org.apache.cxf.jaxrs.ext.ContextResolver");
-        
+        SERVER_PROVIDER_CLASS_NAMES.add("org.apache.cxf.jaxrs.ext.ContextProvider");
     }
     
     private ResourceUtils() {
@@ -779,12 +779,15 @@ public final class ResourceUtils {
                                                       Message m, 
                                                       boolean perRequest,
                                                       Map<Class<?>, Object> contextValues)
{
+        if (m == null) {
+            m = new MessageImpl();
+        }
         Class<?>[] params = c.getParameterTypes();
         Annotation[][] anns = c.getParameterAnnotations();
         Type[] genericTypes = c.getGenericParameterTypes();
         @SuppressWarnings("unchecked")
-        MultivaluedMap<String, String> templateValues = m == null ? null
-            : (MultivaluedMap<String, String>)m.get(URITemplate.TEMPLATE_PARAMETERS);
+        MultivaluedMap<String, String> templateValues = 
+            (MultivaluedMap<String, String>)m.get(URITemplate.TEMPLATE_PARAMETERS);
         Object[] values = new Object[params.length];
         for (int i = 0; i < params.length; i++) {
             if (AnnotationUtils.getAnnotation(anns[i], Context.class) != null) {
@@ -808,19 +811,14 @@ public final class ResourceUtils {
         }
         return values;
     }
-    public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath)
{
-        return createApplication(app, ignoreAppPath, false);
-    }
-    
-    public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath,
-            boolean staticSubresourceResolution) {
-        return createApplication(app, ignoreAppPath, staticSubresourceResolution, null);
-    }
     
     @SuppressWarnings("unchecked")
-    public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath,
-            boolean staticSubresourceResolution, Bus bus) {
-        
+    public static JAXRSServerFactoryBean createApplication(Application app, 
+                                                           boolean ignoreAppPath,
+                                                           boolean staticSubresourceResolution,

+                                                           boolean useSingletonResourceProvider,
+                                                           Bus bus) {
+
         Set<Object> singletons = app.getSingletons();
         verifySingletons(singletons);
         
@@ -839,7 +837,11 @@ public final class ResourceUtils {
                     features.add(createFeatureInstance((Class<? extends Feature>) cls));
                 } else {
                     resourceClasses.add(cls);
-                    map.put(cls, new PerRequestResourceProvider(cls));
+                    if (useSingletonResourceProvider) {
+                        map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
+                    } else {
+                        map.put(cls, new PerRequestResourceProvider(cls));
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
index 2970118..679b43d 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
@@ -249,7 +249,7 @@ public class ClassResourceInfoTest extends Assert {
     @Test
     public void testNameBindings() {
         Application app = new TestApplication();
-        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, true, true);
+        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, true, true, false,
null);
         ClassResourceInfo cri = bean.getServiceFactory().getClassResourceInfo().get(0);
         Set<String> names = cri.getNameBindings();
         assertEquals(Collections.singleton(CustomNameBinding.class.getName()), names);

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/ResourceUtilsTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/ResourceUtilsTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/ResourceUtilsTest.java
index ff873e7..c44183a 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/ResourceUtilsTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/ResourceUtilsTest.java
@@ -164,13 +164,15 @@ public class ResourceUtilsTest extends Assert {
 
     @Test
     public void shouldCreateApplicationWhichInheritsApplicationPath() throws Exception {
-        JAXRSServerFactoryBean application = ResourceUtils.createApplication(new SuperApplication(),
false);
+        JAXRSServerFactoryBean application = ResourceUtils.createApplication(
+                                                 new SuperApplication(), false, false, false,
null);
         assertEquals("/base", application.getAddress());
     }
 
     @Test
     public void shouldCreateApplicationWhichOverridesApplicationPath() throws Exception {
-        JAXRSServerFactoryBean application = ResourceUtils.createApplication(new CustomApplication(),
false);
+        JAXRSServerFactoryBean application = ResourceUtils.createApplication(
+                                                 new CustomApplication(), false, false, false,
null);
         assertEquals("/custom", application.getAddress());
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/d0fb61f7/systests/rs-sse/src/test/resources/jaxrs_sse/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/systests/rs-sse/src/test/resources/jaxrs_sse/WEB-INF/web.xml b/systests/rs-sse/src/test/resources/jaxrs_sse/WEB-INF/web.xml
new file mode 100644
index 0000000..64746acb
--- /dev/null
+++ b/systests/rs-sse/src/test/resources/jaxrs_sse/WEB-INF/web.xml
@@ -0,0 +1,28 @@
+<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
+	<servlet>
+		<servlet-name>CXFServlet</servlet-name>
+		<display-name>CXF Servlet</display-name>
+		<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
   
+		<load-on-startup>1</load-on-startup>
+		<async-supported>true</async-supported>
+		<init-param>
+			<param-name>transportId</param-name>
+			<param-value>http://cxf.apache.org/transports/http/sse</param-value>
+		</init-param>
+		<init-param>
+			<param-name>javax.ws.rs.Application</param-name>
+			<param-value>org.apache.cxf.systest.jaxrs.sse.SseApplication</param-value>
+		</init-param>
+		<init-param>
+			<param-name>jaxrs.scope</param-name>
+			<param-value>singleton</param-value>
+		</init-param>
+	</servlet>
+
+	<servlet-mapping>
+		<servlet-name>CXFServlet</servlet-name>
+		<url-pattern>/rest/*</url-pattern>
+	</servlet-mapping>
+</web-app>
\ No newline at end of file


Mime
View raw message