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

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Mon Feb 16 06:54:02 EST 2009


Author: nickarls
Date: 2009-02-16 06:54:02 -0500 (Mon, 16 Feb 2009)
New Revision: 1538

Modified:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationEntry.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationImpl.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ServletConversationManager.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/JSFHelper.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/WebBeansPhaseListener.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java
Log:
Conversation stuff

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/AbstractBeanMapContext.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -25,6 +25,9 @@
 import javax.inject.manager.Bean;
 
 import org.jboss.webbeans.context.beanmap.BeanMap;
+import org.jboss.webbeans.jsf.JSFHelper;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
 
 /**
  * Base for the Context implementations. Delegates calls to the abstract
@@ -39,6 +42,8 @@
  */
 public abstract class AbstractBeanMapContext extends AbstractContext
 {
+   private static LogProvider log = Logging.getLogProvider(AbstractBeanMapContext.class);
+
    /**
     * Constructor
     * 
@@ -98,6 +103,7 @@
     */
    private <T> void destroy(Contextual<T> bean)
    {
+      log.trace("Destroying " + bean);
       bean.destroy(getBeanMap().get(bean));
    }
 
@@ -106,6 +112,7 @@
     */
    public void destroy()
    {
+      log.trace("Destroying context");
       for (Contextual<? extends Object> bean : getBeanMap().keySet())
       {
          destroy(bean);

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/beanmap/AbstractBeanMap.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -39,7 +39,7 @@
 public abstract class AbstractBeanMap implements BeanMap
 {
    // The log provider
-   private static LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class);
+   private static LogProvider log = Logging.getLogProvider(AbstractBeanMap.class);
 
    /**
     * Gets a bean from the map
@@ -108,8 +108,10 @@
    private List<String> getFilteredAttributeNames()
    {
       List<String> attributeNames = new ArrayList<String>();
-      for (String attributeName : new EnumerationIterable<String>(getAttributeNames()))
+      Enumeration<String> e = getAttributeNames();
+      while (e.hasMoreElements())
       {
+         String attributeName = e.nextElement();
          if (attributeName.startsWith(getKeyPrefix()))
          {
             attributeNames.add(attributeName);
@@ -148,7 +150,8 @@
    protected abstract void removeAttribute(String key);
 
    /**
-    * Gets an enumeration of the attribute names present in the underlying storage
+    * Gets an enumeration of the attribute names present in the underlying
+    * storage
     * 
     * @return The attribute names
     */

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationEntry.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationEntry.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationEntry.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -103,6 +103,7 @@
       ConversationContext terminationContext = new ConversationContext();
       terminationContext.setBeanMap(new ConversationBeanMap(session, cid));
       terminationContext.destroy();
+      log.trace("Conversation " + cid + " destroyed");
    }
 
    /**
@@ -133,7 +134,7 @@
    {
       if (concurrencyLock.isHeldByCurrentThread())
       {
-         log.debug("Unlocked conversation " + cid);
+         log.trace("Unlocked conversation " + cid);
          concurrencyLock.unlock();
       }
       else

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationImpl.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ConversationImpl.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -24,6 +24,8 @@
 
 import org.jboss.webbeans.WebBean;
 import org.jboss.webbeans.conversation.bindings.ConversationInactivityTimeout;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
 
 /**
  * The current conversation implementation
@@ -36,6 +38,9 @@
 @WebBean
 public class ConversationImpl implements Conversation
 {
+   
+   private static LogProvider log = Logging.getLogProvider(ConversationImpl.class);
+
    // The conversation ID
    private String cid;
    // Is the conversation long-running?
@@ -61,21 +66,24 @@
    {
       this.cid = conversationIdGenerator.nextId();
       this.timeoutInMilliseconds = timeoutInMilliseconds;
+      log.debug("Created a new conversation " + this);
    }
 
    public void begin()
    {
+      log.debug("Promoted conversation " + cid + " to long-running");
       longRunning = true;
    }
 
    public void begin(String id)
    {
-      longRunning = true;
       cid = id;
+      begin();
    }
 
    public void end()
    {
+      log.debug("Demoted conversation " + cid + " to transient");
       longRunning = false;
    }
 
@@ -96,6 +104,7 @@
 
    public void setTimeout(long timeout)
    {
+      log.debug("Set timeout of conversation " + cid + " to " + timeout);
       this.timeoutInMilliseconds = timeout;
    }
 
@@ -108,9 +117,11 @@
     */
    public void switchTo(String cid, boolean longRunning, long timeoutInMilliseconds)
    {
+      log.debug("Switched conversation from " + this);
       this.cid = cid;
       this.longRunning = longRunning;
       this.timeoutInMilliseconds = timeoutInMilliseconds;
+      log.debug("to " + this);
    }
 
    @Override
@@ -121,6 +132,7 @@
 
    public void setLongRunning(boolean longRunning)
    {
+      log.debug("Set conversation " + cid + " to long-running: " + longRunning);
       this.longRunning = longRunning;
    }
 }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ServletConversationManager.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ServletConversationManager.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/conversation/ServletConversationManager.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -22,6 +22,7 @@
 import java.util.concurrent.Future;
 
 import javax.context.SessionScoped;
+import javax.faces.context.FacesContext;
 import javax.inject.Current;
 import javax.inject.Produces;
 import javax.servlet.http.HttpSession;
@@ -46,9 +47,9 @@
 {
    private static LogProvider log = Logging.getLogProvider(ServletConversationManager.class);
 
-   // FIXME short temp
-   private static final long CONVERSATION_TIMEOUT_IN_MS = 10 * 30 * 1000;
+   private static final long CONVERSATION_TIMEOUT_IN_MS = 10 * 60 * 1000;
    private static final long CONVERSATION_CONCURRENT_ACCESS_TIMEOUT_IN_MS = 1 * 1000;
+   private static final String CONVERSATION_ID_NAME = "cid";
 
    // The conversation terminator
    @Current
@@ -106,7 +107,8 @@
    @WebBean
    public static String getConversationIdName()
    {
-      return "cid";
+      log.trace("Produced conversation id name " + CONVERSATION_ID_NAME);
+      return CONVERSATION_ID_NAME;
    }
 
    public void beginOrRestoreConversation(String cid)
@@ -158,9 +160,11 @@
       }
    }
 
+   // TODO: check that stuff gets terminated when you flip between several long-running conversations
    public void cleanupConversation()
    {
       String cid = currentConversation.getId();
+      log.trace("Cleaning up conversations for cid " + cid);
       if (currentConversation.isLongRunning())
       {
          Future<?> terminationHandle = scheduleForTermination(cid);
@@ -180,7 +184,7 @@
             ConversationEntry conversationEntry = ConversationEntry.of(cid, terminationHandle);
             longRunningConversations.put(cid, conversationEntry);
          }
-         log.trace("Scheduled " + currentConversation + " for termination");
+         log.trace("Scheduled " + currentConversation + " for termination, there are now " + longRunningConversations.size() + " long-running conversations");
       }
       else
       {
@@ -205,7 +209,7 @@
     */
    private Future<?> scheduleForTermination(String cid)
    {
-      Runnable terminationTask = new TerminationTask(cid);
+      Runnable terminationTask = new TerminationTask(cid, (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true));
       return conversationTerminator.scheduleForTermination(terminationTask, inactivityTimeout);
    }
 
@@ -219,15 +223,17 @@
    {
       // The conversation ID to terminate
       private String cid;
+      private HttpSession session;
 
       /**
        * Creates a new termination task
        * 
        * @param cid The conversation ID
        */
-      public TerminationTask(String cid)
+      public TerminationTask(String cid, HttpSession session)
       {
          this.cid = cid;
+         this.session = session;
       }
 
       /**
@@ -235,14 +241,15 @@
        */
       public void run()
       {
-         log.trace("Conversation " + cid + " timed out. Destroying it");
+         log.debug("Conversation " + cid + " timed out. Destroying it");
          longRunningConversations.remove(cid).destroy(session);
+         log.trace("There are now " + longRunningConversations.size() + " long-running conversations");
       }
    }
 
    public void destroyAllConversations()
    {
-      log.trace("Destroying " + longRunningConversations.size() + " long-running conversations in session " + session.getId());
+      log.debug("Destroying " + longRunningConversations.size() + " long-running conversations in session " + session.getId());
       for (ConversationEntry conversationEntry : longRunningConversations.values())
       {
          conversationEntry.destroy(session);

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/JSFHelper.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/JSFHelper.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/JSFHelper.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -1,3 +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.
+ */
 package org.jboss.webbeans.jsf;
 
 import javax.faces.component.html.HtmlInputHidden;
@@ -4,47 +20,101 @@
 import javax.faces.context.FacesContext;
 import javax.servlet.http.HttpSession;
 
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * Helper class for JSF related operations
+ * 
+ * @author Nicklas Karlsson
+ *
+ */
 public class JSFHelper
 {
+   private static LogProvider log = Logging.getLogProvider(JSFHelper.class);
+
    private static final String CONVERSATION_PROPAGATION_COMPONENT_ID = "webbeans_conversation_propagation";
    private static final String CONVERSATION_ID_NAME = "cid";
 
+   /**
+    * Gets a FacesContext instance
+    * 
+    * @return The current instance
+    */
+   private static FacesContext context()
+   {
+      return FacesContext.getCurrentInstance();
+   }
+
+   /**
+    * Checks if current request is a JSF postback
+    * 
+    * @return True if postback, false otherwise
+    */
    public static boolean isPostback()
    {
-      return FacesContext.getCurrentInstance().getRenderKit().getResponseStateManager().isPostback(FacesContext.getCurrentInstance());
+      return context().getRenderKit().getResponseStateManager().isPostback(context());
    }
 
+   /**
+    * Removes the conversation propagation component from the ui view root
+    */
    public static void removePropagationComponent()
    {
+      log.debug("Removed propagation component");
       HtmlInputHidden propagationComponent = getPropagationComponent();
       if (propagationComponent != null)
       {
-         FacesContext.getCurrentInstance().getViewRoot().getChildren().remove(propagationComponent);
+         context().getViewRoot().getChildren().remove(propagationComponent);
       }
    }
 
+   /**
+    * Creates and/or updates the conversation propagation component in the UI view root
+    * 
+    * @param cid The conversation id to propagate
+    */
    public static void createOrUpdatePropagationComponent(String cid)
    {
       HtmlInputHidden propagationComponent = getPropagationComponent();
       if (propagationComponent == null)
       {
-         propagationComponent = (HtmlInputHidden) FacesContext.getCurrentInstance().getApplication().createComponent(HtmlInputHidden.COMPONENT_TYPE);
+         log.trace("Created propagation component");
+         propagationComponent = (HtmlInputHidden) context().getApplication().createComponent(HtmlInputHidden.COMPONENT_TYPE);
          propagationComponent.setId(CONVERSATION_PROPAGATION_COMPONENT_ID);
-         FacesContext.getCurrentInstance().getViewRoot().getChildren().add(propagationComponent);
+         context().getViewRoot().getChildren().add(propagationComponent);
       }
+      log.debug("Updated propagation component with cid " + cid);
       propagationComponent.setValue(cid);
    }
 
+   /**
+    * Gets the propagation component from the UI view root
+    * 
+    * @return The component (or null if not found)
+    */
    private static HtmlInputHidden getPropagationComponent()
    {
-      return (HtmlInputHidden) FacesContext.getCurrentInstance().getViewRoot().findComponent(CONVERSATION_PROPAGATION_COMPONENT_ID);
+      return (HtmlInputHidden) context().getViewRoot().findComponent(CONVERSATION_PROPAGATION_COMPONENT_ID);
    }
 
-   private static String getConversationIdFromRequest()
+   /**
+    * Gets the propagated conversation id parameter from the request
+    * 
+    * @return The conversation id (or null if not found)
+    */
+   public static String getConversationIdFromRequest()
    {
-      return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(CONVERSATION_ID_NAME);
+      String cid = context().getExternalContext().getRequestParameterMap().get(CONVERSATION_ID_NAME);
+      log.trace("Got cid " + cid + " from request");
+      return cid;
    }
 
+   /**
+    * Gets the propagated conversation id from the propagation component
+    *  
+    * @return The conversation id (or null if not found)
+    */
    public static String getConversationIdFromPropagationComponent()
    {
       String cid = null;
@@ -53,19 +123,38 @@
       {
          cid = propagationComponent.getValue().toString();
       }
+      log.trace("Got cid " + cid + " from propagation component");
       return cid;
    }
 
+   /**
+    * Gets the propagated conversation id
+    * 
+    * @return The conversatio nid (or null if not found)
+    */
    public static String getConversationId()
    {
+      String cid = null;
       if (isPostback())
       {
-         return getConversationIdFromPropagationComponent();
+         cid = getConversationIdFromPropagationComponent();
       }
       else
       {
-         return getConversationIdFromRequest();
+         cid = getConversationIdFromRequest();
       }
+      log.debug("Resuming conversation " + cid);
+      return cid;
    }
 
+   /**
+    * Gets the HTTP session
+    * 
+    * @return The session
+    */
+   public static HttpSession getHttpSession()
+   {
+      return (HttpSession) context().getExternalContext().getSession(true);
+   }
+
 }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/WebBeansPhaseListener.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/WebBeansPhaseListener.java	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/jsf/WebBeansPhaseListener.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -20,12 +20,15 @@
 import javax.faces.event.PhaseEvent;
 import javax.faces.event.PhaseId;
 import javax.faces.event.PhaseListener;
+import javax.servlet.http.HttpSession;
 
 import org.jboss.webbeans.CurrentManager;
 import org.jboss.webbeans.context.ConversationContext;
 import org.jboss.webbeans.conversation.ConversationManager;
 import org.jboss.webbeans.log.LogProvider;
 import org.jboss.webbeans.log.Logging;
+import org.jboss.webbeans.servlet.ConversationBeanMap;
+import org.jboss.webbeans.servlet.HttpSessionManager;
 
 /**
  * A phase listener for propagating conversation id over postbacks through a
@@ -36,11 +39,14 @@
  */
 public class WebBeansPhaseListener implements PhaseListener
 {
-   // The ID/name of the conversation-propagating component
-   private static final String CONVERSATION_PROPAGATION_COMPONENT = "webbeans_conversation_propagation";
-
+   // The logging provider
    private static LogProvider log = Logging.getLogProvider(WebBeansPhaseListener.class);
 
+   /**
+    * Run before a given phase
+    * 
+    * @param phaseEvent The phase event
+    */
    public void beforePhase(PhaseEvent phaseEvent)
    {
       if (phaseEvent.getPhaseId().equals(PhaseId.RENDER_RESPONSE))
@@ -51,30 +57,39 @@
       {
          beforeApplyRequestValues();
       }
-   }   
+   }
 
+   /**
+    * Run before the response is rendered
+    */
    private void beforeRenderReponse()
    {
-      if (JSFHelper.isPostback())
+      log.trace("In before render response phase");
+      Conversation conversation = CurrentManager.rootManager().getInstanceByType(Conversation.class);
+      if (conversation.isLongRunning())
       {
-         Conversation conversation = CurrentManager.rootManager().getInstanceByType(Conversation.class);
-         if (conversation.isLongRunning())
-         {
-            JSFHelper.createOrUpdatePropagationComponent(conversation.getId());
-         }
-         else
-         {
-            JSFHelper.removePropagationComponent();
-         }
-
+         JSFHelper.createOrUpdatePropagationComponent(conversation.getId());
       }
+      else
+      {
+         JSFHelper.removePropagationComponent();
+      }
    }
-   
+
+   /**
+    * Run before request values are applied
+    */
    private void beforeApplyRequestValues()
    {
+      log.trace("In before apply values phase");
       ConversationContext.INSTANCE.setActive(true);
-   }   
-   
+   }
+
+   /**
+    * Run after a given phase
+    * 
+    * @param phaseEvent The phase event
+    */
    public void afterPhase(PhaseEvent phaseEvent)
    {
       if (phaseEvent.getPhaseId().equals(PhaseId.RESTORE_VIEW))
@@ -85,18 +100,28 @@
       {
          afterRenderResponse();
       }
-   }   
-   
+   }
+
+   /**
+    * Run after the view is restored
+    */
    private void afterRestoreView()
    {
-      if (JSFHelper.isPostback())
-      {
-         CurrentManager.rootManager().getInstanceByType(ConversationManager.class).beginOrRestoreConversation(JSFHelper.getConversationId());
-      }
+      log.trace("In after restore view phase");
+      HttpSession session = JSFHelper.getHttpSession();
+      CurrentManager.rootManager().getInstanceByType(HttpSessionManager.class).setSession(session);
+      CurrentManager.rootManager().getInstanceByType(ConversationManager.class).beginOrRestoreConversation(JSFHelper.getConversationId());
+      String cid = CurrentManager.rootManager().getInstanceByType(Conversation.class).getId();
+      ConversationContext.INSTANCE.setBeanMap(new ConversationBeanMap(session, cid));
    }
 
+   /**
+    * Run after the response is rendered
+    */
    private void afterRenderResponse()
    {
+      log.trace("In after render reponse phase");
+      CurrentManager.rootManager().getInstanceByType(ConversationManager.class).cleanupConversation();
       ConversationContext.INSTANCE.setActive(false);
    }
 

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	2009-02-16 07:53:42 UTC (rev 1537)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java	2009-02-16 11:54:02 UTC (rev 1538)
@@ -101,20 +101,10 @@
    public static void beginRequest(HttpServletRequest request)
    {
       log.trace("Processing HTTP request " + request.getRequestURI() + " begins");
-      CurrentManager.rootManager().getInstanceByType(HttpSessionManager.class).setSession(request.getSession());
       SessionContext.INSTANCE.setBeanMap(new HttpSessionBeanMap(request.getSession()));
-      beginConversation(request);
       DependentContext.INSTANCE.setActive(true);
    }
 
-   private static void beginConversation(HttpServletRequest request)
-   {
-      ConversationManager conversationManager = CurrentManager.rootManager().getInstanceByType(ConversationManager.class);
-      conversationManager.beginOrRestoreConversation(request.getParameter("cid"));
-      Conversation conversation = CurrentManager.rootManager().getInstanceByType(Conversation.class);
-      ConversationContext.INSTANCE.setBeanMap(new ConversationBeanMap(request.getSession(), conversation.getId()));
-   }
-
    /**
     * Ends a HTTP request
     * 
@@ -123,12 +113,9 @@
    public static void endRequest(HttpServletRequest request)
    {
       log.trace("Processing HTTP request " + request.getRequestURI() + " ends");
-      CurrentManager.rootManager().getInstanceByType(HttpSessionManager.class).setSession(null);
-      CurrentManager.rootManager().getInstanceByType(ConversationManager.class).cleanupConversation();
       DependentContext.INSTANCE.setActive(false);
       RequestContext.INSTANCE.destroy();
       SessionContext.INSTANCE.setBeanMap(null);
-      ConversationContext.INSTANCE.setBeanMap(null);
    }
 
 }




More information about the weld-commits mailing list