You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by im...@apache.org on 2006/05/13 21:46:03 UTC

svn commit: r406146 - in /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation: Conversation.java ConversationContext.java ConversationManager.java ConversationViewHandler.java UIEndConversation.java

Author: imario
Date: Sat May 13 12:46:01 2006
New Revision: 406146

URL: http://svn.apache.org/viewcvs?rev=406146&view=rev
Log:
removed dead code
started to implement the conversationContext timeout handling (time to live currently hardcoded, will change to servlet context init param)

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/Conversation.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationContext.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationViewHandler.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/UIEndConversation.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/Conversation.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/Conversation.java?rev=406146&r1=406145&r2=406146&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/Conversation.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/Conversation.java Sat May 13 12:46:01 2006
@@ -22,12 +22,17 @@
 import javax.faces.context.FacesContext;
 import javax.faces.el.ValueBinding;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * handle conversation related stuff like beans
  * @author imario@apache.org 
  */
 public class Conversation
 {
+	private final static Log log = LogFactory.getLog(Conversation.class);
+	
 	private final String name;
 	private final boolean persistence;
 
@@ -40,6 +45,11 @@
 	{
 		this.name = name;
 		this.persistence = persistence;
+		
+		if (log.isDebugEnabled())
+		{
+			log.debug("start conversation:" + name + "(persistence=" + persistence + ")");
+		}
 	}
 
 	/**
@@ -58,6 +68,10 @@
 			// already there
 			return;
 		}
+		if (log.isDebugEnabled())
+		{
+			log.debug("put bean to conversation:" + name + "(bean=" + name + ")");
+		}
 		beans.put(name, vb.getValue(context));
 	}
 
@@ -78,6 +92,11 @@
 	 */
 	public void endConversation(boolean regularEnd)
 	{
+		if (log.isDebugEnabled())
+		{
+			log.debug("end conversation:" + name);
+		}
+		
 		Iterator iterBeans = beans.values().iterator();
 		while (iterBeans.hasNext())
 		{
@@ -121,7 +140,7 @@
 	{
 		return beans.get(name);
 	}
-
+	
 	/**
 	 * returns true if this conversation hold the persistence manager (aka EntityManager)
 	 */

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationContext.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationContext.java?rev=406146&r1=406145&r2=406146&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationContext.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationContext.java Sat May 13 12:46:01 2006
@@ -16,10 +16,14 @@
 package org.apache.myfaces.custom.conversation;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * The ConversationContext handles all conversations within the current context 
  *  
@@ -27,14 +31,49 @@
  */
 public class ConversationContext
 {
+	private final static Log log = LogFactory.getLog(ConversationContext.class);
+	
+	private final long id;
+	
+	private final Object mutex = new Object();
 	private final Map conversations = new TreeMap();
 	private final List conversationStack = new ArrayList(10);
 	private Conversation currentConversation;
 	
-	protected ConversationContext()
+	private long lastAccess;
+	
+	protected ConversationContext(long id)
+	{
+		this.id = id;
+		touch();
+	}
+
+	protected void touch()
+	{
+		lastAccess = System.currentTimeMillis();
+	}
+	
+	public long getLastAccess()
 	{
+		return lastAccess;
 	}
 
+	public void shutdownContext()
+	{
+		synchronized (mutex)
+		{
+			Iterator iterConversation = conversations.values().iterator();
+			while (iterConversation.hasNext())
+			{
+				Conversation conversation = (Conversation) iterConversation.next();
+				conversation.endConversation(false);
+			}
+			
+			conversations.clear();
+			conversationStack.clear();			
+		}
+	}
+	
 	/**
 	 * Start a conversation if not already started.<br />
 	 * All nested conversations (if any) are closed if the conversation already existed.  
@@ -42,18 +81,22 @@
 	 */
 	public void startConversation(String name, boolean persistence)
 	{
-		Conversation conversation = (Conversation) conversations.get(name);
-		if (conversation == null)
-		{
-			conversation = new Conversation(name, persistence);
-			conversations.put(name, conversation);
-			conversationStack.add(conversation);
-		}
-		else
+		synchronized (mutex)
 		{
-			endNestedConversations(conversation, false);
+			touch();
+			Conversation conversation = (Conversation) conversations.get(name);
+			if (conversation == null)
+			{
+				conversation = new Conversation(name, persistence);
+				conversations.put(name, conversation);
+				conversationStack.add(conversation);
+			}
+			else
+			{
+				endNestedConversations(conversation, false);
+			}
+			currentConversation = conversation;
 		}
-		currentConversation = conversation;
 	}
 
 	/**
@@ -61,16 +104,20 @@
 	 */
 	protected void endNestedConversations(Conversation conversation, boolean regularEnd)
 	{
-		while (conversationStack.size()>0)
+		synchronized (mutex)
 		{
-			int index = conversationStack.size()-1;
-			Conversation dependingConversation = (Conversation) conversationStack.get(index);
-			if (conversation == dependingConversation)
+			touch();
+			while (conversationStack.size()>0)
 			{
-				return;
+				int index = conversationStack.size()-1;
+				Conversation dependingConversation = (Conversation) conversationStack.get(index);
+				if (conversation == dependingConversation)
+				{
+					return;
+				}
+	
+				endConversation((Conversation) conversationStack.remove(index), regularEnd);
 			}
-			
-			endConversation((Conversation) conversationStack.remove(index), regularEnd);			
 		}
 	}
 
@@ -79,32 +126,41 @@
 	 */
 	protected void endConversation(Conversation conversation, boolean regularEnd)
 	{
-		conversation.endConversation(regularEnd);
+		synchronized (mutex)
+		{
+			touch();
+			conversation.endConversation(regularEnd);
+			conversations.remove(conversation.getName());
+		}
 	}
-
+	
 	/**
 	 * End the conversation with given name.<br />
 	 * This also automatically closes all nested conversations.
 	 */
 	public void endConversation(String name)
 	{
-		Conversation conversation = (Conversation) conversations.remove(name);
-		if (conversation != null)
+		synchronized (mutex)
 		{
-			while (conversationStack.size()>0)
+			touch();
+			Conversation conversation = (Conversation) conversations.get(name);
+			if (conversation != null)
 			{
-				Conversation dependingConversation = (Conversation) conversationStack.remove(conversationStack.size()-1);
-				endConversation(dependingConversation, false);
-				if (dependingConversation == conversation)
+				while (conversationStack.size()>0)
 				{
-					if (conversationStack.size() > 0)
+					Conversation dependingConversation = (Conversation) conversationStack.get(conversationStack.size()-1);
+					endConversation(dependingConversation, false);
+					if (dependingConversation == conversation)
 					{
-						currentConversation = (Conversation) conversationStack.get(conversationStack.size()-1);
+						if (conversationStack.size() > 0)
+						{
+							currentConversation = (Conversation) conversationStack.get(conversationStack.size()-1);
+						}
+						return;
 					}
-					return;
 				}
+				endConversation(conversation, true);
 			}
-			endConversation(conversation, true);
 		}
 	}
 	
@@ -114,6 +170,7 @@
 	 */
 	public Conversation getCurrentConversation()
 	{
+		touch();
 		return currentConversation;
 	}
 
@@ -122,7 +179,11 @@
 	 */
 	public boolean hasConversation()
 	{
-		return conversations.size() > 0;
+		synchronized (mutex)
+		{
+			touch();
+			return conversations.size() > 0;
+		}
 	}
 
 	/**
@@ -130,7 +191,11 @@
 	 */
 	public Conversation getConversation(String name)
 	{
-		return (Conversation) conversations.get(name);
+		synchronized (mutex)
+		{
+			touch();
+			return (Conversation) conversations.get(name);
+		}
 	}
 
 	/**
@@ -160,16 +225,20 @@
 	 */
 	public Object findBean(String name)
 	{
-		for (int i = conversationStack.size(); i>0; i--)
+		synchronized (mutex)
 		{
-			Conversation conversation = (Conversation) conversationStack.get(i-1);
-			if (conversation.hasBean(name))
+			touch();
+			for (int i = conversationStack.size(); i>0; i--)
 			{
-				return conversation.getBean(name);
+				Conversation conversation = (Conversation) conversationStack.get(i-1);
+				if (conversation.hasBean(name))
+				{
+					return conversation.getBean(name);
+				}
 			}
+			
+			return null;
 		}
-		
-		return null;
 	}
 
 	/**
@@ -177,16 +246,20 @@
 	 */
 	public PersistenceManager getPersistenceManager()
 	{
-		for (int i = conversationStack.size(); i>0; i--)
+		synchronized (mutex)
 		{
-			Conversation conversation = (Conversation) conversationStack.get(i-1);
-			if (conversation.isPersistence())
+			touch();
+			for (int i = conversationStack.size(); i>0; i--)
 			{
-				return conversation.getPersistenceManager();
+				Conversation conversation = (Conversation) conversationStack.get(i-1);
+				if (conversation.isPersistence())
+				{
+					return conversation.getPersistenceManager();
+				}
 			}
+			
+			return null;
 		}
-		
-		return null;
 	}
 
 	/**
@@ -194,12 +267,16 @@
 	 */
 	public void detachPersistence()
 	{
-		for (int i = conversationStack.size(); i>0; i--)
+		synchronized (mutex)
 		{
-			Conversation conversation = (Conversation) conversationStack.get(i-1);
-			if (conversation.isPersistence())
+			touch();
+			for (int i = conversationStack.size(); i>0; i--)
 			{
-				conversation.getPersistenceManager().detach();
+				Conversation conversation = (Conversation) conversationStack.get(i-1);
+				if (conversation.isPersistence())
+				{
+					conversation.getPersistenceManager().detach();
+				}
 			}
 		}
 	}
@@ -209,13 +286,22 @@
 	 */
 	public void attachPersistence()
 	{
-		for (int i = conversationStack.size(); i>0; i--)
+		synchronized (mutex)
 		{
-			Conversation conversation = (Conversation) conversationStack.get(i-1);
-			if (conversation.isPersistence())
+			touch();
+			for (int i = conversationStack.size(); i>0; i--)
 			{
-				conversation.getPersistenceManager().attach();
+				Conversation conversation = (Conversation) conversationStack.get(i-1);
+				if (conversation.isPersistence())
+				{
+					conversation.getPersistenceManager().attach();
+				}
 			}
 		}
+	}
+
+	public long getId()
+	{
+		return id;
 	}
 }

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java?rev=406146&r1=406145&r2=406146&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java Sat May 13 12:46:01 2006
@@ -15,15 +15,16 @@
  */
 package org.apache.myfaces.custom.conversation;
 
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Map;
 
 import javax.faces.FacesException;
 import javax.faces.context.FacesContext;
 import javax.servlet.http.HttpSession;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.custom.requestParameterProvider.RequestParameterProviderManager;
 import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
 
@@ -35,6 +36,8 @@
  */
 public class ConversationManager
 {
+	private final static Log log = LogFactory.getLog(ConversationManager.class);
+	
 	public final static String CONVERSATION_CONTEXT_PARAM = "conversationContext";
 	
 	private final static String INIT_PERSISTENCE_MANAGER_FACOTRY = "org.apache.myfaces.conversation.PERSISTENCE_MANAGER_FACTORY";
@@ -46,10 +49,39 @@
 	private PersistenceManagerFactory persistenceManagerFactory;
 	
 	private final Map conversationContexts = new HashMap();
-	private final List registeredEndConversations = new ArrayList(10);
+	// private final List registeredEndConversations = new ArrayList(10);
+
+	private class ContextWiperThread extends Thread
+	{
+		private final static long CHECK_TIME = 5 * 60 * 1000; // every 5 min
+		public ContextWiperThread()
+		{
+			setDaemon(true);
+			setName("ConversationManager.ContextWiperThread");
+		}
+
+		public void run()
+		{
+			while (!isInterrupted())
+			{
+				checkContextTimeout();
+				
+				try
+				{
+					Thread.sleep(CHECK_TIME);
+				}
+				catch (InterruptedException e)
+				{
+					log.warn(e.getLocalizedMessage(), e);
+				}
+			}
+		}
+	}
+	private ContextWiperThread wiperThread = new ContextWiperThread(); 
 	
 	protected ConversationManager()
 	{
+		wiperThread.start();
 	}
 
 	/**
@@ -161,7 +193,10 @@
 	 */
 	protected ConversationContext getConversationContext(Long conversationContextId)
 	{
-		return (ConversationContext) conversationContexts.get(conversationContextId);
+		synchronized (conversationContexts)
+		{
+			return (ConversationContext) conversationContexts.get(conversationContextId);
+		}
 	}
 	
 	/**
@@ -170,14 +205,17 @@
 	 */
 	protected ConversationContext getOrCreateConversationContext(Long conversationContextId)
 	{
-		ConversationContext conversationContext = (ConversationContext) conversationContexts.get(conversationContextId);
-		if (conversationContext == null)
+		synchronized (conversationContexts)
 		{
-			conversationContext = new ConversationContext();			
-			conversationContexts.put(conversationContextId, conversationContext);
+			ConversationContext conversationContext = (ConversationContext) conversationContexts.get(conversationContextId);
+			if (conversationContext == null)
+			{
+				conversationContext = new ConversationContext(conversationContextId.longValue());			
+				conversationContexts.put(conversationContextId, conversationContext);
+			}
+			
+			return conversationContext;
 		}
-		
-		return conversationContext;
 	}
 
 	/**
@@ -185,7 +223,10 @@
 	 */
 	protected void destroyConversationContext(Long conversationContextId)
 	{
-		conversationContexts.remove(conversationContextId);
+		synchronized (conversationContexts)
+		{
+			conversationContexts.remove(conversationContextId);
+		}
 	}
 	
 	/**
@@ -246,7 +287,6 @@
 
 	/**
 	 * Register the conversation to be ended after the cycle  
-	 */
 	protected void registerEndConversation(String conversationName)
 	{
 		synchronized (registeredEndConversations)
@@ -254,14 +294,15 @@
 			registeredEndConversations.add(conversationName);
 		}
 	}
+	 */
 
 	/**
 	 * Get all registered conversations
-	 */
 	protected List getRegisteredEndConversations()
 	{
 		return registeredEndConversations;
 	}
+	 */
 
 	/**
 	 * check if we have a conversation context
@@ -350,16 +391,28 @@
 			conversationContext.detachPersistence();
 		}
 	}
-
-	public boolean equals(Object obj)
-	{
-		return obj != null && obj.getClass().equals(getClass());
-	}
-
-	public int hashCode()
+	
+	protected void checkContextTimeout()
 	{
-		return getClass().hashCode();
+		synchronized (conversationContexts)
+		{
+			long timeToLive = 30 * 60 * 1000;
+			long checkTime = System.currentTimeMillis();
+			
+			Iterator iterContexts = conversationContexts.values().iterator();
+			while (iterContexts.hasNext())
+			{
+				ConversationContext conversationContext = (ConversationContext) iterContexts.next();
+				if (conversationContext.getLastAccess() + timeToLive < checkTime)
+				{
+					if (log.isDebugEnabled())
+					{
+						log.debug("end conversation context due to timeout: " + conversationContext.getId());
+					}
+					conversationContext.shutdownContext();
+					iterContexts.remove();
+				}
+			}
+		}
 	}
-	
-	
 }

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationViewHandler.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationViewHandler.java?rev=406146&r1=406145&r2=406146&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationViewHandler.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationViewHandler.java Sat May 13 12:46:01 2006
@@ -16,8 +16,6 @@
 package org.apache.myfaces.custom.conversation;
 
 import java.io.IOException;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Locale;
 
 import javax.faces.FacesException;
@@ -106,7 +104,7 @@
 
 	public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException
 	{
-		endConversations(context);
+		// endConversations(context);
 		
 		original.renderView(context, viewToRender);
 	}
@@ -135,7 +133,6 @@
 	 * <li>{@link #createView(FacesContext, String)}</li>
 	 * <li>{@link #restoreView(FacesContext, String)}</li>
 	 * </ul> 
-	 */
 	protected void endConversations(FacesContext context)
 	{
 		ConversationManager conversationManager = ConversationManager.getInstance(context); 
@@ -155,6 +152,7 @@
 			}
 		}
 	}
+	 */
 	
 	/**
 	 * @see ConversationManager#injectConversationBeans(FacesContext)

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/UIEndConversation.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/UIEndConversation.java?rev=406146&r1=406145&r2=406146&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/UIEndConversation.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/UIEndConversation.java Sat May 13 12:46:01 2006
@@ -18,7 +18,6 @@
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Set;
 
 import javax.faces.component.UICommand;
 import javax.faces.context.FacesContext;
@@ -39,7 +38,8 @@
     private String onOutcome;
     
     private boolean inited = false;
-    
+
+    /*
 	public static class ConversationEndAction extends AbstractConversationActionListener
 	{
 		public void doConversationAction(AbstractConversationComponent abstractConversationComponent)
@@ -47,6 +47,7 @@
 			ConversationManager.getInstance().registerEndConversation(getConversationName());
 		}
 	}
+	*/
 	
     public void encodeBegin(FacesContext context) throws IOException
 	{