001: //========================================================================
002: //$Id: StateInterceptor.java,v 1.4 2004/05/09 20:30:47 gregwilkins Exp $
003: //Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.j2ee.session;
017:
018: //----------------------------------------
019:
020: import java.rmi.RemoteException;
021: import java.util.Enumeration;
022: import java.util.Map;
023:
024: import javax.servlet.http.HttpSession;
025:
026: //----------------------------------------
027: /**
028: * Superlass for StateInterceptors - objects which
029: * wrap-n-delegate/decorate a State instance. A stack of
030: * StateInterceptors form a StateContainer.
031: *
032: * @author <a href="mailto:jules@mortbay.com">Jules Gosnell</a>
033: * @version 1.0
034: */
035: public class StateInterceptor implements State, Cloneable {
036: // protected final ThreadLocal _state =new ThreadLocal();
037: // protected State getState () {return (State)_state.get();}
038: // protected void setState(State state) {_state.set(state);}
039: //
040: private final static ThreadLocal _manager = new ThreadLocal();
041:
042: protected Manager getManager() {
043: return (Manager) _manager.get();
044: }
045:
046: protected void setManager(Manager manager) {
047: _manager.set(manager);
048: }
049:
050: private final static ThreadLocal _session = new ThreadLocal();
051:
052: protected HttpSession getSession() {
053: return (HttpSession) _session.get();
054: }
055:
056: protected void setSession(HttpSession session) {
057: _session.set(session);
058: }
059:
060: // management of this attribute needs to move into the container...
061: private State _state;
062:
063: protected State getState() {
064: return _state;
065: }
066:
067: protected void setState(State state) {
068: _state = state;
069: }
070:
071: // protected HttpSession _session;
072: // protected HttpSession getSession () {return _session;}
073: // protected void setSession(HttpSession session) {_session=session;}
074:
075: //----------------------------------------
076: // 'StateInterceptor' API
077: //----------------------------------------
078:
079: // lifecycle
080: public void start() {
081: }
082:
083: public void stop() {
084: }
085:
086: // misc
087: public String toString() {
088: return "<" + getClass() + "->" + getState() + ">";
089: }
090:
091: //----------------------------------------
092: // wrapped-n-delegated-to 'State' API
093: //----------------------------------------
094: // invariant field accessors
095: public String getId() throws RemoteException {
096: return getState().getId();
097: }
098:
099: public int getActualMaxInactiveInterval() throws RemoteException {
100: return getState().getActualMaxInactiveInterval();
101: }
102:
103: public long getCreationTime() throws RemoteException {
104: return getState().getCreationTime();
105: }
106:
107: // variant field accessors
108: public Map getAttributes() throws RemoteException {
109: return getState().getAttributes();
110: }
111:
112: public void setAttributes(Map attributes) throws RemoteException {
113: getState().setAttributes(attributes);
114: }
115:
116: public long getLastAccessedTime() throws RemoteException {
117: return getState().getLastAccessedTime();
118: }
119:
120: public void setLastAccessedTime(long time) throws RemoteException {
121: getState().setLastAccessedTime(time);
122: }
123:
124: public int getMaxInactiveInterval() throws RemoteException {
125: return getState().getMaxInactiveInterval();
126: }
127:
128: public void setMaxInactiveInterval(int interval)
129: throws RemoteException {
130: getState().setMaxInactiveInterval(interval);
131: }
132:
133: // compound fn-ality
134: public Object getAttribute(String name) throws RemoteException {
135: return getState().getAttribute(name);
136: }
137:
138: public Object setAttribute(String name, Object value,
139: boolean returnValue) throws RemoteException {
140: return getState().setAttribute(name, value, returnValue);
141: }
142:
143: public Object removeAttribute(String name, boolean returnValue)
144: throws RemoteException {
145: return getState().removeAttribute(name, returnValue);
146: }
147:
148: public Enumeration getAttributeNameEnumeration()
149: throws RemoteException {
150: return getState().getAttributeNameEnumeration();
151: }
152:
153: public String[] getAttributeNameStringArray()
154: throws RemoteException {
155: return getState().getAttributeNameStringArray();
156: }
157:
158: public boolean isValid() throws RemoteException {
159: return getState().isValid();
160: }
161:
162: public Object clone() {
163: Object tmp = null;
164: try {
165: tmp = getClass().newInstance();
166: } catch (Exception e) {
167: // _log.error("could not clone "+getClass().getName(),e); - TODO
168: }
169:
170: return tmp;
171: }
172: }
|