📄 httpsessionwrapper.java
字号:
package org.jahia.services.applications;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import org.jahia.utils.*;/** * This wrapper ensures the fact that Sessions values are not shared between applications * running under Jahia. This isn't the best solution because ideally we should reimplement * the SessionManager thread in order to make sure that Session objects are never shared. * For development time reasons, we have chose to just modify the keys of storage for * attribute names to make sure they are unique. * * @author Serge Huber * @version 1.0 */public class HttpSessionWrapper implements HttpSession { private String appName; private String contextID; private HttpSession originalSession; private final static String KEY_PREFIX = "org.jahia."; private final static String KEY_SEPARATOR = "."; private final static boolean debugOutput = false; private boolean inheritJahiaSessionAttributes = false; /** * This constructor sets the wrapping parameters internally. Nothing special done here. * @param origSession Original session object around which this wrapper wraps :) * @param appName Name of the application used to create unique keys. FIXME : This key is * not checked to see if it is globally unique */ public HttpSessionWrapper(HttpSession origSession, String applicationName, String contextID, boolean inheritJahiaSessionAttributes) { if (debugOutput) { JahiaConsole.println("SessionWrapper", "Creating session wrapper for application [" + applicationName + "], contextID [" + contextID + "] from original session object [" + origSession.toString() + "]"); } originalSession = origSession; appName = applicationName; this.contextID = contextID; this.inheritJahiaSessionAttributes = inheritJahiaSessionAttributes; if (originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID) == null) { originalSession.setAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID, new Hashtable()); } } public long getCreationTime() { return originalSession.getCreationTime(); } public String getId() { return originalSession.getId(); } public long getLastAccessedTime() { return originalSession.getLastAccessedTime(); } public void setMaxInactiveInterval(int interval) { originalSession.setMaxInactiveInterval(interval); } public int getMaxInactiveInterval() { return originalSession.getMaxInactiveInterval(); } /** * @deprecated */ public HttpSessionContext getSessionContext() { return originalSession.getSessionContext(); } public ServletContext getServletContext() { //return originalSession.getServletContext(); // the following is a way to test if the servlet API contains this // method. This is the only way to allow support for old versions // of the yet unfinalized servlet API try { Class sessionClass = originalSession.getClass(); java.lang.reflect.Method theMethod = sessionClass.getMethod("getServletContext", null); if (theMethod == null) { return null; } else { Object returnValue = theMethod.invoke(sessionClass, null); if (returnValue instanceof ServletContext) { return (ServletContext)returnValue; } else { return null; } } } catch (Throwable t) { return null; } } public Object getAttribute(String name) { if (debugOutput) { JahiaConsole.println("SessionWrapper", "emulatedSession.getAttribute("+name+") for app ["+KEY_PREFIX + appName + "]"); } Hashtable appAttributes = (Hashtable) originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID); if (debugOutput) { JahiaConsole.println("SessionWrapper", "...result=[" + appAttributes.get(name) + "]"); } // Try the application attributes, then the global attributes if the // flag has been activated. Object result = appAttributes.get(name); if (inheritJahiaSessionAttributes) { if (result == null) result = originalSession.getAttribute(name); } return result; } /** * @deprecated */ public Object getValue(String name) { return getAttribute(name); } public Enumeration getAttributeNames() { Hashtable appAttributes = (Hashtable) originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID); return appAttributes.keys(); } /** * @deprecated */ public String[] getValueNames() { Hashtable appAttributes = (Hashtable) originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID); return (String[]) appAttributes.keySet().toArray(); } public void setAttribute(String name, Object value) { // JahiaConsole.println("SessionWrapper", "emulatedSession.setAttribute(" + name + "," + value.toString() + ")"); Hashtable appAttributes = (Hashtable) originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID); appAttributes.put(name, value); } /** * @deprecated */ public void putValue(java.lang.String name, java.lang.Object value) { setAttribute(name, value); } public void removeAttribute(String name) { Hashtable appAttributes = (Hashtable) originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID); appAttributes.remove(name); } /** * @deprecated */ public void removeValue(String name) { removeAttribute(name); } public void invalidate() { originalSession.removeAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID); // originalSession.invalidate(); if (originalSession.getAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID) == null) { originalSession.setAttribute(KEY_PREFIX + appName + KEY_SEPARATOR + contextID, new Hashtable()); } } public boolean isNew() { return originalSession.isNew(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -