openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1848334 - in /openwebbeans/meecrowave/trunk/meecrowave-core/src: main/java/org/apache/meecrowave/cxf/ main/java/org/apache/meecrowave/openwebbeans/ main/resources/META-INF/openwebbeans/ test/java/org/apache/meecrowave/ test/java/org/superb...
Date Thu, 06 Dec 2018 15:21:36 GMT
Author: rmannibucau
Date: Thu Dec  6 15:21:36 2018
New Revision: 1848334

URL: http://svn.apache.org/viewvc?rev=1848334&view=rev
Log:
MEECROWAVE-165 ensure contract driven services are deployed until cxf fixes its impl

Added:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/MeecrowaveLoaderService.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/InterfaceBasedTest.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceApi.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceBased.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/Cxfs.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/Cxfs.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/Cxfs.java?rev=1848334&r1=1848333&r2=1848334&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/Cxfs.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/cxf/Cxfs.java
Thu Dec  6 15:21:36 2018
@@ -18,7 +18,14 @@
  */
 package org.apache.meecrowave.cxf;
 
-import org.apache.cxf.BusFactory;
+import java.lang.reflect.Field;
+import java.util.List;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessBean;
+import javax.ws.rs.Path;
 
 public class Cxfs {
     public static final boolean IS_PRESENT;
@@ -43,8 +50,56 @@ public class Cxfs {
     }
 
     public static void resetDefaultBusIfEquals(final ConfigurableBus clientBus) {
-        if (clientBus != null && BusFactory.getDefaultBus(false) == clientBus) {
-            BusFactory.setDefaultBus(null);
+        if (clientBus != null && org.apache.cxf.BusFactory.getDefaultBus(false) ==
clientBus) {
+            org.apache.cxf.BusFactory.setDefaultBus(null);
+        }
+    }
+
+    public static Extension mapCdiExtensionIfNeeded(final Extension extension) {
+        if ("org.apache.cxf.cdi.JAXRSCdiResourceExtension".equals(extension.getClass().getName()))
{
+            final Field serviceBeans;
+            try {
+                serviceBeans = org.apache.cxf.cdi.JAXRSCdiResourceExtension.class
+                        .getDeclaredField("serviceBeans");
+            } catch (final NoSuchFieldException e) {
+                new org.apache.meecrowave.logging.tomcat.LogFacade(Cxfs.class.getName()).warn(e.getMessage(),
e);
+                return extension;
+            }
+            if (!serviceBeans.isAccessible()) {
+                serviceBeans.setAccessible(true);
+            }
+            return new ContractFriendlyJAXRSCdiResourceExtension(serviceBeans);
+        }
+        return extension;
+    }
+
+    // to drop when we will have a cxf version with https://issues.apache.org/jira/browse/CXF-7921
+    private static class ContractFriendlyJAXRSCdiResourceExtension extends org.apache.cxf.cdi.JAXRSCdiResourceExtension
{
+        private final Field serviceBeans;
+
+        private ContractFriendlyJAXRSCdiResourceExtension(final Field serviceBeans) {
+            this.serviceBeans = serviceBeans;
+        }
+
+        @Override
+        public <T> void collect(@Observes final ProcessBean<T> event) {
+            if (!event.getAnnotated().isAnnotationPresent(Path.class) && AnnotatedType.class.isInstance(event.getAnnotated()))
{
+                final AnnotatedType<?> type = AnnotatedType.class.cast(event.getAnnotated());
+                // note: should we use Annotated for interfaces as well?
+                if (type.getTypeClosure().stream()
+                        .filter(it -> Class.class.isInstance(it) && Class.class.cast(it).isInterface())
+                        .map(Class.class::cast)
+                        .anyMatch(c -> c.isAnnotationPresent(Path.class))) {
+                    try {
+                        List.class.cast(serviceBeans.get(this)).add(event.getBean());
+                        return;
+                    } catch (final IllegalAccessException e) {
+                        new org.apache.meecrowave.logging.tomcat.LogFacade(Cxfs.class.getName())
+                                .error(e.getMessage(), e);
+                    }
+                }
+            }
+            super.collect(event);
         }
     }
 }

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/MeecrowaveLoaderService.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/MeecrowaveLoaderService.java?rev=1848334&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/MeecrowaveLoaderService.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/MeecrowaveLoaderService.java
Thu Dec  6 15:21:36 2018
@@ -0,0 +1,53 @@
+/*
+ * 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.meecrowave.openwebbeans;
+
+import static java.util.stream.Collectors.toList;
+
+import java.util.List;
+import java.util.ServiceLoader;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+import javax.enterprise.inject.spi.Extension;
+
+import org.apache.meecrowave.cxf.Cxfs;
+import org.apache.webbeans.spi.LoaderService;
+
+public class MeecrowaveLoaderService implements LoaderService {
+    @Override
+    public <T> List<T> load(final Class<T> serviceType) {
+        return load(serviceType, Thread.currentThread().getContextClassLoader());
+    }
+
+    @Override
+    public <T> List<T> load(final Class<T> serviceType, final ClassLoader
classLoader) {
+        if (Extension.class == serviceType) {
+            return doLoad(serviceType, classLoader)
+                    .map(e -> serviceType.cast(Cxfs.mapCdiExtensionIfNeeded(Extension.class.cast(e))))
+                    .collect(toList());
+        }
+        return doLoad(serviceType, classLoader)
+                .collect(toList());
+    }
+
+    private <T> Stream<T> doLoad(final Class<T> serviceType, final ClassLoader
classLoader) {
+        return StreamSupport.stream(ServiceLoader.load(serviceType, classLoader).spliterator(),
false);
+    }
+}

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/resources/META-INF/openwebbeans/openwebbeans.properties?rev=1848334&r1=1848333&r2=1848334&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
Thu Dec  6 15:21:36 2018
@@ -18,3 +18,4 @@ configuration.ordinal=1000
 org.apache.xbean.finder.filter.Filter=org.apache.meecrowave.openwebbeans.KnownClassesFilter
 org.apache.webbeans.spi.ScannerService=org.apache.meecrowave.openwebbeans.OWBTomcatWebScannerService
 org.apache.webbeans.spi.SecurityService=org.apache.meecrowave.openwebbeans.MeecrowaveSecurityService
+org.apache.webbeans.spi.LoaderService=org.apache.meecrowave.openwebbeans.MeecrowaveLoaderService

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/InterfaceBasedTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/InterfaceBasedTest.java?rev=1848334&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/InterfaceBasedTest.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/InterfaceBasedTest.java
Thu Dec  6 15:21:36 2018
@@ -0,0 +1,46 @@
+/*
+ * 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.meecrowave;
+
+import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
+import static org.junit.Assert.assertEquals;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+
+import org.junit.Test;
+
+public class InterfaceBasedTest {
+    @Test
+    public void run() {
+        try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder()
+                .randomHttpPort()
+                .includePackages("org.superbiz.app.Interface")).bake()) {
+            final Client client = ClientBuilder.newClient();
+            try {
+                assertEquals("api", client
+                        .target("http://localhost:" + meecrowave.getConfiguration().getHttpPort()
+ "/interfacebased")
+                        .request(TEXT_PLAIN_TYPE)
+                        .get(String.class));
+            } finally {
+                client.close();
+            }
+        }
+    }
+}

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java?rev=1848334&r1=1848333&r2=1848334&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java
Thu Dec  6 15:21:36 2018
@@ -24,6 +24,7 @@ import org.apache.meecrowave.runner.cli.
 import org.junit.Test;
 import org.superbiz.app.Bounced;
 import org.superbiz.app.Endpoint;
+import org.superbiz.app.InterfaceApi;
 import org.superbiz.app.RsApp;
 
 import java.io.File;
@@ -117,7 +118,7 @@ public class MeecrowaveTest {
             assertEquals("simple", slurp(new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort()
+ "/api/test")));
             assertEquals("simplefiltertrue", slurp(new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort()
+ "/filter")));
             assertEquals(
-                    "sci:" + Bounced.class.getName() + Endpoint.class.getName() + RsApp.class.getName(),
+                    "sci:" + Bounced.class.getName() + Endpoint.class.getName() + InterfaceApi.class.getName()
+ RsApp.class.getName(),
                     slurp(new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort()
+ "/sci")));
         } catch (final IOException e) {
             fail(e.getMessage());

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceApi.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceApi.java?rev=1848334&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceApi.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceApi.java
Thu Dec  6 15:21:36 2018
@@ -0,0 +1,31 @@
+/*
+ * 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.superbiz.app;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Produces(MediaType.TEXT_PLAIN)
+@Path("interfacebased")
+public interface InterfaceApi {
+    @GET
+    String get();
+}

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceBased.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceBased.java?rev=1848334&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceBased.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/superbiz/app/InterfaceBased.java
Thu Dec  6 15:21:36 2018
@@ -0,0 +1,29 @@
+/*
+ * 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.superbiz.app;
+
+import javax.enterprise.context.ApplicationScoped;
+
+@ApplicationScoped
+public class InterfaceBased implements InterfaceApi {
+    @Override
+    public String get() {
+        return "api";
+    }
+}



Mime
View raw message