cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cxf git commit: CXF-7132: Use the original class when it comes to determining classes for ResourceProvider usage. This closes #198.
Date Wed, 16 Nov 2016 02:46:35 GMT
Repository: cxf
Updated Branches:
  refs/heads/master ad094bde1 -> 5b7b014cc


CXF-7132: Use the original class when it comes to determining classes for ResourceProvider
usage. This closes #198.


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

Branch: refs/heads/master
Commit: 5b7b014cc1d24cba1191bc07fd48b13dbf4d4391
Parents: ad094bd
Author: reta <drreta@gmail.com>
Authored: Tue Nov 15 21:28:06 2016 -0500
Committer: reta <drreta@gmail.com>
Committed: Tue Nov 15 21:44:27 2016 -0500

----------------------------------------------------------------------
 .../org/apache/cxf/cdi/CdiResourceProvider.java | 31 +++++++--
 .../cxf/cdi/JAXRSCdiResourceExtension.java      | 68 ++++++++++++++------
 .../cdi/base/AbstractCdiMultiAppTest.java       | 16 +++++
 .../systests/cdi/base/BookStoreVersioned.java   | 39 +++++++++++
 .../jaxrs/BookStoreScopedApplication.java       | 35 ++++++++++
 .../jaxrs/BookStoreScopedApplication.java       | 35 ++++++++++
 6 files changed, 198 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/5b7b014c/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java
index 997cf40..a417c0f 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java
@@ -18,34 +18,51 @@
  */
 package org.apache.cxf.cdi;
 
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+
 import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
 import org.apache.cxf.message.Message;
 
 public class CdiResourceProvider implements ResourceProvider {
-    private final Object instance;
-    private final Class<?> resourceClass;
+    private Object instance;
+    private CreationalContext< ? > context;
+    
+    private final BeanManager beanManager;
+    private final Bean< ? > bean;
     
-    CdiResourceProvider(final Class<?> resourceClass, final Object instance) {
-        this.resourceClass = resourceClass;
-        this.instance = instance;
+    CdiResourceProvider(final BeanManager beanManager, final Bean< ? > bean) {
+        this.beanManager = beanManager;
+        this.bean = bean;
     }
     
     @Override
     public Object getInstance(Message m) {
+        if (instance == null) {
+            context = beanManager.createCreationalContext(bean);
+            instance = beanManager.getReference(bean, bean.getBeanClass(), context);
+        }
+        
         return instance;
     }
 
     @Override
     public void releaseInstance(Message m, Object o) {
+        if (context != null) {
+            context.release();
+            instance = null;
+        }
     }
 
     @Override
     public Class<?> getResourceClass() {
-        return resourceClass;
+        return bean.getBeanClass();
     }
 
     @Override
     public boolean isSingleton() {
-        return true;
+        return !bean.getScope().isAssignableFrom(RequestScoped.class);
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/5b7b014c/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 52ff3f8..4b8086e 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
@@ -21,10 +21,8 @@ package org.apache.cxf.cdi;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
 
@@ -50,7 +48,6 @@ import org.apache.cxf.Bus;
 import org.apache.cxf.bus.extension.ExtensionManagerBus;
 import org.apache.cxf.cdi.extension.JAXRSServerFactoryCustomizationExtension;
 import org.apache.cxf.feature.Feature;
-import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
 
@@ -68,6 +65,40 @@ public class JAXRSCdiResourceExtension implements Extension {
     private final List< CreationalContext< ? > > disposableCreationalContexts
= 
         new ArrayList< CreationalContext< ? > >();
 
+    /**
+     * Holder of the classified resource classes, converted to appropriate instance
+     * representations.
+     */
+    private static class ClassifiedClasses {
+        private List< Object > providers = new ArrayList<>();
+        private List< Feature > features = new ArrayList<>();
+        private List< CdiResourceProvider > resourceProviders = new ArrayList<>();
+        
+        public void addProviders(final Collection< Object > others) {
+            this.providers.addAll(others);
+        }
+        
+        public void addFeatures(final Collection< Feature > others) {
+            this.features.addAll(others);
+        }
+        
+        public void addResourceProvider(final CdiResourceProvider other) {
+            this.resourceProviders.add(other);
+        }
+        
+        public List< Object > getProviders() {
+            return providers;
+        }
+        
+        public List< Feature > getFeatures() {
+            return features;
+        }
+        
+        public List<CdiResourceProvider> getResourceProviders() {
+            return resourceProviders;
+        }
+    }
+    
     @SuppressWarnings("unchecked")
     public <T> void collect(@Observes final ProcessBean< T > event) {
         if (event.getAnnotated().isAnnotationPresent(ApplicationPath.class)) {
@@ -171,14 +202,13 @@ public class JAXRSCdiResourceExtension implements Extension {
     private JAXRSServerFactoryBean createFactoryInstance(final Application application, final
BeanManager beanManager) {
 
         final JAXRSServerFactoryBean instance = ResourceUtils.createApplication(application,
false, false, bus);
-        final Map< Class< ? >, List< Object > > classified = classes2singletons(application,
beanManager);
+        final ClassifiedClasses classified = classes2singletons(application, beanManager);
         
-        instance.setProviders(classified.get(Provider.class));
-        instance.getFeatures().addAll(CastUtils.cast(classified.get(Feature.class), Feature.class));
+        instance.setProviders(classified.getProviders());
+        instance.getFeatures().addAll(classified.getFeatures());
         
-        for (final Object resource: classified.get(Path.class)) {
-            instance.setResourceProvider(resource.getClass(), 
-                new CdiResourceProvider(resource.getClass(), resource));
+        for (final CdiResourceProvider resourceProvider: classified.getResourceProviders())
{
+            instance.setResourceProvider(resourceProvider.getResourceClass(), resourceProvider);
         }
 
         return instance;
@@ -191,20 +221,20 @@ public class JAXRSCdiResourceExtension implements Extension {
      * @param application the application instance
      * @return classified instances of classes by instance types
      */
-    private Map< Class< ? >, List< Object > > classes2singletons(final
Application application,
-                                                                 final BeanManager beanManager)
{
-        final Map< Class< ? >, List< Object > > classified = new HashMap<>();
-
-        classified.put(Feature.class, new ArrayList<>());
-        classified.put(Provider.class, new ArrayList<>());
-        classified.put(Path.class, new ArrayList<>());
+    private ClassifiedClasses classes2singletons(final Application application, final BeanManager
beanManager) {
+        final ClassifiedClasses classified = new ClassifiedClasses(); 
 
         // now loop through the classes
         Set<Class<?>> classes = application.getClasses();
         if (!classes.isEmpty()) {
-            classified.get(Path.class).addAll(loadServices(beanManager, classes));
-            classified.get(Provider.class).addAll(loadProviders(beanManager, classes));
-            classified.get(Feature.class).addAll(loadFeatures(beanManager, classes));
+            classified.addProviders(loadProviders(beanManager, classes));
+            classified.addFeatures(loadFeatures(beanManager, classes));
+            
+            for (final Bean< ? > bean: serviceBeans) {
+                if (classes.contains(bean.getBeanClass())) {
+                    classified.addResourceProvider(new CdiResourceProvider(beanManager, bean));
+                }
+            }
         }
         
         return classified;

http://git-wip-us.apache.org/repos/asf/cxf/blob/5b7b014c/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiMultiAppTest.java
----------------------------------------------------------------------
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiMultiAppTest.java
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiMultiAppTest.java
index 68815d8..ebaea91 100644
--- a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiMultiAppTest.java
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiMultiAppTest.java
@@ -31,6 +31,10 @@ import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.startsWith;
+
 public abstract class AbstractCdiMultiAppTest extends AbstractCdiSingleAppTest {
     @Test
     public void testAddOneBookWithValidation() {
@@ -42,6 +46,18 @@ public abstract class AbstractCdiMultiAppTest extends AbstractCdiSingleAppTest
{
         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), r.getStatus());
     }
 
+    @Test
+    public void testGetBookStoreVersion() {
+        Response r1 = createWebClient("/rest/v3/bookstore/versioned/version", MediaType.TEXT_PLAIN).get();
+        assertEquals(Response.Status.OK.getStatusCode(), r1.getStatus());
+        assertThat(r1.readEntity(String.class), startsWith("1.0."));
+        
+        Response r2 = createWebClient("/rest/v3/bookstore/versioned/version", MediaType.TEXT_PLAIN).get();
+        assertEquals(Response.Status.OK.getStatusCode(), r2.getStatus());
+        assertThat(r2.readEntity(String.class), startsWith("1.0."));
+        
+        assertThat(r2.readEntity(String.class), not(equalTo(r1.readEntity(String.class))));
+    }
 
     protected WebClient createWebClient(final String url) {
         return createWebClient(url, MediaType.APPLICATION_JSON);

http://git-wip-us.apache.org/repos/asf/cxf/blob/5b7b014c/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreVersioned.java
----------------------------------------------------------------------
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreVersioned.java
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreVersioned.java
new file mode 100644
index 0000000..ff46aed
--- /dev/null
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/BookStoreVersioned.java
@@ -0,0 +1,39 @@
+/**
+ * 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.systests.cdi.base;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@RequestScoped
+@Path("/bookstore/versioned")
+public class BookStoreVersioned {
+    @Inject private String version;
+    
+    @GET
+    @Path("/version")
+    @Produces(MediaType.TEXT_PLAIN)
+    public String getVersion() {
+        return version + "." + this.hashCode();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/5b7b014c/systests/cdi/cdi-owb/cdi-multiple-apps-owb/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
----------------------------------------------------------------------
diff --git a/systests/cdi/cdi-owb/cdi-multiple-apps-owb/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
b/systests/cdi/cdi-owb/cdi-multiple-apps-owb/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
new file mode 100644
index 0000000..ca6f836
--- /dev/null
+++ b/systests/cdi/cdi-owb/cdi-multiple-apps-owb/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
@@ -0,0 +1,35 @@
+/**
+ * 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 java.util.Collections;
+import java.util.Set;
+
+import javax.ws.rs.ApplicationPath;
+import javax.ws.rs.core.Application;
+
+import org.apache.cxf.systests.cdi.base.BookStoreVersioned;
+
+@ApplicationPath("/v3")
+public class BookStoreScopedApplication extends Application {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return Collections.<Class<?>>singleton(BookStoreVersioned.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/5b7b014c/systests/cdi/cdi-weld/cdi-multiple-apps-weld/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
----------------------------------------------------------------------
diff --git a/systests/cdi/cdi-weld/cdi-multiple-apps-weld/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
b/systests/cdi/cdi-weld/cdi-multiple-apps-weld/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
new file mode 100644
index 0000000..ca6f836
--- /dev/null
+++ b/systests/cdi/cdi-weld/cdi-multiple-apps-weld/src/test/java/org/apache/cxf/systest/jaxrs/BookStoreScopedApplication.java
@@ -0,0 +1,35 @@
+/**
+ * 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 java.util.Collections;
+import java.util.Set;
+
+import javax.ws.rs.ApplicationPath;
+import javax.ws.rs.core.Application;
+
+import org.apache.cxf.systests.cdi.base.BookStoreVersioned;
+
+@ApplicationPath("/v3")
+public class BookStoreScopedApplication extends Application {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return Collections.<Class<?>>singleton(BookStoreVersioned.class);
+    }
+}


Mime
View raw message