You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2016/10/22 18:30:58 UTC

svn commit: r1766222 - in /openwebbeans/microwave/trunk/microwave-core/src: main/java/org/apache/catalina/startup/ test/java/org/apache/microwave/ test/java/org/superbiz/app/ test/resources/META-INF/ test/resources/META-INF/services/

Author: rmannibucau
Date: Sat Oct 22 18:30:58 2016
New Revision: 1766222

URL: http://svn.apache.org/viewvc?rev=1766222&view=rev
Log:
reuse our finder even for SCI

Added:
    openwebbeans/microwave/trunk/microwave-core/src/test/java/org/superbiz/app/Init.java
    openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/
    openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/services/
    openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer
Modified:
    openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/catalina/startup/MicrowaveContextConfig.java
    openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/MicrowaveTest.java

Modified: openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/catalina/startup/MicrowaveContextConfig.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/catalina/startup/MicrowaveContextConfig.java?rev=1766222&r1=1766221&r2=1766222&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/catalina/startup/MicrowaveContextConfig.java (original)
+++ openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/catalina/startup/MicrowaveContextConfig.java Sat Oct 22 18:30:58 2016
@@ -24,24 +24,30 @@ import org.apache.microwave.logging.tomc
 import org.apache.tomcat.util.descriptor.web.WebXml;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.corespi.scanner.xbean.CdiArchive;
+import org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder;
 import org.apache.webbeans.web.scanner.WebScannerService;
 
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.annotation.HandlesTypes;
 import javax.servlet.annotation.WebFilter;
 import javax.servlet.annotation.WebListener;
 import javax.servlet.annotation.WebServlet;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Modifier;
 import java.net.URL;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Stream;
 
 public class MicrowaveContextConfig extends ContextConfig {
     private final Microwave.Builder configuration;
     private final Map<String, Collection<Class<?>>> webClasses = new HashMap<>();
+    private OwbAnnotationFinder finder;
 
     public MicrowaveContextConfig(final Microwave.Builder configuration) {
         this.configuration = configuration;
@@ -49,6 +55,11 @@ public class MicrowaveContextConfig exte
 
     @Override
     protected void webConfig() {
+        if (!configuration.isTomcatScanning()) {
+            super.webConfig();
+            return;
+        }
+
         // eagerly start CDI to scan only once and not twice (tomcat+CDI)
         final ClassLoader loader = context.getLoader().getClassLoader(); // should already be started at that point
         final Thread thread = Thread.currentThread();
@@ -57,9 +68,11 @@ public class MicrowaveContextConfig exte
         try {
             final WebScannerService scannerService = WebScannerService.class.cast(WebBeansContext.getInstance().getScannerService());
             scannerService.scan();
-            final CdiArchive archive = CdiArchive.class.cast(scannerService.getFinder().getArchive());
+            finder = scannerService.getFinder();
+            finder.link();
+            final CdiArchive archive = CdiArchive.class.cast(finder.getArchive());
             Stream.of(WebServlet.class, WebFilter.class, WebListener.class)
-                    .forEach(marker -> scannerService.getFinder().findAnnotatedClasses(marker).stream()
+                    .forEach(marker -> finder.findAnnotatedClasses(marker).stream()
                             .filter(c -> !Modifier.isAbstract(c.getModifiers()) && Modifier.isPublic(c.getModifiers()))
                             .forEach(webComponent -> webClasses.computeIfAbsent(
                                     archive.classesByUrl().entrySet().stream()
@@ -73,6 +86,7 @@ public class MicrowaveContextConfig exte
             super.webConfig();
         } finally {
             webClasses.clear();
+            finder = null;
         }
     }
 
@@ -80,26 +94,64 @@ public class MicrowaveContextConfig exte
     protected void processAnnotationsWebResource(final WebResource webResource, final WebXml fragment,
                                                  final boolean handlesTypesOnly,
                                                  final Map<String, JavaClassCacheEntry> javaClassCache) {
-        if (configuration.isTomcatScanning()) {
+        if (configuration.isTomcatScanning()) { // TODO: use our finder
             super.processAnnotationsWebResource(webResource, fragment, handlesTypesOnly, javaClassCache);
         }
     }
 
     @Override
     protected void processAnnotationsUrl(final URL url, final WebXml fragment, final boolean handlesTypesOnly,
-                                         final Map<String, JavaClassCacheEntry> javaClassCache) {
-        if (configuration.isTomcatScanning()) {
-            final Collection<Class<?>> classes = webClasses.remove(url.toExternalForm());
-            if (classes != null && !classes.isEmpty()) {
-                final ClassLoader loader = Thread.currentThread().getContextClassLoader();
-                classes.forEach(c -> {
-                    try (final InputStream stream = loader.getResourceAsStream(c.getName().replace('.', '/') + ".class")) {
-                        super.processAnnotationsStream(stream, fragment, handlesTypesOnly, javaClassCache);
-                    } catch (final IOException e) {
-                        new LogFacade(MicrowaveContextConfig.class.getName()).error("Can't parse " + c);
+                                         final Map<String, JavaClassCacheEntry> javaClassCache) { // use our finder
+        if (!configuration.isTomcatScanning()) {
+            return;
+        }
+
+        final Collection<Class<?>> classes = webClasses.remove(url.toExternalForm());
+        if (classes != null && !classes.isEmpty()) {
+            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+            classes.forEach(c -> {
+                try (final InputStream stream = loader.getResourceAsStream(c.getName().replace('.', '/') + ".class")) {
+                    super.processAnnotationsStream(stream, fragment, handlesTypesOnly, javaClassCache);
+                } catch (final IOException e) {
+                    new LogFacade(MicrowaveContextConfig.class.getName()).error("Can't parse " + c);
+                }
+            });
+        }
+    }
+
+    @Override
+    protected void processServletContainerInitializers() { // use our finder
+        if (!configuration.isTomcatScanning()) {
+            return;
+        }
+
+        try {
+            new WebappServiceLoader<ServletContainerInitializer>(context).load(ServletContainerInitializer.class).forEach(sci -> {
+                final Set<Class<?>> classes = new HashSet<>();
+                initializerClassMap.put(sci, classes);
+
+                final HandlesTypes ht;
+                try {
+                    ht = sci.getClass().getAnnotation(HandlesTypes.class);
+                } catch (final Exception | NoClassDefFoundError e) {
+                    return;
+                }
+                if (ht == null) {
+                    return;
+                }
+                Stream.of(ht.value()).forEach(t -> {
+                    if (t.isAnnotation()) {
+                        final Class<? extends Annotation> annotation = Class.class.cast(t);
+                        finder.findAnnotatedClasses(annotation).forEach(classes::add);
+                    } else if (t.isInterface()) {
+                        finder.findImplementations(t).forEach(classes::add);
+                    } else {
+                        finder.findSubclasses(t).forEach(classes::add);
                     }
                 });
-            }
+            });
+        } catch (final IOException e) {
+            ok = false;
         }
     }
 }

Modified: openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/MicrowaveTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/MicrowaveTest.java?rev=1766222&r1=1766221&r2=1766222&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/MicrowaveTest.java (original)
+++ openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/MicrowaveTest.java Sat Oct 22 18:30:58 2016
@@ -88,6 +88,9 @@ public class MicrowaveTest {
         try (final Microwave microwave = new Microwave(new Microwave.Builder().randomHttpPort()).bake()) {
             assertEquals("simple", IOUtils.toString(new URL("http://localhost:" + microwave.getConfiguration().getHttpPort() + "/api/test")));
             assertEquals("simplefiltertrue", IOUtils.toString(new URL("http://localhost:" + microwave.getConfiguration().getHttpPort() + "/filter")));
+            assertEquals(
+                    "sci:" + Endpoint.class.getName() + RsApp.class.getName(),
+                    IOUtils.toString(new URL("http://localhost:" + microwave.getConfiguration().getHttpPort() + "/sci")));
         } catch (final IOException e) {
             fail(e.getMessage());
         }

Added: openwebbeans/microwave/trunk/microwave-core/src/test/java/org/superbiz/app/Init.java
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/test/java/org/superbiz/app/Init.java?rev=1766222&view=auto
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/test/java/org/superbiz/app/Init.java (added)
+++ openwebbeans/microwave/trunk/microwave-core/src/test/java/org/superbiz/app/Init.java Sat Oct 22 18:30:58 2016
@@ -0,0 +1,60 @@
+/*
+ * 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.servlet.DispatcherType;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.HandlesTypes;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+import java.io.IOException;
+import java.util.EnumSet;
+import java.util.Set;
+
+import static java.util.stream.Collectors.joining;
+
+@HandlesTypes({Path.class, Application.class})
+public class Init implements ServletContainerInitializer {
+    @Override
+    public void onStartup(final Set<Class<?>> c, final ServletContext ctx) throws ServletException {
+        ctx.addFilter("sci", new Filter() {
+            @Override
+            public void init(final FilterConfig filterConfig) throws ServletException {
+                // no-op
+            }
+
+            @Override
+            public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
+                response.getWriter().write("sci:" + c.stream().map(Class::getName).sorted().collect(joining()));
+            }
+
+            @Override
+            public void destroy() {
+                // no-op
+            }
+        }).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/sci");
+    }
+}

Added: openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer
URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer?rev=1766222&view=auto
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer (added)
+++ openwebbeans/microwave/trunk/microwave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer Sat Oct 22 18:30:58 2016
@@ -0,0 +1 @@
+org.superbiz.app.Init