Skip to content

Commit

Permalink
Switch to attribute in view root for conversation propagation (thanks…
Browse files Browse the repository at this point in the history
… for the tip, Dan).

Name ConversationImpl to javax.webbeans.conversation

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1931 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Mar 11, 2009
1 parent 779b28f commit 6a9f62b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 49 deletions.
Expand Up @@ -34,7 +34,7 @@
* @see javax.context.Conversation
*/
@RequestScoped
@Named("conversation")
@Named("javax.webbeans.conversation")
@Standard
public class ConversationImpl implements Conversation
{
Expand Down Expand Up @@ -73,6 +73,7 @@ public ConversationImpl(ConversationImpl conversation)
* Initializes a new conversation
*
* @param conversationIdGenerator The conversation ID generator
* @param timeout The conversation inactivity timeout
*/
@Initializer
public void init(ConversationIdGenerator conversationIdGenerator, @ConversationInactivityTimeout long timeout)
Expand All @@ -91,6 +92,8 @@ public void begin()

public void begin(String id)
{
// Store away the (first) change to the conversation ID. If the original conversation was long-running,
// we might have to place it back for termination once the request is over.
if (originalCid == null)
{
originalCid = cid;
Expand Down Expand Up @@ -128,7 +131,7 @@ public void setTimeout(long timeout)
/**
* Assumes the identity of another conversation
*
* @param conversation The new conversation identity
* @param conversation The new conversation
*
*/
public void switchTo(Conversation conversation)
Expand All @@ -143,7 +146,7 @@ public void switchTo(Conversation conversation)
@Override
public String toString()
{
return "ID: " + cid + ", long-running: " + longRunning + ", timeout: " + timeout;
return "ID: " + cid + ", long-running: " + longRunning + ", timeout: " + timeout + "ms";
}

public void setLongRunning(boolean longRunning)
Expand Down
59 changes: 15 additions & 44 deletions impl/src/main/java/org/jboss/webbeans/jsf/PhaseHelper.java
Expand Up @@ -16,7 +16,6 @@
*/
package org.jboss.webbeans.jsf;

import javax.faces.component.html.HtmlInputHidden;
import javax.faces.context.FacesContext;
import javax.inject.AnnotationLiteral;
import javax.servlet.http.HttpSession;
Expand All @@ -36,7 +35,7 @@ public class PhaseHelper
{
private static LogProvider log = Logging.getLogProvider(PhaseHelper.class);

private static final String CONVERSATION_PROPAGATION_COMPONENT_ID = "webbeans_conversation_propagation";
private static final String CONVERSATION_PROPAGATION_KEY = "webbeans_conversation_propagationz";

/**
* Gets a FacesContext instance
Expand All @@ -58,47 +57,16 @@ public static boolean isPostback()
return context().getRenderKit().getResponseStateManager().isPostback(context());
}

/**
* Removes the conversation propagation component from the ui view root
*/
public static void removePropagationComponent()
{
log.debug("Removed propagation component");
HtmlInputHidden propagationComponent = getPropagationComponent();
if (propagationComponent != null)
{
context().getViewRoot().getChildren().remove(propagationComponent);
}
}

/**
* Creates and/or updates the conversation propagation component in the UI
* view root
*
* @param cid The conversation id to propagate
*/
public static void createOrUpdatePropagationComponent(String cid)
public static void propagateConversation(String cid)
{
HtmlInputHidden propagationComponent = getPropagationComponent();
if (propagationComponent == null)
{
log.trace("Created propagation component");
propagationComponent = (HtmlInputHidden) context().getApplication().createComponent(HtmlInputHidden.COMPONENT_TYPE);
propagationComponent.setId(CONVERSATION_PROPAGATION_COMPONENT_ID);
context().getViewRoot().getChildren().add(propagationComponent);
}
context().getViewRoot().getAttributes().put(CONVERSATION_PROPAGATION_KEY, cid);
log.debug("Updated propagation component with cid " + cid);
propagationComponent.setValue(cid);
}

/**
* Gets the propagation component from the UI view root
*
* @return The component (or null if not found)
*/
private static HtmlInputHidden getPropagationComponent()
{
return (HtmlInputHidden) context().getViewRoot().findComponent(CONVERSATION_PROPAGATION_COMPONENT_ID);
}

/**
Expand All @@ -115,18 +83,13 @@ public static String getConversationIdFromRequest()
}

/**
* Gets the propagated conversation id from the propagation component
* Gets the propagated conversation id from the view root attribute map
*
* @return The conversation id (or null if not found)
*/
public static String getConversationIdFromPropagationComponent()
public static String getConversationIdFromViewRoot()
{
String cid = null;
HtmlInputHidden propagationComponent = getPropagationComponent();
if (propagationComponent != null)
{
cid = propagationComponent.getValue().toString();
}
String cid = (String) context().getViewRoot().getAttributes().get(CONVERSATION_PROPAGATION_KEY);
log.trace("Got cid " + cid + " from propagation component");
return cid;
}
Expand All @@ -141,7 +104,7 @@ public static String getConversationId()
String cid = null;
if (isPostback())
{
cid = getConversationIdFromPropagationComponent();
cid = getConversationIdFromViewRoot();
}
else
{
Expand All @@ -161,4 +124,12 @@ public static HttpSession getHttpSession()
return (HttpSession) context().getExternalContext().getSession(true);
}

/**
* Stops conversation propagation through the view root
*/
public static void stopConversationPropagation()
{
context().getViewRoot().getAttributes().remove(CONVERSATION_PROPAGATION_KEY);
}

}
Expand Up @@ -64,11 +64,11 @@ private void beforeRenderReponse()
Conversation conversation = CurrentManager.rootManager().getInstanceByType(Conversation.class);
if (conversation.isLongRunning())
{
PhaseHelper.createOrUpdatePropagationComponent(conversation.getId());
PhaseHelper.propagateConversation(conversation.getId());
}
else
{
PhaseHelper.removePropagationComponent();
PhaseHelper.stopConversationPropagation();
}
}

Expand Down

0 comments on commit 6a9f62b

Please sign in to comment.