This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 8442117 OWB-1296 ensure OWB can start in a SCI instead of a servlet
context listener
8442117 is described below
commit 844211708106e20f34baee7505de1903a1eae1a0
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Thu Aug 1 11:04:08 2019 +0200
OWB-1296 ensure OWB can start in a SCI instead of a servlet context listener
---
.../servlet/WebBeansConfigurationListener.java | 58 ++++++++++++++-----
.../javax.servlet.ServletContainerInitializer | 17 ++++++
.../java/WebBeansConfigurationListenerTest.java | 66 ++++++++++++++++++++++
.../lifeycle/SingleStartContainerLifecycle.java | 34 +++++++++++
.../webbeans/web/tests/scanner/EmptyScanner.java | 35 ++++++++++++
5 files changed, 196 insertions(+), 14 deletions(-)
diff --git
a/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
b/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
index 6cb451b..b6d56d6 100644
---
a/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
+++
b/webbeans-web/src/main/java/org/apache/webbeans/servlet/WebBeansConfigurationListener.java
@@ -30,12 +30,16 @@ import
org.apache.webbeans.web.util.ServletCompatibilityUtil;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.Context;
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
+
+import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -78,20 +82,7 @@ public class WebBeansConfigurationListener implements
ServletContextListener, Se
@Override
public void contextInitialized(ServletContextEvent event)
{
- this.lifeCycle = webBeansContext.getService(ContainerLifecycle.class);
-
- try
- {
- this.lifeCycle.startApplication(event);
- }
- catch (Exception e)
- {
- logger.log(Level.SEVERE,
- WebBeansLoggerFacade.constructMessage(
- OWBLogConst.ERROR_0018,
-
ServletCompatibilityUtil.getServletInfo(event.getServletContext())));
- WebBeansUtil.throwRuntimeExceptions(e);
- }
+ doStart(event);
}
@@ -202,6 +193,30 @@ public class WebBeansConfigurationListener implements
ServletContextListener, Se
}
}
+ private void doStart(final ServletContextEvent event)
+ {
+ if (event.getServletContext().getAttribute(getClass().getName()) !=
null)
+ {
+ return;
+ }
+
+ this.lifeCycle = webBeansContext.getService(ContainerLifecycle.class);
+
+ try
+ {
+ this.lifeCycle.startApplication(event);
+ event.getServletContext().setAttribute(getClass().getName(), true);
+ }
+ catch (Exception e)
+ {
+ logger.log(Level.SEVERE,
+ WebBeansLoggerFacade.constructMessage(
+ OWBLogConst.ERROR_0018,
+
ServletCompatibilityUtil.getServletInfo(event.getServletContext())));
+ WebBeansUtil.throwRuntimeExceptions(e);
+ }
+ }
+
private boolean ensureRequestScope()
{
Context context =
this.lifeCycle.getContextService().getCurrentContext(RequestScoped.class);
@@ -226,4 +241,19 @@ public class WebBeansConfigurationListener implements
ServletContextListener, Se
}
}
+ public static class Auto implements ServletContainerInitializer
+ {
+ @Override
+ public void onStartup(final Set<Class<?>> set, final ServletContext
servletContext)
+ {
+ final String key = "openwebbeans.web.sci.active";
+ if (!Boolean.parseBoolean(System.getProperty(key,
servletContext.getInitParameter(key))))
+ {
+ return;
+ }
+ final WebBeansConfigurationListener listener = new
WebBeansConfigurationListener();
+ listener.doStart(new ServletContextEvent(servletContext));
+ servletContext.addListener(listener);
+ }
+ }
}
diff --git
a/webbeans-web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
b/webbeans-web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
new file mode 100644
index 0000000..b203f88
--- /dev/null
+++
b/webbeans-web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
@@ -0,0 +1,17 @@
+#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.
+org.apache.webbeans.servlet.WebBeansConfigurationListener$Auto
diff --git a/webbeans-web/src/test/java/WebBeansConfigurationListenerTest.java
b/webbeans-web/src/test/java/WebBeansConfigurationListenerTest.java
new file mode 100644
index 0000000..e8d54fd
--- /dev/null
+++ b/webbeans-web/src/test/java/WebBeansConfigurationListenerTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+import static java.util.Collections.emptySet;
+import static org.apache.webbeans.util.Asserts.assertNotNull;
+
+import java.util.EventListener;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.apache.webbeans.servlet.WebBeansConfigurationListener;
+import org.apache.webbeans.spi.ContainerLifecycle;
+import org.apache.webbeans.spi.ScannerService;
+import org.apache.webbeans.web.lifecycle.test.MockServletContext;
+import org.apache.webbeans.web.tests.lifeycle.SingleStartContainerLifecycle;
+import org.apache.webbeans.web.tests.scanner.EmptyScanner;
+import org.junit.Test;
+
+public class WebBeansConfigurationListenerTest
+{
+ @Test
+ public void avoidDoubleInitWithSci()
+ {
+ final WebBeansConfigurationListener.Auto auto = new
WebBeansConfigurationListener.Auto();
+ final AtomicReference<ServletContextListener> listener = new
AtomicReference<>();
+ final MockServletContext context = new MockServletContext()
+ {
+ @Override
+ public <T extends EventListener> void addListener(final T t)
+ {
+ listener.set(ServletContextListener.class.cast(t));
+ }
+ };
+ context.setAttribute("openwebbeans.web.sci.active", "true");
+ System.setProperty(ScannerService.class.getName(),
EmptyScanner.class.getName());
+ System.setProperty(ContainerLifecycle.class.getName(),
SingleStartContainerLifecycle.class.getName());
+ auto.onStartup(emptySet(), context);
+ System.clearProperty(ScannerService.class.getName());
+ System.clearProperty(ContainerLifecycle.class.getName());
+
+ final ServletContextListener wbcl = listener.get();
+ assertNotNull(wbcl);
+
+ final ServletContextEvent event = new ServletContextEvent(context);
+ wbcl.contextInitialized(event);
+ wbcl.contextDestroyed(event);
+ }
+}
diff --git
a/webbeans-web/src/test/java/org/apache/webbeans/web/tests/lifeycle/SingleStartContainerLifecycle.java
b/webbeans-web/src/test/java/org/apache/webbeans/web/tests/lifeycle/SingleStartContainerLifecycle.java
new file mode 100644
index 0000000..d5f40f1
--- /dev/null
+++
b/webbeans-web/src/test/java/org/apache/webbeans/web/tests/lifeycle/SingleStartContainerLifecycle.java
@@ -0,0 +1,34 @@
+/*
+ * 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.webbeans.web.tests.lifeycle;
+
+import org.apache.webbeans.web.lifecycle.WebContainerLifecycle;
+
+public class SingleStartContainerLifecycle extends WebContainerLifecycle
+{
+ @Override
+ public void startApplication(final Object startupObject)
+ {
+ if (started.get())
+ {
+ throw new IllegalStateException("already started");
+ }
+ super.startApplication(startupObject);
+ }
+}
diff --git
a/webbeans-web/src/test/java/org/apache/webbeans/web/tests/scanner/EmptyScanner.java
b/webbeans-web/src/test/java/org/apache/webbeans/web/tests/scanner/EmptyScanner.java
new file mode 100644
index 0000000..504b9bf
--- /dev/null
+++
b/webbeans-web/src/test/java/org/apache/webbeans/web/tests/scanner/EmptyScanner.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.webbeans.web.tests.scanner;
+
+import org.apache.webbeans.web.scanner.WebScannerService;
+
+public class EmptyScanner extends WebScannerService {
+ @Override
+ protected void addWarBeansArchive()
+ {
+ // no-op
+ }
+
+ @Override
+ protected void registerBeanArchives(final ClassLoader loader)
+ {
+ // no-op
+ }
+}