ConversationScope.java 7.8 KB
Newer Older
M
Micha Kiener 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2002-2008 the original author or authors.
 * 
 * Licensed 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.springframework.conversation.scope;

18
import org.springframework.beans.factory.ObjectFactory;
M
Micha Kiener 已提交
19
import org.springframework.beans.factory.config.Scope;
20 21 22 23 24 25
import org.springframework.conversation.Conversation;
import org.springframework.conversation.JoinMode;
import org.springframework.conversation.manager.ConversationManager;
import org.springframework.conversation.manager.ConversationResolver;
import org.springframework.conversation.manager.ConversationStore;
import org.springframework.util.Assert;
M
Micha Kiener 已提交
26

M
Micha Kiener 已提交
27
/**
28 29 30 31
 * The default implementation of the {@link ConversationScope} exposed as
 * <code>"conversation"</code> scope. It needs the {@link ConversationStore} and
 * the {@link ConversationResolver} to resolve and request the current
 * conversation where attributes are resolved with and registered in.
M
Micha Kiener 已提交
32 33 34 35
 * 
 * @author Micha Kiener
 * @since 3.1
 */
36
public class ConversationScope implements Scope {
M
Micha Kiener 已提交
37 38 39
	/** The name of the scope being exposed within the application context. */
	public static final String CONVERSATION_SCOPE_NAME = "conversation";

M
Micha Kiener 已提交
40 41
	/**
	 * The name of the contextual object for the conversation manager (see
42
	 * {@link org.springframework.beans.factory.config.Scope#resolveContextualObject(String)}).
M
Micha Kiener 已提交
43 44
	 */
	public static final String REFERENCE_CONVERSATION_MANAGER = "conversationManager";
M
Micha Kiener 已提交
45

M
Micha Kiener 已提交
46 47
	/**
	 * The name of the contextual object for the conversation store (see
48
	 * {@link org.springframework.beans.factory.config.Scope#resolveContextualObject(String)}).
M
Micha Kiener 已提交
49 50 51 52 53
	 */
	public static final String REFERENCE_CONVERSATION_STORE = "conversationStore";

	/**
	 * The name of the contextual object for the conversation resolver (see
54
	 * {@link org.springframework.beans.factory.config.Scope#resolveContextualObject(String)}).
M
Micha Kiener 已提交
55 56
	 */
	public static final String REFERENCE_CONVERSATION_RESOLVER = "conversationResolver";
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227


	/** Holds the conversation manager reference, if statically injected. */
	private ConversationManager conversationManager;

	/** Holds the conversation store reference, if statically injected. */
	private ConversationStore conversationStore;

	/** Holds the conversation resolver reference, if statically injected. */
	private ConversationResolver conversationResolver;

	/**
	 * This method is invoked to resolve the current conversation used where
	 * attributes having conversation scope are being resolved with or stored
	 * in.
	 * 
	 * @return the currently used conversation, or <code>null</code>, if no one
	 * currently available and <code>createIfNotExisting</code> is
	 * <code>false</code>
	 */
	protected Conversation getCurrentConversation(boolean createNewIfNotExisting) {
		ConversationResolver resolver = getConversationResolver();
		Assert.notNull(resolver, "No conversation resolver available within the conversation scope");

		String conversationId = resolver.getCurrentConversationId();
		Conversation conversation;
		if (conversationId == null) {
			if (createNewIfNotExisting) {
				// start a new, temporary conversation using the default join
				// mode
				ConversationManager manager = getConversationManager();
				conversation = manager.beginConversation(true, JoinMode.getDefaultJoinMode());
			} else {
				return null;
			}
		} else {
			ConversationStore store = getConversationStore();
			Assert.notNull(store, "No conversation store available within the conversation scope");
			conversation = store.getConversation(conversationId);
			Assert.notNull(conversation, "The conversation with id <" + conversationId
					+ "> is not available within the store");
		}

		return conversation;
	}

	/**
	 * @see org.springframework.beans.factory.config.Scope#get(java.lang.String,
	 * org.springframework.beans.factory.ObjectFactory)
	 */
	public Object get(String name, ObjectFactory<?> objectFactory) {
		Conversation conversation = getCurrentConversation(true);
		Object attribute = conversation.getAttribute(name);
		if (attribute == null) {
			attribute = objectFactory.getObject();
			conversation.setAttribute(name, attribute);
		}

		return attribute;
	}

	/**
	 * @see org.springframework.beans.factory.config.Scope#getConversationId()
	 */
	public String getConversationId() {
		Conversation conversation = getCurrentConversation(false);
		if (conversation != null) {
			return conversation.getId();
		}

		return null;
	}

	/**
	 * @see org.springframework.beans.factory.config.Scope#registerDestructionCallback(java.lang.String,
	 * java.lang.Runnable)
	 */
	public void registerDestructionCallback(String name, Runnable callback) {
		Conversation conversation = getCurrentConversation(false);
		if (conversation != null) {
			conversation.registerDestructionCallback(name, callback);
		}
	}

	/**
	 * @see org.springframework.beans.factory.config.Scope#remove(java.lang.String)
	 */
	public Object remove(String name) {
		Conversation conversation = getCurrentConversation(false);
		if (conversation != null) {
			return conversation.removeAttribute(name);
		}

		return null;
	}

	/**
	 * Supports the following objects:
	 * <ul>
	 * <li><code>"conversationManager"</code>, returns the {@link ConversationManager}</li>
	 * <li><code>"conversationStore"</code>, returns the {@link ConversationStore}</li>
	 * <li><code>"conversationResolver"</code>, returns the {@link ConversationResolver}</li>
	 * </ul>
	 *
	 * @see org.springframework.beans.factory.config.Scope#resolveContextualObject(java.lang.String)
	 */
	public Object resolveContextualObject(String key) {
		if (REFERENCE_CONVERSATION_MANAGER.equals(key)) {
			return getConversationManager();
		} else if (REFERENCE_CONVERSATION_STORE.equals(key)) {
			return getConversationStore();
		} else if (REFERENCE_CONVERSATION_RESOLVER.equals(key)) {
			return getConversationResolver();
		}

		return null;
	}

	/**
	 * @param conversationManager the conversation manager reference to be used
	 * by this scope
	 */
	public void setConversationManager(ConversationManager conversationManager) {
		this.conversationManager = conversationManager;
	}

	/**
	 * @return the conversation manager reference
	 */
	public ConversationManager getConversationManager() {
		return conversationManager;
	}

	/**
	 * @param conversationStore the reference of the conversation store to be
	 * injected and internally used to request registered conversation objects
	 */
	public void setConversationStore(ConversationStore conversationStore) {
		this.conversationStore = conversationStore;
	}

	/**
	 * Returns the reference of the conversation store to be used. If the store
	 * is not within the same scope as the conversation scope, this method has
	 * to be injected.
	 * 
	 * @return the reference of the conversation store
	 */
	public ConversationStore getConversationStore() {
		return conversationStore;
	}

	/**
	 * @param conversationResolver the reference of the conversation resolver
	 * used to determine the currently used conversation id
	 */
	public void setConversationResolver(ConversationResolver conversationResolver) {
		this.conversationResolver = conversationResolver;
	}

	/**
	 * Returns the reference of the conversation resolver to be used. If the
	 * resolver is not within the same scope as the conversation scope, this
	 * method has to be injected.
	 *
	 * @return the reference of the conversation resolver
	 */
	public ConversationResolver getConversationResolver() {
		return conversationResolver;
	}
}