1 4 package com.terracotta.session.util; 5 6 import javax.servlet.ServletContext ; 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpSessionContext ; 9 10 public class DefaultContextMgr implements ContextMgr { 11 12 private final ServletContext servletContext; 13 private final String appName; 14 15 public static DefaultContextMgr makeInstance(HttpServletRequest req, ServletContext servletContext) { 16 return new DefaultContextMgr(computeAppName(req), servletContext); 17 } 18 19 public static DefaultContextMgr makeInstance(String contextPath, ServletContext servletContext) { 20 return new DefaultContextMgr(computeAppName(contextPath), servletContext); 21 } 22 23 protected DefaultContextMgr(String appName, ServletContext servletContext) { 24 Assert.pre(appName != null); 25 this.servletContext = servletContext; 26 this.appName = appName; 27 } 28 29 public ServletContext getServletContext() { 30 return servletContext; 31 } 32 33 public HttpSessionContext getSessionContext() { 34 return DefaultSessionContext.theInstance; 35 } 36 37 public String getAppName() { 38 Assert.post(appName != null); 39 return appName; 40 } 41 42 public static String computeAppName(HttpServletRequest request) { 43 Assert.pre(request != null); 44 String app = request.getContextPath(); 45 return computeAppName(app); 46 } 47 48 private static String computeAppName(String app) { 49 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 |