1/*2 * Copyright 2000-2002,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617// package18packageorg.apache.jetspeed.services.statemanager;
1920// imports21import java.util.Map;
22import java.util.Vector;
23import java.util.Enumeration;
2425import javax.servlet.http.HttpSession;
26import javax.servlet.http.HttpSessionBindingListener;
27import javax.servlet.http.HttpSessionBindingEvent;
2829import org.apache.jetspeed.services.statemanager.BaseStateManagerService;
3031/***32* <p>JetspeedHttpStateManagerService is an implementation of the BaseStateManagerService33* which manages the states stored in the "current" HttpSession.</p>34* <p>Note: This implementation of the StateManagerService takes advantage of the35* Servlet container's management of the HttpSession.36* When the session is invalidated, the states we manage will be automatically cleaned up.37* When this happens, the objects placed into our states will have their38* SessionStateBindingListener mechanism invoked.</p>39* <p>Note: This implementation segments the states by session. States created in one session will NOT BE AVAILABLE40* from other sessions.</p>41* @version $Revision: 1.4 $42* @see org.apache.jetspeed.services.statemanager.BaseStateManagerService43* @see org.apache.jetspeed.services.statemanager.StateManagerService44* @see org.apache.jetspeed.services.statemanager.SessionState45* @author <a href="mailto:ggolden@apache.org">Glenn R. Golden</a>46*/47publicclassJetspeedHttpStateManagerService48extendsBaseStateManagerService49 {
50/***51 * Initialize the states storage.52 */53protectedvoid initStates()
54 {
55 } // initStates5657/***58 * Cleanup the states storage.59 */60protectedvoid shutdownStates()
61 {
62 } // shutdownStates6364/***65 * Access the current HttpSession.66 */67private HttpSession getSession()
68 {
69// get the current session that was installed for this thread70 HttpSession session = (HttpSession) m_httpSessions.get(Thread.currentThread());
71if (session == null) returnnull;
7273// call isNew just to see if the session has been invalidated already74try75 {
76 session.isNew();
77 }
78catch (IllegalStateException e)
79 {
80returnnull;
81 }
8283return session;
8485 } // getSession8687/***88 * Convert the key to a name safe to store directly in the session.89 * @param key The state key.90 * @return a name safe to store directly in the session based on key.91 */92private String getSessionKey( String key )
93 {
94// we want our keys not to conflict with any other session usage...95return JetspeedHttpStateManagerService.class.getName() + "." + key;
9697 } // getSessionKey9899/***100 * Access the Map which is the set of attributes for a state.101 * @param key The state key.102 * @return The Map which is the set of attributes for a state.103 */104protected Map getState( String key )
105 {
106// get the session107 HttpSession session = getSession();
108if (session == null) returnnull;
109110// get this state from our entry in the session111 StateEntry stateEntry = (StateEntry) session.getAttribute(getSessionKey(key));
112if (stateEntry == null) returnnull;
113114return stateEntry.getMap();
115116 } // getState117118/***119 * Add a new state to the states we are managing.120 * @param key The state key.121 * @param state The Map which is the set of attributes for the state.122 */123protectedvoid addState( String key, Map state )
124 {
125// get the session126 HttpSession session = getSession();
127if (session == null) return;
128129// create a stateEntry to hold our state Map130 StateEntry stateEntry = new StateEntry(key, state);
131132// put it in the session133 session.setAttribute(getSessionKey(key), stateEntry);
134135 } // addState136137/***138 * Remove a state from the states we are managing.139 * @param key The state key.140 */141protectedvoid removeState( String key )
142 {
143// get the session144 HttpSession session = getSession();
145if (session == null) return;
146147// remove the key from the session - the StateEntry will be notified148 session.removeAttribute(getSessionKey(key));
149150 } // removeState151152/***153 * Access an array of the keys of all states managed, those that start with the parameter.154 * @param start The starting string used to select the keys.155 * @return an array of the keys of all states managed.156 */157protected String[] getStateKeys( String start )
158 {
159// get the session160 HttpSession session = getSession();
161if (session == null) returnnull;
162163// use this as the test pattern164 String pattern = getSessionKey(start);
165166// for those that match, this starts the session key167int subStart = getSessionKey("").length();
168169// collect for return170 Vector rv = new Vector();
171172// get the session names173 Enumeration names = session.getAttributeNames();
174while (names.hasMoreElements())
175 {
176 String sessionName = (String) names.nextElement();
177178// pick our states, and those whose key starts with the pattern179if (sessionName.startsWith(pattern))
180 {
181 rv.add(sessionName.substring(subStart));
182 }
183 }
184185if (rv.size() == 0) returnnull;
186187return (String[]) rv.toArray(new String[rv.size()]);
188189 } // getStateKeys190191/***192 * Store the Map for the state, and listen for HttpSessionBinding events193 */194privateclass StateEntry
195 implements HttpSessionBindingListener
196 {
197/*** Store the map. */198private Map m_map = null;
199200/*** The state key. */201private String m_key = null;
202203/***204 * Construct.205 * @param key The state key.206 * @param map The map to hold.207 */208public StateEntry( String key, Map map )
209 {
210 m_key = key;
211 m_map = map;
212213 } // StateEntry214215/***216 * Access the map we are holding.217 * @return the Map we are holding.218 */219public Map getMap()
220 {
221return m_map;
222223 } // getMap224225/***226 * We don't care about when we are bound...227 */228publicvoid valueBound( HttpSessionBindingEvent event ) {}
229230/***231 * When we are unbound, unbind our state's (map's) attributes232 */233publicvoid valueUnbound( HttpSessionBindingEvent event )
234 {
235// notify all attribute and clear the state236 retireAttributes(m_key, m_map);
237 m_map = null;
238 m_key = null;
239 }
240241 } // class StateEntry242243 } // JetspeedHttpStateManagerService244245/***********************************************************************************246*247* $Header: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/statemanager/JetspeedHttpStateManagerService.java,v 1.4 2004/02/23 03:38:28 jford Exp $248*249**********************************************************************************/250