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 13:35:10 UTC

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

Author: skitching
Date: Fri Sep  7 04:35:09 2007
New Revision: 573548

URL: http://svn.apache.org/viewvc?rev=573548&view=rev
Log:
Make conversation lifetime a property of the scope, not a subclass.
Merge all subclasses of AbstractSpringOrchestraScope.
Detect 2 beans with same conversation but different scopes.

Added:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFlashLifetimeAspect.java
Removed:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFlashScopeAspect.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/SpringFlashScope.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/spring/SpringManualScope.java
Modified:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationTimeoutableAspect.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/SpringConversationScope.java

Added: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFlashLifetimeAspect.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFlashLifetimeAspect.java?rev=573548&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFlashLifetimeAspect.java (added)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationFlashLifetimeAspect.java Fri Sep  7 04:35:09 2007
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+/**
+ * Connect the conversation to the FlashScopeManager
+ */
+public class ConversationFlashLifetimeAspect extends ConversationAspect
+{
+	public ConversationFlashLifetimeAspect(Conversation conversation)
+	{
+		super(conversation);
+	}
+
+	public void markAsAccessed()
+	{
+		FlashScopeManager.getInstance().addConversationAccess(getConversation().getName());
+	}
+
+	public boolean isAccessed()
+	{
+		return FlashScopeManager.getInstance().isConversationAccessed(getConversation().getName());
+	}
+}

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationTimeoutableAspect.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationTimeoutableAspect.java?rev=573548&r1=573547&r2=573548&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationTimeoutableAspect.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationTimeoutableAspect.java Fri Sep  7 04:35:09 2007
@@ -23,10 +23,7 @@
  */
 public class ConversationTimeoutableAspect extends ConversationAspect
 {
-	/**
-	 * The default timeout: 30 minutes
-	 */
-	private long timeout = 30 * 60 * 1000;
+	private long timeout = -1;
 
 	public ConversationTimeoutableAspect(Conversation conversation)
 	{

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=573548&r1=573547&r2=573548&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 04:35:09 2007
@@ -24,7 +24,6 @@
 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;
@@ -43,7 +42,18 @@
 import org.springframework.context.ConfigurableApplicationContext;
 
 /**
- * Abstract basis class for all the Orchestra scopes
+ * Abstract basis class for all the Orchestra scopes.
+ * <p>
+ * A scope object has two quite different roles:
+ * <ol>
+ * <li>It handles the lookup of beans in a scope, and creates them if needed</li>
+ * <li>It handles the creation of Conversation objects, using the spring properties
+ * configured on the scope object.</li>
+ * </ol>
+ * <p>
+ * This base class handles item 1 above, and leaves item 2 to a subclass. The
+ * declaration of interface ConversationFactory needs to be on this class, however,
+ * as the createBean method needs to invoke it. 
  */
 public abstract class AbstractSpringOrchestraScope implements ConversationFactory, 
 	Scope, BeanFactoryAware, ApplicationContextAware
@@ -109,6 +119,7 @@
 		return getBean(name, objectFactory);
 	}
 
+	/** See method get(name, objectFactory). */
 	protected Object getBean(String beanName, ObjectFactory objectFactory)
 	{
 		String conversationName = getConversationNameForBean(beanName);
@@ -126,6 +137,33 @@
 				// callback to the createConversation method on this class.
 				conversation = manager.startConversation(conversationName, this);
 			}
+			else
+			{
+				// Check that the conversation's factory is this one.
+				//
+				// This handles the case where two different beans declare themselves
+				// as belonging to the same conversation but with different scope
+				// objects. Allowing that would be nasty as the conversation
+				// properties (eg flash or normal) would depend upon which bean
+				// got created first; some other ConversationFactory would have
+				// created the conversation using its configured properties then
+				// we are now adding to that conversation a bean that really wants
+				// the conversation properties defined on this ConversationFactory.
+				//
+				// Ideally the conversation properties would be defined using
+				// the conversation name, not the scope name; this problem would
+				// then not exist. However that would lead to some fairly clumsy
+				// configuration, particularly where lots of beans without explicit
+				// conversationName attributes are used.
+				
+				if (conversation.getFactory() != this) 
+				{
+					throw new IllegalArgumentException(
+						"Inconsistent scope and conversation name detected for bean "
+							+ beanName);
+				}
+				
+			}
 		}
 
 		// get the conversation
@@ -157,27 +195,6 @@
 
 		// get the bean
 		return conversation.getAttribute(beanName);
-	}
-
-	public Conversation createConversation(ConversationContext context, String name)
-	{
-		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)

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=573548&r1=573547&r2=573548&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 04:35:09 2007
@@ -20,20 +20,26 @@
 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.ConversationContext;
+import org.apache.myfaces.orchestra.conversation.ConversationFlashLifetimeAspect;
 import org.apache.myfaces.orchestra.conversation.ConversationTimeoutableAspect;
 
 /**
  * Handles creation and lookup of any bean whose bean-definition specifies a scope
  * of "conversation".
- *
- * <p>adapts the conversation scope to a new spring scope</p>
- * <p>It will also apply any advice configured</p>
- * <p>A sample configuration for a conversation scope with persistence
+ * <p>
+ * A scope bean handles Spring-specific callbacks in order to locate or create any beans whose definition
+ * specifies that scope. A scope can also be thought of as a "conversation template", as this object
+ * is responsible for creating a conversation when one is needed but does not yet exist.
+ * <p>
+ * <h2>Example</h2>
+ * A sample configuration for a conversation scope with persistence:
  * <pre>
  * 	&lt;bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"&gt;
  * &lt;property name="scopes"&gt;
  * &lt;map&gt;
- * &lt;entry key="conversation"&gt;
+ * &lt;entry key="conversation.normal"&gt;
  * &lt;bean class="org.apache.myfaces.orchestra.conversation.spring.SpringConversationScope"&gt;
  * &lt;property name="advices"&gt;
  * &lt;list&gt;
@@ -50,14 +56,112 @@
  * &lt;property name="persistenceContextFactory" ref="yourPersistentContextFactory" /&gt;
  * &lt;/bean&gt;
  * </pre>
- * </p>
- * <p/>
- * If no conversation exists for the given bean name a new one will be created.<br />
- * The beanName will also be the conversation name.
- * </p>
+ * <p>
+ * 
+ * <h2>Conversation properties</h2>
+ * The following properties can be defined on a scope and then apply to any conversation that is created
+ * to hold a bean of this scope:
+ * <ul>
+ * <li>lifetime: may be "normal" or "flash". If not specified, then defaults to "normal".</li>
+ * <li>timeout: a time period (in minutes) after which inactive conversations are terminated.
+ * If not specified, then inactive conversations are never automatically terminated.</li>
+ * </ul>
+ * 
+ * <h2>Other Notes</h2>
+ * If the bean definition does not specify a conversation name, then the bean name is used
+ * as the conversation name. 
+ * <p>
+ * As shown above, a list of AOP Advices can be specified for the scope, in which case each of the
+ * advices gets configured for any bean declared with this scope.
  */
-public class SpringConversationScope extends AbstractSpringTimeoutableScope
+public class SpringConversationScope extends AbstractSpringOrchestraScope
 {
-	// no custom aspects to configure here; just the time aspect from the parent 
-	// class is needed.
+	public static final String LIFETIME_FLASH = "flash";
+	public static final String LIFETIME_MANUAL = "manual";
+	
+	private Integer timeout;
+	private String lifetime = "manual";
+
+	/**
+	 * @see #setTimeout
+	 */
+	public Integer getTimeout()
+	{
+		return timeout;
+	}
+
+	/**
+	 * The timeout in minutes when the conversation will end.
+	 * See {@link ConversationTimeoutableAspect#timeout} for the default timeout. 
+	 */
+	public void setTimeout(Integer timeout)
+	{
+		this.timeout = timeout;
+	}
+
+	public String getLifetime() 
+	{
+		return lifetime;
+	}
+
+	/**
+	 * Must be one of "flash" or "manual". 
+	 * <p>
+	 * Defaults to "manual".
+	 */
+	public void setLifetime(String lifetime)
+	{
+		// Check for validity here so that an exception gets thrown on startup
+		// rather than when the first bean in this scope is created.
+		if (LIFETIME_FLASH.equals(lifetime))
+		{
+			this.lifetime = LIFETIME_FLASH;
+		}
+		else if (LIFETIME_MANUAL.equals(lifetime))
+		{
+			this.lifetime = LIFETIME_MANUAL;
+		}
+		else
+		{
+			throw new IllegalArgumentException("Invalid lifetime:" + lifetime);
+		}
+	}
+
+	/**
+	 * Implementation of ConversationFactory interface.
+	 */
+	public Conversation createConversation(ConversationContext context, String name)
+	{
+		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)
+	{
+		// conversation timeout
+		if (timeout != null)
+		{
+			long timeoutMsecs = timeout.longValue() * 60L * 1000L;
+			ConversationTimeoutableAspect aspect = new ConversationTimeoutableAspect(conversation);
+			aspect.setTimeout(timeoutMsecs);
+			conversation.addAspect(aspect);
+		}
+
+		// conversation lifetime
+		if (LIFETIME_FLASH == lifetime)
+		{
+			ConversationAspect aspect = new ConversationFlashLifetimeAspect(conversation);
+			conversation.addAspect(aspect);
+		}
+	}
 }