1 15 package org.apache.tapestry.engine.state; 16 17 import org.apache.tapestry.web.WebRequest; 18 import org.apache.tapestry.web.WebSession; 19 20 27 public class SessionScopeManager implements StateObjectPersistenceManager 28 { 29 private WebRequest _request; 30 31 private String _applicationId; 32 33 private String buildKey(String objectName) 34 { 35 38 return "state:" + _applicationId + ":" + objectName; 39 } 40 41 44 45 private WebSession getSession() 46 { 47 return _request.getSession(true); 48 } 49 50 public boolean exists(String objectName) 51 { 52 WebSession session = _request.getSession(false); 53 54 if (session == null) 55 return false; 56 57 return session.getAttribute(buildKey(objectName)) != null; 58 } 59 60 public Object get(String objectName, StateObjectFactory factory) 61 { 62 String key = buildKey(objectName); 63 WebSession session = getSession(); 64 65 Object result = session.getAttribute(key); 66 if (result == null) 67 { 68 result = factory.createStateObject(); 69 session.setAttribute(key, result); 70 } 71 72 return result; 73 } 74 75 public void store(String objectName, Object stateObject) 76 { 77 String key = buildKey(objectName); 78 79 WebSession session = getSession(); 80 81 session.setAttribute(key, stateObject); 82 } 83 84 public void setApplicationId(String applicationName) 85 { 86 _applicationId = applicationName; 87 } 88 89 public void setRequest(WebRequest request) 90 { 91 _request = request; 92 } 93 } | Popular Tags |