|
|||||||||||||||||||
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 | |||||||||||||||
SessionPropertyPersistenceStrategy.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.record; | |
16 | ||
17 | import java.util.ArrayList; | |
18 | import java.util.Collection; | |
19 | import java.util.Collections; | |
20 | import java.util.Iterator; | |
21 | ||
22 | import org.apache.hivemind.util.Defense; | |
23 | import org.apache.tapestry.IRequestCycle; | |
24 | import org.apache.tapestry.TapestryUtils; | |
25 | import org.apache.tapestry.engine.ServiceEncoding; | |
26 | import org.apache.tapestry.web.WebRequest; | |
27 | import org.apache.tapestry.web.WebSession; | |
28 | ||
29 | /** | |
30 | * The most basic {@link org.apache.tapestry.record.PropertyPersistenceStrategy}, which stores | |
31 | * properties in the HttpSession as attributes. | |
32 | * | |
33 | * @author Howard M. Lewis Ship | |
34 | * @since 4.0 | |
35 | */ | |
36 | public class SessionPropertyPersistenceStrategy implements PropertyPersistenceStrategy | |
37 | { | |
38 | // Really, the name of the servlet; used as a prefix on all HttpSessionAttribute keys | |
39 | // to keep things straight if multiple Tapestry apps are deployed | |
40 | // in the same WAR. | |
41 | ||
42 | private String _applicationId; | |
43 | ||
44 | private WebRequest _request; | |
45 | ||
46 | 23 | public void store(String pageName, String idPath, String propertyName, Object newValue) |
47 | { | |
48 | 23 | Defense.notNull(pageName, "pageName"); |
49 | 23 | Defense.notNull(propertyName, "propertyName"); |
50 | ||
51 | 23 | WebSession session = _request.getSession(true); |
52 | ||
53 | 23 | StringBuffer buffer = new StringBuffer(); |
54 | ||
55 | 23 | buffer.append(_applicationId); |
56 | 23 | buffer.append(","); |
57 | 23 | buffer.append(pageName); |
58 | ||
59 | 23 | if (idPath != null) |
60 | { | |
61 | 1 | buffer.append(","); |
62 | 1 | buffer.append(idPath); |
63 | } | |
64 | ||
65 | 23 | buffer.append(","); |
66 | 23 | buffer.append(propertyName); |
67 | ||
68 | 23 | String key = buffer.toString(); |
69 | ||
70 | 23 | session.setAttribute(key, newValue); |
71 | } | |
72 | ||
73 | 192 | public Collection getStoredChanges(String pageName, IRequestCycle cycle) |
74 | { | |
75 | 192 | Defense.notNull(pageName, "pageName"); |
76 | ||
77 | 192 | WebSession session = _request.getSession(false); |
78 | ||
79 | 192 | if (session == null) |
80 | 144 | return Collections.EMPTY_LIST; |
81 | ||
82 | 48 | Collection result = new ArrayList(); |
83 | ||
84 | 48 | String prefix = _applicationId + "," + pageName + ","; |
85 | ||
86 | 48 | Iterator i = session.getAttributeNames().iterator(); |
87 | 48 | while (i.hasNext()) |
88 | { | |
89 | 82 | String key = (String) i.next(); |
90 | ||
91 | 82 | if (key.startsWith(prefix)) |
92 | { | |
93 | 13 | PropertyChange change = buildChange(key, session.getAttribute(key)); |
94 | ||
95 | 13 | result.add(change); |
96 | } | |
97 | } | |
98 | ||
99 | 48 | return result; |
100 | } | |
101 | ||
102 | 13 | private PropertyChange buildChange(String key, Object value) |
103 | { | |
104 | 13 | String[] tokens = TapestryUtils.split(key); |
105 | ||
106 | // Either app-name,page-name,id-path,property | |
107 | // or app-name,page-name,property | |
108 | ||
109 | 13 | String idPath = (tokens.length == 4) ? tokens[2] : null; |
110 | 13 | String propertyName = tokens[tokens.length - 1]; |
111 | ||
112 | 13 | return new PropertyChangeImpl(idPath, propertyName, value); |
113 | } | |
114 | ||
115 | 3 | public void discardStoredChanges(String pageName, IRequestCycle cycle) |
116 | { | |
117 | 3 | Defense.notNull(pageName, "pageName"); |
118 | ||
119 | 3 | WebSession session = _request.getSession(false); |
120 | ||
121 | 3 | if (session == null) |
122 | 1 | return; |
123 | ||
124 | 2 | String prefix = _applicationId + "," + pageName + ","; |
125 | ||
126 | 2 | Iterator i = session.getAttributeNames().iterator(); |
127 | 2 | while (i.hasNext()) |
128 | { | |
129 | 2 | String key = (String) i.next(); |
130 | ||
131 | 2 | if (key.startsWith(prefix)) |
132 | 1 | session.setAttribute(key, null); |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * Does nothing; session persistence does not make use of query parameters. | |
138 | */ | |
139 | ||
140 | 140 | public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle) |
141 | { | |
142 | } | |
143 | ||
144 | 48 | public void setApplicationId(String applicationName) |
145 | { | |
146 | 48 | _applicationId = applicationName; |
147 | } | |
148 | ||
149 | 51 | public void setRequest(WebRequest request) |
150 | { | |
151 | 51 | _request = request; |
152 | } | |
153 | } |
|