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 bd76a6c OWB-1310 ensure conversation stop warning does not appear
when irrelevant
bd76a6c is described below
commit bd76a6c9eed73dd95c1157203a7e448126694967
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Wed Jan 15 15:11:25 2020 +0100
OWB-1310 ensure conversation stop warning does not appear when irrelevant
---
.../webbeans/context/AbstractContextsService.java | 5 +-
.../web/lifecycle/WebContainerLifecycle.java | 16 ++-
.../web/tests/ConversationLoggingTest.java | 130 +++++++++++++++++++++
3 files changed, 145 insertions(+), 6 deletions(-)
diff --git
a/webbeans-impl/src/main/java/org/apache/webbeans/context/AbstractContextsService.java
b/webbeans-impl/src/main/java/org/apache/webbeans/context/AbstractContextsService.java
index 9cf0a2f..a1f2033 100644
---
a/webbeans-impl/src/main/java/org/apache/webbeans/context/AbstractContextsService.java
+++
b/webbeans-impl/src/main/java/org/apache/webbeans/context/AbstractContextsService.java
@@ -133,5 +133,8 @@ public abstract class AbstractContextsService implements
ContextsService
}
}
-
+ public boolean isSupportsConversation()
+ {
+ return supportsConversation;
+ }
}
diff --git
a/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
b/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
index 8f8a2d3..a96a301 100644
---
a/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
+++
b/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
@@ -21,6 +21,7 @@ package org.apache.webbeans.web.lifecycle;
import org.apache.webbeans.annotation.InitializedLiteral;
import org.apache.webbeans.config.OWBLogConst;
import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.context.AbstractContextsService;
import org.apache.webbeans.el.ELContextStore;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.lifecycle.AbstractLifeCycle;
@@ -161,11 +162,16 @@ public class WebContainerLifecycle extends
AbstractLifeCycle
@Override
protected void beforeStopApplication(Object stopObject)
{
- webBeansContext.getContextsService().endContext(RequestScoped.class,
null);
-
webBeansContext.getContextsService().endContext(ConversationScoped.class, null);
- webBeansContext.getContextsService().endContext(SessionScoped.class,
null);
-
webBeansContext.getContextsService().endContext(ApplicationScoped.class, null);
- webBeansContext.getContextsService().endContext(Singleton.class, null);
+ final ContextsService contextsService =
webBeansContext.getContextsService();
+ contextsService.endContext(RequestScoped.class, null);
+ if (AbstractContextsService.class.isInstance(contextsService) &&
+
AbstractContextsService.class.cast(contextsService).isSupportsConversation())
+ {
+ contextsService.endContext(ConversationScoped.class, null);
+ }
+ contextsService.endContext(SessionScoped.class, null);
+ contextsService.endContext(ApplicationScoped.class, null);
+ contextsService.endContext(Singleton.class, null);
// clean up the EL caches after each request
ELContextStore elStore = ELContextStore.getInstance(false);
diff --git
a/webbeans-web/src/test/java/org/apache/webbeans/web/tests/ConversationLoggingTest.java
b/webbeans-web/src/test/java/org/apache/webbeans/web/tests/ConversationLoggingTest.java
new file mode 100644
index 0000000..a8e41a2
--- /dev/null
+++
b/webbeans-web/src/test/java/org/apache/webbeans/web/tests/ConversationLoggingTest.java
@@ -0,0 +1,130 @@
+package org.apache.webbeans.web.tests;
+
+/*
+ * 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.Arrays.asList;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Consumer;
+import java.util.logging.Handler;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+import java.util.stream.Stream;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.ConversationScoped;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.servlet.WebBeansConfigurationListener;
+import org.apache.webbeans.spi.ContainerLifecycle;
+import org.apache.webbeans.spi.ContextsService;
+import org.apache.webbeans.spi.ConversationService;
+import org.apache.webbeans.spi.ScannerService;
+import org.apache.webbeans.web.context.WebContextsService;
+import org.apache.webbeans.web.context.WebConversationService;
+import org.apache.webbeans.web.lifecycle.WebContainerLifecycle;
+import org.apache.webbeans.web.lifecycle.test.MockServletContext;
+import org.apache.webbeans.web.scanner.WebScannerService;
+import org.apache.webbeans.web.tests.scanner.EmptyScanner;
+import org.junit.Test;
+
+public class ConversationLoggingTest
+{
+ @Test
+ public void dontLogIfThereIsNoConversation()
+ {
+ doTest(EmptyScanner.class, l -> {});
+ }
+
+ @Test
+ public void ensureUnsupportedScopeIsLogged()
+ {
+ doTest(EmptyScanner.class, l -> {
+ try {
+ final Field wbc =
l.getClass().getDeclaredField("webBeansContext");
+ wbc.setAccessible(true);
+ final ContextsService service =
WebBeansContext.class.cast(wbc.get(l)).getContextsService();
+ service.startContext(ConversationScoped.class, null);
+ service.endContext(ConversationScoped.class, null);
+ } catch (final Exception e) {
+ throw new IllegalStateException(e);
+ }
+ },
+ "CDI-OpenWebBeans container does not support context scope
ConversationScoped. " +
+ "Scopes @Dependent, @RequestScoped, @ApplicationScoped and
@Singleton are supported scope types");
+ }
+
+ private void doTest(final Class<? extends WebScannerService>
scannerServiceClass,
+ final Consumer<WebBeansConfigurationListener> listener,
+ final String... messages) {
+ final Logger logger =
Logger.getLogger(WebContextsService.class.getName());
+ final Collection<LogRecord> records = new ArrayList<>();
+ final Handler spy = new Handler()
+ {
+ @Override
+ public void publish(final LogRecord record)
+ {
+ records.add(record);
+ }
+
+ @Override
+ public void flush()
+ {
+ // no-op
+ }
+
+ @Override
+ public void close() throws SecurityException
+ {
+ // no-op
+ }
+ };
+ logger.addHandler(spy);
+ final MockServletContext context = new MockServletContext();
+ final ServletContextEvent event = new ServletContextEvent(context);
+ System.setProperty(ScannerService.class.getName(),
scannerServiceClass.getName());
+ System.setProperty(ContainerLifecycle.class.getName(),
WebContainerLifecycle.class.getName());
+ System.setProperty(ConversationService.class.getName(),
WebConversationService.class.getName());
+
System.setProperty("org.apache.webbeans.application.supportsConversation",
"false");
+ try
+ {
+ final WebBeansConfigurationListener wbcl = new
WebBeansConfigurationListener();
+ wbcl.contextInitialized(event);
+ listener.accept(wbcl);
+ wbcl.contextDestroyed(event);
+ }
+ finally
+ {
+ logger.removeHandler(spy);
+ System.clearProperty(ScannerService.class.getName());
+ System.clearProperty(ContainerLifecycle.class.getName());
+ }
+
+ final String[] values =
records.stream().map(LogRecord::getMessage).distinct().toArray(String[]::new);
+ assertEquals(asList(values).toString(), messages.length,
values.length);
+ assertArrayEquals(messages, values);
+ }
+}