com.google.code.rees.scope.conversation.context.DefaultConversationContextManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of struts2-conversation-scope-plugin Show documentation
Show all versions of struts2-conversation-scope-plugin Show documentation
A Struts2 plugin for leveraging conversation scopes in Struts2
/*******************************************************************************
*
* Struts2-Conversation-Plugin - An Open Source Conversation- and Flow-Scope Solution for Struts2-based Applications
* =================================================================================================================
*
* Copyright (C) 2012 by Rees Byars
* http://code.google.com/p/struts2-conversation/
*
* **********************************************************************************************************************
*
* 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.
*
* **********************************************************************************************************************
*
* $Id: DefaultConversationContextManager.java reesbyars $
******************************************************************************/
package com.google.code.rees.scope.conversation.context;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.code.rees.scope.conversation.ConversationConstants;
/**
* The default implementation of the {@link ConversationContextManager}.
*
* @author rees.byars
*
*/
public class DefaultConversationContextManager implements ConversationContextManager {
private static final long serialVersionUID = 3699451038473294837L;
private static final Logger LOG = LoggerFactory.getLogger(DefaultConversationContextManager.class);
protected ConversationContextFactory contextFactory;
protected Map> conversations = Collections.synchronizedMap(new HashMap>());
protected int maxInstances = ConversationConstants.DEFAULT_MAXIMUM_NUMBER_OF_A_GIVEN_CONVERSATION;
protected long nextId = 0L;
/**
* {@inheritDoc}
*/
@Override
public void setMaxInstances(int maxInstances) {
this.maxInstances = maxInstances;
}
/**
* {@inheritDoc}
*/
@Override
public void setContextFactory(ConversationContextFactory contextFactory) {
this.contextFactory = contextFactory;
}
/**
* {@inheritDoc}
*/
@Override
public ConversationContext createContext(String conversationName, long maxIdleTimeMillis) {
ConversationContext context = null;
synchronized (this.conversations) {
Map conversationContexts = this.conversations.get(conversationName);
if (conversationContexts == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Creating new context cache for " + conversationName);
}
conversationContexts = Collections.synchronizedMap(new HashMap());
this.conversations.put(conversationName, conversationContexts);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Creating new ConversationContext for " + conversationName);
}
String conversationId = this.getNextId();
context = this.contextFactory.create(conversationName, conversationId, maxIdleTimeMillis);
conversationContexts.put(conversationId, context);
if (conversationContexts.size() > this.maxInstances) {
if (LOG.isDebugEnabled()) {
LOG.debug("Cached instances of conversation " + conversationName + " exceeds limit. Removing stale conversations.");
}
this.removeMostStaleConversation(conversationContexts, conversationName, context.getRemainingTime());
}
}
return context;
}
/**
* {@inheritDoc}
*/
@Override
public ConversationContext getContext(String conversationName, String conversationId) {
ConversationContext context = null;
synchronized (this.conversations) {
Map conversationContexts = this.conversations.get(conversationName);
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving cached instance for conversation " + conversationName);
}
if (conversationContexts != null) {
context = conversationContexts.get(conversationId);
if (context != null) {
context.reset(); //reset the timeout
}
}
}
return context;
}
/**
* {@inheritDoc}
*/
@Override
public ConversationContext remove(String conversationName, String conversationId) {
if (LOG.isDebugEnabled()) {
LOG.debug("Discarding " + conversationName + " with ID of " + conversationId);
}
ConversationContext context = null;
Map conversationContexts = this.conversations.get(conversationName);
if (conversationContexts != null) {
context = conversationContexts.remove(conversationId);
}
return context;
}
/**
* {@inheritDoc}
*/
@Override
public void destroy() {
LOG.debug("Destroying ConversationContextManager and clearing conversation cache.");
synchronized (this.conversations) {
for (Entry> conversationEntry : this.conversations.entrySet()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Clearing contexts for " + conversationEntry.getKey() + ".");
}
Map conversationContexts = conversationEntry.getValue();
for (ConversationContext context : conversationContexts.values()) {
context.clear();
}
conversationContexts.clear();
}
this.conversations.clear();
}
LOG.debug("ConversationContextManager destroyed and conversation cache cleared.");
}
/**
* Recursively removes the least-recently accessed conversations until the
* number of remaining conversations equals {@link #maxInstances}
*/
protected void removeMostStaleConversation(Map conversationContexts, String conversationName, long defaultDuration) {
String mostStaleId = null;
long leastRemainingTime = defaultDuration;
synchronized (conversationContexts) {
for (Entry entry : conversationContexts.entrySet()) {
long entryRemainingTime = entry.getValue().getRemainingTime();
if (entryRemainingTime <= leastRemainingTime) {
mostStaleId = entry.getKey();
leastRemainingTime = entryRemainingTime;
}
}
this.remove(conversationName, mostStaleId);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Discarding most stale " + conversationName + " context with ID " + mostStaleId);
LOG.debug("Remaining " + conversationName + " contexts for this session: " + conversationContexts.size());
}
if (conversationContexts.size() > this.maxInstances) {
removeMostStaleConversation(conversationContexts, conversationName, defaultDuration);
}
}
protected synchronized String getNextId() {
return String.valueOf(this.nextId++);
}
}