Modified the web server to allow connection related data to be stored in the connection's context by the application. This modifies the contract (interfaces) between the framework and application code, requiring changes to the application (breaks backward compatibility).

This commit is contained in:
wcrisman
2014-07-11 10:39:36 -07:00
parent bb9b1f550e
commit d2027e13f9
9 changed files with 83 additions and 16 deletions

View File

@@ -561,7 +561,7 @@ public class WebServer {
* Provides a place for connection oriented data.
* <p>Note that a client server session can have multiple socket contexts (one for each socket) and that the socket context may be used to access multiple applications hosted on this server.</p>
*/
private class SocketContext extends AbstractSocketContext implements IWebApplicationContainerProvider {
private class SocketContext extends AbstractSocketContext implements IWebApplicationContainerProvider, IConnectionContext {
public final int id;
/** The server socket reference that created the socket. This will be null if the socket was not created with a server socket (shouldn't happen in a web server). */
public ServerSocketContext serverSocketContext = null;
@@ -617,6 +617,8 @@ public class WebServer {
public StringBuffer debugBuffer = null; //debug ? new StringBuffer(1000) : null;
/** Used to pass all traffic (once unencrypted) through another socket to another process. */
private PassThroughSocketContext passThroughSocketContext = null;
/** The optional application specific data indexed by an application spectific key. Elements (values, not keys) which implement ISessionLifecycleAware will be released when the socket context is released. */
private LiteHashMap applicationDataMap;
public SocketContext(ServerSocketContext serverSocketContext) {
super();
@@ -625,6 +627,25 @@ public class WebServer {
}//synchronized//
}//SocketContext()//
protected synchronized void close() {
try {
if(applicationDataMap != null) {
for(IIterator iterator = applicationDataMap.valueIterator(); iterator.hasNext(); ) {
Object next = iterator.next();
//Ensure the code keeps going even if there is a problem cleaning up.//
try {
if(next instanceof ISessionLifecycleAware) ((ISessionLifecycleAware) next).release();
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
}//for//
}//if//
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
try {if(key != null && key.channel() != null) key.channel().close();} catch(Throwable e) {}
try {if(key != null) key.cancel();} catch(Throwable e) {}
//Clean up after the response and request.//
@@ -634,6 +655,22 @@ public class WebServer {
passThroughSocketContext.close();
}//if//
}//close()//
/* (non-Javadoc)
* @see com.foundation.web.interfaces.IConnectionContext#getApplicationData(java.lang.String)
*/
public Object getApplicationData(String key) {
return applicationDataMap == null ? null : applicationDataMap.get(key);
}//getApplicationData()//
/* (non-Javadoc)
* @see com.foundation.web.interfaces.IConnectionContext#setApplicationData(java.lang.String, java.lang.Object)
*/
public void setApplicationData(String key, Object applicationData) {
if(applicationDataMap == null) {
applicationDataMap = new LiteHashMap(10);
}//if//
applicationDataMap.put(key, applicationData);
}//setApplicationData()//
/* (non-Javadoc)
* @see com.foundation.web.server.WebServer.IWebApplicationContainerProvider#getWebApplicationContainer()
*/
@@ -3045,7 +3082,7 @@ private boolean internalProcessClientRequest(final SocketContext context, Select
}//if//
//Send the request to the application to be processed if we are not dealing with an error.//
else if(application != null) {
application.processRequest(request, response, session, allowSecureAccess, clientHadBadSession);
application.processRequest(request, response, session, allowSecureAccess, clientHadBadSession, context);
}//else if//
//Convert the response into a byte stream and send it via the socket.//