openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1779201 - in /openwebbeans/meecrowave/trunk: meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/ meecrowave-core/src/main/java/org/apache/meecrowave/ meecrowave-core/src/test/java/org/apache/meecrowave/ meecrowave-core/sr...
Date Tue, 17 Jan 2017 14:56:56 GMT
Author: rmannibucau
Date: Tue Jan 17 14:56:56 2017
New Revision: 1779201

URL: http://svn.apache.org/viewvc?rev=1779201&view=rev
Log:
MEECROWAVE-5 MEECROWAVE-4 ServletContainerInitializer injection support + bake(Consumer<Context>)
shortcut

Added:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerInitialierInjectionTest.java
      - copied, changed from r1777328, openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MultipartTest.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerProgrammaticRegistrationTest.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/MeecrowaveConfiguration.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer
    openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveExtension.java
    openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveTask.java
    openwebbeans/meecrowave/trunk/meecrowave-maven-plugin/src/main/java/org/apache/meecrowave/maven/MeecrowaveRunMojo.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/MeecrowaveConfiguration.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/MeecrowaveConfiguration.java?rev=1779201&r1=1779200&r2=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/MeecrowaveConfiguration.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-arquillian/src/main/java/org/apache/meecrowave/arquillian/MeecrowaveConfiguration.java
Tue Jan 17 14:56:56 2017
@@ -100,6 +100,7 @@ public class MeecrowaveConfiguration imp
     private String scanningPackageIncludes;
     private String scanningPackageExcludes;
     private boolean tomcatNoJmx = true;
+    private boolean injectServletContainerInitializer = true;
 
     @Override
     public void validate() throws ConfigurationException {
@@ -719,4 +720,12 @@ public class MeecrowaveConfiguration imp
     public void setTomcatNoJmx(final boolean tomcatNoJmx) {
         this.tomcatNoJmx = tomcatNoJmx;
     }
+
+    public boolean isInjectServletContainerInitializer() {
+        return injectServletContainerInitializer;
+    }
+
+    public void setInjectServletContainerInitializer(final boolean injectServletContainerInitializer)
{
+        this.injectServletContainerInitializer = injectServletContainerInitializer;
+    }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java?rev=1779201&r1=1779200&r2=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
Tue Jan 17 14:56:56 2017
@@ -64,6 +64,12 @@ import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.CDI;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.servlet.ServletContainerInitializer;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 import java.io.File;
@@ -86,9 +92,13 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Properties;
+import java.util.Set;
 import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 import java.util.stream.Stream;
 
@@ -157,6 +167,12 @@ public class Meecrowave implements AutoC
         return deployClasspath("");
     }
 
+    // shortcut
+    public Meecrowave bake(final Consumer<Context> customizer) {
+        start();
+        return deployClasspath(new DeploymentMeta("", null, customizer));
+    }
+
     // shortcut (used by plugins)
     public Meecrowave deployClasspath(final String context) {
         return deployClasspath(new DeploymentMeta(context, null, null));
@@ -230,10 +246,47 @@ public class Meecrowave implements AutoC
         });
         ctx.addLifecycleListener(new Tomcat.FixContextListener()); // after having configured
the security!!!
 
+        final AtomicReference<Runnable> releaseSCI = new AtomicReference<>();
         ctx.addServletContainerInitializer((c, ctx1) -> {
             new OWBAutoSetup().onStartup(c, ctx1);
             new CxfCdiAutoSetup().onStartup(c, ctx1);
             new TomcatAutoInitializer().onStartup(c, ctx1);
+
+            if (configuration.isInjectServletContainerInitializer()) {
+                final Field f;
+                try { // now cdi is on, we can inject cdi beans in ServletContainerInitializer
+                    f = StandardContext.class.getDeclaredField("initializers");
+                    if (!f.isAccessible()) {
+                        f.setAccessible(true);
+                    }
+                } catch (final Exception e) {
+                    throw new IllegalStateException("Bad tomcat version", e);
+                }
+
+                final List<AutoCloseable> cc;
+                try {
+                    cc = ((Map<ServletContainerInitializer, Set<Class<?>>>)
f.get(ctx)).keySet().stream()
+                            .filter(i -> !i.getClass().getName().startsWith(Meecrowave.class.getName()))
+                            .map(i -> {
+                                try {
+                                    return this.inject(i);
+                                } catch (final IllegalArgumentException iae) {
+                                    return null;
+                                }
+                            })
+                            .filter(Objects::nonNull)
+                            .collect(toList());
+                } catch (final IllegalAccessException e) {
+                    throw new IllegalStateException("Can't read initializers", e);
+                }
+                releaseSCI.set(() -> cc.forEach(closeable -> {
+                    try {
+                        closeable.close();
+                    } catch (final Exception e) {
+                        throw new IllegalStateException(e);
+                    }
+                }));
+            }
         }, emptySet());
 
         if (configuration.isUseTomcatDefaults()) {
@@ -258,6 +311,7 @@ public class Meecrowave implements AutoC
 
         tomcat.getHost().addChild(ctx);
         contexts.put(meta.context, () -> {
+            ofNullable(releaseSCI.get()).ifPresent(Runnable::run);
             try {
                 tomcat.getHost().removeChild(ctx);
             } finally {
@@ -571,6 +625,15 @@ public class Meecrowave implements AutoC
         // no-op
     }
 
+    public <T> AutoCloseable inject(final T instance) {
+        final BeanManager bm = CDI.current().getBeanManager();
+        final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
+        final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
+        final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
+        injectionTarget.inject(instance, creationalContext);
+        return creationalContext::release;
+    }
+
     @Override
     public void close() {
         if (tomcat == null) {
@@ -930,6 +993,9 @@ public class Meecrowave implements AutoC
         @CliOption(name = "log4j2-jul-bridge", description = "Should JUL logs be redirected
to Log4j2 - only works before JUL usage.")
         private boolean useLog4j2JulLogManager = System.getProperty("java.util.logging.manager")
== null;
 
+        @CliOption(name = "servlet-container-initializer-injection", description = "Should
ServletContainerInitialize support injections.")
+        private boolean injectServletContainerInitializer = true;
+
         private final Map<Class<?>, Object> extensions = new HashMap<>();
 
         public Builder() { // load defaults
@@ -1729,6 +1795,14 @@ public class Meecrowave implements AutoC
             } while (type != Object.class);
             return instance;
         }
+
+        public boolean isInjectServletContainerInitializer() {
+            return injectServletContainerInitializer;
+        }
+
+        public void setInjectServletContainerInitializer(final boolean injectServletContainerInitializer)
{
+            this.injectServletContainerInitializer = injectServletContainerInitializer;
+        }
     }
 
     public static class LoginConfigBuilder {

Copied: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerInitialierInjectionTest.java
(from r1777328, openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MultipartTest.java)
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerInitialierInjectionTest.java?p2=openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerInitialierInjectionTest.java&p1=openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MultipartTest.java&r1=1777328&r2=1779201&rev=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MultipartTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerInitialierInjectionTest.java
Tue Jan 17 14:56:56 2017
@@ -18,63 +18,47 @@
  */
 package org.apache.meecrowave;
 
-import org.apache.johnzon.mapper.reflection.JohnzonParameterizedType;
 import org.junit.Test;
 
 import javax.enterprise.context.ApplicationScoped;
-import javax.json.bind.annotation.JsonbProperty;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.core.GenericType;
-import javax.ws.rs.core.MediaType;
-import java.util.LinkedHashMap;
-import java.util.Map;
+import javax.inject.Inject;
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import java.util.Set;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 // just a sample using multiparts
-public class MultipartTest {
+public class ServletContainerInitialierInjectionTest {
+    @Inject
+    private ServletContext context;
+
     @Test
     public void configBinding() {
         try (final Meecrowave meecrowave = new Meecrowave(
                 new Meecrowave.Builder()
                         .randomHttpPort()
-                        .includePackages(MultipartTest.MultiEndpoint.class.getName())).bake())
{
-            final Client client = ClientBuilder.newClient();
-            try {
-                // for mixed types use org.apache.cxf.jaxrs.ext.multipart.MultipartBody
-                final Map<String, JsonbModel> response = client.target("http://localhost:"
+ meecrowave.getConfiguration().getHttpPort() + "/MultipartTest")
-                        .request()
-                        .get(new GenericType<Map<String, JsonbModel>>(new JohnzonParameterizedType(Map.class,
String.class, JsonbModel.class)) {{}});
-                assertEquals(1, response.size());
-                assertEquals("ok", response.get(MediaType.APPLICATION_JSON).value);
-            } finally {
-                client.close();
-            }
+                        .includePackages(ServletContainerInitialierInjectionTest.class.getName())).bake())
{
+            meecrowave.inject(this);
+            assertTrue(Boolean.class.cast(context.getAttribute("ServletContainerInitialierInjectionTest")));
         }
     }
 
-    @ApplicationScoped
-    @Path("MultipartTest")
-    public static class MultiEndpoint {
-        @Produces("multipart/mixed")
-        @GET
-        public Map<String, Object> getBooks() {
-            final JsonbModel jsonbModel = new JsonbModel();
-            jsonbModel.value = "ok";
-
-            final Map<String, Object> map = new LinkedHashMap<>();
-            map.put("application/json", jsonbModel);
-
-            return map;
+    public static class Init implements ServletContainerInitializer {
+        @Inject
+        private AService bean;
+
+        @Override
+        public void onStartup(final Set<Class<?>> set, final ServletContext servletContext)
throws ServletException {
+            servletContext.setAttribute("ServletContainerInitialierInjectionTest", bean !=
null && bean.isOk());
         }
     }
 
-    public static class JsonbModel {
-        @JsonbProperty("test")
-        public String value;
+    @ApplicationScoped
+    public static class AService {
+        public boolean isOk() {
+            return true;
+        }
     }
 }

Added: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerProgrammaticRegistrationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerProgrammaticRegistrationTest.java?rev=1779201&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerProgrammaticRegistrationTest.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/ServletContainerProgrammaticRegistrationTest.java
Tue Jan 17 14:56:56 2017
@@ -0,0 +1,69 @@
+/*
+ * 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 org.junit.Test;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
+import javax.inject.Inject;
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import java.util.Set;
+
+import static java.util.Collections.emptySet;
+import static org.junit.Assert.assertEquals;
+
+// just a sample using multiparts
+public class ServletContainerProgrammaticRegistrationTest {
+    @Inject
+    private ServletContext context;
+
+    @Test
+    public void configBinding() {
+        try (final Meecrowave meecrowave = new Meecrowave(
+                new Meecrowave.Builder()
+                        .randomHttpPort()
+                        .includePackages(ServletContainerProgrammaticRegistrationTest.class.getName()))
+                .bake(c -> c.addServletContainerInitializer(new ProgrammaticInit(), emptySet())))
{
+            meecrowave.inject(this);
+            assertEquals("> yeah", String.valueOf(context.getAttribute("ServletContainerProgrammaticRegistrationTest")));
+        }
+    }
+
+    @Dependent
+    public static class ProgrammaticInit implements ServletContainerInitializer {
+        @Inject
+        private AService bean;
+
+        @Override
+        public void onStartup(final Set<Class<?>> set, final ServletContext servletContext)
throws ServletException {
+            servletContext.setAttribute(
+                    "ServletContainerProgrammaticRegistrationTest", ">" + (bean != null
? bean.isOk() : "no"));
+        }
+    }
+
+    @ApplicationScoped
+    public static class AService {
+        public String isOk() {
+            return " yeah";
+        }
+    }
+}

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer?rev=1779201&r1=1779200&r2=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/resources/META-INF/services/javax.servlet.ServletContainerInitializer
Tue Jan 17 14:56:56 2017
@@ -1,2 +1,3 @@
 org.superbiz.app.Init
 org.apache.meecrowave.runner.CliTest$BeanTester
+org.apache.meecrowave.ServletContainerInitialierInjectionTest$Init

Modified: openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveExtension.java?rev=1779201&r1=1779200&r2=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveExtension.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveExtension.java
Tue Jan 17 14:56:56 2017
@@ -90,6 +90,7 @@ public class MeecrowaveExtension {
     private String scanningPackageIncludes;
     private String scanningPackageExcludes;
     private boolean tomcatNoJmx = true;
+    private boolean injectServletContainerInitializer = true;
 
     public boolean isJava9SkipWorkarounds() {
         return java9SkipWorkarounds;
@@ -610,4 +611,12 @@ public class MeecrowaveExtension {
     public void setTomcatNoJmx(final boolean tomcatNoJmx) {
         this.tomcatNoJmx = tomcatNoJmx;
     }
+
+    public boolean isInjectServletContainerInitializer() {
+        return injectServletContainerInitializer;
+    }
+
+    public void setInjectServletContainerInitializer(final boolean injectServletContainerInitializer)
{
+        this.injectServletContainerInitializer = injectServletContainerInitializer;
+    }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveTask.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveTask.java?rev=1779201&r1=1779200&r2=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveTask.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-gradle-plugin/src/main/java/org/apache/meecrowave/gradle/MeecrowaveTask.java
Tue Jan 17 14:56:56 2017
@@ -114,6 +114,10 @@ public class MeecrowaveTask extends Defa
 
     @Input
     @Optional
+    private boolean injectServletContainerInitializer = true;
+
+    @Input
+    @Optional
     private boolean ssl;
 
     @Input
@@ -1112,4 +1116,12 @@ public class MeecrowaveTask extends Defa
     public void setTomcatNoJmx(final boolean tomcatNoJmx) {
         this.tomcatNoJmx = tomcatNoJmx;
     }
+
+    public boolean isInjectServletContainerInitializer() {
+        return injectServletContainerInitializer;
+    }
+
+    public void setInjectServletContainerInitializer(final boolean injectServletContainerInitializer)
{
+        this.injectServletContainerInitializer = injectServletContainerInitializer;
+    }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-maven-plugin/src/main/java/org/apache/meecrowave/maven/MeecrowaveRunMojo.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-maven-plugin/src/main/java/org/apache/meecrowave/maven/MeecrowaveRunMojo.java?rev=1779201&r1=1779200&r2=1779201&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-maven-plugin/src/main/java/org/apache/meecrowave/maven/MeecrowaveRunMojo.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-maven-plugin/src/main/java/org/apache/meecrowave/maven/MeecrowaveRunMojo.java
Tue Jan 17 14:56:56 2017
@@ -176,6 +176,9 @@ public class MeecrowaveRunMojo extends A
     @Parameter(property = "meecrowave.logging-global-setup", defaultValue = "true")
     private boolean loggingGlobalSetup;
 
+    @Parameter(property = "meecrowave.servlet-container-initializer-injections", defaultValue
= "true")
+    private boolean injectServletContainerInitializer;
+
     @Parameter(property = "meecrowave.shutdown-hook", defaultValue = "true")
     private boolean useShutdownHook;
 



Mime
View raw message