1 16 package org.mortbay.j2ee.session; 17 18 20 import java.rmi.RemoteException ; 21 import java.util.Enumeration ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpSessionContext ; 25 26 import org.jfox.ioc.logger.Logger; 27 28 34 37 39 public class StateAdaptor 40 implements org.mortbay.jetty.servlet.SessionManager.Session 41 { 42 protected static final Logger _log=Logger.getLogger(StateAdaptor.class); 43 Manager _manager; 44 State _state=null; 45 boolean _new=true; 46 47 final String _id; 49 50 StateAdaptor(String id, Manager manager, int maxInactiveInterval, long lastAccessedTime) 51 { 52 _id=id; 53 _manager=manager; 54 } 55 56 void 57 setState(State state) 58 { 59 _state=state; 60 } 61 62 State 63 getState() 64 { 65 return _state; 66 } 67 68 public boolean 72 isValid() 73 { 74 if (_state==null) 75 return false; 76 77 StateInterceptor si=(StateInterceptor)_state; 78 si.setManager(_manager); 79 si.setSession(this); 80 81 try 82 { 83 _state.getLastAccessedTime(); return true; 85 } 86 catch (IllegalStateException ignore) 87 { 88 return false; 89 } 90 catch (Exception e) 91 { 92 _log.error("problem contacting HttpSession", e); 93 return false; 94 } 95 } 96 97 99 public long 100 getCreationTime() 101 throws IllegalStateException 102 { 103 checkState(); 104 105 try 106 { 107 return _state.getCreationTime(); 108 } 109 catch (RemoteException e) 110 { 111 _log.error("could not get CreationTime", e); 112 throw new IllegalStateException ("problem with distribution layer"); 113 } 114 } 115 116 public String 117 getId() 118 throws IllegalStateException 119 { 120 checkState(); 121 122 return _id; 124 } 125 126 public long 127 getLastAccessedTime() 128 throws IllegalStateException 129 { 130 checkState(); 131 132 try 133 { 134 return _state.getLastAccessedTime(); 135 } 136 catch (RemoteException e) 137 { 138 _log.error("could not get LastAccessedTime", e); 139 throw new IllegalStateException ("problem with distribution layer"); 140 } 141 } 142 143 public void 145 setMaxInactiveInterval(int interval) 146 { 147 checkState(); 148 149 try 150 { 151 _state.setMaxInactiveInterval(interval); 152 } 153 catch (RemoteException e) 154 { 155 _log.error("could not set MaxInactiveInterval", e); 156 } 157 } 158 159 public int 160 getMaxInactiveInterval() 161 { 162 checkState(); 163 164 try 165 { 166 return _state.getMaxInactiveInterval(); 167 } 168 catch (RemoteException e) 169 { 170 _log.error("could not get MaxInactiveInterval", e); 173 return 0; 174 } 175 } 176 177 public Object 178 getAttribute(String name) 179 throws IllegalStateException 180 { 181 checkState(); 182 183 try 184 { 185 return _state.getAttribute(name); 186 } 187 catch (RemoteException e) 188 { 189 _log.error("could not get Attribute", e); 190 throw new IllegalStateException ("problem with distribution layer"); 191 } 192 } 193 194 public Object 195 getValue(String name) 196 throws IllegalStateException 197 { 198 checkState(); 199 200 try 201 { 202 return _state.getAttribute(name); 203 } 204 catch (RemoteException e) 205 { 206 _log.error("could not get Value", e); 207 throw new IllegalStateException ("problem with distribution layer"); 208 } 209 } 210 211 public Enumeration 212 getAttributeNames() 213 throws IllegalStateException 214 { 215 checkState(); 216 217 try 218 { 219 return _state.getAttributeNameEnumeration(); 220 } 221 catch (RemoteException e) 222 { 223 _log.error("could not get AttributeNames", e); 224 throw new IllegalStateException ("problem with distribution layer"); 225 } 226 } 227 228 public String [] 229 getValueNames() 230 throws IllegalStateException 231 { 232 checkState(); 233 234 try 235 { 236 return _state.getAttributeNameStringArray(); 237 } 238 catch (RemoteException e) 239 { 240 _log.error("could not get ValueNames", e); 241 throw new IllegalStateException ("problem with distribution layer"); 242 } 243 } 244 245 public void 246 setAttribute(String name, Object value) 247 throws IllegalStateException 248 { 249 checkState(); 250 251 try 252 { 253 if (value==null) 254 _state.removeAttribute(name, false); 255 else 256 { 257 if (name==null) 258 throw new IllegalArgumentException ("invalid attribute name: "+name); 259 260 _state.setAttribute(name, value, false); 261 } 262 } 263 catch (RemoteException e) 264 { 265 _log.error("could not set Attribute", e); 266 throw new IllegalStateException ("problem with distribution layer"); 267 } 268 } 269 270 public void 271 putValue(String name, Object value) 272 throws IllegalStateException 273 { 274 checkState(); 275 276 if (name==null) 277 throw new IllegalArgumentException ("invalid attribute name: "+name); 278 279 if (value==null) 280 throw new IllegalArgumentException ("invalid attribute value: "+value); 281 282 try 283 { 284 _state.setAttribute(name, value, false); 285 } 286 catch (RemoteException e) 287 { 288 _log.error("could not put Value", e); 289 throw new IllegalStateException ("problem with distribution layer"); 290 } 291 } 292 293 public void 294 removeAttribute(String name) 295 throws IllegalStateException 296 { 297 checkState(); 298 299 try 300 { 301 _state.removeAttribute(name, false); 302 } 303 catch (RemoteException e) 304 { 305 _log.error("could not remove Attribute", e); 306 throw new IllegalStateException ("problem with distribution layer"); 307 } 308 } 309 310 public void 311 removeValue(String name) 312 throws IllegalStateException 313 { 314 checkState(); 315 316 try 317 { 318 _state.removeAttribute(name, false); 319 } 320 catch (RemoteException e) 321 { 322 _log.error("could not remove Value", e); 323 throw new IllegalStateException ("problem with distribution layer"); 324 } 325 } 326 327 public void 328 invalidate() 329 throws IllegalStateException 330 { 331 if (_log.isTraceEnabled()) _log.trace("user invalidated session: "+getId()); 332 _manager.destroySession(this); 333 } 334 335 351 352 public boolean 353 isNew() 354 throws IllegalStateException 355 { 356 return _new; 357 } 358 359 public ServletContext 360 getServletContext() 361 { 362 return _manager.getServletContext(); 363 } 364 365 public HttpSessionContext 366 getSessionContext() 367 { 368 return _manager.getSessionContext(); 369 } 370 371 public void 373 access() 374 { 375 long time=System.currentTimeMillis(); setLastAccessedTime(time); 377 378 _new=false; } 380 381 public void 382 setLastAccessedTime(long time) 383 throws IllegalStateException 384 { 385 checkState(); 386 387 try 388 { 389 _state.setLastAccessedTime(time); 390 } 391 catch (RemoteException e) 392 { 393 _log.error("could not set LastAccessedTime", e); 394 throw new IllegalStateException ("problem with distribution layer"); 395 } 396 } 397 398 protected void 399 checkState() 400 throws IllegalStateException 401 { 402 if (_state==null) 403 throw new IllegalStateException ("invalid session"); 404 405 StateInterceptor si=(StateInterceptor)_state; 407 si.setManager(_manager); 408 si.setSession(this); 409 } 410 411 public String 412 toString() 413 { 414 return "<"+getClass()+"->"+_state+">"; 415 } 416 417 421 public void 430 migrate() 431 { 432 } 435 } 436 | Popular Tags |