KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > DefaultContextMgr


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

4 package com.terracotta.session.util;
5
6 import javax.servlet.ServletContext JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpSessionContext JavaDoc;
9
10 public class DefaultContextMgr implements ContextMgr {
11
12   private final ServletContext JavaDoc servletContext;
13   private final String JavaDoc appName;
14
15   public static DefaultContextMgr makeInstance(HttpServletRequest JavaDoc req, ServletContext JavaDoc servletContext) {
16     return new DefaultContextMgr(computeAppName(req), servletContext);
17   }
18   
19   public static DefaultContextMgr makeInstance(String JavaDoc contextPath, ServletContext JavaDoc servletContext) {
20     return new DefaultContextMgr(computeAppName(contextPath), servletContext);
21   }
22   
23   protected DefaultContextMgr(String JavaDoc appName, ServletContext JavaDoc servletContext) {
24     Assert.pre(appName != null);
25     this.servletContext = servletContext;
26     this.appName = appName;
27   }
28
29   public ServletContext JavaDoc getServletContext() {
30     return servletContext;
31   }
32
33   public HttpSessionContext JavaDoc getSessionContext() {
34     return DefaultSessionContext.theInstance;
35   }
36
37   public String JavaDoc getAppName() {
38     Assert.post(appName != null);
39     return appName;
40   }
41
42   public static String JavaDoc computeAppName(HttpServletRequest JavaDoc request) {
43     Assert.pre(request != null);
44     String JavaDoc app = request.getContextPath();
45     return computeAppName(app);
46   }
47
48   private static String JavaDoc computeAppName(String JavaDoc app) {
49     // compute app name
50
// deal with possible app strings: null, "", "/", "/xyz", "xyz/", "/xyz/"
51
if (app == null) app = "";
52     else app = app.trim();
53     if (app.length() == 0 || "/".equals(app)) return "ROOT";
54     if (app.startsWith("/")) app = app.substring(1);
55     if (app.endsWith("/")) app = app.substring(0, app.length() - 2);
56     Assert.post(app != null);
57     return app;
58   }
59 }
60
Popular Tags