KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
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 JavaDoc _applicationId;
43
44     private WebRequest _request;
45
46     public void store(String JavaDoc pageName, String JavaDoc idPath, String JavaDoc propertyName, Object JavaDoc newValue)
47     {
48         Defense.notNull(pageName, "pageName");
49         Defense.notNull(propertyName, "propertyName");
50
51         WebSession session = _request.getSession(true);
52
53         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54
55         buffer.append(_applicationId);
56         buffer.append(",");
57         buffer.append(pageName);
58
59         if (idPath != null)
60         {
61             buffer.append(",");
62             buffer.append(idPath);
63         }
64
65         buffer.append(",");
66         buffer.append(propertyName);
67
68         String JavaDoc key = buffer.toString();
69
70         session.setAttribute(key, newValue);
71     }
72
73     public Collection JavaDoc getStoredChanges(String JavaDoc pageName, IRequestCycle cycle)
74     {
75         Defense.notNull(pageName, "pageName");
76
77         WebSession session = _request.getSession(false);
78
79         if (session == null)
80             return Collections.EMPTY_LIST;
81
82         Collection JavaDoc result = new ArrayList JavaDoc();
83
84         String JavaDoc prefix = _applicationId + "," + pageName + ",";
85
86         Iterator JavaDoc i = session.getAttributeNames().iterator();
87         while (i.hasNext())
88         {
89             String JavaDoc key = (String JavaDoc) i.next();
90
91             if (key.startsWith(prefix))
92             {
93                 PropertyChange change = buildChange(key, session.getAttribute(key));
94
95                 result.add(change);
96             }
97         }
98
99         return result;
100     }
101
102     private PropertyChange buildChange(String JavaDoc key, Object JavaDoc value)
103     {
104         String JavaDoc[] tokens = TapestryUtils.split(key);
105
106         // Either app-name,page-name,id-path,property
107
// or app-name,page-name,property
108

109         String JavaDoc idPath = (tokens.length == 4) ? tokens[2] : null;
110         String JavaDoc propertyName = tokens[tokens.length - 1];
111
112         return new PropertyChangeImpl(idPath, propertyName, value);
113     }
114
115     public void discardStoredChanges(String JavaDoc pageName, IRequestCycle cycle)
116     {
117         Defense.notNull(pageName, "pageName");
118
119         WebSession session = _request.getSession(false);
120
121         if (session == null)
122             return;
123
124         String JavaDoc prefix = _applicationId + "," + pageName + ",";
125
126         Iterator JavaDoc i = session.getAttributeNames().iterator();
127         while (i.hasNext())
128         {
129             String JavaDoc key = (String JavaDoc) i.next();
130
131             if (key.startsWith(prefix))
132                 session.setAttribute(key, null);
133         }
134     }
135
136     /**
137      * Does nothing; session persistence does not make use of query parameters.
138      */

139
140     public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle)
141     {
142     }
143
144     public void setApplicationId(String JavaDoc applicationName)
145     {
146         _applicationId = applicationName;
147     }
148
149     public void setRequest(WebRequest request)
150     {
151         _request = request;
152     }
153 }
Popular Tags