|
|||||||||||||||||||
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.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 | 33 |
public void store(String pageName, String idPath, String propertyName, Object newValue) |
47 |
{ |
|
48 | 33 |
Defense.notNull(pageName, "pageName");
|
49 | 33 |
Defense.notNull(propertyName, "propertyName");
|
50 |
|
|
51 | 33 |
WebSession session = _request.getSession(true);
|
52 |
|
|
53 | 33 |
StringBuffer buffer = new StringBuffer();
|
54 |
|
|
55 | 33 |
buffer.append(_applicationId); |
56 | 33 |
buffer.append(",");
|
57 | 33 |
buffer.append(pageName); |
58 |
|
|
59 | 33 |
if (idPath != null) |
60 |
{ |
|
61 | 1 |
buffer.append(",");
|
62 | 1 |
buffer.append(idPath); |
63 |
} |
|
64 |
|
|
65 | 33 |
buffer.append(",");
|
66 | 33 |
buffer.append(propertyName); |
67 |
|
|
68 | 33 |
String key = buffer.toString(); |
69 |
|
|
70 | 33 |
session.setAttribute(key, newValue); |
71 |
} |
|
72 |
|
|
73 | 216 |
public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
74 |
{ |
|
75 | 216 |
Defense.notNull(pageName, "pageName");
|
76 |
|
|
77 | 216 |
WebSession session = _request.getSession(false);
|
78 |
|
|
79 | 216 |
if (session == null) |
80 | 156 |
return Collections.EMPTY_LIST;
|
81 |
|
|
82 | 60 |
Collection result = new ArrayList();
|
83 |
|
|
84 | 60 |
String prefix = _applicationId + "," + pageName + ","; |
85 |
|
|
86 | 60 |
Iterator i = session.getAttributeNames().iterator(); |
87 | 60 |
while (i.hasNext())
|
88 |
{ |
|
89 | 104 |
String key = (String) i.next(); |
90 |
|
|
91 | 104 |
if (key.startsWith(prefix))
|
92 |
{ |
|
93 | 24 |
PropertyChange change = buildChange(key, session.getAttribute(key)); |
94 |
|
|
95 | 24 |
result.add(change); |
96 |
} |
|
97 |
} |
|
98 |
|
|
99 | 60 |
return result;
|
100 |
} |
|
101 |
|
|
102 | 24 |
private PropertyChange buildChange(String key, Object value)
|
103 |
{ |
|
104 | 24 |
String[] tokens = TapestryUtils.split(key); |
105 |
|
|
106 |
// Either app-name,page-name,id-path,property
|
|
107 |
// or app-name,page-name,property
|
|
108 |
|
|
109 | 24 |
String idPath = (tokens.length == 4) ? tokens[2] : null;
|
110 | 24 |
String propertyName = tokens[tokens.length - 1]; |
111 |
|
|
112 | 24 |
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 | 160 |
public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle) |
141 |
{ |
|
142 |
} |
|
143 |
|
|
144 | 52 |
public void setApplicationId(String applicationName) |
145 |
{ |
|
146 | 52 |
_applicationId = applicationName; |
147 |
} |
|
148 |
|
|
149 | 55 |
public void setRequest(WebRequest request) |
150 |
{ |
|
151 | 55 |
_request = request; |
152 |
} |
|
153 |
} |
|