You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2007/10/05 14:54:05 UTC

svn commit: r582234 - /myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java

Author: skitching
Date: Fri Oct  5 05:54:05 2007
New Revision: 582234

URL: http://svn.apache.org/viewvc?rev=582234&view=rev
Log:
* Make non-final var lowercase.
* Move object initialisation into the constructor. This allows NEW to be used rather than a create method.
* Make createMessager method use frameworkadapter.getBean, so
  (a) IOC config can define the instance to use, and 
  (b) the framework adapter can provide a framework-specific default.

Modified:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java?rev=582234&r1=582233&r2=582234&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java Fri Oct  5 05:54:05 2007
@@ -29,8 +29,8 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.orchestra.conversation.jsf.JsfConversationMessager;
 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapterInterface;
 import org.apache.myfaces.orchestra.lib.OrchestraException;
 import org.apache.myfaces.orchestra.requestParameterProvider.RequestParameterProviderManager;
 import org.apache.myfaces.shared_orchestra.util.ClassUtils;
@@ -51,9 +51,8 @@
 	private static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator();
 
 	// TODO: consider whether a static value is the best way to generate context ids; they
-	// only need to be unique within a user session. And why is this variable in upper case;
-	// it isn't a constant...
-	private static long NEXT_CONVERSATION_CONTEXT = 1;
+	// only need to be unique within a user session. Statics are also test-unfriendly.
+	private static long nextConversationContextId = 1;
 
 	private final Log log = LogFactory.getLog(ConversationManager.class);
 
@@ -68,6 +67,7 @@
 
 	protected ConversationManager()
 	{
+		conversationMessager = createMessager();
 	}
 
 	/**
@@ -80,14 +80,21 @@
 
 	/**
 	 * Get the conversation manager.
-	 * @param create false if you just would like to get one if it has been created already
+	 * 
+	 * When create is true, an instance is always returned; one is
+	 * created if none currently exists for the current user session.
+	 * . 
+	 * When create is false, null is returned if no instance yet
+	 * exists for the current user session.
 	 */
 	public static ConversationManager getInstance(boolean create)
 	{
 		ConversationManager conversationManager = (ConversationManager) FrameworkAdapter.getInstance().getSessionAttribute(CONVERSATION_MANAGER_KEY);
 		if (conversationManager == null && create)
 		{
-			conversationManager = createConversationManager();
+			// TODO: do not call new directly here, as it makes it impossible to configure
+			// an alternative ConversationManager instance. This is IOC and test unfriendly.
+			conversationManager = new ConversationManager();
 
 			// initialize environmental systems
 			RequestParameterProviderManager.getInstance().register(new ConversationRequestParameterProvider());
@@ -98,24 +105,7 @@
 
 		return conversationManager;
 	}
-
-	/**
-	 * create and initialize a new ConversationManager
-	 */
-	protected static ConversationManager createConversationManager()
-	{
-		ConversationManager conversationManager;
-
-		// create manager
-		conversationManager = new ConversationManager();
-
-		// initialize the messager
-		conversationManager.createMessager();
-		// conversationManager.managedScopes = _SpringUtils.getConversationManagedScopeNames();
-
-		return conversationManager;
-	}
-
+	
 	/**
 	 * Get the current, or create a new unique conversationContextId.
 	 * <p>
@@ -141,8 +131,8 @@
 			{
 				synchronized (ConversationManager.class)
 				{
-					conversationContextId = new Long(NEXT_CONVERSATION_CONTEXT);
-					NEXT_CONVERSATION_CONTEXT++;
+					conversationContextId = new Long(nextConversationContextId);
+					nextConversationContextId++;
 				}
 			}
 
@@ -325,22 +315,25 @@
 	}
 
 	/**
-	 * Create the Messager used to inform the user about anomalies.<br />
-	 * The factory can be configured in your web.xml using the init parameter named
-	 * <code>org.apache.myfaces.conversation.MESSAGER</code>
+	 * Create the Messager used to inform the user about anomalies.
+	 * <p>
+	 * The instance to use can be configured by defining a bean in the IOC configuration
+	 * with the name org.apache.orchestra.conversation.ConversationMessager. This should
+	 * be a "prototype" object, ie one that returns a new instance on each lookup. 
+	 * <p>
+	 * Alternatively the class to use can be configured in your web.xml using a servlet
+	 * config init parameter named <code>org.apache.myfaces.conversation.MESSAGER</code>.
 	 */
-	protected void createMessager()
+	private static ConversationMessager createMessager()
 	{
-		String conversationMessagerName = FrameworkAdapter.getInstance().getInitParameter(INIT_MESSAGER);
-		if (conversationMessagerName == null)
-		{
-			conversationMessager = new JsfConversationMessager();
-		}
-		else
+		// First try init parameter for backwards compatibility
+		FrameworkAdapterInterface adapter = FrameworkAdapter.getInstance(); 
+		String conversationMessagerName = adapter.getInitParameter(INIT_MESSAGER);
+		if (conversationMessagerName != null)
 		{
 			try
 			{
-				conversationMessager = (ConversationMessager) ClassUtils.classForName(conversationMessagerName).newInstance();
+				return (ConversationMessager) ClassUtils.classForName(conversationMessagerName).newInstance();
 			}
 			catch (InstantiationException e)
 			{
@@ -355,6 +348,18 @@
 				throw new OrchestraException("error creating messager: " + conversationMessagerName, e);
 			}
 		}
+
+		// Now try to find a bean with name = ConversationMessager classname.
+		// This allows:
+		// (a) the IOC framework to be used to define the messager
+		// (b) the concrete FrameworkAdapter to provide a suitable default
+
+		ConversationMessager cm = (ConversationMessager) adapter.getBean(ConversationMessager.class.getName());
+		if (cm == null)
+		{
+			throw new OrchestraException("No ConversationMessager defined.");
+		}
+		return cm;
 	}
 
 	protected void checkTimeouts()