01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.terracotta.session.util;
05:
06: import javax.servlet.ServletContext;
07: import javax.servlet.http.HttpServletRequest;
08: import javax.servlet.http.HttpSessionContext;
09:
10: public class DefaultContextMgr implements ContextMgr {
11:
12: private final ServletContext servletContext;
13: private final String appName;
14:
15: public static DefaultContextMgr makeInstance(
16: HttpServletRequest req, ServletContext servletContext) {
17: return new DefaultContextMgr(computeAppName(req),
18: servletContext);
19: }
20:
21: public static DefaultContextMgr makeInstance(String contextPath,
22: ServletContext servletContext) {
23: return new DefaultContextMgr(computeAppName(contextPath),
24: servletContext);
25: }
26:
27: protected DefaultContextMgr(String appName,
28: ServletContext servletContext) {
29: Assert.pre(appName != null);
30: this .servletContext = servletContext;
31: this .appName = appName;
32: }
33:
34: public ServletContext getServletContext() {
35: return servletContext;
36: }
37:
38: public HttpSessionContext getSessionContext() {
39: return DefaultSessionContext.theInstance;
40: }
41:
42: public String getAppName() {
43: Assert.post(appName != null);
44: return appName;
45: }
46:
47: public static String computeAppName(HttpServletRequest request) {
48: Assert.pre(request != null);
49: String app = request.getContextPath();
50: return computeAppName(app);
51: }
52:
53: private static String computeAppName(String app) {
54: // compute app name
55: // deal with possible app strings: null, "", "/", "/xyz", "xyz/", "/xyz/"
56: if (app == null)
57: app = "";
58: else
59: app = app.trim();
60: if (app.length() == 0 || "/".equals(app))
61: return "ROOT";
62: if (app.startsWith("/"))
63: app = app.substring(1);
64: if (app.endsWith("/"))
65: app = app.substring(0, app.length() - 2);
66: Assert.post(app != null);
67: return app;
68: }
69: }
|