001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.tomcat50.session;
006:
007: import org.apache.catalina.Container;
008: import org.apache.catalina.Context;
009: import org.apache.catalina.Engine;
010: import org.apache.catalina.Request;
011: import org.apache.catalina.Response;
012: import org.apache.catalina.ValveContext;
013: import org.apache.catalina.valves.ValveBase;
014: import org.apache.coyote.tomcat5.CoyoteRequest;
015: import org.apache.coyote.tomcat5.CoyoteResponse;
016:
017: import com.tc.object.bytecode.ManagerUtil;
018: import com.terracotta.session.SessionManager;
019: import com.terracotta.session.TerracottaSessionManager;
020: import com.terracotta.session.WebAppConfig;
021: import com.terracotta.session.util.Assert;
022: import com.terracotta.session.util.ConfigProperties;
023: import com.terracotta.session.util.ContextMgr;
024: import com.terracotta.session.util.DefaultContextMgr;
025: import com.terracotta.session.util.DefaultCookieWriter;
026: import com.terracotta.session.util.DefaultIdGenerator;
027: import com.terracotta.session.util.DefaultLifecycleEventMgr;
028: import com.terracotta.session.util.DefaultWebAppConfig;
029: import com.terracotta.session.util.LifecycleEventMgr;
030: import com.terracotta.session.util.SessionCookieWriter;
031: import com.terracotta.session.util.SessionIdGenerator;
032:
033: import java.io.IOException;
034: import java.util.ArrayList;
035: import java.util.HashMap;
036: import java.util.Map;
037:
038: import javax.servlet.ServletException;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpSessionAttributeListener;
041: import javax.servlet.http.HttpSessionListener;
042:
043: public class SessionValve50 extends ValveBase {
044:
045: private final Map mgrs = new HashMap();
046:
047: public void invoke(final Request valveReq, final Response valveRes,
048: final ValveContext valveContext) throws IOException,
049: ServletException {
050: if (valveReq.getContext() != null
051: && valveReq instanceof CoyoteRequest
052: && valveRes instanceof CoyoteResponse
053: && TerracottaSessionManager
054: .isDsoSessionApp((HttpServletRequest) valveReq)) {
055: tcInvoke((CoyoteRequest) valveReq,
056: (CoyoteResponse) valveRes, valveContext);
057: } else {
058: valveContext.invokeNext(valveReq, valveRes);
059: }
060: }
061:
062: private void tcInvoke(final CoyoteRequest valveReq,
063: final CoyoteResponse valveRes,
064: final ValveContext valveContext) throws IOException,
065: ServletException {
066: SessionManager mgr = findOrCreateManager(valveReq, valveReq
067: .getContextPath());
068: SessionRequest50 sReq50 = (SessionRequest50) mgr.preprocess(
069: valveReq, valveRes);
070: SessionResponse50 sRes50 = new SessionResponse50(valveRes,
071: sReq50);
072: sReq50.setSessionResposne50(sRes50);
073: try {
074: valveContext.invokeNext(sReq50, sRes50);
075: } finally {
076: mgr.postprocess(sReq50);
077: }
078: }
079:
080: private SessionManager findOrCreateManager(CoyoteRequest valveReq,
081: String contextPath) {
082: SessionManager rv = null;
083: synchronized (mgrs) {
084: rv = (SessionManager) mgrs.get(contextPath);
085: if (rv == null) {
086: rv = createManager(valveReq, contextPath);
087: mgrs.put(contextPath, rv);
088: }
089: }
090: return rv;
091: }
092:
093: private static SessionManager createManager(CoyoteRequest valveReq,
094: String contextPath) {
095: final WebAppConfig webAppConfig = makeWebAppConfig(valveReq
096: .getContext());
097: final ClassLoader loader = valveReq.getContext().getLoader()
098: .getClassLoader();
099: final ConfigProperties cp = new ConfigProperties(webAppConfig,
100: loader);
101:
102: String appName = DefaultContextMgr.computeAppName(valveReq);
103: int lockType = ManagerUtil.getSessionLockType(appName);
104: final SessionIdGenerator sig = DefaultIdGenerator.makeInstance(
105: cp, lockType);
106:
107: final SessionCookieWriter scw = DefaultCookieWriter
108: .makeInstance(cp);
109: final LifecycleEventMgr eventMgr = DefaultLifecycleEventMgr
110: .makeInstance(cp);
111: final ContextMgr contextMgr = DefaultContextMgr.makeInstance(
112: contextPath, valveReq.getContext().getServletContext());
113:
114: final SessionManager rv = new TerracottaSessionManager(sig,
115: scw, eventMgr, contextMgr,
116: new Tomcat50RequestResponseFactory(), cp);
117: return rv;
118: }
119:
120: private static WebAppConfig makeWebAppConfig(Context context) {
121: Assert.pre(context != null);
122: final ArrayList sessionListeners = new ArrayList();
123: final ArrayList attributeListeners = new ArrayList();
124: sortByType(context.getApplicationEventListeners(),
125: sessionListeners, attributeListeners);
126: sortByType(context.getApplicationLifecycleListeners(),
127: sessionListeners, attributeListeners);
128: HttpSessionAttributeListener[] attrList = (HttpSessionAttributeListener[]) attributeListeners
129: .toArray(new HttpSessionAttributeListener[attributeListeners
130: .size()]);
131: HttpSessionListener[] sessList = (HttpSessionListener[]) sessionListeners
132: .toArray(new HttpSessionListener[sessionListeners
133: .size()]);
134:
135: String jvmRoute = null;
136: for (Container c = context; c != null; c = c.getParent()) {
137: if (c instanceof Engine) {
138: jvmRoute = ((Engine) c).getJvmRoute();
139: break;
140: }
141: }
142:
143: return new DefaultWebAppConfig(context.getManager()
144: .getMaxInactiveInterval(), attrList, sessList, ".",
145: jvmRoute);
146: }
147:
148: private static void sortByType(Object[] listeners,
149: ArrayList sessionListeners, ArrayList attributeListeners) {
150: if (listeners == null)
151: return;
152: for (int i = 0; i < listeners.length; i++) {
153: final Object o = listeners[i];
154: if (o instanceof HttpSessionListener)
155: sessionListeners.add(o);
156: if (o instanceof HttpSessionAttributeListener)
157: attributeListeners.add(o);
158: }
159: }
160:
161: public String getClassName() {
162: return getClass().getName();
163: }
164: }
|