Skip to content

Commit

Permalink
Fix clustering test (don't serialize beanstore)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickarls committed Apr 8, 2010
1 parent 81825f5 commit 28e0d68
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
Expand Up @@ -37,6 +37,7 @@
import org.jboss.weld.context.api.BeanStore;
import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
import org.jboss.weld.context.beanstore.HashMapBeanStore;
import org.jboss.weld.servlet.HttpPassThruSessionBeanStore;
import org.slf4j.cal10n.LocLogger;

/**
Expand Down Expand Up @@ -201,8 +202,7 @@ private void activateConversationContext()

private void activateSessionContext()
{
// activateContext(sessionContext, new HttpPassThruSessionBeanStore());
sessionContext.setActive(true);
activateContext(sessionContext, new HttpPassThruSessionBeanStore());
}

public void endApplication()
Expand Down
15 changes: 8 additions & 7 deletions impl/src/main/java/org/jboss/weld/servlet/ServletLifecycle.java
Expand Up @@ -91,14 +91,15 @@ public ServletLifecycle(ContextLifecycle lifecycle)
*/
public void beginRequest(HttpServletRequest request)
{
if (!RequestBeanStoreCache.isSet(request))
if (RequestBeanStoreCache.isSet(request))
{
BeanStore requestBeanStore = new ConcurrentHashMapBeanStore();
RequestBeanStoreCache.set(request, requestBeanStore);
lifecycle.beginRequest(request.getRequestURI(), requestBeanStore);
restoreSessionContext(request);
restoreConversationContext(request);
return;
}
BeanStore requestBeanStore = new ConcurrentHashMapBeanStore();
RequestBeanStoreCache.set(request, requestBeanStore);
lifecycle.beginRequest(request.getRequestURI(), requestBeanStore);
restoreSessionContext(request);
restoreConversationContext(request);
}

/**
Expand All @@ -111,7 +112,7 @@ public void beginRequest(HttpServletRequest request)
protected BeanStore restoreSessionContext(HttpServletRequest request)
{
HttpPassThruSessionBeanStore sessionBeanStore = HttpPassThruOnDemandSessionBeanStore.of(request);
HttpSession session = request.getSession(true);
HttpSession session = request.getSession();
String sessionId = session == null ? "Inactive session" : session.getId();
lifecycle.restoreSession(sessionId, sessionBeanStore);
if (session != null)
Expand Down
Expand Up @@ -23,36 +23,44 @@
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.spi.PassivationCapable;

import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.api.Singleton;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.context.api.BeanStore;
import org.jboss.weld.context.api.ContextualInstance;
import org.jboss.weld.context.beanstore.HashMapBeanStore;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.mock.MockEELifecycle;
import org.jboss.weld.mock.TestContainer;
import org.jboss.weld.serialization.spi.helpers.SerializableContextual;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class AbstractClusterTest
{

private Singleton<Container> singleton;

@BeforeClass
public void beforeClass() throws Exception
{
singleton = (Singleton) getInstanceField().get(null);
getInstanceField().set(null, new SwitchableSingletonProvider().create(Container.class));
}

private static Field getInstanceField() throws Exception
{
Field field = Container.class.getDeclaredField("instance");
field.setAccessible(true);
return field;
}

@AfterClass
public void afterClass() throws Exception
{
Expand All @@ -63,26 +71,48 @@ protected TestContainer bootstrapContainer(int id, Collection<Class<?>> classes)
{
// Bootstrap container
SwitchableSingletonProvider.use(id);

TestContainer container = new TestContainer(new MockEELifecycle(), classes, null);
container.startContainer();
container.ensureRequestActive();

return container;
}

protected void use(int id)
{
SwitchableSingletonProvider.use(id);
}

private Map<String, ContextualInstance<?>> getContextualInstances(BeanStore beanStore)
{
Map<String, ContextualInstance<?>> instances = new HashMap<String, ContextualInstance<?>>();
for (String id : beanStore.getContextualIds())
{
instances.put(id, beanStore.get(id));
}
return instances;
}

private BeanStore setContextualInstances(Map<String, ContextualInstance<?>> instances)
{
BeanStore beanStore = new HashMapBeanStore();
for (Map.Entry<String, ContextualInstance<?>> i : instances.entrySet())
{
beanStore.put(i.getKey(), i.getValue());
}
return beanStore;
}

@SuppressWarnings("unchecked")
protected void replicateSession(int fromId, BeanManagerImpl fromBeanManager, int toId, BeanManagerImpl toBeanManager) throws Exception
{
// Mimic replicating the session
BeanStore sessionBeanStore = fromBeanManager.getServices().get(ContextLifecycle.class).getSessionContext().getBeanStore();
byte[] bytes = serialize(sessionBeanStore);
Map<String, ContextualInstance<?>> contextuals = getContextualInstances(sessionBeanStore);
byte[] bytes = serialize(contextuals);
use(toId);
BeanStore replicatedSessionBeanStore = (BeanStore) deserialize(bytes);
BeanStore replicatedSessionBeanStore = setContextualInstances((Map<String, ContextualInstance<?>>) deserialize(bytes));
toBeanManager.getServices().get(ContextLifecycle.class).getSessionContext().setBeanStore(replicatedSessionBeanStore);
use(fromId);
}
Expand Down

0 comments on commit 28e0d68

Please sign in to comment.