1 5 package com.terracotta.session; 6 7 import com.tc.session.SessionSupport; 8 import com.terracotta.session.util.ContextMgr; 9 import com.terracotta.session.util.LifecycleEventMgr; 10 import com.terracotta.session.util.StringArrayEnumeration; 11 import com.terracotta.session.util.Timestamp; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import javax.servlet.ServletContext ; 21 import javax.servlet.http.HttpSessionContext ; 22 23 public class SessionData implements Session, SessionSupport { 24 private final Map attributes = new HashMap (); 25 private final Map internalAttributes = new HashMap (); 26 private transient Map transientAttributes; 27 private final long createTime; 28 private final Timestamp timestamp; 29 30 private long lastAccessedTime; 31 private long maxIdleMillis; 32 private transient long requestStartMillis; 33 34 private transient SessionId sessionId; 35 private transient LifecycleEventMgr eventMgr; 36 private transient ContextMgr contextMgr; 37 private transient boolean invalidated = false; 38 39 protected SessionData(int maxIdleSeconds) { 40 this.createTime = System.currentTimeMillis(); 41 this.lastAccessedTime = 0; 42 setMaxInactiveMillis(maxIdleSeconds * 1000); 43 this.timestamp = new Timestamp(this.createTime + maxIdleMillis); 44 } 45 46 void associate(SessionId sid, LifecycleEventMgr lifecycleEventMgr, ContextMgr ctxMgr) { 47 this.sessionId = sid; 48 this.eventMgr = lifecycleEventMgr; 49 this.contextMgr = ctxMgr; 50 } 51 52 public SessionId getSessionId() { 53 return this.sessionId; 54 } 55 56 public SessionData getSessionData() { 57 return this; 58 } 59 60 public ServletContext getServletContext() { 61 return contextMgr.getServletContext(); 62 } 63 64 public HttpSessionContext getSessionContext() { 65 return contextMgr.getSessionContext(); 66 } 67 68 public synchronized boolean isValid() { 69 if (invalidated) { return false; } 70 final boolean isValid = getIdleMillis() < getMaxInactiveMillis(); 71 return isValid; 72 } 73 74 public boolean isNew() { 75 checkIfValid(); 76 return sessionId.isNew(); 77 } 78 79 public synchronized void invalidate() { 80 if (invalidated) { throw new IllegalStateException ("session already invalidated"); } 81 82 try { 83 eventMgr.fireSessionDestroyedEvent(this); 84 85 String [] attrs = (String []) attributes.keySet().toArray(new String [attributes.size()]); 86 87 for (int i = 0; i < attrs.length; i++) { 88 unbindAttribute(attrs[i]); 89 } 90 } finally { 91 invalidated = true; 92 } 93 } 94 95 private void checkIfValid() { 96 if (!isValid()) throw new IllegalStateException ("This session is invalid"); 97 } 98 99 synchronized void startRequest() { 100 requestStartMillis = System.currentTimeMillis(); 101 } 102 103 public void setMaxInactiveInterval(int v) { 104 setMaxInactiveMillis(v * 1000); 105 if (isValid() && v == 0) { 106 invalidate(); 107 } 108 } 109 110 113 synchronized long getIdleMillis() { 114 if (lastAccessedTime == 0) return 0; 115 if (requestStartMillis > lastAccessedTime) return requestStartMillis - lastAccessedTime; 116 return Math.max(System.currentTimeMillis() - lastAccessedTime, 0); 117 } 118 119 synchronized void finishRequest() { 120 requestStartMillis = 0; 121 lastAccessedTime = System.currentTimeMillis(); 122 } 123 124 public synchronized long getCreationTime() { 125 checkIfValid(); 126 return createTime; 127 } 128 129 public synchronized long getLastAccessedTime() { 130 checkIfValid(); 131 return lastAccessedTime; 132 } 133 134 public void setAttribute(String name, Object value) { 135 setAttributeReturnOld(name, value); 136 } 137 138 public synchronized Object setAttributeReturnOld(String name, Object value) { 139 checkIfValid(); 140 if (value == null) { 141 return unbindAttribute(name); 142 } else { 143 return bindAttribute(name, value); 144 } 145 } 146 147 public void putValue(String name, Object val) { 148 setAttribute(name, val); 149 } 150 151 public synchronized Object getAttribute(String name) { 152 checkIfValid(); 153 return attributes.get(name); 154 } 155 156 public Object getValue(String name) { 157 return getAttribute(name); 158 } 159 160 public synchronized String [] getValueNames() { 161 checkIfValid(); 162 Set keys = attributes.keySet(); 163 return (String []) keys.toArray(new String [keys.size()]); 164 } 165 166 public Enumeration getAttributeNames() { 167 return new StringArrayEnumeration(getValueNames()); 168 } 169 170 public void removeAttribute(String name) { 171 removeAttributeReturnOld(name); 172 } 173 174 public synchronized Object removeAttributeReturnOld(String name) { 175 checkIfValid(); 176 return unbindAttribute(name); 177 } 178 179 public void removeValue(String name) { 180 removeAttribute(name); 181 } 182 183 synchronized long getMaxInactiveMillis() { 184 return maxIdleMillis; 185 } 186 187 public int getMaxInactiveInterval() { 188 return (int) getMaxInactiveMillis() / 1000; 189 } 190 191 private void setMaxInactiveMillis(long v) { 192 maxIdleMillis = v; 193 } 194 195 public synchronized Object getInternalAttribute(String name) { 196 checkIfValid(); 197 return internalAttributes.get(name); 198 } 199 200 public synchronized Object setInternalAttribute(String name, Object value) { 201 checkIfValid(); 202 return internalAttributes.put(name, value); 203 } 204 205 public synchronized Object removeInternalAttribute(String name) { 206 checkIfValid(); 207 return internalAttributes.remove(name); 208 } 209 210 public synchronized Object getTransientAttribute(String name) { 211 checkIfValid(); 212 return getTransientAttributes().get(name); 213 } 214 215 public synchronized Object setTransientAttribute(String name, Object value) { 216 checkIfValid(); 217 return getTransientAttributes().put(name, value); 218 } 219 220 public synchronized Object removeTransientAttribute(String name) { 221 checkIfValid(); 222 return getTransientAttributes().remove(name); 223 } 224 225 public synchronized Collection getTransientAttributeKeys() { 226 checkIfValid(); 227 return new ArrayList (getTransientAttributes().keySet()); 228 } 229 230 private Object bindAttribute(String name, Object newVal) { 231 Object oldVal = getAttribute(name); 232 if (newVal != oldVal) eventMgr.bindAttribute(this, name, newVal); 233 234 oldVal = attributes.put(name, newVal); 235 236 if (oldVal != newVal) eventMgr.unbindAttribute(this, name, oldVal); 237 238 if (oldVal != null) eventMgr.replaceAttribute(this, name, oldVal, newVal); 240 else eventMgr.setAttribute(this, name, newVal); 241 242 return oldVal; 243 } 244 245 private Object unbindAttribute(String name) { 246 Object oldVal = attributes.remove(name); 247 if (oldVal != null) { 248 eventMgr.unbindAttribute(this, name, oldVal); 249 eventMgr.removeAttribute(this, name, oldVal); 250 } 251 return oldVal; 252 } 253 254 public void resumeRequest() { 255 TerracottaSessionManager.resumeRequest(this); 256 } 257 258 public void pauseRequest() { 259 TerracottaSessionManager.pauseRequest(this); 260 } 261 262 private Map getTransientAttributes() { 263 if (transientAttributes == null) { 264 transientAttributes = new HashMap (); 265 } 266 return transientAttributes; 267 } 268 269 public String getId() { 270 return sessionId.getExternalId(); 271 } 272 273 Timestamp getTimestamp() { 274 return timestamp; 275 } 276 277 } 278 | Popular Tags |