KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > UserInstance


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender;
31
32 import java.io.Serializable JavaDoc;
33
34 import javax.servlet.http.HttpSession JavaDoc;
35 import javax.servlet.http.HttpSessionActivationListener JavaDoc;
36 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
37 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
38 import javax.servlet.http.HttpSessionEvent JavaDoc;
39
40 /**
41  * An abstract base class representing a single user-instance of an application
42  * hosted in an application container.
43  */

44 public abstract class UserInstance
45 implements HttpSessionActivationListener JavaDoc, HttpSessionBindingListener JavaDoc, Serializable JavaDoc {
46     
47     public static final String JavaDoc PROPERTY_CLIENT_CONFIGURATION = "clientConfiguration";
48     public static final String JavaDoc PROPERTY_SERVER_DELAY_MESSAGE = "serverDelayMessage";
49
50     /**
51      * The default character encoding in which responses should be rendered.
52      */

53     private String JavaDoc characterEncoding = "UTF-8";
54     
55     /**
56      * <code>ClientConfiguration</code> information containing
57      * application-specific client behavior settings.
58      */

59     private ClientConfiguration clientConfiguration;
60
61     /**
62      * The <code>ServerDelayMessage</code> displayed during
63      * client/server-interactions.
64      */

65     private ServerDelayMessage serverDelayMessage;
66     
67     /**
68      * A <code>ClientProperties</code> object describing the web browser
69      * client.
70      */

71     private ClientProperties clientProperties;
72     
73     /**
74      * The URI of the servlet.
75      */

76     private String JavaDoc servletUri;
77     
78     /**
79      * Reference to the <code>HttpSession</code> in which this
80      * <code>UserInstance</code> is stored.
81      */

82     private transient HttpSession JavaDoc session;
83     
84     /**
85      * Provides information about updated <code>UserInstance</code> properties.
86      */

87     private UserInstanceUpdateManager updateManager;
88
89     /**
90      * The current transactionId. Used to ensure incoming ClientMessages reflect
91      * changes made by user against current server-side state of user interface.
92      * This is used to eliminate issues that could be encountered with two
93      * browser windows pointing at the same application instance.
94      */

95     private long transactionId = 0;
96     
97     /**
98      * Creates a new <code>UserInstance</code>.
99      *
100      * @param conn the client/server <code>Connection</code> for which the
101      * instance is being instantiated
102      */

103     public UserInstance(Connection conn) {
104         super();
105         updateManager = new UserInstanceUpdateManager();
106         conn.initUserInstance(this);
107     }
108
109     /**
110      * Returns the default character encoding in which responses should be
111      * rendered.
112      *
113      * @return the default character encoding in which responses should be
114      * rendered
115      */

116     public String JavaDoc getCharacterEncoding() {
117         return characterEncoding;
118     }
119     
120     /**the <code>ServerDelayMessage</code> displayed during
121      * client/server-interactions.
122      * Retrieves the <code>ClientConfiguration</code> information containing
123      * application-specific client behavior settings.
124      *
125      * @return the relevant <code>ClientProperties</code>
126      */

127     public ClientConfiguration getClientConfiguration() {
128         return clientConfiguration;
129     }
130     
131     /**
132      * Retrieves the <code>ClientProperties</code> object providing
133      * information about the client of this instance.
134      *
135      * @return the relevant <code>ClientProperties</code>
136      */

137     public ClientProperties getClientProperties() {
138         return clientProperties;
139     }
140     
141     /**
142      * Returns the current transaction id.
143      *
144      * @return the current transaction id
145      */

146     public long getCurrentTransactionId() {
147         return transactionId;
148     }
149     
150     /**
151      * Increments the current transaction id and returns it.
152      *
153      * @return the current transaction id, after an increment
154      */

155     public long getNextTransactionId() {
156         ++transactionId;
157         return transactionId;
158     }
159
160     /**
161      * Retrieves the <code>ServerDelayMessage</code> displayed during
162      * client/server-interactions.
163      *
164      * @return the <code>ServerDelayMessage</code>
165      */

166     public ServerDelayMessage getServerDelayMessage() {
167         return serverDelayMessage;
168     }
169     
170     /**
171      * Determines the URI to invoke the specified <code>Service</code>.
172      *
173      * @param service the <code>Service</code>
174      * @return the URI
175      */

176     public String JavaDoc getServiceUri(Service service) {
177         return servletUri + "?serviceId=" + service.getId();
178     }
179
180     /**
181      * Determines the URI to invoke the specified <code>Service</code> with
182      * additional request parameters. The additional parameters are provided by
183      * way of the <code>parameterNames</code> and <code>parameterValues</code>
184      * arrays. The value of a parameter at a specific index in the
185      * <code>parameterNames</code> array is provided in the
186      * <code>parameterValues</code> array at the same index. The arrays must
187      * thus be of equal length. Null values are allowed in the
188      * <code>parameterValues</code> array, and in such cases only the parameter
189      * name will be rendered in the returned URI.
190      *
191      * @param service the <code>Service</code>
192      * @param parameterNames the names of the additional URI parameters
193      * @param parameterValues the values of the additional URI parameters
194      * @return the URI
195      */

196     public String JavaDoc getServiceUri(Service service, String JavaDoc[] parameterNames, String JavaDoc[] parameterValues) {
197         StringBuffer JavaDoc out = new StringBuffer JavaDoc(servletUri);
198         out.append("?serviceId=");
199         out.append(service.getId());
200         for (int i = 0; i < parameterNames.length; ++i) {
201             out.append("&");
202             out.append(parameterNames[i]);
203             if (parameterValues[i] != null) {
204                 out.append("=");
205                 out.append(parameterValues[i]);
206             }
207         }
208         return out.toString();
209     }
210
211     /**
212      * Returns the URI of the servlet managing this <code>UserInstance</code>.
213      *
214      * @return the URI
215      */

216     public String JavaDoc getServletUri() {
217         return servletUri;
218     }
219
220     /**
221      * Returns the <code>UserInstanceUpdateManager</code> providing information
222      * about updated <code>UserInstance</code> properties.
223      *
224      * @return the <code>UserInstanceUpdateManager</code>
225      */

226     public UserInstanceUpdateManager getUserInstanceUpdateManager() {
227         return updateManager;
228     }
229     
230     /**
231      * Returns the <code>HttpSession</code> containing this
232      * <code>UserInstance</code>.
233      *
234      * @return the <code>HttpSession</code>
235      */

236     public HttpSession JavaDoc getSession() {
237         return session;
238     }
239     
240     /**
241      * @see javax.servlet.http.HttpSessionActivationListener#sessionDidActivate(javax.servlet.http.HttpSessionEvent)
242      */

243     public void sessionDidActivate(HttpSessionEvent JavaDoc e) {
244         session = e.getSession();
245     }
246
247     /**
248      * @see javax.servlet.http.HttpSessionActivationListener#sessionWillPassivate(javax.servlet.http.HttpSessionEvent)
249      */

250     public void sessionWillPassivate(HttpSessionEvent JavaDoc e) {
251         session = null;
252     }
253
254     /**
255      * Sets the <code>ClientConfiguration</code> information containing
256      * application-specific client behavior settings.
257      *
258      * @param clientConfiguration the new <code>ClientConfiguration</code>
259      */

260     public void setClientConfiguration(ClientConfiguration clientConfiguration) {
261         this.clientConfiguration = clientConfiguration;
262         updateManager.processPropertyUpdate(PROPERTY_CLIENT_CONFIGURATION);
263     }
264     
265    /**
266      * Stores the <code>ClientProperties</code> object that provides
267      * information about the client of this instance.
268      *
269      * @param clientProperties the relevant <code>ClientProperties</code>
270      */

271     void setClientProperties(ClientProperties clientProperties) {
272         this.clientProperties = clientProperties;
273     }
274     
275     /**
276      * Sets the <code>ServerDelayMessage</code> displayed during
277      * client/server-interactions.
278      *
279      * @param serverDelayMessage the new <code>ServerDelayMessage</code>
280      */

281     public void setServerDelayMessage(ServerDelayMessage serverDelayMessage) {
282         this.serverDelayMessage = serverDelayMessage;
283         updateManager.processPropertyUpdate(PROPERTY_SERVER_DELAY_MESSAGE);
284     }
285
286     /**
287      * Sets the URI of the servlet managing this <code>UserInstance</code>.
288      *
289      * @param servletUri the URI
290      */

291     void setServletUri(String JavaDoc servletUri) {
292         this.servletUri = servletUri;
293     }
294
295     /**
296      * Listener implementation of <code>HttpSessionBindingListener</code>.
297      * Stores reference to session when invoked.
298      *
299      * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
300      */

301     public void valueBound(HttpSessionBindingEvent JavaDoc e) {
302         session = e.getSession();
303     }
304
305     /**
306      * Listener implementation of <code>HttpSessionBindingListener</code>.
307      * Removes reference to session when invoked.
308      *
309      * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
310      */

311     public void valueUnbound(HttpSessionBindingEvent JavaDoc e) {
312         session = null;
313     }
314 }
315
Popular Tags