Files
Brainstorm/Foundation Web Application/src/com/foundation/web/SessionData.java

153 lines
5.4 KiB
Java
Raw Normal View History

2014-05-30 10:31:51 -07:00
package com.foundation.web;
import java.io.IOException;
import com.common.io.IExternalizable;
import com.common.io.IObjectInputStream;
import com.common.io.IObjectOutputStream;
import com.common.util.IIterator;
import com.common.util.IList;
import com.common.util.LiteHashMap;
import com.foundation.event.IRequestHandler;
/**
* Contains the basic information about a user web session.
*/
public class SessionData implements IExternalizable {
/** Whether the user is logged in. Actual login and user data must be stored in the secure session to ensure that all requests using it arrive via SSL. */
private boolean isLoggedIn = false;
/** The optional application specific data indexed by an application spectific key. Elements (values, not keys) which implement ISessionLifecycleAware will be released when the session is released. */
private LiteHashMap applicationDataMap;
/** The internal web session. */
private WebSession session;
/**
* SessionData constructor.
*/
public SessionData() {
}//SessionData()//
/**
* Called after the session data has been setup to allow for application code to initialize.
*/
public void initialize() {
}//initialize()//
/**
* Called after the session has been terminated by the web server or when a web application is reloading or being removed, to allow the application code to cleanup.
*/
public void release() {
if(applicationDataMap != null) {
for(IIterator iterator = applicationDataMap.valueIterator(); iterator.hasNext(); ) {
Object next = iterator.next();
if(next instanceof ISessionLifecycleAware) ((ISessionLifecycleAware) next).release();
}//for//
}//if//
}//release()//
/**
* Initializes the session data with the web session reference.
* @param session The private internal session.
*/
void initialize(WebSession session) {
this.session = session;
initialize();
}//initialize()//
/**
* Gets the request handler which threads all requests for the session.
* @return The handler used to ensure all requests that access shared resources (reflections) are single threaded, and is used to create a reflection context for each view.
*/
public IRequestHandler getRequestHandler() {
return session.getRequestHandler();
}//getRequestHandler()//
/**
* Determines whether the user is currently logged in.
* @return Whether the user is logged in according to the application.
*/
public boolean isLoggedIn() {
return isLoggedIn;
}//isLoggedIn()//
/**
* Determines whether the user is currently logged in.
* @return Whether the user is logged in according to the application.
*/
public void isLoggedIn(boolean isLoggedIn) {
this.isLoggedIn = isLoggedIn;
}//isLoggedIn()//
/**
* Flags the session data as having changed in a significant way, requiring a repository update.
*/
public void markChanged() {
session.markChanged();
}//markChanged()//
/**
* Gets the application data.
* @return The application specific data element.
*/
public Object getApplicationData(String key) {
return applicationDataMap == null ? null : applicationDataMap.get(key);
}//getApplicationData()//
/**
* Stores the application data in the session's application data map by the given key.
* The application data may implement ISessionLifecycleAware if it should be called when the session is being released. This will only be called for a normal closing of the session. The session may still be restored at some time in the future.
* The application data may implement IExternalizable if it wishes to persist when the session is serialized.
* @param key The key for the data. If the key is logically equal (equivalent) to an existing key, then the existing data will be replaced and returned.
* @param applicationData The application specific data element.
*/
public void setApplicationData(String key, Object applicationData) {
if(applicationDataMap == null) {
applicationDataMap = new LiteHashMap(10);
}//if//
applicationDataMap.put(key, applicationData);
}//setApplicationData()//
/* (non-Javadoc)
* @see com.common.io.IExternalizable#readExternal(com.common.io.IObjectInputStream)
*/
public Object readExternal(IObjectInputStream in) throws IOException, ClassNotFoundException {
int valueCount = 0;
isLoggedIn = in.readBoolean();
valueCount = in.readInt();
if(valueCount > 0) {
applicationDataMap = new LiteHashMap(valueCount > 7 ? (int) Math.ceil(valueCount * 1.2f) : 10);
for(int index = 0; index < valueCount; index++) {
String key = (String) in.readObject();
Object value = in.readObject();
applicationDataMap.put(key, value);
}//for//
}//if//
return null;
}//readExternal()//
/* (non-Javadoc)
* @see com.common.io.IExternalizable#writeExternal(com.common.io.IObjectOutputStream)
*/
public void writeExternal(IObjectOutputStream out) throws IOException {
String[] keys = applicationDataMap != null ? new String[applicationDataMap.getSize()] : null;
int valueCount = 0;
out.writeBoolean(isLoggedIn);
if(keys != null) {
applicationDataMap.getKeys(keys);
for(int index = 0; index < keys.length; index++) {
if(applicationDataMap.get(keys[index]) instanceof IExternalizable) valueCount++;
}//for//
}//if//
out.writeInt(valueCount);
if(keys != null) {
for(int index = 0; index < keys.length; index++) {
Object next = applicationDataMap.get(keys[index]);
if(next instanceof IExternalizable) {
out.writeObject(keys[index]);
out.writeObject(next);
}//if//
}//for//
}//if//
}//writeExternal()//
}//SessionData//