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.tomcat55.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.connector.Request;
011: import org.apache.catalina.connector.Response;
012: import org.apache.catalina.connector.SessionRequest55;
013: import org.apache.catalina.connector.SessionResponse55;
014: import org.apache.catalina.connector.Tomcat55CookieWriter;
015: import org.apache.catalina.valves.ValveBase;
016:
017: import com.tc.object.bytecode.ManagerUtil;
018: import com.terracotta.session.BaseRequestResponseFactory;
019: import com.terracotta.session.SessionManager;
020: import com.terracotta.session.SessionResponse;
021: import com.terracotta.session.TerracottaRequest;
022: import com.terracotta.session.TerracottaSessionManager;
023: import com.terracotta.session.WebAppConfig;
024: import com.terracotta.session.util.Assert;
025: import com.terracotta.session.util.ConfigProperties;
026: import com.terracotta.session.util.ContextMgr;
027: import com.terracotta.session.util.DefaultContextMgr;
028: import com.terracotta.session.util.DefaultIdGenerator;
029: import com.terracotta.session.util.DefaultLifecycleEventMgr;
030: import com.terracotta.session.util.DefaultWebAppConfig;
031: import com.terracotta.session.util.LifecycleEventMgr;
032: import com.terracotta.session.util.SessionCookieWriter;
033: import com.terracotta.session.util.SessionIdGenerator;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037: import java.util.HashMap;
038: import java.util.Map;
039:
040: import javax.servlet.ServletException;
041: import javax.servlet.http.HttpSessionAttributeListener;
042: import javax.servlet.http.HttpSessionListener;
043:
044: public class SessionValve55 extends ValveBase {
045:
046: private final Map mgrs = new HashMap();
047:
048: public void invoke(Request valveReq, Response valveRes)
049: throws IOException, ServletException {
050: if (valveReq.getContext() != null
051: && TerracottaSessionManager.isDsoSessionApp(valveReq)) {
052: tcInvoke(valveReq, valveRes);
053: } else {
054: if (getNext() != null)
055: getNext().invoke(valveReq, valveRes);
056: }
057: }
058:
059: private void tcInvoke(Request valveReq, Response valveRes)
060: throws IOException, ServletException {
061: SessionManager mgr = findOrCreateManager(valveReq, valveReq
062: .getContextPath());
063: TerracottaRequest sReq = mgr.preprocess(valveReq, valveRes);
064: SessionRequest55 sReq55 = new SessionRequest55(sReq, valveReq,
065: valveReq.getContext().getRealm());
066: SessionResponse sRes = new SessionResponse(sReq, valveRes);
067: SessionResponse55 sRes55 = new SessionResponse55(valveRes,
068: sReq55, sRes);
069: try {
070: if (getNext() != null)
071: getNext().invoke(sReq55, sRes55);
072: } finally {
073: mgr.postprocess(sReq);
074: }
075: }
076:
077: private SessionManager findOrCreateManager(Request valveReq,
078: String contextPath) {
079: SessionManager rv = null;
080: synchronized (mgrs) {
081: rv = (SessionManager) mgrs.get(contextPath);
082: if (rv == null) {
083: rv = createManager(valveReq, contextPath);
084: mgrs.put(contextPath, rv);
085: }
086: }
087: return rv;
088: }
089:
090: private static SessionManager createManager(Request valveReq,
091: String contextPath) {
092: final ClassLoader loader = valveReq.getContext().getLoader()
093: .getClassLoader();
094: final ConfigProperties cp = new ConfigProperties(
095: makeWebAppConfig(valveReq.getContext()), loader);
096:
097: String appName = DefaultContextMgr.computeAppName(valveReq);
098: int lockType = ManagerUtil.getSessionLockType(appName);
099: final SessionIdGenerator sig = DefaultIdGenerator.makeInstance(
100: cp, lockType);
101:
102: final SessionCookieWriter scw = new Tomcat55CookieWriter(cp
103: .getSessionTrackingEnabled(), cp.getCookiesEnabled(),
104: cp.getUrlRewritingEnabled(), cp.getCookieName(), cp
105: .getCookieDomain(), cp.getCookiePath(), cp
106: .getCookieCoomment(), cp
107: .getCookieMaxAgeSeconds(), cp.getCookieSecure());
108:
109: final LifecycleEventMgr eventMgr = DefaultLifecycleEventMgr
110: .makeInstance(cp);
111: final ContextMgr contextMgr = DefaultContextMgr.makeInstance(
112: contextPath, valveReq.getContext().getServletContext());
113: final SessionManager rv = new TerracottaSessionManager(sig,
114: scw, eventMgr, contextMgr,
115: new BaseRequestResponseFactory(), cp);
116: return rv;
117: }
118:
119: private static WebAppConfig makeWebAppConfig(Context context) {
120: Assert.pre(context != null);
121:
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:
137: for (Container c = context; c != null; c = c.getParent()) {
138: if (c instanceof Engine) {
139: jvmRoute = ((Engine) c).getJvmRoute();
140: break;
141: }
142: }
143:
144: return new DefaultWebAppConfig(context.getManager()
145: .getMaxInactiveInterval(), attrList, sessList, ".",
146: jvmRoute);
147: }
148:
149: private static void sortByType(Object[] listeners,
150: ArrayList sessionListeners, ArrayList attributeListeners) {
151: if (listeners == null)
152: return;
153: for (int i = 0; i < listeners.length; i++) {
154: Object o = listeners[i];
155: if (o instanceof HttpSessionListener)
156: sessionListeners.add(o);
157: if (o instanceof HttpSessionAttributeListener)
158: attributeListeners.add(o);
159: }
160: }
161:
162: public String getClassName() {
163: return getClass().getName();
164: }
165: }
|