|
|||||||||||||||||||
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 | |||||||||||||||
ClientPropertyPersistenceStrategy.java | 90% | 100% | 100% | 98% |
|
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.Collection;
|
|
18 |
import java.util.Collections;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.Iterator;
|
|
21 |
import java.util.List;
|
|
22 |
import java.util.Map;
|
|
23 |
|
|
24 |
import org.apache.hivemind.util.Defense;
|
|
25 |
import org.apache.tapestry.IRequestCycle;
|
|
26 |
import org.apache.tapestry.engine.ServiceEncoding;
|
|
27 |
import org.apache.tapestry.web.WebRequest;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Service tapestry.persist.ClientPropertyPersistenceStrategy. Encodes persistent page properties on
|
|
31 |
* the client as query parameters.
|
|
32 |
* <p>
|
|
33 |
* Uses the threaded model.
|
|
34 |
*
|
|
35 |
* @author Howard M. Lewis Ship
|
|
36 |
* @since 4.0
|
|
37 |
* @see org.apache.tapestry.engine.ILink
|
|
38 |
*/
|
|
39 |
public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
|
40 |
{ |
|
41 |
/**
|
|
42 |
* Query parameters consist of this prefix followed by the page name. Each page gets its own
|
|
43 |
* query parameter.
|
|
44 |
*/
|
|
45 |
public static final String PREFIX = "state:"; |
|
46 |
|
|
47 |
/**
|
|
48 |
* Keyed on page name (String), values are
|
|
49 |
* {@link org.apache.tapestry.record.PersistentPropertyData}.
|
|
50 |
*/
|
|
51 |
private final Map _data = new HashMap(); |
|
52 |
|
|
53 |
private final PersistentPropertyDataEncoder _encoder;
|
|
54 |
|
|
55 |
private WebRequest _request;
|
|
56 |
|
|
57 | 150 |
public ClientPropertyPersistenceStrategy()
|
58 |
{ |
|
59 | 150 |
this(new PersistentPropertyDataEncoderImpl()); |
60 |
} |
|
61 |
|
|
62 |
// Alternate constructor used for testing
|
|
63 | 151 |
ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder) |
64 |
{ |
|
65 | 151 |
_encoder = encoder; |
66 |
} |
|
67 |
|
|
68 |
/**
|
|
69 |
* Initializer for this service, invoked every time a service instance is created. This
|
|
70 |
* initializer pulls out of the request and query parameters whose prefix is "client:" and
|
|
71 |
* expects them to be encoded {@link PersistentPropertyData}, which are stored internally.
|
|
72 |
* Because the service model is threaded, this information is specific to a single request, and
|
|
73 |
* will be discarded at the end of the request.
|
|
74 |
*/
|
|
75 |
|
|
76 | 149 |
public void initializeService() |
77 |
{ |
|
78 | 149 |
List names = _request.getParameterNames(); |
79 | 149 |
Iterator i = names.iterator(); |
80 | 149 |
while (i.hasNext())
|
81 |
{ |
|
82 | 453 |
String name = (String) i.next(); |
83 |
|
|
84 | 453 |
if (!name.startsWith(PREFIX))
|
85 | 451 |
continue;
|
86 |
|
|
87 | 2 |
String pageName = name.substring(PREFIX.length()); |
88 | 2 |
String encoded = _request.getParameterValue(name); |
89 |
|
|
90 | 2 |
PersistentPropertyData data = new PersistentPropertyData(_encoder);
|
91 | 2 |
data.storeEncoded(encoded); |
92 |
|
|
93 | 2 |
_data.put(pageName, data); |
94 |
} |
|
95 |
} |
|
96 |
|
|
97 | 1 |
public void store(String pageName, String idPath, String propertyName, Object newValue) |
98 |
{ |
|
99 | 1 |
PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
100 | 1 |
if (data == null) |
101 |
{ |
|
102 | 1 |
data = new PersistentPropertyData(_encoder);
|
103 | 1 |
_data.put(pageName, data); |
104 |
} |
|
105 |
|
|
106 | 1 |
data.store(idPath, propertyName, newValue); |
107 |
} |
|
108 |
|
|
109 | 216 |
public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
110 |
{ |
|
111 | 216 |
PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
112 |
|
|
113 | 216 |
if (data == null) |
114 | 214 |
return Collections.EMPTY_LIST;
|
115 |
|
|
116 | 2 |
return data.getPageChanges();
|
117 |
} |
|
118 |
|
|
119 | 1 |
public void discardStoredChanges(String pageName, IRequestCycle cycle) |
120 |
{ |
|
121 | 1 |
_data.remove(pageName); |
122 |
} |
|
123 |
|
|
124 | 161 |
public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle) |
125 |
{ |
|
126 | 161 |
Defense.notNull(encoding, "encoding");
|
127 | 161 |
Defense.notNull(cycle, "cycle");
|
128 |
|
|
129 | 161 |
Iterator i = _data.entrySet().iterator(); |
130 | 161 |
while (i.hasNext())
|
131 |
{ |
|
132 | 1 |
Map.Entry e = (Map.Entry) i.next(); |
133 |
|
|
134 | 1 |
String pageName = (String) e.getKey(); |
135 | 1 |
PersistentPropertyData data = (PersistentPropertyData) e.getValue(); |
136 |
|
|
137 | 1 |
encoding.setParameterValue(PREFIX + pageName, data.getEncoded()); |
138 |
} |
|
139 |
} |
|
140 |
|
|
141 | 149 |
public void setRequest(WebRequest request) |
142 |
{ |
|
143 | 149 |
_request = request; |
144 |
} |
|
145 |
} |
|