KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > record > ClientPropertyPersistenceStrategy


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 JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc PREFIX = "state:";
46
47     /**
48      * Keyed on page name (String), values are
49      * {@link org.apache.tapestry.record.PersistentPropertyData}.
50      */

51     private final Map JavaDoc _data = new HashMap JavaDoc();
52
53     private final PersistentPropertyDataEncoder _encoder;
54
55     private WebRequest _request;
56     
57     private ClientPropertyPersistenceScope _scope;
58
59     public ClientPropertyPersistenceStrategy()
60     {
61         this(new PersistentPropertyDataEncoderImpl());
62     }
63
64     // Alternate constructor used for testing
65
ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder)
66     {
67         _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     public void initializeService()
79     {
80         List JavaDoc names = _request.getParameterNames();
81         Iterator JavaDoc i = names.iterator();
82         while (i.hasNext())
83         {
84             String JavaDoc name = (String JavaDoc) i.next();
85
86             if (!name.startsWith(PREFIX))
87                 continue;
88
89             String JavaDoc pageName = name.substring(PREFIX.length());
90             String JavaDoc encoded = _request.getParameterValue(name);
91
92             PersistentPropertyData data = new PersistentPropertyData(_encoder);
93             data.storeEncoded(encoded);
94
95             _data.put(pageName, data);
96         }
97     }
98
99     public void store(String JavaDoc pageName, String JavaDoc idPath, String JavaDoc propertyName, Object JavaDoc newValue)
100     {
101         PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
102         if (data == null)
103         {
104             data = new PersistentPropertyData(_encoder);
105             _data.put(pageName, data);
106         }
107
108         data.store(idPath, propertyName, newValue);
109     }
110
111     public Collection JavaDoc getStoredChanges(String JavaDoc pageName, IRequestCycle cycle)
112     {
113         PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
114
115         if (data == null)
116             return Collections.EMPTY_LIST;
117
118         return data.getPageChanges();
119     }
120
121     public void discardStoredChanges(String JavaDoc pageName, IRequestCycle cycle)
122     {
123         _data.remove(pageName);
124     }
125
126     public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle)
127     {
128         Defense.notNull(encoding, "encoding");
129         Defense.notNull(cycle, "cycle");
130
131         Iterator JavaDoc i = _data.entrySet().iterator();
132         while (i.hasNext())
133         {
134             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
135
136             String JavaDoc pageName = (String JavaDoc) e.getKey();
137             PersistentPropertyData data = (PersistentPropertyData) e.getValue();
138             
139             ClientPropertyPersistenceScope scope = getScope();
140
141             if (scope.addParametersForPersistentProperties(encoding, cycle, pageName, data))
142                 encoding.setParameterValue(PREFIX + pageName, data.getEncoded());
143         }
144     }
145
146     public void setRequest(WebRequest request)
147     {
148         _request = request;
149     }
150
151     public ClientPropertyPersistenceScope getScope() {
152         return _scope;
153     }
154
155     public void setScope(ClientPropertyPersistenceScope scope) {
156         _scope = scope;
157     }
158 }
Popular Tags