1 16 17 package org.apache.jetspeed.services.statemanager; 19 20 import java.util.Map ; 22 import java.util.Vector ; 23 import java.util.Enumeration ; 24 25 import javax.servlet.http.HttpSession ; 26 import javax.servlet.http.HttpSessionBindingListener ; 27 import javax.servlet.http.HttpSessionBindingEvent ; 28 29 import org.apache.jetspeed.services.statemanager.BaseStateManagerService; 30 31 47 public class JetspeedHttpStateManagerService 48 extends BaseStateManagerService 49 { 50 53 protected void initStates() 54 { 55 } 57 60 protected void shutdownStates() 61 { 62 } 64 67 private HttpSession getSession() 68 { 69 HttpSession session = (HttpSession ) m_httpSessions.get(Thread.currentThread()); 71 if (session == null) return null; 72 73 try 75 { 76 session.isNew(); 77 } 78 catch (IllegalStateException e) 79 { 80 return null; 81 } 82 83 return session; 84 85 } 87 92 private String getSessionKey( String key ) 93 { 94 return JetspeedHttpStateManagerService.class.getName() + "." + key; 96 97 } 99 104 protected Map getState( String key ) 105 { 106 HttpSession session = getSession(); 108 if (session == null) return null; 109 110 StateEntry stateEntry = (StateEntry) session.getAttribute(getSessionKey(key)); 112 if (stateEntry == null) return null; 113 114 return stateEntry.getMap(); 115 116 } 118 123 protected void addState( String key, Map state ) 124 { 125 HttpSession session = getSession(); 127 if (session == null) return; 128 129 StateEntry stateEntry = new StateEntry(key, state); 131 132 session.setAttribute(getSessionKey(key), stateEntry); 134 135 } 137 141 protected void removeState( String key ) 142 { 143 HttpSession session = getSession(); 145 if (session == null) return; 146 147 session.removeAttribute(getSessionKey(key)); 149 150 } 152 157 protected String [] getStateKeys( String start ) 158 { 159 HttpSession session = getSession(); 161 if (session == null) return null; 162 163 String pattern = getSessionKey(start); 165 166 int subStart = getSessionKey("").length(); 168 169 Vector rv = new Vector (); 171 172 Enumeration names = session.getAttributeNames(); 174 while (names.hasMoreElements()) 175 { 176 String sessionName = (String ) names.nextElement(); 177 178 if (sessionName.startsWith(pattern)) 180 { 181 rv.add(sessionName.substring(subStart)); 182 } 183 } 184 185 if (rv.size() == 0) return null; 186 187 return (String []) rv.toArray(new String [rv.size()]); 188 189 } 191 194 private class StateEntry 195 implements HttpSessionBindingListener 196 { 197 198 private Map m_map = null; 199 200 201 private String m_key = null; 202 203 208 public StateEntry( String key, Map map ) 209 { 210 m_key = key; 211 m_map = map; 212 213 } 215 219 public Map getMap() 220 { 221 return m_map; 222 223 } 225 228 public void valueBound( HttpSessionBindingEvent event ) {} 229 230 233 public void valueUnbound( HttpSessionBindingEvent event ) 234 { 235 retireAttributes(m_key, m_map); 237 m_map = null; 238 m_key = null; 239 } 240 241 } 243 } 245 250 251 | Popular Tags |