[webbeans-commits] Webbeans SVN: r320 - ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Tue Nov 18 02:14:06 EST 2008
Author: nickarls
Date: 2008-11-18 02:14:06 -0500 (Tue, 18 Nov 2008)
New Revision: 320
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SimpleBeanMap.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/BeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java
Log:
Contexts refactoring
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/BeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/BeanMap.java 2008-11-18 04:15:08 UTC (rev 319)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/BeanMap.java 2008-11-18 07:14:06 UTC (rev 320)
@@ -1,38 +1,12 @@
package org.jboss.webbeans.contexts;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
import javax.webbeans.manager.Bean;
-import com.google.common.collect.ForwardingMap;
-
-public class BeanMap extends ForwardingMap<Bean<? extends Object>, Object>
+public interface BeanMap
{
-
- protected Map<Bean<? extends Object>, Object> delegate;
-
- public BeanMap()
- {
- delegate = new ConcurrentHashMap<Bean<? extends Object>, Object>();
- }
-
- @SuppressWarnings("unchecked")
- public <T extends Object> T get(Bean<? extends T> bean)
- {
- return (T) super.get(bean);
- }
-
- @Override
- public Map<Bean<? extends Object>, Object> delegate()
- {
- return delegate;
- }
-
- @SuppressWarnings("unchecked")
- public <T extends Object> T remove(Bean<? extends T> bean)
- {
- return (T) super.remove(bean);
- }
-
+ public abstract <T extends Object> T get(Bean<? extends T> bean);
+ public abstract <T extends Object> T remove(Bean<? extends T> bean);
+ public abstract void clear();
+ public abstract Iterable<Bean<? extends Object>> keySet();
+ public abstract <T extends Object> T put(Bean<? extends T> bean, T instance);
}
\ No newline at end of file
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java 2008-11-18 04:15:08 UTC (rev 319)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java 2008-11-18 07:14:06 UTC (rev 320)
@@ -12,16 +12,11 @@
{
super(scopeType);
beans = new ThreadLocal<BeanMap>();
- beans.set(new BeanMap());
+ beans.set(new SimpleBeanMap());
active = new ThreadLocal<AtomicBoolean>();
active.set(new AtomicBoolean(true));
}
- public void setBeans(BeanMap beans)
- {
- this.beans.set(beans);
- }
-
@Override
protected AtomicBoolean getActive()
{
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java 2008-11-18 04:15:08 UTC (rev 319)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java 2008-11-18 07:14:06 UTC (rev 320)
@@ -1,40 +1,93 @@
package org.jboss.webbeans.contexts;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
import javax.servlet.http.HttpSession;
import javax.webbeans.manager.Bean;
import org.jboss.webbeans.ManagerImpl;
-public class SessionBeanMap extends BeanMap
+public class SessionBeanMap implements BeanMap
{
+ private static final String KEY_PREFIX = "SessionScoped#";
+
private HttpSession session;
private ManagerImpl manager;
-
- public SessionBeanMap(ManagerImpl manager) {
+ private BeanMap cache;
+
+ public SessionBeanMap(ManagerImpl manager)
+ {
super();
this.manager = manager;
+ cache = new SimpleBeanMap();
}
- public void setSession(HttpSession session) {
+ public void setSession(HttpSession session)
+ {
this.session = session;
}
-
- @Override
+
+ @SuppressWarnings("unchecked")
public <T> T get(Bean<? extends T> bean)
{
- String id = Integer.toString(manager.getBeans().indexOf(bean));
- T instance = super.get(bean);
- session.setAttribute(id, instance);
+ T instance = cache.get(bean);
+ if (instance != null)
+ {
+ return instance;
+ }
+ String id = KEY_PREFIX + manager.getBeans().indexOf(bean);
+ instance = (T) session.getAttribute(id);
+ if (instance != null)
+ {
+ cache.put(bean, instance);
+ }
return instance;
}
- @Override
public <T> T remove(Bean<? extends T> bean)
{
- T instance = super.remove(bean);
- String id = Integer.toString(manager.getBeans().indexOf(bean));
+ T instance = get(bean);
+ String id = KEY_PREFIX + manager.getBeans().indexOf(bean);
session.removeAttribute(id);
+ cache.remove(bean);
return instance;
}
+ @SuppressWarnings("unchecked")
+ public void clear()
+ {
+ Enumeration names = session.getAttributeNames();
+ while (names.hasMoreElements()) {
+ String name = (String) names.nextElement();
+ session.removeAttribute(name);
+ }
+ cache.clear();
+ }
+
+ public Iterable<Bean<? extends Object>> keySet()
+ {
+ List<Bean<?>> beans = new ArrayList<Bean<?>>();
+
+ Enumeration names = session.getAttributeNames();
+ while (names.hasMoreElements()) {
+ String name = (String) names.nextElement();
+ if (name.startsWith(KEY_PREFIX)) {
+ String id = name.substring(KEY_PREFIX.length());
+ Bean<?> bean = manager.getBeans().get(Integer.parseInt(id));
+ beans.add(bean);
+ }
+ }
+
+ return beans;
+ }
+
+ public <T> T put(Bean<? extends T> bean, T instance)
+ {
+ String id = KEY_PREFIX + manager.getBeans().indexOf(bean);
+ session.setAttribute(id, instance);
+ return cache.put(bean, instance);
+ }
+
}
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-18 04:15:08 UTC (rev 319)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java 2008-11-18 07:14:06 UTC (rev 320)
@@ -6,12 +6,12 @@
public class SessionContext extends PrivateContext {
- private ThreadLocal<SessionBeanMap> beans;
+ private ThreadLocal<BeanMap> beans;
public SessionContext(ManagerImpl manager)
{
super(SessionScoped.class);
- beans = new ThreadLocal<SessionBeanMap>();
+ beans = new ThreadLocal<BeanMap>();
beans.set(new SessionBeanMap(manager));
}
Modified: 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 2008-11-18 04:15:08 UTC (rev 319)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java 2008-11-18 07:14:06 UTC (rev 320)
@@ -11,7 +11,7 @@
public SharedContext(Class<? extends Annotation> scopeType)
{
super(scopeType);
- beans = new BeanMap();
+ beans = new SimpleBeanMap();
active = new AtomicBoolean(true);
}
Copied: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SimpleBeanMap.java (from rev 319, ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/BeanMap.java)
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SimpleBeanMap.java (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SimpleBeanMap.java 2008-11-18 07:14:06 UTC (rev 320)
@@ -0,0 +1,53 @@
+package org.jboss.webbeans.contexts;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.webbeans.manager.Bean;
+
+import com.google.common.collect.ForwardingMap;
+
+public class SimpleBeanMap extends ForwardingMap<Bean<? extends Object>, Object> implements BeanMap
+{
+
+ protected Map<Bean<? extends Object>, Object> delegate;
+
+ public SimpleBeanMap()
+ {
+ delegate = new ConcurrentHashMap<Bean<? extends Object>, Object>();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Object> T get(Bean<? extends T> bean)
+ {
+ return (T) super.get(bean);
+ }
+
+ @Override
+ public Map<Bean<? extends Object>, Object> delegate()
+ {
+ return delegate;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Object> T remove(Bean<? extends T> bean)
+ {
+ return (T) super.remove(bean);
+ }
+
+ public void clear() {
+ delegate.clear();
+ }
+
+ public Set<Bean<? extends Object>> keySet() {
+ return delegate.keySet();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T put(Bean<? extends T> bean, T instance)
+ {
+ return (T) delegate.put(bean, instance);
+ }
+
+}
\ No newline at end of file
Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SimpleBeanMap.java
___________________________________________________________________
Name: svn:mergeinfo
+
More information about the weld-commits
mailing list