[webbeans-commits] Webbeans SVN: r311 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/contexts and 2 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Sun Nov 16 07:38:37 EST 2008
Author: nickarls
Date: 2008-11-16 07:38:37 -0500 (Sun, 16 Nov 2008)
New Revision: 311
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java
Removed:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Lifecycle.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/NormalContext.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ApplicationContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ConversationContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/DependentContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/RequestContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ManagerTest.java
Log:
ThreadLocal private contexts (still serialized whole beanmap, thoug)
Deleted: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Lifecycle.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Lifecycle.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/Lifecycle.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -1,23 +0,0 @@
-package org.jboss.webbeans;
-
-
-public class Lifecycle
-{
- public static void beginApplication()
- {
- }
-
- public static void endApplication()
- {
- }
-
- public static void beginSession()
- {
- }
-
- public static void endSession()
- {
- }
-
-
-}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -2,7 +2,6 @@
import java.lang.annotation.Annotation;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -3,6 +3,7 @@
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Context;
@@ -21,10 +22,10 @@
public abstract class AbstractContext implements Context
{
- protected class BeanMap extends ForwardingMap<Bean<? extends Object>, Object>
+ public class BeanMap extends ForwardingMap<Bean<? extends Object>, Object>
{
- Map<Bean<? extends Object>, Object> delegate;
+ protected Map<Bean<? extends Object>, Object> delegate;
public BeanMap()
{
@@ -46,12 +47,12 @@
}
private Class<? extends Annotation> scopeType;
- protected boolean active;
+ protected AtomicBoolean active;
public AbstractContext(Class<? extends Annotation> scopeType)
{
this.scopeType = scopeType;
-
+ active = new AtomicBoolean(true);
}
public abstract <T> T get(Bean<T> bean, boolean create);
@@ -65,12 +66,12 @@
public boolean isActive()
{
- return active;
+ return active.get();
}
- public void setActive(boolean active)
+ public void setActive(boolean value)
{
- this.active = active;
+ active.set(value);
}
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ApplicationContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ApplicationContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ApplicationContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -2,7 +2,7 @@
import javax.webbeans.ApplicationScoped;
-public class ApplicationContext extends NormalContext {
+public class ApplicationContext extends SharedContext {
public ApplicationContext()
{
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ConversationContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ConversationContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ConversationContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -3,7 +3,7 @@
import javax.webbeans.ConversationScoped;
-public class ConversationContext extends NormalContext {
+public class ConversationContext extends PrivateContext {
public ConversationContext()
{
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/DependentContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/DependentContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/DependentContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -11,13 +11,13 @@
public DependentContext()
{
super(Dependent.class);
- active = false;
+ setActive(false);
}
@Override
public <T> T get(Bean<T> bean, boolean create)
{
- if (!active)
+ if (!isActive())
{
throw new ContextNotActiveException();
}
Deleted: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/NormalContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/NormalContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/NormalContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -1,58 +0,0 @@
-package org.jboss.webbeans.contexts;
-
-import java.lang.annotation.Annotation;
-
-import javax.webbeans.ContextNotActiveException;
-import javax.webbeans.manager.Bean;
-import javax.webbeans.manager.Manager;
-
-public abstract class NormalContext extends AbstractContext
-{
- private BeanMap beans;
-
- public NormalContext(Class<? extends Annotation> scopeType)
- {
- super(scopeType);
- beans = new BeanMap();
- // TODO active on create?
- active = true;
- }
-
- public <T> T get(Bean<T> bean, boolean create)
- {
- if (!active)
- {
- throw new ContextNotActiveException();
- }
- T instance = beans.get(bean);
- if (instance != null)
- {
- return instance;
- }
- if (!create)
- {
- return null;
- }
-
- // TODO should bean creation be synchronized?
- instance = bean.create();
- beans.put(bean, instance);
- return instance;
- }
-
- private <T> void destroy(Manager manager, Bean<T> bean)
- {
- bean.destroy(beans.get(bean));
- }
-
- public void destroy(Manager manager)
- {
- for (Bean<? extends Object> bean : beans.keySet())
- {
- destroy(manager, bean);
- }
- beans = new BeanMap();
- active = false;
- }
-
-}
Copied: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java (from rev 310, ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/NormalContext.java)
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -0,0 +1,76 @@
+package org.jboss.webbeans.contexts;
+
+import java.lang.annotation.Annotation;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.webbeans.ContextNotActiveException;
+import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Manager;
+
+public abstract class PrivateContext extends AbstractContext
+{
+ private ThreadLocal<AtomicBoolean> active;
+ private ThreadLocal<BeanMap> beans;
+
+ public PrivateContext(Class<? extends Annotation> scopeType)
+ {
+ super(scopeType);
+ beans = new ThreadLocal<BeanMap>();
+ beans.set(new BeanMap());
+ active = new ThreadLocal<AtomicBoolean>();
+ active.set(new AtomicBoolean(true));
+ }
+
+ public void setBeans(BeanMap beans) {
+ this.beans.set(beans);
+ }
+
+ public <T> T get(Bean<T> bean, boolean create)
+ {
+ if (!isActive())
+ {
+ throw new ContextNotActiveException();
+ }
+ T instance = beans.get().get(bean);
+ if (instance != null)
+ {
+ return instance;
+ }
+ if (!create)
+ {
+ return null;
+ }
+
+ // TODO should bean creation be synchronized?
+ instance = bean.create();
+ beans.get().put(bean, instance);
+ return instance;
+ }
+
+ private <T> void destroy(Manager manager, Bean<T> bean)
+ {
+ bean.destroy(beans.get().get(bean));
+ }
+
+ public void destroy(Manager manager)
+ {
+ for (Bean<? extends Object> bean : beans.get().keySet())
+ {
+ destroy(manager, bean);
+ }
+ beans.set(new BeanMap());
+ }
+
+ @Override
+ public boolean isActive()
+ {
+ return active.get().get();
+ }
+
+ @Override
+ public void setActive(boolean value)
+ {
+ active.get().set(value);
+ }
+
+}
Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/RequestContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/RequestContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/RequestContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -2,7 +2,7 @@
import javax.webbeans.RequestScoped;
-public class RequestContext extends NormalContext
+public class RequestContext extends PrivateContext
{
public RequestContext()
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -2,7 +2,7 @@
import javax.webbeans.SessionScoped;
-public class SessionContext extends NormalContext {
+public class SessionContext extends PrivateContext {
public SessionContext()
{
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -0,0 +1,56 @@
+package org.jboss.webbeans.contexts;
+
+import java.lang.annotation.Annotation;
+
+import javax.webbeans.ContextNotActiveException;
+import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Manager;
+
+public abstract class SharedContext extends AbstractContext
+{
+ private BeanMap beans;
+
+ public SharedContext(Class<? extends Annotation> scopeType)
+ {
+ super(scopeType);
+ beans = new BeanMap();
+ setActive(true);
+ }
+
+ public <T> T get(Bean<T> bean, boolean create)
+ {
+ if (!isActive())
+ {
+ throw new ContextNotActiveException();
+ }
+ T instance = beans.get(bean);
+ if (instance != null)
+ {
+ return instance;
+ }
+ if (!create)
+ {
+ return null;
+ }
+
+ // TODO should bean creation be synchronized?
+ instance = bean.create();
+ beans.put(bean, instance);
+ return instance;
+ }
+
+ private <T> void destroy(Manager manager, Bean<T> bean)
+ {
+ bean.destroy(beans.get(bean));
+ }
+
+ public void destroy(Manager manager)
+ {
+ for (Bean<? extends Object> bean : beans.keySet())
+ {
+ destroy(manager, bean);
+ }
+ beans = new BeanMap();
+ }
+
+}
\ No newline at end of file
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -3,12 +3,17 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
+import javax.webbeans.SessionScoped;
-import org.jboss.webbeans.Lifecycle;
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.contexts.SessionContext;
+import org.jboss.webbeans.contexts.AbstractContext.BeanMap;
+import org.jboss.webbeans.util.JNDI;
public class ServletLifecycle
{
private static ServletContext servletContext;
+ private static final String SESSION_BEANMAP_KEY = "org.jboss.webbeans.context.session.data";
public static void beginApplication(ServletContext context)
{
@@ -21,14 +26,16 @@
public static void beginSession(HttpSession session)
{
- Lifecycle.beginSession();
}
public static void endSession(HttpSession session) {
- Lifecycle.endSession();
}
public static void beginRequest(HttpServletRequest request) {
+ ManagerImpl manager = (ManagerImpl) JNDI.lookup("manager");
+ SessionContext sessionContext = (SessionContext) manager.getContext(SessionScoped.class);
+ BeanMap sessionBeans = (BeanMap) request.getAttribute(SESSION_BEANMAP_KEY);
+ sessionContext.setBeans(sessionBeans);
}
public static void endRequest(HttpServletRequest request) {
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ManagerTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ManagerTest.java 2008-11-14 13:12:23 UTC (rev 310)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ManagerTest.java 2008-11-16 12:38:37 UTC (rev 311)
@@ -4,7 +4,7 @@
import javax.webbeans.RequestScoped;
import javax.webbeans.manager.Context;
-import org.jboss.webbeans.contexts.NormalContext;
+import org.jboss.webbeans.contexts.PrivateContext;
import org.jboss.webbeans.contexts.RequestContext;
import org.testng.annotations.Test;
@@ -22,7 +22,7 @@
public void testGetContextWithNoActiveContextsFails()
{
Context requestContext = new RequestContext();
- ((NormalContext)requestContext).setActive(false);
+ ((PrivateContext)requestContext).setActive(false);
manager.setContexts(requestContext);
manager.getContext(RequestScoped.class);
}
More information about the weld-commits
mailing list