📄 abstractsessionmanager.java
字号:
// ========================================================================// Copyright 199-2004 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// ========================================================================package org.mortbay.jetty.servlet;import java.io.Serializable;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import java.util.EventListener;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionActivationListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;import javax.servlet.http.HttpSessionContext;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import org.mortbay.component.AbstractLifeCycle;import org.mortbay.jetty.HttpOnlyCookie;import org.mortbay.jetty.Server;import org.mortbay.jetty.SessionIdManager;import org.mortbay.jetty.SessionManager;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.util.LazyList;/* ------------------------------------------------------------ *//** * An Abstract implementation of SessionManager. The partial implementation of * SessionManager interface provides the majority of the handling required to * implement a SessionManager. Concrete implementations of SessionManager based * on AbstractSessionManager need only implement the newSession method to return * a specialized version of the Session inner class that provides an attribute * Map. * <p> * If the property * org.mortbay.jetty.servlet.AbstractSessionManager.23Notifications is set to * true, the 2.3 servlet spec notification style will be used. * <p> * * @author Greg Wilkins (gregw) */public abstract class AbstractSessionManager extends AbstractLifeCycle implements SessionManager{ /* ------------------------------------------------------------ */ public final static int __distantFuture=60*60*24*7*52*20; private static final HttpSessionContext __nullSessionContext=new NullSessionContext(); private boolean _usingCookies=true; /* ------------------------------------------------------------ */ // Setting of max inactive interval for new sessions // -1 means no timeout protected int _dftMaxIdleSecs=-1; protected SessionHandler _sessionHandler; protected boolean _httpOnly=false; protected int _maxSessions=0; protected int _minSessions=0; protected SessionIdManager _sessionIdManager; protected boolean _secureCookies=false; protected Object _sessionAttributeListeners; protected Object _sessionListeners; protected ClassLoader _loader; protected ContextHandler.SContext _context; protected String _sessionCookie=__DefaultSessionCookie; protected String _sessionURL=__DefaultSessionURL; protected String _sessionURLPrefix=";"+_sessionURL+"="; protected String _sessionDomain; protected String _sessionPath; protected int _maxCookieAge=-1; protected int _refreshCookieAge; protected boolean _nodeIdInSessionId; /* ------------------------------------------------------------ */ public AbstractSessionManager() { } /* ------------------------------------------------------------ */ public Cookie access(HttpSession session,boolean secure) { long now=System.currentTimeMillis(); Session s = ((SessionIf)session).getSession(); s.access(now); // Do we need to refresh the cookie? if (isUsingCookies() && (s.isIdChanged() || (getMaxCookieAge()>0 && getRefreshCookieAge()>0 && ((now-s.getCookieSetTime())/1000>getRefreshCookieAge())) ) ) { Cookie cookie=getSessionCookie(session,_context.getContextPath(),secure); s.cookieSet(); s.setIdChanged(false); return cookie; } return null; } /* ------------------------------------------------------------ */ public void addEventListener(EventListener listener) { if (listener instanceof HttpSessionAttributeListener) _sessionAttributeListeners=LazyList.add(_sessionAttributeListeners,listener); if (listener instanceof HttpSessionListener) _sessionListeners=LazyList.add(_sessionListeners,listener); } /* ------------------------------------------------------------ */ public void clearEventListeners() { _sessionAttributeListeners=null; _sessionListeners=null; } /* ------------------------------------------------------------ */ public void complete(HttpSession session) { Session s = ((SessionIf)session).getSession(); s.complete(); } /* ------------------------------------------------------------ */ public void doStart() throws Exception { _context=ContextHandler.getCurrentContext(); _loader=Thread.currentThread().getContextClassLoader(); if (_sessionIdManager==null) { Server server=getSessionHandler().getServer(); synchronized (server) { _sessionIdManager=server.getSessionIdManager(); if (_sessionIdManager==null) { _sessionIdManager=new HashSessionIdManager(); server.setSessionIdManager(_sessionIdManager); } } } if (!_sessionIdManager.isStarted()) _sessionIdManager.start(); // Look for a session cookie name String tmp=_context.getInitParameter(SessionManager.__SessionCookieProperty); if (tmp!=null) _sessionCookie=tmp; tmp=_context.getInitParameter(SessionManager.__SessionURLProperty); if (tmp!=null) { _sessionURL=(tmp==null||"none".equals(tmp))?null:tmp; _sessionURLPrefix=(tmp==null||"none".equals(tmp))?null:(";"+_sessionURL+"="); } // set up the max session cookie age if it isn't already if (_maxCookieAge==-1) { if (_context!=null) { String str=_context.getInitParameter(SessionManager.__MaxAgeProperty); if (str!=null) _maxCookieAge=Integer.parseInt(str.trim()); } } // set up the session domain if it isn't already if (_sessionDomain==null) { // only try the context initParams if (_context!=null) _sessionDomain=_context.getInitParameter(SessionManager.__SessionDomainProperty); } // set up the sessionPath if it isn't already if (_sessionPath==null) { // only the context initParams if (_context!=null) _sessionPath=_context.getInitParameter(SessionManager.__SessionPathProperty); } super.doStart(); } /* ------------------------------------------------------------ */ public void doStop() throws Exception { super.doStop(); invalidateSessions(); _loader=null; } /* ------------------------------------------------------------ */ /** * @return Returns the httpOnly. */ public boolean getHttpOnly() { return _httpOnly; } /* ------------------------------------------------------------ */ public HttpSession getHttpSession(String nodeId) { String cluster_id = getIdManager().getClusterId(nodeId); synchronized (this) { Session session = getSession(cluster_id); if (session!=null && !session.getNodeId().equals(nodeId)) session.setIdChanged(true); return session; } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * @return Returns the metaManager used for cross context session management */ public SessionIdManager getIdManager() { return _sessionIdManager; } /* ------------------------------------------------------------ */ public int getMaxCookieAge() { return _maxCookieAge; } /* ------------------------------------------------------------ */ /** * @return seconds */ public int getMaxInactiveInterval() { return _dftMaxIdleSecs; } /* ------------------------------------------------------------ */ public int getMaxSessions() { return _maxSessions; } /* ------------------------------------------------------------ */ /** * @deprecated use {@link #getIdManager()} */ public SessionIdManager getMetaManager() { return getIdManager(); } /* ------------------------------------------------------------ */ public int getMinSessions() { return _minSessions; } /* ------------------------------------------------------------ */ public int getRefreshCookieAge() { return _refreshCookieAge; } /* ------------------------------------------------------------ */ /** * @return Returns the secureCookies. */ public boolean getSecureCookies() { return _secureCookies; } /* ------------------------------------------------------------ */ public String getSessionCookie() { return _sessionCookie; } /* ------------------------------------------------------------ */ public Cookie getSessionCookie(HttpSession session, String contextPath, boolean requestIsSecure) { if (isUsingCookies()) { String id = getNodeId(session); Cookie cookie=getHttpOnly()?new HttpOnlyCookie(_sessionCookie,id):new Cookie(_sessionCookie,id); cookie.setPath((contextPath==null||contextPath.length()==0)?"/":contextPath); cookie.setMaxAge(getMaxCookieAge()); cookie.setSecure(requestIsSecure&&getSecureCookies()); // set up the overrides if (_sessionDomain!=null) cookie.setDomain(_sessionDomain); if (_sessionPath!=null) cookie.setPath(_sessionPath); return cookie; } return null; } public String getSessionDomain() { return _sessionDomain; } /* ------------------------------------------------------------ */ /** * @return Returns the sessionHandler. */ public SessionHandler getSessionHandler() { return _sessionHandler; } /* ------------------------------------------------------------ */ /** * @deprecated. Need to review if it is needed. */ public abstract Map getSessionMap(); /* ------------------------------------------------------------ */ public String getSessionPath() { return _sessionPath; } /* ------------------------------------------------------------ */ public abstract int getSessions(); /* ------------------------------------------------------------ */ public String getSessionURL() { return _sessionURL; } /* ------------------------------------------------------------ */ public String getSessionURLPrefix() { return _sessionURLPrefix; } /* ------------------------------------------------------------ */ /** * @return Returns the usingCookies. */ public boolean isUsingCookies() { return _usingCookies; } /* ------------------------------------------------------------ */ public boolean isValid(HttpSession session) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -