KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > SessionFilter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session;
6
7 import com.tc.object.bytecode.ManagerUtil;
8 import com.terracotta.session.util.Assert;
9 import com.terracotta.session.util.ConfigProperties;
10 import com.terracotta.session.util.ContextMgr;
11 import com.terracotta.session.util.DefaultContextMgr;
12 import com.terracotta.session.util.DefaultCookieWriter;
13 import com.terracotta.session.util.DefaultIdGenerator;
14 import com.terracotta.session.util.DefaultLifecycleEventMgr;
15 import com.terracotta.session.util.LifecycleEventMgr;
16 import com.terracotta.session.util.SessionCookieWriter;
17 import com.terracotta.session.util.SessionIdGenerator;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.Filter JavaDoc;
22 import javax.servlet.FilterChain JavaDoc;
23 import javax.servlet.FilterConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 public class SessionFilter implements Filter JavaDoc {
32
33   private TerracottaSessionManager mgr = null;
34   private ServletContext JavaDoc servletContext = null;
35   public static final String JavaDoc FILTER_CLASS = SessionFilter.class.getName();
36   public static final String JavaDoc FILTER_NAME = "Terracotta Session Filter";
37
38   public SessionFilter() {
39     // nothing
40
}
41
42   public void init(FilterConfig JavaDoc config) {
43     servletContext = config.getServletContext();
44   }
45
46   public void destroy() {
47     // not used
48
}
49
50   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc,
51       ServletException JavaDoc {
52     if (request instanceof HttpServletRequest JavaDoc && response instanceof HttpServletResponse JavaDoc) {
53       doHttpFilter((HttpServletRequest JavaDoc) request, (HttpServletResponse JavaDoc) response, chain);
54     } else {
55       chain.doFilter(request, response);
56     }
57   }
58
59   private void doHttpFilter(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc chain)
60       throws IOException JavaDoc, ServletException JavaDoc {
61     Assert.pre(request != null);
62     Assert.pre(response != null);
63     Assert.pre(chain != null);
64
65     TerracottaSessionManager tcMgr = getManager(request);
66     TerracottaRequest sReq = tcMgr.preprocess(request, response);
67     TerracottaResponse sRes = tcMgr.createResponse(sReq, response);
68     try {
69       chain.doFilter(sReq, sRes);
70     } finally {
71       tcMgr.postprocess(sReq);
72     }
73   }
74
75   protected synchronized TerracottaSessionManager getManager(HttpServletRequest JavaDoc req) {
76     if (mgr == null) {
77       if (servletContext instanceof WebAppConfig) mgr = createWebAppConfigManager(req, (WebAppConfig) servletContext,
78                                                                                   servletContext);
79       else mgr = createDefaultManager(req, servletContext);
80     }
81     Assert.post(mgr != null);
82     return mgr;
83   }
84
85   protected static TerracottaSessionManager createDefaultManager(final HttpServletRequest JavaDoc req,
86                                                                  final ServletContext JavaDoc servletContext) {
87     final TerracottaSessionManager rv = createManager(req, null, new BaseRequestResponseFactory(), servletContext);
88     return rv;
89   }
90
91   protected static TerracottaSessionManager createWebAppConfigManager(final HttpServletRequest JavaDoc req,
92                                                                       final WebAppConfig wac,
93                                                                       final ServletContext JavaDoc servletContext) {
94     final TerracottaSessionManager rv = createManager(req, wac, pickFactory(), servletContext);
95     return rv;
96   }
97
98   protected static TerracottaSessionManager createManager(final HttpServletRequest JavaDoc req, final WebAppConfig wac,
99                                                           final RequestResponseFactory factory,
100                                                           final ServletContext JavaDoc servletContext) {
101     final ConfigProperties cp = new ConfigProperties(wac);
102
103     String JavaDoc appName = DefaultContextMgr.computeAppName(req);
104     int lockType = ManagerUtil.getSessionLockType(appName);
105     final SessionIdGenerator sig = DefaultIdGenerator.makeInstance(cp, lockType);
106
107     final SessionCookieWriter scw = DefaultCookieWriter.makeInstance(cp);
108     final LifecycleEventMgr eventMgr = DefaultLifecycleEventMgr.makeInstance(cp);
109     final ContextMgr contextMgr = DefaultContextMgr.makeInstance(req, servletContext);
110     final TerracottaSessionManager rv = new TerracottaSessionManager(sig, scw, eventMgr, contextMgr, factory, cp);
111     return rv;
112   }
113
114   private static RequestResponseFactory pickFactory() {
115     // XXX: this is NOT a good way to implement the factory picking, but it will suffice for Lawton
116
// XXX: It should probably be passed in through init(FilterConfig)
117

118     try {
119       ClassLoader.getSystemClassLoader().loadClass("weblogic.Server");
120       return (RequestResponseFactory) Class.forName("com.terracotta.session.WeblogicRequestResponseFactory")
121           .newInstance();
122     } catch (Exception JavaDoc e) {
123       return new BaseRequestResponseFactory();
124     }
125   }
126
127 }
128
Popular Tags