|
|||||||||||||||||||
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 | |||||||||||||||
ClientPropertyPersistenceStrategy.java | 91.7% | 100% | 100% | 98.3% |
|
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 | private ClientPropertyPersistenceScope _scope; | |
58 | ||
59 | 266 | public ClientPropertyPersistenceStrategy() |
60 | { | |
61 | 266 | this(new PersistentPropertyDataEncoderImpl()); |
62 | } | |
63 | ||
64 | // Alternate constructor used for testing | |
65 | 267 | ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder) |
66 | { | |
67 | 267 | _encoder = encoder; |
68 | } | |
69 | ||
70 | /** | |
71 | * Initializer for this service, invoked every time a service instance is created. This | |
72 | * initializer pulls out of the request and query parameters whose prefix is "client:" and | |
73 | * expects them to be encoded {@link PersistentPropertyData}, which are stored internally. | |
74 | * Because the service model is threaded, this information is specific to a single request, and | |
75 | * will be discarded at the end of the request. | |
76 | */ | |
77 | ||
78 | 265 | public void initializeService() |
79 | { | |
80 | 265 | List names = _request.getParameterNames(); |
81 | 265 | Iterator i = names.iterator(); |
82 | 265 | while (i.hasNext()) |
83 | { | |
84 | 741 | String name = (String) i.next(); |
85 | ||
86 | 741 | if (!name.startsWith(PREFIX)) |
87 | 737 | continue; |
88 | ||
89 | 4 | String pageName = name.substring(PREFIX.length()); |
90 | 4 | String encoded = _request.getParameterValue(name); |
91 | ||
92 | 4 | PersistentPropertyData data = new PersistentPropertyData(_encoder); |
93 | 4 | data.storeEncoded(encoded); |
94 | ||
95 | 4 | _data.put(pageName, data); |
96 | } | |
97 | } | |
98 | ||
99 | 1 | public void store(String pageName, String idPath, String propertyName, Object newValue) |
100 | { | |
101 | 1 | PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
102 | 1 | if (data == null) |
103 | { | |
104 | 1 | data = new PersistentPropertyData(_encoder); |
105 | 1 | _data.put(pageName, data); |
106 | } | |
107 | ||
108 | 1 | data.store(idPath, propertyName, newValue); |
109 | } | |
110 | ||
111 | 568 | public Collection getStoredChanges(String pageName, IRequestCycle cycle) |
112 | { | |
113 | 568 | PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
114 | ||
115 | 568 | if (data == null) |
116 | 566 | return Collections.EMPTY_LIST; |
117 | ||
118 | 2 | return data.getPageChanges(); |
119 | } | |
120 | ||
121 | 1 | public void discardStoredChanges(String pageName, IRequestCycle cycle) |
122 | { | |
123 | 1 | _data.remove(pageName); |
124 | } | |
125 | ||
126 | 422 | public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle) |
127 | { | |
128 | 422 | Defense.notNull(encoding, "encoding"); |
129 | 422 | Defense.notNull(cycle, "cycle"); |
130 | ||
131 | 422 | Iterator i = _data.entrySet().iterator(); |
132 | 422 | while (i.hasNext()) |
133 | { | |
134 | 3 | Map.Entry e = (Map.Entry) i.next(); |
135 | ||
136 | 3 | String pageName = (String) e.getKey(); |
137 | 3 | PersistentPropertyData data = (PersistentPropertyData) e.getValue(); |
138 | ||
139 | 3 | ClientPropertyPersistenceScope scope = getScope(); |
140 | ||
141 | 3 | if (scope.addParametersForPersistentProperties(encoding, cycle, pageName, data)) |
142 | 2 | encoding.setParameterValue(PREFIX + pageName, data.getEncoded()); |
143 | } | |
144 | } | |
145 | ||
146 | 265 | public void setRequest(WebRequest request) |
147 | { | |
148 | 265 | _request = request; |
149 | } | |
150 | ||
151 | 3 | public ClientPropertyPersistenceScope getScope() { |
152 | 3 | return _scope; |
153 | } | |
154 | ||
155 | 264 | public void setScope(ClientPropertyPersistenceScope scope) { |
156 | 264 | _scope = scope; |
157 | } | |
158 | } |
|