|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SessionScopeManager.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.engine.state; | |
16 | ||
17 | import org.apache.tapestry.web.WebRequest; | |
18 | import org.apache.tapestry.web.WebSession; | |
19 | ||
20 | /** | |
21 | * Manager for the 'session' scope; state objects are stored as HttpSession attributes. The | |
22 | * HttpSession is created as necessary. | |
23 | * | |
24 | * @author Howard M. Lewis Ship | |
25 | * @since 4.0 | |
26 | */ | |
27 | public class SessionScopeManager implements StateObjectPersistenceManager | |
28 | { | |
29 | private WebRequest _request; | |
30 | ||
31 | private String _applicationId; | |
32 | ||
33 | 8 | private String buildKey(String objectName) |
34 | { | |
35 | // For Portlets, the application id is going to be somewhat redundant, since | |
36 | // the Portlet API keeps portlets seperate anyway. | |
37 | ||
38 | 8 | return "state:" + _applicationId + ":" + objectName; |
39 | } | |
40 | ||
41 | /** | |
42 | * Returns the session for the current request, creating it if necessary. | |
43 | */ | |
44 | ||
45 | 6 | private WebSession getSession() |
46 | { | |
47 | 6 | return _request.getSession(true); |
48 | } | |
49 | ||
50 | 3 | public boolean exists(String objectName) |
51 | { | |
52 | 3 | WebSession session = _request.getSession(false); |
53 | ||
54 | 3 | if (session == null) |
55 | 1 | return false; |
56 | ||
57 | 2 | return session.getAttribute(buildKey(objectName)) != null; |
58 | } | |
59 | ||
60 | 4 | public Object get(String objectName, StateObjectFactory factory) |
61 | { | |
62 | 4 | String key = buildKey(objectName); |
63 | 4 | WebSession session = getSession(); |
64 | ||
65 | 4 | Object result = session.getAttribute(key); |
66 | 4 | if (result == null) |
67 | { | |
68 | 3 | result = factory.createStateObject(); |
69 | 2 | session.setAttribute(key, result); |
70 | } | |
71 | ||
72 | 3 | return result; |
73 | } | |
74 | ||
75 | 2 | public void store(String objectName, Object stateObject) |
76 | { | |
77 | 2 | String key = buildKey(objectName); |
78 | ||
79 | 2 | WebSession session = getSession(); |
80 | ||
81 | 2 | session.setAttribute(key, stateObject); |
82 | } | |
83 | ||
84 | 7 | public void setApplicationId(String applicationName) |
85 | { | |
86 | 7 | _applicationId = applicationName; |
87 | } | |
88 | ||
89 | 8 | public void setRequest(WebRequest request) |
90 | { | |
91 | 8 | _request = request; |
92 | } | |
93 | } |
|