You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/02/29 02:46:17 UTC
git commit: DELTASPIKE-104 reStartContexts test
Updated Branches:
refs/heads/master de859888d -> 4b648d062
DELTASPIKE-104 reStartContexts test
+ initial workarounds for existing versions of owb and weld.
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/4b648d06
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/4b648d06
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/4b648d06
Branch: refs/heads/master
Commit: 4b648d062a3447ca1e1034ba6cb0c128839be2c3
Parents: de85988
Author: gpetracek <gp...@apache.org>
Authored: Tue Feb 28 23:58:30 2012 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Wed Feb 29 02:33:41 2012 +0100
----------------------------------------------------------------------
.../cdise/owb/OpenWebBeansContainerControl.java | 224 +++++++++------
.../deltaspike/cdise/weld/ContextController.java | 136 +++++++--
.../deltaspike/cdise/tck/ContainerCtrlTckTest.java | 13 +-
3 files changed, 257 insertions(+), 116 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b648d06/deltaspike/cdise/impl-owb/src/main/java/org/apache/deltaspike/cdise/owb/OpenWebBeansContainerControl.java
----------------------------------------------------------------------
diff --git a/deltaspike/cdise/impl-owb/src/main/java/org/apache/deltaspike/cdise/owb/OpenWebBeansContainerControl.java b/deltaspike/cdise/impl-owb/src/main/java/org/apache/deltaspike/cdise/owb/OpenWebBeansContainerControl.java
index 1c3a629..108c43f 100644
--- a/deltaspike/cdise/impl-owb/src/main/java/org/apache/deltaspike/cdise/owb/OpenWebBeansContainerControl.java
+++ b/deltaspike/cdise/impl-owb/src/main/java/org/apache/deltaspike/cdise/owb/OpenWebBeansContainerControl.java
@@ -18,9 +18,11 @@
*/
package org.apache.deltaspike.cdise.owb;
-import java.lang.annotation.Annotation;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import org.apache.deltaspike.cdise.api.CdiContainer;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.context.ContextFactory;
+import org.apache.webbeans.context.type.ContextTypes;
+import org.apache.webbeans.spi.ContainerLifecycle;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.ConversationScoped;
@@ -29,13 +31,11 @@ import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Singleton;
-
-import org.apache.deltaspike.cdise.api.CdiContainer;
-
-import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.context.ContextFactory;
-import org.apache.webbeans.context.type.ContextTypes;
-import org.apache.webbeans.spi.ContainerLifecycle;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* OpenWebBeans specific implementation of {@link org.apache.deltaspike.cdise.api.CdiContainer}.
@@ -44,9 +44,17 @@ public class OpenWebBeansContainerControl implements CdiContainer
{
private static final Logger LOG = Logger.getLogger(OpenWebBeansContainerControl.class.getName());
- private ContainerLifecycle lifecycle = null;
- private MockServletContext servletContext = null;
- private MockHttpSession session = null;
+ private ContainerLifecycle lifecycle;
+ private MockServletContext servletContext;
+ private MockHttpSession session;
+
+ private Boolean resetSuccessful;
+
+ @Override
+ public BeanManager getBeanManager()
+ {
+ return lifecycle.getBeanManager();
+ }
@Override
public void start()
@@ -62,14 +70,17 @@ public class OpenWebBeansContainerControl implements CdiContainer
shutdownContainer();
}
+ @Override
public void bootContainer()
{
servletContext = new MockServletContext();
session = new MockHttpSession();
+
lifecycle = WebBeansContext.getInstance().getService(ContainerLifecycle.class);
lifecycle.startApplication(servletContext);
}
+ @Override
public void shutdownContainer()
{
if (lifecycle != null)
@@ -78,34 +89,19 @@ public class OpenWebBeansContainerControl implements CdiContainer
}
}
+ @Override
public void startContexts()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ ContextFactory contextFactory = getContextFactory();
contextFactory.initSingletonContext(servletContext);
contextFactory.initApplicationContext(servletContext);
contextFactory.initSessionContext(session);
- contextFactory.initConversationContext(null);
contextFactory.initRequestContext(null);
- }
-
- private void startApplicationScope()
- {
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
-
- contextFactory.initApplicationContext(servletContext);
- }
-
- public void startConversationScope()
- {
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
-
contextFactory.initConversationContext(null);
}
+ @Override
public void startContext(Class<? extends Annotation> scopeClass)
{
if (scopeClass.isAssignableFrom(ApplicationScoped.class))
@@ -126,53 +122,96 @@ public class OpenWebBeansContainerControl implements CdiContainer
}
}
- private void startRequestScope()
+ public void stopContexts()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ stopSessionScope();
+ stopConversationScope();
+ stopRequestScope();
+ stopApplicationScope();
+ stopSingletonScope();
+ }
- contextFactory.initRequestContext(null);
+ public void stopContext(Class<? extends Annotation> scopeClass)
+ {
+ if (scopeClass.isAssignableFrom(ApplicationScoped.class))
+ {
+ stopApplicationScope();
+ }
+ else if (scopeClass.isAssignableFrom(SessionScoped.class))
+ {
+ stopSessionScope();
+ }
+ else if (scopeClass.isAssignableFrom(RequestScoped.class))
+ {
+ stopRequestScope();
+ }
+ else if (scopeClass.isAssignableFrom(ConversationScoped.class))
+ {
+ stopConversationScope();
+ }
+ }
+
+ /*
+ * start scopes
+ */
+
+ private void startApplicationScope()
+ {
+ ContextFactory contextFactory = getContextFactory();
+
+ contextFactory.initApplicationContext(servletContext);
}
private void startSessionScope()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ ContextFactory contextFactory = getContextFactory();
contextFactory.initSessionContext(session);
}
- public void stopContexts()
+ private void startRequestScope()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ ContextFactory contextFactory = getContextFactory();
- stopSessionScope();
- stopConversationScope();
- stopRequestScope();
- stopApplicationScope();
+ contextFactory.initRequestContext(null);
+ }
+
+ private void startConversationScope()
+ {
+ ContextFactory contextFactory = getContextFactory();
+
+ contextFactory.initConversationContext(null);
+ }
+
+ /*
+ * stop scopes
+ */
+
+ private void stopSingletonScope()
+ {
+ ContextFactory contextFactory = getContextFactory();
Context context = contextFactory.getStandardContext(ContextTypes.SINGLETON);
if (context != null && context.isActive())
{
contextFactory.destroySingletonContext(servletContext);
+ resetCache();
}
else
{
logDestroyOfInactiveContext(Singleton.class.getName());
}
-
}
- public void stopApplicationScope()
+ private void stopApplicationScope()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ ContextFactory contextFactory = getContextFactory();
Context context = contextFactory.getStandardContext(ContextTypes.APPLICATION);
if (context != null && context.isActive())
{
contextFactory.destroyApplicationContext(servletContext);
+ resetCache();
}
else
{
@@ -180,77 +219,94 @@ public class OpenWebBeansContainerControl implements CdiContainer
}
}
- public void stopConversationScope()
+ private void stopSessionScope()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ ContextFactory contextFactory = getContextFactory();
- Context context = contextFactory.getStandardContext(ContextTypes.CONVERSATION);
+ Context context = contextFactory.getStandardContext(ContextTypes.SESSION);
if (context != null && context.isActive())
{
- contextFactory.destroyConversationContext();
+ contextFactory.destroySessionContext(session);
+ resetCache();
}
else
{
- logDestroyOfInactiveContext(ConversationScoped.class.getName());
+ logDestroyOfInactiveContext(SessionScoped.class.getName());
}
}
- public void stopContext(Class<? extends Annotation> scopeClass)
+ private void stopRequestScope()
{
- if (scopeClass.isAssignableFrom(ApplicationScoped.class))
- {
- stopApplicationScope();
- }
- else if (scopeClass.isAssignableFrom(SessionScoped.class))
- {
- stopSessionScope();
- }
- else if (scopeClass.isAssignableFrom(RequestScoped.class))
+ ContextFactory contextFactory = getContextFactory();
+
+ Context context = contextFactory.getStandardContext(ContextTypes.REQUEST);
+ if (context != null && context.isActive())
{
- stopRequestScope();
+ contextFactory.destroyRequestContext(null);
+ resetCache();
}
- else if (scopeClass.isAssignableFrom(ConversationScoped.class))
+ else
{
- stopConversationScope();
+ logDestroyOfInactiveContext(RequestScoped.class.getName());
}
}
- public void stopRequestScope()
+ private void stopConversationScope()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ ContextFactory contextFactory = getContextFactory();
- Context context = contextFactory.getStandardContext(ContextTypes.REQUEST);
+ Context context = contextFactory.getStandardContext(ContextTypes.CONVERSATION);
if (context != null && context.isActive())
{
- contextFactory.destroyRequestContext(null);
+ contextFactory.destroyConversationContext();
+ resetCache();
}
else
{
- logDestroyOfInactiveContext(RequestScoped.class.getName());
+ logDestroyOfInactiveContext(ConversationScoped.class.getName());
}
}
- public void stopSessionScope()
+ //workaround for OWB-650
+ private void resetCache()
{
- WebBeansContext webBeansContext = WebBeansContext.getInstance();
- ContextFactory contextFactory = webBeansContext.getContextFactory();
+ if (Boolean.FALSE.equals(this.resetSuccessful))
+ {
+ return;
+ }
- Context context = contextFactory.getStandardContext(ContextTypes.SESSION);
- if (context != null && context.isActive())
+ BeanManager beanManager = getBeanManager();
+
+ try
{
- contextFactory.destroySessionContext(session);
+ Field cacheProxiesField = beanManager.getClass().getDeclaredField("cacheProxies");
+ cacheProxiesField.setAccessible(true);
+ Map cacheProxies = (Map)cacheProxiesField.get(beanManager);
+
+ if (cacheProxies != null)
+ {
+ cacheProxies.clear();
+ this.resetSuccessful = true;
+ }
}
- else
+ catch (Exception e)
{
- logDestroyOfInactiveContext(SessionScoped.class.getName());
+ //do nothing - it's a different version of OWB which isn't tested but
+ //might not have a cache and is therefore compatible.
+ this.resetSuccessful = false;
+ }
+ catch (LinkageError e)
+ {
+ //do nothing - a new version of owb is used which introduced other required dependencies.
+ //OWB-650 should be fixed in this version already
+ this.resetSuccessful = false;
}
}
-
- public BeanManager getBeanManager()
+
+ private ContextFactory getContextFactory()
{
- return lifecycle.getBeanManager();
+ WebBeansContext webBeansContext = WebBeansContext.getInstance();
+ return webBeansContext.getContextFactory();
}
private void logDestroyOfInactiveContext(String contextName)
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b648d06/deltaspike/cdise/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/ContextController.java
----------------------------------------------------------------------
diff --git a/deltaspike/cdise/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/ContextController.java b/deltaspike/cdise/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/ContextController.java
index 8beae7f..26b3ade 100644
--- a/deltaspike/cdise/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/ContextController.java
+++ b/deltaspike/cdise/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/ContextController.java
@@ -19,6 +19,8 @@
package org.apache.deltaspike.cdise.weld;
import org.jboss.weld.context.ApplicationContext;
+import org.jboss.weld.context.api.ContextualInstance;
+import org.jboss.weld.context.beanstore.BeanStore;
import org.jboss.weld.context.bound.BoundConversationContext;
import org.jboss.weld.context.bound.BoundRequestContext;
import org.jboss.weld.context.bound.BoundSessionContext;
@@ -30,7 +32,9 @@ import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Typed;
import javax.inject.Inject;
import javax.inject.Singleton;
+import java.lang.reflect.Field;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
/**
@@ -59,6 +63,8 @@ public class ContextController
private boolean singletonScopeStarted;
+ private Boolean resetSuccessful;
+
//X TODO check if we can remove it
void startApplicationScope()
{
@@ -71,10 +77,102 @@ public class ContextController
void stopApplicationScope()
{
+ if (Boolean.FALSE.equals(this.resetSuccessful) /*|| TODO detect weld 2.x+*/)
+ {
+ if (applicationContext.isActive())
+ {
+ applicationContext.invalidate();
+ this.applicationScopeStarted = false;
+ }
+ return;
+ }
+
if (applicationContext.isActive())
{
+ //workaround for weld 1.x (see WELD-1072)
+ org.jboss.weld.bootstrap.api.Singleton<BeanStore> beanStoreHolder = null;
+ BeanStore originalBeanStore = null;
+ try
+ {
+ Field field = this.applicationContext.getClass().getSuperclass().getDeclaredField("beanStore");
+ field.setAccessible(true);
+ beanStoreHolder = (org.jboss.weld.bootstrap.api.Singleton)field.get(this.applicationContext);
+ final BeanStore beanStore = beanStoreHolder.get();
+ originalBeanStore = beanStore;
+
+ beanStoreHolder.set(new BeanStore()
+ {
+ @Override
+ public <T> ContextualInstance<T> get(String id)
+ {
+ return beanStore.get(id);
+ }
+
+ @Override
+ public boolean contains(String id)
+ {
+ return beanStore.contains(id);
+ }
+
+ @Override
+ public void clear()
+ {
+ //do nothing
+ }
+
+ @Override
+ public Iterator<String> iterator()
+ {
+ return beanStore.iterator();
+ }
+
+ @Override
+ public <T> void put(String id, ContextualInstance<T> contextualInstance)
+ {
+ beanStore.put(id, contextualInstance);
+ }
+ });
+ }
+ catch (Exception e)
+ {
+ //do nothing
+ this.resetSuccessful = false;
+ }
+ catch (LinkageError e)
+ {
+ //do nothing - a new version of weld is used which introduced other required dependencies.
+ //WELD-1072 should be fixed in this version already
+ this.resetSuccessful = false;
+ }
+
applicationContext.invalidate();
+
+ if (beanStoreHolder != null)
+ {
+ Iterator<String> idIterator = originalBeanStore.iterator();
+
+ String currentId;
+ ContextualInstance<Object> currentContextualInstance;
+ while (idIterator.hasNext())
+ {
+ currentId = idIterator.next();
+ currentContextualInstance = originalBeanStore.get(currentId);
+
+ //keep (weld) internal application scoped beans - TODO check possible side-effects
+ if (currentContextualInstance.getInstance().getClass().getName().startsWith("org.jboss."))
+ {
+ //internalBeanList.add(currentContextualInstance);
+ continue;
+ }
+ idIterator.remove();
+ }
+
+ beanStoreHolder.set(originalBeanStore);
+ }
+
this.applicationScopeStarted = false;
+
+ this.resetSuccessful = true;
}
}
@@ -112,15 +210,10 @@ public class ContextController
{
if (this.sessionContext.isActive())
{
- try
- {
- this.sessionContext.invalidate();
- this.sessionContext.deactivate();
- }
- finally
- {
- this.sessionContext.dissociate(this.sessionMap);
- }
+ this.sessionContext.invalidate();
+ this.sessionContext.deactivate();
+ this.sessionContext.dissociate(this.sessionMap);
+ this.sessionMap = null;
}
}
@@ -134,15 +227,9 @@ public class ContextController
{
if (conversationContext.isActive())
{
- try
- {
- this.conversationContext.invalidate();
- this.conversationContext.deactivate();
- }
- finally
- {
- this.conversationContext.dissociate(new MutableBoundRequest(this.requestMap, this.sessionMap));
- }
+ this.conversationContext.invalidate();
+ this.conversationContext.deactivate();
+ this.conversationContext.dissociate(new MutableBoundRequest(this.requestMap, this.sessionMap));
}
}
@@ -165,15 +252,10 @@ public class ContextController
{
if (this.requestContext.isActive())
{
- try
- {
- this.requestContext.invalidate();
- this.requestContext.deactivate();
- }
- finally
- {
- this.requestContext.dissociate(this.requestMap);
- }
+ this.requestContext.invalidate();
+ this.requestContext.deactivate();
+ this.requestContext.dissociate(this.requestMap);
+ this.requestMap = null;
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/4b648d06/deltaspike/cdise/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/cdise/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java b/deltaspike/cdise/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
index ff9fc8f..da1bfa3 100644
--- a/deltaspike/cdise/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
+++ b/deltaspike/cdise/tck/src/main/java/org/apache/deltaspike/cdise/tck/ContainerCtrlTckTest.java
@@ -84,9 +84,6 @@ public class ContainerCtrlTckTest
cc.stop();
}
- //X TODO reactivate after the update to owb 1.1.4
- //@Test
-
/**
* Stops and starts: application-, session- and request-scope.
* <p/>
@@ -96,6 +93,7 @@ public class ContainerCtrlTckTest
* <p/>
* If the deepest ref has the expected value, all levels in between were resetted correctly.
*/
+ @Test
public void reStartContexts()
{
CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
@@ -129,11 +127,16 @@ public class ContainerCtrlTckTest
try
{
Assert.assertNotNull(carRepair.getCar());
- Assert.fail();
+
+ //not supported by weld - AbstractSharedContext#isActive always return true
+ if (!cdiContainer.getClass().getName().contains(".weld."))
+ {
+ Assert.fail();
+ }
}
catch (ContextNotActiveException e)
{
- //exception expected
+ //do nothing - exception expected
}
cdiContainer.startContexts();