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/09/07 11:39:15 UTC

svn commit: r573520 - in /myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra: conversation/ conversation/spring/ viewController/spring/

Author: skitching
Date: Fri Sep  7 02:39:14 2007
New Revision: 573520

URL: http://svn.apache.org/viewvc?rev=573520&view=rev
Log:
Rework way conversation objects are instantiated. Only the original Scope object has
the necessary data to configure the ConversationAspects correctly, so define a
ConversationFactory interface and make the Scope class implement it. Aspects can then
simply be created directly rather than via a list of template classes.

Added:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFactory.java
Removed:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/ConversationPolicy.java
Modified:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/Conversation.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationContext.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationManager.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringOrchestraScope.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringTimeoutableScope.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringConversationScope.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringFlashScope.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringManualScope.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/viewController/spring/SpringViewControllerScope.java

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/Conversation.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/Conversation.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/Conversation.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/Conversation.java Fri Sep  7 02:39:14 2007
@@ -19,13 +19,12 @@
 
 package org.apache.myfaces.orchestra.conversation;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.orchestra.conversation.spring.ConversationPolicy;
-
 import java.util.Map;
 import java.util.TreeMap;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * The container for all beans managed by this conversation.
  * <p>
@@ -54,7 +53,10 @@
 	private final Log log = LogFactory.getLog(Conversation.class);
 
 	private final String name;
-	private final ConversationPolicy policy;
+	
+	// the factory that created this conversation instance; needed
+	// when restarting the conversation.
+	private final ConversationFactory factory;
 
 	private final Map beans = new TreeMap();
 
@@ -70,11 +72,11 @@
 	private Object activeCountMutex = new Object();
 	private int activeCount;
 
-	protected Conversation(ConversationContext conversationContext, String name, ConversationPolicy policy)
+	public Conversation(ConversationContext conversationContext, String name, ConversationFactory factory)
 	{
 		this.conversationContext = conversationContext;
 		this.name = name;
-		this.policy = policy;
+		this.factory = factory;
 
 		if (log.isDebugEnabled())
 		{
@@ -147,9 +149,9 @@
 	/**
 	 * the policy the conversation should use. the policy will define the lifetime of the conversation
 	 */
-	public ConversationPolicy getPolicy()
+	public ConversationFactory getFactory()
 	{
-		return policy;
+		return factory;
 	}
 
 	/**
@@ -186,11 +188,11 @@
 	public Conversation invalidateAndRestart()
 	{
 		String conversationName = getName();
-		ConversationPolicy conversationPolicy = getPolicy();
+		ConversationFactory factory = getFactory();
 
 		destroy();
 
-		return conversationContext.startConversation(conversationName, conversationPolicy);
+		return conversationContext.startConversation(conversationName, factory);
 	}
 
 	/**
@@ -375,5 +377,10 @@
 	public ConversationAspect getAspect(Class conversationAspectClass)
 	{
 		return conversationAspects.getAspect(conversationAspectClass);
+	}
+	
+	public void addAspect(ConversationAspect aspect)
+	{
+		conversationAspects.addAspect(aspect);
 	}
 }

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationContext.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationContext.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationContext.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationContext.java Fri Sep  7 02:39:14 2007
@@ -19,17 +19,14 @@
 
 package org.apache.myfaces.orchestra.conversation;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.orchestra.conversation.spring.ConversationPolicy;
-import org.apache.myfaces.orchestra.lib.OrchestraException;
-
-import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.TreeMap;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * <p>The ConversationContext handles all conversations within the current context</p>
  * <p/>
@@ -123,7 +120,7 @@
 	/**
 	 * Start a conversation if not already started.
 	 */
-	protected Conversation startConversation(String name, ConversationPolicy policy)
+	protected Conversation startConversation(String name, ConversationFactory factory)
 	{
 		synchronized (this)
 		{
@@ -131,57 +128,12 @@
 			Conversation conversation = (Conversation) conversations.get(name);
 			if (conversation == null)
 			{
-				conversation = createConversation(name, policy);
+				conversation = factory.createConversation(this, name);
 
 				conversations.put(name, conversation);
 			}
 			return conversation;
 		}
-	}
-
-	protected Conversation createConversation(String name, ConversationPolicy policy)
-	{
-		Conversation conversation;
-		conversation = new Conversation(this, name, policy);
-
-		Class[] aspectClasses = policy.getConversationAspects();
-		if (aspectClasses != null)
-		{
-			for (int i = 0; i<aspectClasses.length; i++)
-			{
-				Class aspectClass = aspectClasses[i];
-				ConversationAspect aspect;
-				try
-				{
-					aspect = (ConversationAspect) aspectClass.getConstructor(new Class[]
-					{
-						Conversation.class
-					}).newInstance(new Object[]
-					{
-						conversation
-					});
-				}
-				catch (InstantiationException e)
-				{
-					throw new OrchestraException(e);
-				}
-				catch (IllegalAccessException e)
-				{
-					throw new OrchestraException(e);
-				}
-				catch (InvocationTargetException e)
-				{
-					throw new OrchestraException(e);
-				}
-				catch (NoSuchMethodException e)
-				{
-					throw new OrchestraException(e);
-				}
-
-				conversation.getAspects().addAspect(aspect);
-			}
-		}
-		return conversation;
 	}
 
 	/**

Added: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFactory.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFactory.java?rev=573520&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFactory.java (added)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFactory.java Fri Sep  7 02:39:14 2007
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.myfaces.orchestra.conversation;
+
+
+/**
+ * An object that is capable of creating a fully-configured instance of a
+ * conversation with a particular name.
+ */
+public interface ConversationFactory
+{
+	Conversation createConversation(ConversationContext context, String name);
+}
\ No newline at end of file

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=573520&r1=573519&r2=573520&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 Sep  7 02:39:14 2007
@@ -19,14 +19,6 @@
 
 package org.apache.myfaces.orchestra.conversation;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.orchestra.conversation.spring.ConversationPolicy;
-import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
-import org.apache.myfaces.orchestra.lib.OrchestraException;
-import org.apache.myfaces.orchestra.requestParameterProvider.RequestParameterProviderManager;
-import org.apache.myfaces.shared_orchestra.util.ClassUtils;
-
 import java.io.IOException;
 import java.io.ObjectStreamException;
 import java.io.Serializable;
@@ -34,6 +26,13 @@
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
+import org.apache.myfaces.orchestra.lib.OrchestraException;
+import org.apache.myfaces.orchestra.requestParameterProvider.RequestParameterProviderManager;
+import org.apache.myfaces.shared_orchestra.util.ClassUtils;
+
 /**
  * The manager will deal with the various contexts in the current session.
  * A new context will be created if the current window has none associated.
@@ -211,11 +210,11 @@
 	 *
 	 * @see ConversationContext#startConversation(String, ConversationPolicy)
 	 */
-	public Conversation startConversation(String name, ConversationPolicy policy)
+	public Conversation startConversation(String name, ConversationFactory factory)
 	{
 		Long conversationContextId = getConversationContextId();
 		ConversationContext conversationContext = getOrCreateConversationContext(conversationContextId);
-		return conversationContext.startConversation(name, policy);
+		return conversationContext.startConversation(name, factory);
 	}
 
 	/**

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringOrchestraScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringOrchestraScope.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringOrchestraScope.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringOrchestraScope.java Fri Sep  7 02:39:14 2007
@@ -24,6 +24,8 @@
 import org.apache.myfaces.orchestra.conversation.ConversationAware;
 import org.apache.myfaces.orchestra.conversation.ConversationBindingEvent;
 import org.apache.myfaces.orchestra.conversation.ConversationBindingListener;
+import org.apache.myfaces.orchestra.conversation.ConversationContext;
+import org.apache.myfaces.orchestra.conversation.ConversationFactory;
 import org.apache.myfaces.orchestra.conversation.ConversationManager;
 import org.apache.myfaces.orchestra.conversation.CurrentConversationAdvice;
 import org.springframework.aop.framework.ProxyFactory;
@@ -43,7 +45,7 @@
 /**
  * Abstract basis class for all the Orchestra scopes
  */
-public abstract class AbstractSpringOrchestraScope implements
+public abstract class AbstractSpringOrchestraScope implements ConversationFactory, 
 	Scope, BeanFactoryAware, ApplicationContextAware
 {
 	private ConfigurableApplicationContext applicationContext;
@@ -112,19 +114,21 @@
 		String conversationName = getConversationNameForBean(beanName);
 
 		ConversationManager manager = ConversationManager.getInstance();
+		Conversation conversation;
 
 		// check if we have a conversation
 		synchronized(manager)
 		{
-			if (!manager.hasConversation(conversationName))
+			conversation = manager.getConversation(conversationName);
+			if (conversation == null)
 			{
-				// start the conversation
-				startConversation(manager, conversationName);
+				// Start the conversation. This eventually results in a 
+				// callback to the createConversation method on this class.
+				conversation = manager.startConversation(conversationName, this);
 			}
 		}
 
 		// get the conversation
-		Conversation conversation = manager.getConversation(conversationName);
 		notifyAccessConversation(conversation);
 		synchronized(conversation)
 		{
@@ -155,11 +159,27 @@
 		return conversation.getAttribute(beanName);
 	}
 
-	protected Conversation startConversation(ConversationManager manager, String conversationName)
+	public Conversation createConversation(ConversationContext context, String name)
 	{
-		return manager.startConversation(conversationName, getConversationPolicy());
+		Conversation conversation = new Conversation(context, name, this);
+
+		// invoke child scope classes so they can add any aspects they desire.
+		initAspects(conversation);
+
+		return conversation;
 	}
 
+	/**
+	 * Add aspects to a newly-created conversation.
+	 * <p>
+	 * Subclasses are expected to call super.initAspects, then make
+	 * zero or more calls to conversation.addAspect.
+	 */
+	protected void initAspects(Conversation conversation)
+	{
+		// nothing to do in default implementation.
+	}
+	
 	protected void notifyAccessConversation(Conversation conversation)
 	{
 	}
@@ -288,6 +308,4 @@
 
 		this.applicationContext = (ConfigurableApplicationContext) applicationContext;
 	}
-
-	protected abstract ConversationPolicy getConversationPolicy();
 }

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringTimeoutableScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringTimeoutableScope.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringTimeoutableScope.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/AbstractSpringTimeoutableScope.java Fri Sep  7 02:39:14 2007
@@ -19,7 +19,6 @@
 package org.apache.myfaces.orchestra.conversation.spring;
 
 import org.apache.myfaces.orchestra.conversation.Conversation;
-import org.apache.myfaces.orchestra.conversation.ConversationManager;
 import org.apache.myfaces.orchestra.conversation.ConversationTimeoutableAspect;
 
 /**
@@ -46,20 +45,16 @@
 		this.timeout = timeout;
 	}
 
-	protected Conversation startConversation(ConversationManager manager, String conversationName)
+	protected void initAspects(Conversation conversation)
 	{
-		Conversation conversation = super.startConversation(manager, conversationName);
-
+		super.initAspects(conversation);
+		
+		// configure the aspects attached to the conversation.
 		if (getTimeout() != null)
 		{
-			ConversationTimeoutableAspect timeoutAspect =
-				(ConversationTimeoutableAspect) conversation.getAspect(ConversationTimeoutableAspect.class);
-			if (timeoutAspect != null)
-			{
-				timeoutAspect.setTimeout(getTimeout().longValue() * 60 * 1000);
-			}
+			ConversationTimeoutableAspect aspect = new ConversationTimeoutableAspect(conversation);
+			aspect.setTimeout(getTimeout().longValue() * 60 * 1000);
+			conversation.addAspect(aspect);
 		}
-
-		return conversation;
 	}
 }

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringConversationScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringConversationScope.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringConversationScope.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringConversationScope.java Fri Sep  7 02:39:14 2007
@@ -19,6 +19,9 @@
 
 package org.apache.myfaces.orchestra.conversation.spring;
 
+import org.apache.myfaces.orchestra.conversation.Conversation;
+import org.apache.myfaces.orchestra.conversation.ConversationTimeoutableAspect;
+
 /**
  * Handles creation and lookup of any bean whose bean-definition specifies a scope
  * of "conversation".
@@ -55,8 +58,6 @@
  */
 public class SpringConversationScope extends AbstractSpringTimeoutableScope
 {
-	protected ConversationPolicy getConversationPolicy()
-	{
-		return ConversationPolicy.TIME;
-	}
+	// no custom aspects to configure here; just the time aspect from the parent 
+	// class is needed.
 }

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringFlashScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringFlashScope.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringFlashScope.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringFlashScope.java Fri Sep  7 02:39:14 2007
@@ -20,10 +20,11 @@
 package org.apache.myfaces.orchestra.conversation.spring;
 
 import org.apache.myfaces.orchestra.conversation.Conversation;
+import org.apache.myfaces.orchestra.conversation.ConversationAspect;
 import org.apache.myfaces.orchestra.conversation.ConversationFlashScopeAspect;
 
 /**
- * provides a flash scope, that is, a bean will be discarded as soon as it is no longer referenced
+ * Provides a flash scope, that is, a bean will be discarded as soon as it is no longer referenced
  * by the view.
  */
 public class SpringFlashScope extends AbstractSpringTimeoutableScope
@@ -32,9 +33,11 @@
 	{
 	}
 
-	protected ConversationPolicy getConversationPolicy()
+	protected void initAspects(Conversation conversation)
 	{
-		return ConversationPolicy.TIME_FLASH;
+		super.initAspects(conversation);
+		ConversationAspect aspect = new ConversationFlashScopeAspect(conversation);
+		conversation.addAspect(aspect);
 	}
 
 	protected void notifyAccessConversation(Conversation conversation)

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringManualScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringManualScope.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringManualScope.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringManualScope.java Fri Sep  7 02:39:14 2007
@@ -19,6 +19,8 @@
 
 package org.apache.myfaces.orchestra.conversation.spring;
 
+import org.apache.myfaces.orchestra.conversation.Conversation;
+
 /**
  * Handles creation and lookup of any bean whose bean-definition specifies a scope
  * of "manual".
@@ -59,8 +61,5 @@
  */
 public class SpringManualScope extends AbstractSpringOrchestraScope
 {
-	protected ConversationPolicy getConversationPolicy()
-	{
-		return ConversationPolicy.MANUAL;
-	}
+	// No custom aspects to configure here.
 }

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/viewController/spring/SpringViewControllerScope.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/viewController/spring/SpringViewControllerScope.java?rev=573520&r1=573519&r2=573520&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/viewController/spring/SpringViewControllerScope.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/viewController/spring/SpringViewControllerScope.java Fri Sep  7 02:39:14 2007
@@ -19,16 +19,15 @@
 
 package org.apache.myfaces.orchestra.viewController.spring;
 
+import javax.faces.context.FacesContext;
+
 import org.apache.myfaces.orchestra.conversation.Conversation;
 import org.apache.myfaces.orchestra.conversation.ConversationManager;
 import org.apache.myfaces.orchestra.conversation.spring.AbstractSpringOrchestraScope;
-import org.apache.myfaces.orchestra.conversation.spring.ConversationPolicy;
 import org.apache.myfaces.orchestra.lib.OrchestraException;
 import org.apache.myfaces.orchestra.viewController.ViewControllerManager;
 import org.apache.myfaces.orchestra.viewController._ViewControllerUtils;
 
-import javax.faces.context.FacesContext;
-
 /**
  * provides a dummy scope which will place a bean configured for into the same conversation as the
  * viewController.
@@ -37,11 +36,6 @@
 {
 	public SpringViewControllerScope()
 	{
-	}
-
-	protected ConversationPolicy getConversationPolicy()
-	{
-		return ConversationPolicy.MANUAL;
 	}
 
 	protected Conversation startConversation(ConversationManager manager, String conversationName)