[webbeans-commits] Webbeans SVN: r413 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/contexts and 3 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Fri Dec 5 07:04:56 EST 2008


Author: pete.muir at jboss.org
Date: 2008-12-05 07:04:56 -0500 (Fri, 05 Dec 2008)
New Revision: 413

Added:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractBeanMapAdaptor.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java
Removed:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.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/ApplicationContext.java
   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/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/contexts/SharedContext.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/AbstractTest.java
   ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
Log:
better structure for contexts

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -75,7 +75,7 @@
 {
    public static final String JNDI_KEY = "java:comp/Manager";
    
-   private static ManagerImpl instance = new ManagerImpl();
+   protected static ManagerImpl instance = new ManagerImpl();
    
    public static ManagerImpl instance()
    {
@@ -157,7 +157,7 @@
       {
          addContext(new DependentContext());
          addContext(new RequestContext());
-         addContext(new SessionContext(this));
+         addContext(new SessionContext());
          addContext(new ApplicationContext());
       }
       else
@@ -690,12 +690,6 @@
       return buffer.toString();
    }
 
-   // TODO: Temp
-   public static void setInstance(ManagerImpl manager)
-   {
-      ManagerImpl.instance = manager;
-   }
-
    public Manager parse(InputStream xmlStream)
    {
       // TODO Auto-generated method stub

Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractBeanMapAdaptor.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractBeanMapAdaptor.java	                        (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractBeanMapAdaptor.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -0,0 +1,24 @@
+package org.jboss.webbeans.contexts;
+
+import javax.webbeans.manager.Contextual;
+
+import org.jboss.webbeans.ManagerImpl;
+
+public abstract class AbstractBeanMapAdaptor implements BeanMap
+{
+   
+   protected abstract String getKeyPrefix();
+   
+   /**
+    * Returns a map key to a bean. Uses a known prefix and appends the index of
+    * the Bean in the Manager bean list.
+    * 
+    * @param bean The bean to generate a key for.
+    * @return A unique key;
+    */
+   protected String getBeanKey(Contextual<?> bean)
+   {
+      return getKeyPrefix() + "#" + ManagerImpl.instance().getBeans().indexOf(bean);
+   }
+   
+}


Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/AbstractBeanMapAdaptor.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/ApplicationContext.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -19,6 +19,8 @@
 
 import javax.webbeans.ApplicationScoped;
 
+import org.jboss.webbeans.ManagerImpl;
+
 /**
  * The Application context
  * 
@@ -29,9 +31,27 @@
 public class ApplicationContext extends SharedContext
 {
 
+   private BeanMap beanMap;
+   
    public ApplicationContext()
    {
       super(ApplicationScoped.class);
    }
 
+   @Override
+   public BeanMap getBeanMap()
+   {
+      return this.beanMap;
+   }
+   
+   public void setBeanMap(BeanMap applicationBeanMap)
+   {
+      this.beanMap = applicationBeanMap;
+   }
+   
+   public static ApplicationContext instance()
+   {
+      return (ApplicationContext) ManagerImpl.instance().getBuiltInContext(ApplicationScoped.class);
+   }
+   
 }

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/BeanMap.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -26,7 +26,7 @@
  * @author Nicklas Karlsson
  * 
  * @see org.jboss.webbeans.contexts.SimpleBeanMap
- * @see org.jboss.webbeans.contexts.SessionBeanMap
+ * @see org.jboss.webbeans.servlet.SessionBeanMap
  */
 public interface BeanMap
 {

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -24,6 +24,8 @@
  * The abstraction of a private context, on that operates on a ThreadLocal
  * BeanMap and ThreadLocal active state
  * 
+ * A private context doesn't rely on some external context to hold it's state
+ * 
  * @author Nicklas Karlsson
  * 
  * @see org.jboss.webbeans.contexts.DependentContext
@@ -39,8 +41,16 @@
    public PrivateContext(Class<? extends Annotation> scopeType)
    {
       super(scopeType);
-      beans = new ThreadLocal<BeanMap>();
-      beans.set(new SimpleBeanMap());
+      beans = new ThreadLocal<BeanMap>()
+      {
+         
+         @Override
+         protected BeanMap initialValue()
+         {
+            return new SimpleBeanMap();
+         }
+         
+      };
       active = new ThreadLocal<AtomicBoolean>();
       active.set(new AtomicBoolean(true));
    }

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/RequestContext.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -19,6 +19,8 @@
 
 import javax.webbeans.RequestScoped;
 
+import org.jboss.webbeans.ManagerImpl;
+
 /**
  * The request context
  * 
@@ -31,5 +33,11 @@
    {
       super(RequestScoped.class);
    }
+   
+   public static RequestContext instance()
+   {
+      return (RequestContext) ManagerImpl.instance().getBuiltInContext(RequestScoped.class);
+   }
+   
 
 }

Deleted: 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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -1,232 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed 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.
- */
-
-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 javax.webbeans.manager.Contextual;
-
-import org.jboss.webbeans.ManagerImpl;
-import org.jboss.webbeans.log.LogProvider;
-import org.jboss.webbeans.log.Logging;
-
-/**
- * A BeanMap that uses a HTTP session as backing map
- * 
- * @author Nicklas Karlsson
- * 
- * @see org.jboss.webbeans.contexts.SessionContext
- */
-public class SessionBeanMap implements BeanMap
-{
-   private static LogProvider log = Logging.getLogProvider(SessionBeanMap.class);
-
-   // The HTTP session to use as backing map
-   private HttpSession session;
-   // The storage prefix to put before names
-   private String keyPrefix;
-
-   /**
-    * Constructor
-    * 
-    * @param manager The Web Beans manager
-    * @param keyPrefix The storage names prefix
-    */
-   public SessionBeanMap(String keyPrefix)
-   {
-      super();
-      this.keyPrefix = keyPrefix;
-      log.trace("SessionBeanMap created with prefix " + keyPrefix);
-   }
-
-   /**
-    * The SessionBeanMap requires a HTTP session to work. It is created without
-    * one so this method must be called before it can be operated upon
-    * 
-    * @param session The session to use as a backing map
-    */
-   public void setSession(HttpSession session)
-   {
-      this.session = session;
-      log.trace("Session context associated with session id " + session.getId());
-   }
-
-   /**
-    * Used to check if the session has been set and throws an exception if it's
-    * null.
-    */
-   private void checkSession()
-   {
-      if (session == null)
-      {
-         throw new IllegalArgumentException("Session has not been initialized in SessionBeanMap");
-      }
-   }
-
-   /**
-    * Returns a map key to a bean. Uses a known prefix and appends the index of
-    * the Bean in the Manager bean list.
-    * 
-    * @param bean The bean to generate a key for.
-    * @return A unique key;
-    */
-   private String getBeanKey(Contextual<?> bean)
-   {
-      return keyPrefix + ManagerImpl.instance().getBeans().indexOf(bean);
-   }
-
-   /**
-    * Gets a bean from the session
-    * 
-    * First, checks that the session is present. It determines an ID for the
-    * bean which and looks for it in the session. The bean instance is returned
-    * (null if not found in the session).
-    * 
-    * @param bean The bean to get from the session
-    * @return An instance of the bean
-    * 
-    * @see org.jboss.webbeans.contexts.BeanMap#get(Bean)
-    */
-   @SuppressWarnings("unchecked")
-   public <T> T get(Contextual<? extends T> bean)
-   {
-      checkSession();
-      String key = getBeanKey(bean);
-      T instance = (T) session.getAttribute(key);
-      log.trace("Searched session for key " + key + " and got " + instance);
-      return instance;
-   }
-
-   /**
-    * Removes a bean instance from the session
-    * 
-    * First, checks that the session is present. It determines an ID for the
-    * bean and that key is then removed from the session, whether it was present
-    * in the first place or not.
-    * 
-    * @param bean The bean whose instance to remove.
-    * @return The instance removed
-    * 
-    * @see org.jboss.webbeans.contexts.BeanMap#remove(Bean)
-    */
-   public <T> T remove(Contextual<? extends T> bean)
-   {
-      checkSession();
-      T instance = get(bean);
-      String key = getBeanKey(bean);
-      session.removeAttribute(key);
-      log.trace("Removed bean " + bean + " with key " + key + " from session");
-      return instance;
-   }
-
-   /**
-    * Clears the session of any beans.
-    * 
-    * First, checks that the session is present. Then, iterates over the
-    * attribute names in the session and removes them if they start with the
-    * know prefix.
-    * 
-    * @see org.jboss.webbeans.contexts.BeanMap#clear()
-    */
-   @SuppressWarnings("unchecked")
-   public void clear()
-   {
-      checkSession();
-      Enumeration names = session.getAttributeNames();
-      while (names.hasMoreElements())
-      {
-         String name = (String) names.nextElement();
-         session.removeAttribute(name);
-      }
-      log.trace("Session cleared");
-   }
-
-   /**
-    * Gets an iterable over the beans present in the storage.
-    * 
-    * Iterates over the names in the session. If a name starts with the known
-    * prefix, strips it out to get the index to the bean in the manager bean
-    * list. Retrieves the bean from that list and puts it in the result-list.
-    * Finally, returns the list.
-    * 
-    * @return An Iterable to the beans in the storage
-    * 
-    * @see org.jboss.webbeans.contexts.BeanMap#keySet()
-    */
-   @SuppressWarnings("unchecked")
-   public Iterable<Contextual<? extends Object>> keySet()
-   {
-      checkSession();
-
-      List<Contextual<?>> beans = new ArrayList<Contextual<?>>();
-
-      Enumeration names = session.getAttributeNames();
-      while (names.hasMoreElements())
-      {
-         String name = (String) names.nextElement();
-         if (name.startsWith(keyPrefix))
-         {
-            String id = name.substring(keyPrefix.length());
-            Contextual<?> bean = ManagerImpl.instance().getBeans().get(Integer.parseInt(id));
-            beans.add(bean);
-         }
-      }
-
-      return beans;
-   }
-
-   /**
-    * Puts a bean instance in the session
-    * 
-    * First, checks that the session is present. Generates a bean map key, puts
-    * the instance in the session under that key.
-    * 
-    * @param bean The bean to use as key
-    * @param instance The bean instance to add
-    * 
-    * @see org.jboss.webbeans.contexts.BeanMap#put(Bean, Object)
-    */
-   public <T> void put(Contextual<? extends T> bean, T instance)
-   {
-      checkSession();
-      String key = getBeanKey(bean);
-      session.setAttribute(key, instance);
-      log.trace("Stored instance " + instance + " for bean " + bean + " under key " + key + " in session");
-   }
-
-   @SuppressWarnings("unchecked")
-   @Override
-   public String toString()
-   {
-      StringBuilder buffer = new StringBuilder();
-      List<Contextual<?>> beans = (List) keySet();
-      buffer.append("Bean -> bean instance mappings in HTTP session: " + beans.size() + "\n");
-      int i = 0;
-      for (Contextual<?> bean : beans)
-      {
-         Object instance = get(bean);
-         buffer.append(++i + " - " + getBeanKey(bean) + ": " + instance + "\n");
-      }
-      return buffer.toString();
-   }
-
-}

Added: 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	                        (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -0,0 +1,232 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.
+ */
+
+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 javax.webbeans.manager.Contextual;
+
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * A BeanMap that uses a HTTP session as backing map
+ * 
+ * @author Nicklas Karlsson
+ * 
+ * @see org.jboss.webbeans.contexts.SessionContext
+ */
+public class SessionBeanMap implements BeanMap
+{
+   private static LogProvider log = Logging.getLogProvider(SessionBeanMap.class);
+
+   // The HTTP session to use as backing map
+   private HttpSession session;
+   // The storage prefix to put before names
+   private String keyPrefix;
+
+   /**
+    * Constructor
+    * 
+    * @param manager The Web Beans manager
+    * @param keyPrefix The storage names prefix
+    */
+   public SessionBeanMap(String keyPrefix)
+   {
+      super();
+      this.keyPrefix = keyPrefix;
+      log.trace("SessionBeanMap created with prefix " + keyPrefix);
+   }
+
+   /**
+    * The SessionBeanMap requires a HTTP session to work. It is created without
+    * one so this method must be called before it can be operated upon
+    * 
+    * @param session The session to use as a backing map
+    */
+   public void setSession(HttpSession session)
+   {
+      this.session = session;
+      log.trace("Session context associated with session id " + session.getId());
+   }
+
+   /**
+    * Used to check if the session has been set and throws an exception if it's
+    * null.
+    */
+   private void checkSession()
+   {
+      if (session == null)
+      {
+         throw new IllegalArgumentException("Session has not been initialized in SessionBeanMap");
+      }
+   }
+
+   /**
+    * Returns a map key to a bean. Uses a known prefix and appends the index of
+    * the Bean in the Manager bean list.
+    * 
+    * @param bean The bean to generate a key for.
+    * @return A unique key;
+    */
+   private String getBeanKey(Contextual<?> bean)
+   {
+      return keyPrefix + ManagerImpl.instance().getBeans().indexOf(bean);
+   }
+
+   /**
+    * Gets a bean from the session
+    * 
+    * First, checks that the session is present. It determines an ID for the
+    * bean which and looks for it in the session. The bean instance is returned
+    * (null if not found in the session).
+    * 
+    * @param bean The bean to get from the session
+    * @return An instance of the bean
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#get(Bean)
+    */
+   @SuppressWarnings("unchecked")
+   public <T> T get(Contextual<? extends T> bean)
+   {
+      checkSession();
+      String key = getBeanKey(bean);
+      T instance = (T) session.getAttribute(key);
+      log.trace("Searched session for key " + key + " and got " + instance);
+      return instance;
+   }
+
+   /**
+    * Removes a bean instance from the session
+    * 
+    * First, checks that the session is present. It determines an ID for the
+    * bean and that key is then removed from the session, whether it was present
+    * in the first place or not.
+    * 
+    * @param bean The bean whose instance to remove.
+    * @return The instance removed
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#remove(Bean)
+    */
+   public <T> T remove(Contextual<? extends T> bean)
+   {
+      checkSession();
+      T instance = get(bean);
+      String key = getBeanKey(bean);
+      session.removeAttribute(key);
+      log.trace("Removed bean " + bean + " with key " + key + " from session");
+      return instance;
+   }
+
+   /**
+    * Clears the session of any beans.
+    * 
+    * First, checks that the session is present. Then, iterates over the
+    * attribute names in the session and removes them if they start with the
+    * know prefix.
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#clear()
+    */
+   @SuppressWarnings("unchecked")
+   public void clear()
+   {
+      checkSession();
+      Enumeration names = session.getAttributeNames();
+      while (names.hasMoreElements())
+      {
+         String name = (String) names.nextElement();
+         session.removeAttribute(name);
+      }
+      log.trace("Session cleared");
+   }
+
+   /**
+    * Gets an iterable over the beans present in the storage.
+    * 
+    * Iterates over the names in the session. If a name starts with the known
+    * prefix, strips it out to get the index to the bean in the manager bean
+    * list. Retrieves the bean from that list and puts it in the result-list.
+    * Finally, returns the list.
+    * 
+    * @return An Iterable to the beans in the storage
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#keySet()
+    */
+   @SuppressWarnings("unchecked")
+   public Iterable<Contextual<? extends Object>> keySet()
+   {
+      checkSession();
+
+      List<Contextual<?>> beans = new ArrayList<Contextual<?>>();
+
+      Enumeration names = session.getAttributeNames();
+      while (names.hasMoreElements())
+      {
+         String name = (String) names.nextElement();
+         if (name.startsWith(keyPrefix))
+         {
+            String id = name.substring(keyPrefix.length());
+            Contextual<?> bean = ManagerImpl.instance().getBeans().get(Integer.parseInt(id));
+            beans.add(bean);
+         }
+      }
+
+      return beans;
+   }
+
+   /**
+    * Puts a bean instance in the session
+    * 
+    * First, checks that the session is present. Generates a bean map key, puts
+    * the instance in the session under that key.
+    * 
+    * @param bean The bean to use as key
+    * @param instance The bean instance to add
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#put(Bean, Object)
+    */
+   public <T> void put(Contextual<? extends T> bean, T instance)
+   {
+      checkSession();
+      String key = getBeanKey(bean);
+      session.setAttribute(key, instance);
+      log.trace("Stored instance " + instance + " for bean " + bean + " under key " + key + " in session");
+   }
+
+   @SuppressWarnings("unchecked")
+   @Override
+   public String toString()
+   {
+      StringBuilder buffer = new StringBuilder();
+      List<Contextual<?>> beans = (List) keySet();
+      buffer.append("Bean -> bean instance mappings in HTTP session: " + beans.size() + "\n");
+      int i = 0;
+      for (Contextual<?> bean : beans)
+      {
+         Object instance = get(bean);
+         buffer.append(++i + " - " + getBeanKey(bean) + ": " + instance + "\n");
+      }
+      return buffer.toString();
+   }
+
+}


Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -17,7 +17,6 @@
 
 package org.jboss.webbeans.contexts;
 
-import javax.servlet.http.HttpSession;
 import javax.webbeans.SessionScoped;
 
 import org.jboss.webbeans.ManagerImpl;
@@ -29,27 +28,34 @@
  * 
  * @author Nicklas Karlsson
  */
-public class SessionContext extends PrivateContext
+public class SessionContext extends SharedContext
 {
 
    private static LogProvider log = Logging.getLogProvider(SessionContext.class);
+   
+   private ThreadLocal<BeanMap> beanMap;
 
-   public SessionContext(ManagerImpl manager)
+   public SessionContext()
    {
       super(SessionScoped.class);
-      // Replaces the BeanMap implementation with a session-based one
-      beans.set(new SessionBeanMap(getScopeType().getName() + "#"));
       log.trace("Created session context");
    }
-
-   /**
-    * Sets the session in the session bean map
-    * 
-    * @param session The session to set
-    */
-   public void setSession(HttpSession session)
+   
+   @Override
+   public BeanMap getBeanMap()
    {
-      ((SessionBeanMap) getBeanMap()).setSession(session);
+      return beanMap.get();
    }
+   
+   @Override
+   public void setBeanMap(BeanMap beanMap)
+   {
+      this.beanMap.set(beanMap);
+   }
+   
+   public static SessionContext instance()
+   {
+      return (SessionContext) ManagerImpl.instance().getBuiltInContext(SessionScoped.class);
+   }
 
 }

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SharedContext.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -27,15 +27,13 @@
  * 
  * @see org.jboss.webbeans.contexts.ApplicationContext
  */
-public class SharedContext extends AbstractContext
+public abstract class SharedContext extends AbstractContext
 {
-   private BeanMap beans;
    private AtomicBoolean active;
 
    public SharedContext(Class<? extends Annotation> scopeType)
    {
       super(scopeType);
-      beans = new SimpleBeanMap();
       active = new AtomicBoolean(true);
    }
 
@@ -47,14 +45,9 @@
    {
       return active;
    }
+   
+   protected abstract BeanMap getBeanMap();
+   
+   public abstract void setBeanMap(BeanMap beanMap); 
 
-   /**
-    * Delegates to the map implementation
-    */
-   @Override
-   protected BeanMap getBeanMap()
-   {
-      return beans;
-   }
-
 }
\ No newline at end of file

Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java	                        (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.webbeans.servlet;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.webbeans.manager.Contextual;
+
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor;
+import org.jboss.webbeans.contexts.ApplicationContext;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * Abstracts the servlet API specific application context
+ * as a Map.
+ * 
+ * @author Gavin King
+ */
+public class ApplicationBeanMap extends AbstractBeanMapAdaptor
+{
+   private LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class);
+   
+   private ServletContext servletContext;
+
+   public ApplicationBeanMap(ServletContext servletContext)
+   {
+      this.servletContext = servletContext;
+   }
+
+   public void clear()
+   {
+      throw new UnsupportedOperationException(); 
+   }
+
+   @SuppressWarnings("unchecked")
+   public <T> T get(Contextual<? extends T> bean)
+   {
+      String key = getBeanKey(bean);
+      T instance = (T) servletContext.getAttribute(key);
+      log.trace("Searched application for key " + key + " and got " + instance);
+      return instance; 
+   }
+
+   public <T> void put(Contextual<? extends T> bean, T instance)
+   {
+      String key = getBeanKey(bean);
+      servletContext.setAttribute(key, instance);
+      log.trace("Stored instance " + instance + " for bean " + bean + " under key " + key + " in session");
+   }
+
+   @SuppressWarnings("unchecked")
+   public <T> T remove(Contextual<? extends T> bean)
+   {
+      String key = getBeanKey(bean);
+      T result = (T) servletContext.getAttribute(key);
+      servletContext.removeAttribute(key);
+      return result;
+   }
+
+   @SuppressWarnings("unchecked")
+   public Iterable<Contextual<? extends Object>> keySet()
+   {
+      List<Contextual<?>> beans = new ArrayList<Contextual<?>>();
+
+      Enumeration names = servletContext.getAttributeNames();
+      while (names.hasMoreElements())
+      {
+         String name = (String) names.nextElement();
+         if (name.startsWith(getKeyPrefix()))
+         {
+            String id = name.substring(getKeyPrefix().length());
+            Contextual<?> bean = ManagerImpl.instance().getBeans().get(Integer.parseInt(id));
+            beans.add(bean);
+         }
+      }
+
+      return beans;
+   }
+
+   @Override
+   protected String getKeyPrefix()
+   {
+      return ApplicationContext.class.getName();
+   }
+
+}


Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

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-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -23,11 +23,10 @@
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
-import javax.webbeans.SessionScoped;
 
-import org.jboss.webbeans.ManagerImpl;
 import org.jboss.webbeans.bootstrap.Bootstrap;
 import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
+import org.jboss.webbeans.contexts.ApplicationContext;
 import org.jboss.webbeans.contexts.SessionContext;
 import org.jboss.webbeans.log.LogProvider;
 import org.jboss.webbeans.log.Logging;
@@ -57,12 +56,15 @@
       servletContext = context;
       Bootstrap bootstrap = new Bootstrap();
       bootstrap.boot(getWebBeanDiscovery());
+      ApplicationContext.instance().setBeanMap(new ApplicationBeanMap(servletContext));
    }
    
    /**
     * Ends the application
     */
-   public static void endApplication() {
+   public static void endApplication() 
+   {
+      ApplicationContext.instance().setBeanMap(null);
       servletContext = null;
    }
    
@@ -80,7 +82,8 @@
     * 
     * @param session The HTTP session
     */
-   public static void endSession(HttpSession session) {
+   public static void endSession(HttpSession session) 
+   {
    }   
    
    /**
@@ -90,10 +93,9 @@
     * 
     * @param request The request
     */
-   public static void beginRequest(HttpServletRequest request) {
-      ManagerImpl manager = ManagerImpl.instance();
-      SessionContext sessionContext = (SessionContext) manager.getBuiltInContext(SessionScoped.class);
-      sessionContext.setSession(request.getSession(true));
+   public static void beginRequest(HttpServletRequest request) 
+   {
+      SessionContext.instance().setBeanMap(new SessionBeanMap(request.getSession()));
    }
    
    /**
@@ -101,7 +103,9 @@
     * 
     * @param request The request
     */
-   public static void endRequest(HttpServletRequest request) {
+   public static void endRequest(HttpServletRequest request) 
+   {
+      SessionContext.instance().setBeanMap(null);
    }
    
    /**

Copied: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java (from rev 408, ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java)
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java	                        (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -0,0 +1,197 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.
+ */
+
+package org.jboss.webbeans.servlet;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.servlet.http.HttpSession;
+import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Contextual;
+
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor;
+import org.jboss.webbeans.contexts.SessionContext;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * A BeanMap that uses a HTTP session as backing map
+ * 
+ * @author Nicklas Karlsson
+ * 
+ * @see org.jboss.webbeans.contexts.SessionContext
+ */
+public class SessionBeanMap extends AbstractBeanMapAdaptor
+{
+   private static LogProvider log = Logging.getLogProvider(SessionBeanMap.class);
+
+   // The HTTP session to use as backing map
+   private HttpSession session;
+
+   /**
+    * Constructor
+    * 
+    * @param manager The Web Beans manager
+    * @param keyPrefix The storage names prefix
+    */
+   public SessionBeanMap(HttpSession httpSession)
+   {
+      super();
+      this.session = httpSession;
+      log.trace("SessionBeanMap created with prefix " + getKeyPrefix());
+   }
+
+   /**
+    * Gets a bean from the session
+    * 
+    * It determines an ID for the
+    * bean which and looks for it in the session. The bean instance is returned
+    * (null if not found in the session).
+    * 
+    * @param bean The bean to get from the session
+    * @return An instance of the bean
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#get(Bean)
+    */
+   @SuppressWarnings("unchecked")
+   public <T> T get(Contextual<? extends T> bean)
+   {
+      String key = getBeanKey(bean);
+      T instance = (T) session.getAttribute(key);
+      log.trace("Searched session for key " + key + " and got " + instance);
+      return instance;
+   }
+
+   /**
+    * Removes a bean instance from the session
+    * 
+    * It determines an ID for the
+    * bean and that key is then removed from the session, whether it was present
+    * in the first place or not.
+    * 
+    * @param bean The bean whose instance to remove.
+    * @return The instance removed
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#remove(Bean)
+    */
+   public <T> T remove(Contextual<? extends T> bean)
+   {
+      T instance = get(bean);
+      String key = getBeanKey(bean);
+      session.removeAttribute(key);
+      log.trace("Removed bean " + bean + " with key " + key + " from session");
+      return instance;
+   }
+
+   /**
+    * Clears the session of any beans.
+    * 
+    * First, checks that the session is present. Then, iterates over the
+    * attribute names in the session and removes them if they start with the
+    * know prefix.
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#clear()
+    */
+   @SuppressWarnings("unchecked")
+   public void clear()
+   {
+      Enumeration names = session.getAttributeNames();
+      while (names.hasMoreElements())
+      {
+         String name = (String) names.nextElement();
+         session.removeAttribute(name);
+      }
+      log.trace("Session cleared");
+   }
+
+   /**
+    * Gets an iterable over the beans present in the storage.
+    * 
+    * Iterates over the names in the session. If a name starts with the known
+    * prefix, strips it out to get the index to the bean in the manager bean
+    * list. Retrieves the bean from that list and puts it in the result-list.
+    * Finally, returns the list.
+    * 
+    * @return An Iterable to the beans in the storage
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#keySet()
+    */
+   @SuppressWarnings("unchecked")
+   public Iterable<Contextual<? extends Object>> keySet()
+   {
+
+      List<Contextual<?>> beans = new ArrayList<Contextual<?>>();
+
+      Enumeration names = session.getAttributeNames();
+      while (names.hasMoreElements())
+      {
+         String name = (String) names.nextElement();
+         if (name.startsWith(getKeyPrefix()))
+         {
+            String id = name.substring(getKeyPrefix().length());
+            Contextual<?> bean = ManagerImpl.instance().getBeans().get(Integer.parseInt(id));
+            beans.add(bean);
+         }
+      }
+
+      return beans;
+   }
+
+   /**
+    * Puts a bean instance in the session
+    * 
+    * Generates a bean map key, puts
+    * the instance in the session under that key.
+    * 
+    * @param bean The bean to use as key
+    * @param instance The bean instance to add
+    * 
+    * @see org.jboss.webbeans.contexts.BeanMap#put(Bean, Object)
+    */
+   public <T> void put(Contextual<? extends T> bean, T instance)
+   {
+      String key = getBeanKey(bean);
+      session.setAttribute(key, instance);
+      log.trace("Stored instance " + instance + " for bean " + bean + " under key " + key + " in session");
+   }
+
+   @SuppressWarnings("unchecked")
+   @Override
+   public String toString()
+   {
+      StringBuilder buffer = new StringBuilder();
+      List<Contextual<?>> beans = (List) keySet();
+      buffer.append("Bean -> bean instance mappings in HTTP session: " + beans.size() + "\n");
+      int i = 0;
+      for (Contextual<?> bean : beans)
+      {
+         Object instance = get(bean);
+         buffer.append(++i + " - " + getBeanKey(bean) + ": " + instance + "\n");
+      }
+      return buffer.toString();
+   }
+
+   @Override
+   protected String getKeyPrefix()
+   {
+      return SessionContext.class.getName();
+   }
+
+}

Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java	2008-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/AbstractTest.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -3,8 +3,9 @@
 import javax.webbeans.Production;
 import javax.webbeans.Standard;
 
-import org.jboss.webbeans.ManagerImpl;
 import org.jboss.webbeans.bootstrap.Bootstrap;
+import org.jboss.webbeans.contexts.ApplicationContext;
+import org.jboss.webbeans.contexts.SimpleBeanMap;
 import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
 import org.jboss.webbeans.test.annotations.HornedAnimalDeploymentType;
 import org.jboss.webbeans.test.mock.MockBootstrap;
@@ -22,7 +23,9 @@
    public final void before()
    {
       manager = new MockManagerImpl();
-      ManagerImpl.setInstance(manager);
+      MockManagerImpl.setInstance(manager);
+      // Mock the ApplicationContext as a simple map
+      ApplicationContext.instance().setBeanMap(new SimpleBeanMap());
       bootstrap = new MockBootstrap();
       init();
    }

Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java	2008-12-05 11:58:59 UTC (rev 412)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java	2008-12-05 12:04:56 UTC (rev 413)
@@ -5,15 +5,18 @@
 import java.util.HashSet;
 import java.util.Set;
 
-import javax.webbeans.Observer;
-import javax.webbeans.TypeLiteral;
 import javax.webbeans.manager.Context;
-import javax.webbeans.manager.Manager;
 
 import org.jboss.webbeans.ManagerImpl;
 
 public class MockManagerImpl extends ManagerImpl
 {
+   
+   public static void setInstance(ManagerImpl manager)
+   {
+      ManagerImpl.instance = manager;
+   }
+   
    private Object       event = null;
    private Class<? extends Object>     eventType = null;
    private Annotation[] eventBindings = null;




More information about the weld-commits mailing list