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

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Fri Dec 19 02:37:28 EST 2008


Author: nickarls
Date: 2008-12-19 02:37:28 -0500 (Fri, 19 Dec 2008)
New Revision: 566

Modified:
   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
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java
Log:
Clean up duplication in Session/ApplicationBeanMap & AbstractBeanMapAdaptor

Modified: 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	2008-12-19 01:41:24 UTC (rev 565)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ApplicationBeanMap.java	2008-12-19 07:37:28 UTC (rev 566)
@@ -1,110 +1,100 @@
-/*
- * 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.CurrentManager;
-import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor;
-import org.jboss.webbeans.contexts.ApplicationContext;
-import org.jboss.webbeans.log.LogProvider;
-import org.jboss.webbeans.log.Logging;
-import org.jboss.webbeans.util.EnumerationIterable;
-
-/**
- * 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);
-   
-   // The current servlet context
-   private ServletContext servletContext;
-
-   /**
-    * Constructor
-    * 
-    * @param servletContext The servlet context
-    */
-   public ApplicationBeanMap(ServletContext servletContext)
-   {
-      this.servletContext = servletContext;
-   }
-
-   
-   @SuppressWarnings("unchecked")
-   public void clear()
-   {
-      for (String name : new EnumerationIterable<String>(servletContext.getAttributeNames()))
-      {
-         if (name.startsWith(getKeyPrefix()))
-         {
-            servletContext.removeAttribute(name);
-         }
-      }
-   }
-
-   @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() + 1);
-            Contextual<?> bean = CurrentManager.rootManager().getBeans().get(Integer.parseInt(id));
-            beans.add(bean);
-         }
-      }
-
-      return beans;
-   }
-
-   @Override
-   protected String getKeyPrefix()
-   {
-      return ApplicationContext.class.getName();
-   }
-
-}
+/*
+ * 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.Enumeration;
+
+import javax.servlet.ServletContext;
+
+import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor;
+import org.jboss.webbeans.contexts.ApplicationContext;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * A BeanMap that uses a servlet context as backing map
+ * 
+ * @author Nicklas Karlsson
+ * 
+ * @see org.jboss.webbeans.contexts.ApplicationContext
+ */
+public class ApplicationBeanMap extends AbstractBeanMapAdaptor
+{
+   // The log provider
+   private static LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class);
+   // The servlet context to use as backing map
+   private ServletContext context;
+
+   /**
+    * Constructor 
+    * 
+    * @param context The servlet context instance
+    */
+   public ApplicationBeanMap(ServletContext context)
+   {
+      super();
+      this.context = context;
+   }
+
+   /**
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getKeyPrefix()
+    */
+   @Override
+   protected String getKeyPrefix()
+   {
+      return ApplicationContext.class.getName();
+   }
+
+   /**
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getAttribute()
+    */
+   @Override
+   protected Object getAttribute(String key)
+   {
+      return context.getAttribute(key);
+   }
+
+   /**
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getAttributeNames()
+    */
+   @SuppressWarnings("unchecked")
+   @Override
+   protected Enumeration<String> getAttributeNames()
+   {
+      return context.getAttributeNames();
+   }
+
+   /**
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#removeAttributes()
+    */
+   @Override
+   protected void removeAttribute(String key)
+   {
+      context.removeAttribute(key);
+   }
+
+   /**
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#setAttribute()
+    */
+   @Override
+   protected void setAttribute(String key, Object instance)
+   {
+      context.setAttribute(key, instance);
+   }
+
+}

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java	2008-12-19 01:41:24 UTC (rev 565)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/SessionBeanMap.java	2008-12-19 07:37:28 UTC (rev 566)
@@ -17,17 +17,12 @@
 
 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.CurrentManager;
 import org.jboss.webbeans.contexts.AbstractBeanMapAdaptor;
-import org.jboss.webbeans.contexts.SessionContext;
+import org.jboss.webbeans.contexts.ApplicationContext;
 import org.jboss.webbeans.log.LogProvider;
 import org.jboss.webbeans.log.Logging;
 
@@ -36,161 +31,70 @@
  * 
  * @author Nicklas Karlsson
  * 
- * @see org.jboss.webbeans.contexts.SessionContext
+ * @see org.jboss.webbeans.contexts.ApplicationContext
  */
 public class SessionBeanMap extends AbstractBeanMapAdaptor
 {
-   private static LogProvider log = Logging.getLogProvider(SessionBeanMap.class);
-
-   // The HTTP session to use as backing map
+   // The log provider
+   private static LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class);
+   // The HTTP session context to use as backing map
    private HttpSession session;
 
    /**
     * Constructor
     * 
-    * @param keyPrefix The storage names prefix
+    * @param session The HTTP session
     */
-   public SessionBeanMap(HttpSession httpSession)
+   public SessionBeanMap(HttpSession session)
    {
       super();
-      this.session = httpSession;
-      log.trace("SessionBeanMap created with prefix " + getKeyPrefix());
+      this.session = session;
    }
 
    /**
-    * 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)
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getKeyPrefix()
     */
-   @SuppressWarnings("unchecked")
-   public <T> T get(Contextual<? extends T> bean)
+   @Override
+   protected String getKeyPrefix()
    {
-      String key = getBeanKey(bean);
-      T instance = (T) session.getAttribute(key);
-      log.trace("Searched session for key " + key + " and got " + instance);
-      return instance;
+      return ApplicationContext.class.getName();
    }
 
    /**
-    * 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)
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getAttribute()
     */
-   public <T> T remove(Contextual<? extends T> bean)
+   @Override
+   protected Object getAttribute(String key)
    {
-      T instance = get(bean);
-      String key = getBeanKey(bean);
-      session.removeAttribute(key);
-      log.trace("Removed bean " + bean + " with key " + key + " from session");
-      return instance;
+      return session.getAttribute(key);
    }
 
    /**
-    * 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()
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#getAttributeNames()
     */
    @SuppressWarnings("unchecked")
-   public void clear()
+   @Override
+   protected Enumeration<String> getAttributeNames()
    {
-      Enumeration names = session.getAttributeNames();
-      while (names.hasMoreElements())
-      {
-         String name = (String) names.nextElement();
-         session.removeAttribute(name);
-      }
-      log.trace("Session cleared");
+      return session.getAttributeNames();
    }
 
    /**
-    * 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()
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#removeAttributes()
     */
-   @SuppressWarnings("unchecked")
-   public Iterable<Contextual<? extends Object>> keySet()
+   @Override
+   protected void removeAttribute(String key)
    {
-
-      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() + 1);
-            Contextual<?> bean = CurrentManager.rootManager().getBeans().get(Integer.parseInt(id));
-            beans.add(bean);
-         }
-      }
-
-      return beans;
+      session.removeAttribute(key);
    }
 
    /**
-    * 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)
+    * @see org.jboss.webbeans.contexts.AbstractBeanMapAdaptor#setAttribute()
     */
-   public <T> void put(Contextual<? extends T> bean, T instance)
+   @Override
+   protected void setAttribute(String key, Object 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/main/java/org/jboss/webbeans/servlet/WebBeansListener.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java	2008-12-19 01:41:24 UTC (rev 565)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java	2008-12-19 07:37:28 UTC (rev 566)
@@ -78,11 +78,16 @@
       ServletLifecycle.endApplication();
    }
 
-   public void requestDestroyed(ServletRequestEvent sre)
+   /**
+    * Called when the request is destroyed
+    * 
+    * @param event The request event
+    */
+   public void requestDestroyed(ServletRequestEvent event)
    {
-      if (sre.getServletRequest() instanceof HttpServletRequest)
+      if (event.getServletRequest() instanceof HttpServletRequest)
       {
-         ServletLifecycle.endRequest((HttpServletRequest) sre.getServletRequest());
+         ServletLifecycle.endRequest((HttpServletRequest) event.getServletRequest());
       }
       else
       {
@@ -90,11 +95,16 @@
       }
    }
 
-   public void requestInitialized(ServletRequestEvent sre)
+   /**
+    * Called when the request is initialized
+    * 
+    * @param event The request event
+    */
+   public void requestInitialized(ServletRequestEvent event)
    {
-      if (sre.getServletRequest() instanceof HttpServletRequest)
+      if (event.getServletRequest() instanceof HttpServletRequest)
       {
-         ServletLifecycle.beginRequest((HttpServletRequest) sre.getServletRequest());
+         ServletLifecycle.beginRequest((HttpServletRequest) event.getServletRequest());
       }
       else
       {




More information about the weld-commits mailing list