1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.lib.UnserializeReader; 34 import com.caucho.util.CacheListener; 35 36 import java.io.IOException ; 37 import java.io.ObjectInputStream ; 38 import java.io.ObjectOutputStream ; 39 import java.io.Serializable ; 40 import java.util.IdentityHashMap ; 41 import java.util.Map ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 48 public class SessionArrayValue extends ArrayValueWrapper 49 implements CacheListener, Serializable 50 { 51 static protected final Logger log 52 = Logger.getLogger(SessionArrayValue.class.getName()); 53 54 private String _id; 55 56 private int _useCount; 57 58 protected long _accessTime; 59 private long _maxInactiveInterval; 60 61 private boolean _isValid; 62 63 public SessionArrayValue(String id, long now, 64 long maxInactiveInterval) 65 { 66 this(id, now, maxInactiveInterval, new ArrayValueImpl()); 67 } 68 69 public SessionArrayValue(String id, long now, 70 long maxInactiveInterval, ArrayValue array) 71 { 72 super(array); 73 74 _id = id; 75 _accessTime = now; 76 _maxInactiveInterval = maxInactiveInterval; 77 } 78 79 82 public String getId() 83 { 84 return _id; 85 } 86 87 90 public Object toObject() 91 { 92 return null; 93 } 94 95 98 public Value copy(Env env, IdentityHashMap <Value,Value> map) 99 { 100 long accessTime = _accessTime; 101 102 SessionArrayValue copy = 103 new SessionArrayValue(_id, accessTime, _maxInactiveInterval, 104 (ArrayValue) getArray().copy(env, map)); 105 106 return copy; 107 } 108 109 112 public String encode() 113 { 114 StringBuilder sb = new StringBuilder (); 115 ArrayValue array = getArray(); 116 117 synchronized (array) { 118 for (Map.Entry <Value,Value> entry : array.entrySet()) { 119 sb.append(entry.getKey().toString()); 120 sb.append("|"); 121 entry.getValue().serialize(sb); 122 } 123 } 124 125 return sb.toString(); 126 } 127 128 131 public boolean decode(Env env, String encoded) 132 { 133 ArrayValue array = getArray(); 134 135 try { 136 UnserializeReader is = new UnserializeReader(encoded); 137 138 StringBuilder sb = new StringBuilder (); 139 140 synchronized (array) { 141 while (true) { 142 int ch; 143 144 sb.setLength(0); 145 146 while ((ch = is.read()) > 0 && ch != '|') { 147 sb.append((char) ch); 148 } 149 150 if (sb.length() == 0) 151 return true; 152 153 String key = sb.toString(); 154 155 array.put(new StringValueImpl(key), is.unserialize(env)); 156 } 157 } 158 } catch (IOException e) { 159 throw new QuercusModuleException(e); 160 } 161 } 162 163 public synchronized boolean inUse() 164 { 165 return _useCount > 0; 166 } 167 168 public synchronized void addUse() 169 { 170 _useCount++; 171 } 172 173 public boolean load() 174 { 175 return true; 176 } 177 178 181 public void store(ObjectOutputStream out) 182 throws IOException 183 { 184 out.writeObject(encode()); 185 } 186 187 public void load(Env env, ObjectInputStream in) 188 throws IOException 189 { 190 try { 191 String encoded = in.readObject().toString(); 192 193 decode(env, encoded); 194 } catch (ClassNotFoundException e) { 195 log.log(Level.WARNING, "Can't deserialize session", e); 196 } 197 } 198 199 206 public void finish() 207 { 208 int count; 209 210 synchronized (this) { 211 count = --_useCount; 212 } 213 214 if (count > 0) 215 return; 216 217 if (count < 0) 218 throw new IllegalStateException (); 219 220 store(); 221 } 222 223 226 public void storeOnShutdown() 227 { 228 store(); 229 } 230 231 protected void store() 232 { 233 } 234 235 public long getMaxInactiveInterval() 236 { 237 return _maxInactiveInterval; 238 } 239 240 246 247 public void reset(long now) 248 { 249 setValid(true); 250 setAccess(now); 251 clear(); 252 } 253 254 public synchronized long getAccessTime() 255 { 256 return _accessTime; 257 } 258 259 public synchronized void setAccess(long now) 260 { 261 _accessTime = now; 262 } 263 264 public synchronized boolean isValid() 265 { 266 return _isValid; 267 } 268 269 public synchronized void setValid(boolean isValid) 270 { 271 _isValid = isValid; 272 } 273 274 277 public void invalidate() 278 { 279 if (! _isValid) 280 throw new IllegalStateException (L.l("Can't call invalidate() when session is no longer valid.")); 281 282 try { 283 remove(); 284 285 clear(); 286 } finally { 287 _isValid = false; 288 } 289 } 290 291 protected void remove() 292 { 293 } 294 295 public boolean isEmpty() 296 { 297 return getSize() == 0; 298 } 299 300 304 public void removeEvent() 305 { 306 308 328 } 329 330 334 private Object writeReplace() 335 { 336 return new ArrayValueImpl(this); 337 } 338 } 339 | Popular Tags |