[webbeans-commits] Webbeans SVN: r334 - ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Thu Nov 20 02:33:02 EST 2008


Author: nickarls
Date: 2008-11-20 02:33:02 -0500 (Thu, 20 Nov 2008)
New Revision: 334

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/SessionContext.java
Log:
traces and toString

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-20 05:46:31 UTC (rev 333)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java	2008-11-20 07:33:02 UTC (rev 334)
@@ -1,19 +1,19 @@
 /*
-* 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.
-*/
+ * 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;
 
@@ -25,6 +25,8 @@
 import javax.webbeans.manager.Bean;
 
 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
@@ -35,6 +37,8 @@
  */
 public class SessionBeanMap implements BeanMap
 {
+   private static LogProvider log = Logging.getLogProvider(SessionBeanMap.class);
+   
    private HttpSession session;
    private ManagerImpl manager;
    private String keyPrefix;
@@ -44,60 +48,71 @@
       super();
       this.manager = manager;
       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
+    * 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.
+    * Used to check if the session has been set and throws an exception if it's
+    * null.
     */
-   private void checkSession() {
-      if (session == 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.
+    * 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(Bean<?> bean) {
+   private String getBeanKey(Bean<?> bean)
+   {
       return keyPrefix + manager.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).
+    * 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 
+    * @param bean The bean to get from the session
     */
    @SuppressWarnings("unchecked")
    public <T> T get(Bean<? extends T> bean)
    {
       checkSession();
-      return (T) session.getAttribute(getBeanKey(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
     * 
-    * 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.
+    * 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.
     */
@@ -105,35 +120,40 @@
    {
       checkSession();
       T instance = get(bean);
-      session.removeAttribute(getBeanKey(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. 
+    * 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.
+    * 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.
     */
    @SuppressWarnings("unchecked")
    public void clear()
    {
       checkSession();
       Enumeration names = session.getAttributeNames();
-      while (names.hasMoreElements()) {
+      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. 
+    * 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. 
-    *  
+    * 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
     */
    @SuppressWarnings("unchecked")
@@ -144,23 +164,25 @@
       List<Bean<?>> beans = new ArrayList<Bean<?>>();
 
       Enumeration names = session.getAttributeNames();
-      while (names.hasMoreElements()) {
+      while (names.hasMoreElements())
+      {
          String name = (String) names.nextElement();
-         if (name.startsWith(keyPrefix)) {
+         if (name.startsWith(keyPrefix))
+         {
             String id = name.substring(keyPrefix.length());
             Bean<?> bean = manager.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.
+    * 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
     * 
@@ -169,7 +191,22 @@
    public <T> void put(Bean<? extends T> bean, T instance)
    {
       checkSession();
-      session.setAttribute(getBeanKey(bean), instance);
+      String key = getBeanKey(bean);
+      session.setAttribute(key, instance);
+      log.trace("Stored bean " + bean + " under key " + key + " in session");
    }
+   
+   @SuppressWarnings("unchecked")
+   @Override
+   public String toString() {
+      StringBuffer buffer = new StringBuffer();
+      List<Bean<?>> beans = (List) keySet();
+      buffer.append(beans.size() + " found in session");
+      for (Bean<?> bean : beans) {
+         Object instance = get(bean);
+         buffer.append(getBeanKey(bean) + ": " + instance);
+      }
+      return buffer.toString();
+   }
 
 }

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-20 05:46:31 UTC (rev 333)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java	2008-11-20 07:33:02 UTC (rev 334)
@@ -20,7 +20,12 @@
 import javax.servlet.http.HttpSession;
 import javax.webbeans.SessionScoped;
 
+import org.apache.log4j.Logger;
 import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.bootstrap.Bootstrap;
+import org.jboss.webbeans.log.Log;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
 
 /**
  * The session context
@@ -29,19 +34,16 @@
  */
 public class SessionContext extends PrivateContext {
 
+   private static LogProvider log = Logging.getLogProvider(SessionContext.class);
+   
    public SessionContext(ManagerImpl manager)
    {
       super(SessionScoped.class);
       // Replaces the BeanMap implementation with a session-based one
       beans.set(new SessionBeanMap(manager, getScopeType().getName() + "#"));
+      log.trace("Created session context");
    }
-   
-   @Override
-   public String toString()
-   {
-      return "Session context";
-   }   
-   
+ 
    /**
     * Sets the session in the session bean map
     * 
@@ -50,4 +52,15 @@
    public void setSession(HttpSession session) {
       ((SessionBeanMap)getBeanMap()).setSession(session);
    }
+   
+   @Override
+   public String toString()
+   {
+      return 
+         "Session context:\n" + 
+         "Active: " + getActive().toString() + 
+         "Beans: " + getBeanMap().toString();
+   }   
+   
+
 }




More information about the weld-commits mailing list