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 2008/05/20 10:26:56 UTC
svn commit: r658160 - in
/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation:
ConversationManager.java ConversationRequestParameterProvider.java
Author: skitching
Date: Tue May 20 01:26:55 2008
New Revision: 658160
URL: http://svn.apache.org/viewvc?rev=658160&view=rev
Log:
Allow external code to query whether a conversation context already exists or not, without creating one as a side-effect.
Clean up some confusing APIs. Previously, some of the getXXX methods would create things as a side-effect, and others would not. And some took a boolean create-or-not parameter. Now, all get methods are side-effect-free except for getInstance(), and a getOrCreate version exists to force creation of objects.
Modified:
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/ConversationRequestParameterProvider.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=658160&r1=658159&r2=658160&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 Tue May 20 01:26:55 2008
@@ -130,6 +130,28 @@
}
/**
+ * Get the current conversationContextId.
+ * <p>
+ * If there is no current conversationContext, then null is returned.
+ */
+ private Long findConversationContextId()
+ {
+ FrameworkAdapter fa = FrameworkAdapter.getCurrentInstance();
+
+ // Has it been extracted from the req params and cached as a req attr?
+ Long conversationContextId = (Long)fa.getRequestAttribute(CONVERSATION_CONTEXT_REQ);
+ if (conversationContextId == null)
+ {
+ if (fa.containsRequestParameterAttribute(CONVERSATION_CONTEXT_PARAM))
+ {
+ String urlConversationContextId = fa.getRequestParameterAttribute(CONVERSATION_CONTEXT_PARAM).toString();
+ conversationContextId = new Long(Long.parseLong(urlConversationContextId, Character.MAX_RADIX));
+ }
+ }
+ return conversationContextId;
+ }
+
+ /**
* Get the current, or create a new unique conversationContextId.
* <p>
* The current conversationContextId will be retrieved from the request
@@ -146,56 +168,66 @@
* <p>
* This method <i>never</i> returns null.
*/
- public Long getConversationContextId()
+ private Long getOrCreateConversationContextId()
{
- FrameworkAdapter fa = FrameworkAdapter.getCurrentInstance();
-
- // first, check whether we have previously found this value and cached it as a
- // request-scoped value.
- Long conversationContextId = (Long)fa.getRequestAttribute(CONVERSATION_CONTEXT_REQ);
-
+ Long conversationContextId = findConversationContextId();
if (conversationContextId == null)
{
- if (fa.containsRequestParameterAttribute(CONVERSATION_CONTEXT_PARAM))
- {
- String urlConversationContextId = fa.getRequestParameterAttribute(CONVERSATION_CONTEXT_PARAM).toString();
- conversationContextId = new Long(Long.parseLong(urlConversationContextId, Character.MAX_RADIX));
- }
- else
- {
- conversationContextId = createNextConversationContextId();
- }
-
+ conversationContextId = createNextConversationContextId();
+ FrameworkAdapter fa = FrameworkAdapter.getCurrentInstance();
fa.setRequestAttribute(CONVERSATION_CONTEXT_REQ, conversationContextId);
}
return conversationContextId;
}
- protected Long createNextConversationContextId() {
- Long conversationContextId;
- synchronized(this)
- {
- conversationContextId = new Long(nextConversationContextId);
- nextConversationContextId++;
- }
- return conversationContextId;
+ /**
+ * Get the current, or create a new unique conversationContextId.
+ * <p>
+ * This method is deprecated because, unlike all the other get methods, it
+ * actually creates the value if it does not exist. Other get methods (except
+ * getInstance) return null if the data does not exist. In addition, this
+ * method is not really useful to external code and probably should never
+ * have been exposed as a public API in the first place; external code should
+ * never need to force the creation of a ConversationContext.
+ * <p>
+ * For internal use within this class, use either findConversationContextId()
+ * or getOrCreateConversationContextId().
+ * <p>
+ * To just obtain the current ConversationContext <i>if it exists</i>, see
+ * method getCurrentConversationContext().
+ *
+ * @deprecated This method should not be needed by external classes, and
+ * was inconsistent with other methods on this class.
+ */
+ public Long getConversationContextId()
+ {
+ return getOrCreateConversationContextId();
}
/**
- * Get the conversation context for the given id
+ * Allocate a new Long value for use as a conversation context id.
+ * <p>
+ * The returned value must not match any conversation context id already in
+ * use within this ConversationManager instance (which is scoped to the
+ * current http session).
*/
- protected ConversationContext getConversationContext(Long conversationContextId)
+ private Long createNextConversationContextId()
{
- synchronized (this)
+ Long conversationContextId;
+ synchronized(this)
{
- return (ConversationContext) conversationContexts.get(conversationContextId);
+ conversationContextId = new Long(nextConversationContextId);
+ nextConversationContextId++;
}
+ return conversationContextId;
}
/**
* Return a snapshot array of all conversation contexts associated with the
* current topmost conversation context.
+ *
+ * @since 1.2
*/
protected ConversationContext[] getConversationContexts()
{
@@ -203,7 +235,7 @@
{
List ret = new ArrayList();
- ConversationContext topmost = getTopmostConversationContext(false);
+ ConversationContext topmost = getTopmostConversationContext();
if (topmost == null)
{
return EMPTY_CONTEXTS;
@@ -234,13 +266,42 @@
}
/**
- * Get the conversation context for the given id. <br />
- * If there is no conversation context a new one will be created. The new conversation context
- * will be a "top-level" context.
+ * Get the conversation context for the given id.
+ * <p>
+ * Null is returned if there is no ConversationContext with the specified id.
+ * <p>
+ * Param conversationContextId must not be null.
*/
- protected ConversationContext createConversationContext(Long conversationContextId)
+ protected ConversationContext getConversationContext(Long conversationContextId)
{
- return new ConversationContext(null, conversationContextId);
+ synchronized (this)
+ {
+ return (ConversationContext) conversationContexts.get(conversationContextId);
+ }
+ }
+
+ /**
+ * Get the conversation context for the given id.
+ * <p>
+ * If there is no such conversation context a new one will be created. The new conversation
+ * context will be a "top-level" context (ie has no parent).
+ */
+ protected ConversationContext getOrCreateConversationContext(Long conversationContextId)
+ {
+ synchronized (this)
+ {
+ ConversationContext conversationContext = (ConversationContext) conversationContexts.get(conversationContextId);
+ if (conversationContext == null)
+ {
+ conversationContext = new ConversationContext(null, conversationContextId);
+ conversationContexts.put(conversationContextId, conversationContext);
+
+ // TODO: add the "user" name here, otherwise this debugging is not very useful
+ // except when testing a webapp with only one user.
+ log.debug("Created context " + conversationContextId);
+ }
+ return conversationContext;
+ }
}
/**
@@ -250,6 +311,8 @@
* <br />
* Notice: The created conversation context is not activated after this method call. You have to call
* {@link #activateConversationContext(ConversationContext)} on the newly create context too.
+ *
+ * @since 1.2
*/
protected ConversationContext startNewConversationContext()
{
@@ -258,7 +321,7 @@
ConversationContext parent = getCurrentConversationContext();
if (parent == null)
{
- parent = getTopmostConversationContext(true);
+ parent = getOrCreateTopmostConversationContext();
}
Long conversationContextId = createNextConversationContextId();
@@ -277,6 +340,8 @@
/**
* Actually activate the given conversation context. This means the current conversation context will be
* deactivated and the conversations of the passed in conversation context are those used again.
+ *
+ * @since 1.2
*/
public void activateConversationContext(ConversationContext ctx)
{
@@ -289,18 +354,21 @@
*/
public void clearCurrentConversationContext()
{
- Long conversationContextId = getConversationContextId();
- ConversationContext conversationContext = getConversationContext(conversationContextId);
- if (conversationContext != null)
+ Long conversationContextId = findConversationContextId();
+ if (conversationContextId != null)
{
- conversationContext.clear();
+ ConversationContext conversationContext = getConversationContext(conversationContextId);
+ if (conversationContext != null)
+ {
+ conversationContext.clear();
+ }
}
}
/**
- * <p>Destroy the given conversation context</p>
- * <p/>
- * <p>Notice: its assumed that the context is already been destroyed</p>
+ * Destroy the given conversation context.
+ * <p>
+ * Note: it is assumed that the context is already been destroyed
*/
protected void removeConversationContext(Long conversationContextId)
{
@@ -317,28 +385,27 @@
*/
public Conversation startConversation(String name, ConversationFactory factory)
{
- ConversationContext conversationContext = getCurrentConversationContext();
- if (conversationContext == null)
- {
- conversationContext = getTopmostConversationContext(true);
- }
+ ConversationContext conversationContext = getOrCreateCurrentConversationContext();
return conversationContext.startConversation(name, factory);
}
/**
* Remove a conversation
*
- * <p>Notice: Its assumed that the conversation has already been invalidated</p>
+ * Note: It is assumed that the conversation has already been invalidated
*
* @see ConversationContext#removeConversation(String)
*/
protected void removeConversation(String name)
{
- Long conversationContextId = getConversationContextId();
- ConversationContext conversationContext = getConversationContext(conversationContextId);
- if (conversationContext != null)
+ Long conversationContextId = findConversationContextId();
+ if (conversationContextId != null)
{
- conversationContext.removeConversation(name);
+ ConversationContext conversationContext = getConversationContext(conversationContextId);
+ if (conversationContext != null)
+ {
+ conversationContext.removeConversation(name);
+ }
}
}
@@ -388,61 +455,88 @@
/**
* Get the current conversation context. In an plain Orchestra installation this will also be the tompost
* conversation context. When using a dialog/page-flow environment this might be the bottom conversation context.
+ * <p>
+ * Null is returned if there is no current conversationContext.
*/
public ConversationContext getCurrentConversationContext()
{
- Long conversationContextId = getConversationContextId();
- return getConversationContext(conversationContextId);
+ Long ccid = findConversationContextId();
+ if (ccid == null)
+ {
+ return null;
+ }
+ else
+ {
+ return getConversationContext(ccid);
+ }
+ }
+
+ /**
+ * Return a ConversationContext, creating one if none currently exists.
+ * <p>
+ * This is currently package-scoped because it is not clear that code
+ * outside orchestra can have any use for this method. The only user
+ * outside of this class is ConversationRequestParameterProvider.
+ *
+ * @since 1.2
+ */
+ ConversationContext getOrCreateCurrentConversationContext()
+ {
+ Long ccid = getOrCreateConversationContextId();
+ return getOrCreateConversationContext(ccid);
+ }
+
+ /**
+ * Return true if there is a conversation context associated with the
+ * current request.
+ */
+ public boolean hasConversationContext()
+ {
+ return getCurrentConversationContext() == null;
}
/**
* Get the topmost conversation context (aka the window conversation context).
+ * <p>
+ * Null is returned if it does not exist.
+ *
+ * @since 1.2
*/
- public ConversationContext getTopmostConversationContext(boolean create)
+ public ConversationContext getTopmostConversationContext()
{
- Long conversationContextId = getConversationContextId();
+ Long ccid = findConversationContextId();
+ if (ccid == null)
+ {
+ return null;
+ }
+
synchronized (this)
{
- ConversationContext conversationContext = getConversationContext(conversationContextId);
- while (conversationContext != null && conversationContext.getParent() != null)
+ ConversationContext conversationContext = getConversationContext(ccid);
+ if (conversationContext == null)
{
- conversationContext = conversationContext.getParent();
+ return null;
}
- if (conversationContext == null && create)
+ else
{
- conversationContext = (ConversationContext) conversationContexts.get(conversationContextId);
- if (conversationContext == null)
- {
- conversationContext = createConversationContext(conversationContextId);
- conversationContexts.put(conversationContextId, conversationContext);
- }
-
- return conversationContext;
+ return conversationContext.getTopmostConversationContext();
}
- return conversationContext;
}
}
/**
- * Get the current conversation context.
- *
- * @return null if there is no context active
- */
- public ConversationContext getTopmostConversationContext()
- {
- return getTopmostConversationContext(false);
- }
-
- /**
- * check if we have a conversation context
+ * Get the topmost conversation context (aka the window conversation context).
+ * <p>
+ * Create it if it does not exist.
*/
- public boolean hasConversationContext()
+ private ConversationContext getOrCreateTopmostConversationContext()
{
- return
- (
- FrameworkAdapter.getCurrentInstance().containsRequestAttribute(CONVERSATION_CONTEXT_REQ) ||
- FrameworkAdapter.getCurrentInstance().containsRequestParameterAttribute(CONVERSATION_CONTEXT_REQ)) &&
- getCurrentConversationContext() != null;
+ Long ccid = getOrCreateConversationContextId();
+ synchronized (this)
+ {
+ ConversationContext conversationContext = getOrCreateConversationContext(ccid);
+ return conversationContext.getTopmostConversationContext();
+ }
}
/**
@@ -503,24 +597,6 @@
}
}
- /*
- * check if a bean with the given name will be managed by the conversationManager.
- public boolean isManagedBean(String beanName)
- {
- ConfigurableApplicationContext applicationContext = FrameworkAdapter.getInstance().getApplicationContext();
- ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
-
- // XXX: Hack to get the real definition in case of scoped targets
- BeanDefinition beanDefinition = beanFactory.getBeanDefinition("scopedTarget." + beanName);
- if (beanDefinition == null)
- {
- beanDefinition = beanFactory.getBeanDefinition(beanName);
- }
-
- return beanDefinition.getScope() != null && managedScopes.contains(beanDefinition.getScope());
- }
- */
-
private void writeObject(java.io.ObjectOutputStream out) throws IOException
{
// the conversation manager is not (yet) serializable, we just implement it
Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationRequestParameterProvider.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationRequestParameterProvider.java?rev=658160&r1=658159&r2=658160&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationRequestParameterProvider.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/ConversationRequestParameterProvider.java Tue May 20 01:26:55 2008
@@ -83,12 +83,8 @@
throw new IllegalStateException("can find the conversationManager");
}
- ConversationContext ctx = conversationManager.getCurrentConversationContext();
- if (ctx == null)
- {
- ctx = conversationManager.getTopmostConversationContext(true);
- }
-
+ // fetch the conversation context, creating one if it does not yet exist.
+ ConversationContext ctx = conversationManager.getOrCreateCurrentConversationContext();
return Long.toString(ctx.getId().longValue(), Character.MAX_RADIX);
}