All Downloads are FREE. Search and download functionalities are using the official Maven repository.

clime.messadmin.model.Application Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show newest version
/**
 * 
 */
package clime.messadmin.model;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import clime.messadmin.core.Constants;
import clime.messadmin.filter.MessAdminRequestWrapper;
import clime.messadmin.filter.MessAdminResponseWrapper;
import clime.messadmin.utils.StringUtils;

/**
 * @author Cédrik LIME
 */
public class Application implements HttpSessionListener, HttpSessionActivationListener, IRequestListener {
	protected final ApplicationInfo applicationInfo;
	protected final Request cumulativeRequests = new Request(null);
	protected final Map activeSessions = new Hashtable(); // // must be synchronized
	protected final Map passiveSessions = new Hashtable(); // // must be synchronized
	/**
	 * Map of user-defined data to store in the Application scope.
	 * This is mainly used for plugins (key == plugin FQ name, for example)
	 */
	protected final Map userData = new Hashtable();

	/**
	 * @param servletContext
	 */
	public Application(ServletContext servletContext) {
		super();
		applicationInfo = new ApplicationInfo(servletContext);
	}

	/**
	 * @return Returns the applicationInfo.
	 */
	public IApplicationInfo getApplicationInfo() {
		return applicationInfo;
	}

	/**
	 * @return Returns the userData.
	 */
	public Map getUserData() {
		return userData;
	}

	/**
	 * @param sessionId
	 * @return active Session associated with sessionId
	 */
	public Session getSession(String sessionId) {
		return (Session) activeSessions.get(sessionId);
	}

	/**
	 * @return all known active Sessions for this application
	 */
	public Set/**/ getActiveSessions() {
		return new HashSet(activeSessions.values());
	}
	/**
	 * @return all known active SessionsInfo for this application
	 */
	public Set/**/ getActiveSessionInfos() {
		Set result = new HashSet(activeSessions.size());
		Iterator iter = getActiveSessions().iterator();
		while (iter.hasNext()) {
			Session session = (Session) iter.next();
			result.add(session.getSessionInfo());
		}
		return result;
	}
	/**
	 * @return all known active Sessions ids for this application
	 */
	public Set/**/ getActiveSessionsIds() {
		return new HashSet(activeSessions.keySet());
	}

	/**
	 * @return all known passive Sessions for this application
	 */
	public Set/**/ getPassiveSessions() {
		return new HashSet(passiveSessions.values());
	}
	/**
	 * @return all known passive Sessions ids for this application
	 */
	public Set/**/ getPassiveSessionsIds() {
		return new HashSet(passiveSessions.keySet());
	}


	protected void hit() {
		++applicationInfo.hits;
	}
	public void addUsedTime(int duration) {
		applicationInfo.cumulativeRequestStats.usedTime.registerValue(duration);
	}

	/**
	 * If message if blank or null, remove ServletContext attribute, otherwise inject message into ServletContext
	 * @param message
	 */
	public boolean injectPermanentMessage(String message) {
		boolean inject = StringUtils.isNotBlank(message);
		boolean actionDone = false;
		if (inject) {
			if (! message.equals(getApplicationInfo().getAttribute(Constants.GLOBAL_MESSAGE_KEY))) {
				// Setting message
				getApplicationInfo().setAttribute(Constants.GLOBAL_MESSAGE_KEY, message);
				actionDone = true;
			}
		} else {
			if (getApplicationInfo().getAttribute(Constants.GLOBAL_MESSAGE_KEY) != null) {
				// Removing message
				getApplicationInfo().removeAttribute(Constants.GLOBAL_MESSAGE_KEY);
				actionDone = true;
			}
		}
		return actionDone;
	}


	/*****************************************/
	/**	Request/Response Listener methods	**/
	/*****************************************/

	public void registerContextPath(final String contextPath) {
		if (applicationInfo.contextPath == null) {
			applicationInfo.contextPath = contextPath;
		}
	}

	/** {@inheritDoc} */
	public void requestInitialized(final HttpServletRequest request, final ServletContext servletContext) {
		if (request == null || servletContext == null) {
			return;
		}
		registerContextPath(request.getContextPath());
		final HttpSession httpSession = request.getSession(false);
		if (httpSession == null) {
			return;
		} // else
		try {
			Session session = getSession(httpSession.getId());
			if (session == null) {
				// Maybe this session comes from a serialized state. We need to register it.
				try {
					httpSession.getAttributeNames(); // this throws an IllegalStateException for an old session
					this.sessionCreated(new HttpSessionEvent(httpSession));
					session = getSession(httpSession.getId());
				} catch (IllegalStateException ise) {
					// session is invalidated: it's not new, it's old! So, don't create anything...
				}
			}
			if (session != null) {
				// we need this test in case session comes from a serialized state @ startup
				session.requestInitialized(request, servletContext);
			}
			cumulativeRequests.requestInitialized(applicationInfo.cumulativeRequestStats, request, servletContext);
		} catch (IllegalStateException ise) {
			// session is invalidated
		}
	}

	/** {@inheritDoc} */
	public void requestDestroyed(final MessAdminRequestWrapper request, final MessAdminResponseWrapper response, final ServletContext servletContext) {
		if (request == null || response == null || servletContext == null) {
			return;
		}
		hit();
		cumulativeRequests.requestDestroyed(applicationInfo.cumulativeRequestStats, request, response, servletContext);
		final HttpSession httpSession = request.getSession(false);
		if (httpSession != null) {
			try {
				final Session session = getSession(httpSession.getId());
				if (session != null) {
					session.requestDestroyed(request, response, servletContext);
				}
			} catch (IllegalStateException ise) {
				// session is invalidated
			}
		}
	}

	/** {@inheritDoc} */
	public void requestException(Exception e, MessAdminRequestWrapper request, MessAdminResponseWrapper response, ServletContext servletContext) {
		if (request == null || response == null || servletContext == null) {
			return;
		}
		cumulativeRequests.requestException(applicationInfo.cumulativeRequestStats, e, request, response, servletContext);
		final HttpSession httpSession = request.getSession(false);
		if (httpSession != null) {
			try {
				final Session session = getSession(httpSession.getId());
				if (session != null) {
					session.requestException(e, request, response, servletContext);
				}
			} catch (IllegalStateException ise) {
				// session is invalidated
			}
		}
	}

	/*********************************************/
	/**	HttpSession[Activation]Listener methods	**/
	/*********************************************/

	/**
	 * {@inheritDoc}
	 */
	public void sessionCreated(final HttpSessionEvent event) {
		HttpSession httpSession = event.getSession();
		Session session = new Session(httpSession);
		activeSessions.put(httpSession.getId(), session);
		++applicationInfo.totalCreatedSessions;
		applicationInfo.activeSessions.addValue(1);
	}

	/**
	 * {@inheritDoc}
	 */
	public void sessionDestroyed(final HttpSessionEvent event) {
		HttpSession httpSession = event.getSession();
		String sessionId = httpSession.getId();
		activeSessions.remove(sessionId);
		passiveSessions.remove(sessionId);
		applicationInfo.activeSessions.addValue(-1);
	}

	/**
	 * {@inheritDoc}
	 */
	public void sessionWillPassivate(final HttpSessionEvent se) {
		String sessionId = se.getSession().getId();
		// session is not active anymore: remove it from active list
		Session session = (Session) activeSessions.remove(sessionId);
		passiveSessions.put(sessionId, session);
		session.sessionWillPassivate(se);
		applicationInfo.activeSessions.addValue(-1);
		++applicationInfo.passiveSessionsCount;
	}

	/**
	 * {@inheritDoc}
	 */
	public void sessionDidActivate(final HttpSessionEvent se) {
		String sessionId = se.getSession().getId();
		Session session = (Session) passiveSessions.remove(sessionId);
		// refresh list of active sessions
		activeSessions.put(sessionId, session);
		session.sessionDidActivate(se);
		--applicationInfo.passiveSessionsCount;
		applicationInfo.activeSessions.addValue(1);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy