|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover 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.Enumeration;
|
|
21 |
|
|
22 |
import javax.servlet.http.HttpServletRequest;
|
|
23 |
import javax.servlet.http.HttpSession;
|
|
24 |
|
|
25 |
import org.apache.hivemind.util.Defense;
|
|
26 |
import org.apache.tapestry.IRequestCycle;
|
|
27 |
import org.apache.tapestry.util.StringSplitter;
|
|
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 3.1
|
|
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 HttpServletRequest _request;
|
|
45 |
|
|
46 |
private StringSplitter _splitter = new StringSplitter(','); |
|
47 |
|
|
48 | 34 |
public void store(String pageName, String idPath, String propertyName, Object newValue) |
49 |
{ |
|
50 | 34 |
Defense.notNull(pageName, "pageName");
|
51 | 34 |
Defense.notNull(propertyName, "propertyName");
|
52 |
|
|
53 | 34 |
HttpSession session = _request.getSession(true);
|
54 |
|
|
55 | 34 |
StringBuffer buffer = new StringBuffer();
|
56 |
|
|
57 | 34 |
buffer.append(_applicationId); |
58 | 34 |
buffer.append(",");
|
59 | 34 |
buffer.append(pageName); |
60 |
|
|
61 | 34 |
if (idPath != null) |
62 |
{ |
|
63 | 1 |
buffer.append(",");
|
64 | 1 |
buffer.append(idPath); |
65 |
} |
|
66 |
|
|
67 | 34 |
buffer.append(",");
|
68 | 34 |
buffer.append(propertyName); |
69 |
|
|
70 | 34 |
String key = buffer.toString(); |
71 |
|
|
72 | 34 |
if (newValue == null) |
73 | 4 |
session.removeAttribute(key); |
74 |
else
|
|
75 | 30 |
session.setAttribute(key, newValue); |
76 |
} |
|
77 |
|
|
78 | 267 |
public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
79 |
{ |
|
80 | 267 |
Defense.notNull(pageName, "pageName");
|
81 |
|
|
82 | 267 |
HttpSession session = _request.getSession(false);
|
83 |
|
|
84 | 267 |
if (session == null) |
85 | 200 |
return Collections.EMPTY_LIST;
|
86 |
|
|
87 | 67 |
Collection result = new ArrayList();
|
88 |
|
|
89 | 67 |
String prefix = _applicationId + "," + pageName + ","; |
90 |
|
|
91 | 67 |
Enumeration e = session.getAttributeNames(); |
92 | 67 |
while (e.hasMoreElements())
|
93 |
{ |
|
94 | 127 |
String key = (String) e.nextElement(); |
95 |
|
|
96 | 127 |
if (key.startsWith(prefix))
|
97 |
{ |
|
98 | 25 |
IPageChange change = buildChange(key, session.getAttribute(key)); |
99 |
|
|
100 | 25 |
result.add(change); |
101 |
} |
|
102 |
} |
|
103 |
|
|
104 | 67 |
return result;
|
105 |
} |
|
106 |
|
|
107 | 25 |
private IPageChange buildChange(String key, Object value)
|
108 |
{ |
|
109 | 25 |
String[] tokens = _splitter.splitToArray(key); |
110 |
|
|
111 |
// Either app-name,page-name,id-path,property
|
|
112 |
// or app-name,page-name,property
|
|
113 |
|
|
114 | 25 |
String idPath = (tokens.length == 4) ? tokens[2] : null;
|
115 | 25 |
String propertyName = tokens[tokens.length - 1]; |
116 |
|
|
117 | 25 |
return new PageChange(idPath, propertyName, value); |
118 |
} |
|
119 |
|
|
120 | 3 |
public void discardStoredChanges(String pageName, IRequestCycle cycle) |
121 |
{ |
|
122 | 3 |
Defense.notNull(pageName, "pageName");
|
123 |
|
|
124 | 3 |
HttpSession session = _request.getSession(false);
|
125 |
|
|
126 | 3 |
if (session == null) |
127 | 1 |
return;
|
128 |
|
|
129 | 2 |
String prefix = _applicationId + "," + pageName + ","; |
130 |
|
|
131 | 2 |
Enumeration e = session.getAttributeNames(); |
132 | 2 |
while (e.hasMoreElements())
|
133 |
{ |
|
134 | 2 |
String key = (String) e.nextElement(); |
135 |
|
|
136 | 2 |
if (key.startsWith(prefix))
|
137 | 1 |
session.removeAttribute(key); |
138 |
} |
|
139 |
} |
|
140 |
|
|
141 | 59 |
public void setApplicationId(String applicationName) |
142 |
{ |
|
143 | 59 |
_applicationId = applicationName; |
144 |
} |
|
145 |
|
|
146 | 62 |
public void setRequest(HttpServletRequest request) |
147 |
{ |
|
148 | 62 |
_request = request; |
149 |
} |
|
150 |
} |
|