org.springframework.conversation.manager
Class DefaultConversation

java.lang.Object
  extended by org.springframework.conversation.manager.DefaultConversation
All Implemented Interfaces:
Serializable, Conversation, MutableConversation

public class DefaultConversation
extends Object
implements MutableConversation, Serializable

The default implementation of the Conversation and MutableConversation interfaces.
This default implementation is also used within the AbstractConversationRepository.

The implementation supports destruction callbacks for attributes. The conversation object is serializable as long as all of its attributes are serializable as well.

Since:
3.1
Author:
Micha Kiener
See Also:
Serialized Form

Constructor Summary
DefaultConversation()
           
 
Method Summary
 void addChildConversation(MutableConversation conversation, boolean isIsolated)
          Method being invoked to add the given conversation as a child conversation to this parent conversation.
protected  void checkValidity()
           
 void clear()
          Clears the state of this conversation by removing all of its attributes.
 Object getAttribute(String name)
          Considers the internal attribute map as well as the map from the parent, if this is a nested conversation and only if it is not isolated.
 List<? extends Conversation> getChildren()
          Returns a list of child conversations, if any, an empty list otherwise, must never return null.
 long getCreationTime()
          Returns the timestamp in milliseconds this conversation has been created.
 String getId()
          Returns the id of this conversation which must be unique within the scope it is used to identify the conversation object.
 long getLastAccessedTime()
          Returns the timestamp in milliseconds this conversation was last accessed (usually through a Conversation.getAttribute(String), Conversation.setAttribute(String, Object) or Conversation.removeAttribute(String) access).
 Conversation getParent()
          Returns the parent conversation, if this is a nested conversation, null otherwise.
 Conversation getRoot()
          Returns the top level root conversation, if this is a nested conversation or this conversation, if it is the top level root conversation.
 int getTimeout()
          Always returns the timeout value being set on the root as the root conversation is responsible for the timeout management.
 void invalidate()
          Invalidates this conversation object.
 boolean isExpired()
          Return true if the top root conversation has expired as the timeout is only tracked on the root conversation.
 boolean isIsolated()
          Returns true, if this is a nested, isolated conversation so that it does not inherit the state from its parent but rather has its own state.
 boolean isNested()
          Returns true, if this is a nested conversation and hence Conversation.getParent() will returns a non-null value.
 boolean isParent()
           
 void registerDestructionCallback(String name, Runnable callback)
          Registers the given callback to be invoked if the attribute having the specified name is being removed from this conversation.
 Object removeAttribute(String name)
          Removes the value in the current conversation having the given name and returns it, if found and removed, null otherwise.
Removes the attribute from this specific conversation, does not remove it, if found within its parent.
 void removeChildConversation(MutableConversation conversation)
          Removes the given child conversation from this parent conversation.
protected  void removeParent()
           
 Object setAttribute(String name, Object value)
          Stores the given value in this conversation using the specified name.
 void setId(String id)
          Set the id for this conversation which must be unique within the scope the conversation objects are being stored.
protected  void setParentConversation(MutableConversation parentConversation, boolean isIsolated)
           
 void setTimeout(int timeout)
          The timeout will be set on the root only.
 void touch()
          Reset the last access timestamp using the current time in milliseconds from the system.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultConversation

public DefaultConversation()
Method Detail

getAttribute

public Object getAttribute(String name)
Considers the internal attribute map as well as the map from the parent, if this is a nested conversation and only if it is not isolated.

Specified by:
getAttribute in interface Conversation
Parameters:
name - the name of the value to be retrieved
Returns:
the value, if available in the current state, null otherwise

setAttribute

public Object setAttribute(String name,
                           Object value)
Description copied from interface: Conversation
Stores the given value in this conversation using the specified name. If this state already contains a value attached to the given name, it is returned, null otherwise.
This method stores the attribute within this conversation so it will be available through this and all nested conversations.

Specified by:
setAttribute in interface Conversation
Parameters:
name - the name of the value to be stored in this conversation
value - the value to be stored
Returns:
the old value attached to the same name, if any, null otherwise

removeAttribute

public Object removeAttribute(String name)
Description copied from interface: Conversation
Removes the value in the current conversation having the given name and returns it, if found and removed, null otherwise.
Removes the attribute from this specific conversation, does not remove it, if found within its parent.

Specified by:
removeAttribute in interface Conversation
Parameters:
name - the name of the value to be removed from this conversation
Returns:
the removed value, if found, null otherwise

clear

public void clear()
Description copied from interface: MutableConversation
Clears the state of this conversation by removing all of its attributes. It will, however, not invalidate the conversation. All attributes having a destruction callback being registered will fire, if the underlying ConversationRepository supports destruction callbacks.

Specified by:
clear in interface MutableConversation

setId

public void setId(String id)
Description copied from interface: MutableConversation
Set the id for this conversation which must be unique within the scope the conversation objects are being stored. The id of the conversation objects is usually managed by the ConversationRepository.

Specified by:
setId in interface MutableConversation
Parameters:
id - the id of the conversation

getId

public String getId()
Description copied from interface: Conversation
Returns the id of this conversation which must be unique within the scope it is used to identify the conversation object. The id is set by the ConversationRepository and most likely be used by the ConversationResolver in order to manage the current conversation.

Specified by:
getId in interface Conversation
Returns:
the id of this conversation

getRoot

public Conversation getRoot()
Description copied from interface: Conversation
Returns the top level root conversation, if this is a nested conversation or this conversation, if it is the top level root conversation. This method never returns null.

Specified by:
getRoot in interface Conversation
Returns:
the root conversation (top level conversation)

getParent

public Conversation getParent()
Description copied from interface: Conversation
Returns the parent conversation, if this is a nested conversation, null otherwise.

Specified by:
getParent in interface Conversation
Returns:
the parent conversation, if any, null otherwise

getChildren

public List<? extends Conversation> getChildren()
Description copied from interface: Conversation
Returns a list of child conversations, if any, an empty list otherwise, must never return null.

Specified by:
getChildren in interface Conversation
Returns:
a list of child conversations (may be empty, never null)

setParentConversation

protected void setParentConversation(MutableConversation parentConversation,
                                     boolean isIsolated)

addChildConversation

public void addChildConversation(MutableConversation conversation,
                                 boolean isIsolated)
Description copied from interface: MutableConversation
Method being invoked to add the given conversation as a child conversation to this parent conversation. If isIsolated is true, the state of the child conversation is isolated from its parent state, if it is set to false, the child conversation will inherit the state from its parent.

Specified by:
addChildConversation in interface MutableConversation
Parameters:
conversation - the conversation to be added as a child to this parent conversation
isIsolated - flag indicating whether this conversation should be isolated from the given parent conversation

removeChildConversation

public void removeChildConversation(MutableConversation conversation)
Description copied from interface: MutableConversation
Removes the given child conversation from this parent conversation.

Specified by:
removeChildConversation in interface MutableConversation
Parameters:
conversation - the conversation to be removed from this one

removeParent

protected void removeParent()

isNested

public boolean isNested()
Description copied from interface: Conversation
Returns true, if this is a nested conversation and hence Conversation.getParent() will returns a non-null value.

Specified by:
isNested in interface Conversation
Returns:
true, if this is a nested conversation, false otherwise

isParent

public boolean isParent()

isIsolated

public boolean isIsolated()
Description copied from interface: Conversation
Returns true, if this is a nested, isolated conversation so that it does not inherit the state from its parent but rather has its own state. See ConversationType.ISOLATED for more details.

Specified by:
isIsolated in interface Conversation
Returns:
true, if this is a nested, isolated conversation

getTimeout

public int getTimeout()
Always returns the timeout value being set on the root as the root conversation is responsible for the timeout management.

Specified by:
getTimeout in interface Conversation
Returns:
the timeout in seconds if any, 0 otherwise

setTimeout

public void setTimeout(int timeout)
The timeout will be set on the root only.

Specified by:
setTimeout in interface Conversation
Parameters:
timeout - the timeout in seconds to set, 0 for no timeout

getCreationTime

public long getCreationTime()
Description copied from interface: Conversation
Returns the timestamp in milliseconds this conversation has been created.

Specified by:
getCreationTime in interface Conversation
Returns:
the creation timestamp in millis

getLastAccessedTime

public long getLastAccessedTime()
Description copied from interface: Conversation
Returns the timestamp in milliseconds this conversation was last accessed (usually through a Conversation.getAttribute(String), Conversation.setAttribute(String, Object) or Conversation.removeAttribute(String) access).

Specified by:
getLastAccessedTime in interface Conversation
Returns:
the system time in milliseconds for the last access of this conversation

invalidate

public void invalidate()
Description copied from interface: MutableConversation
Invalidates this conversation object. An invalidated conversation will throw an IllegalStateException, if it is accessed or modified.

Specified by:
invalidate in interface MutableConversation

checkValidity

protected void checkValidity()

isExpired

public boolean isExpired()
Return true if the top root conversation has expired as the timeout is only tracked on the root conversation.

Specified by:
isExpired in interface MutableConversation
Returns:
true if the root of this conversation has been expired

touch

public void touch()
Description copied from interface: MutableConversation
Reset the last access timestamp using the current time in milliseconds from the system. This is usually done if a conversation is used behind a scope and beans are being accessed or added to it.

Specified by:
touch in interface MutableConversation

registerDestructionCallback

public void registerDestructionCallback(String name,
                                        Runnable callback)
Description copied from interface: MutableConversation
Registers the given callback to be invoked if the attribute having the specified name is being removed from this conversation. Supporting destruction callbacks is dependant of the underlying ConversationRepository, so this operation is optional and might not be supported.

Specified by:
registerDestructionCallback in interface MutableConversation
Parameters:
name - the name of the attribute to register the destruction callback for
callback - the callback to be invoked if the specified attribute is removed from this conversation