001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender;
031:
032: import java.io.Serializable;
033:
034: import javax.servlet.http.HttpSession;
035: import javax.servlet.http.HttpSessionActivationListener;
036: import javax.servlet.http.HttpSessionBindingEvent;
037: import javax.servlet.http.HttpSessionBindingListener;
038: import javax.servlet.http.HttpSessionEvent;
039:
040: /**
041: * An abstract base class representing a single user-instance of an application
042: * hosted in an application container.
043: */
044: public abstract class UserInstance implements
045: HttpSessionActivationListener, HttpSessionBindingListener,
046: Serializable {
047:
048: public static final String PROPERTY_CLIENT_CONFIGURATION = "clientConfiguration";
049: public static final String PROPERTY_SERVER_DELAY_MESSAGE = "serverDelayMessage";
050:
051: /**
052: * The default character encoding in which responses should be rendered.
053: */
054: private String characterEncoding = "UTF-8";
055:
056: /**
057: * <code>ClientConfiguration</code> information containing
058: * application-specific client behavior settings.
059: */
060: private ClientConfiguration clientConfiguration;
061:
062: /**
063: * The <code>ServerDelayMessage</code> displayed during
064: * client/server-interactions.
065: */
066: private ServerDelayMessage serverDelayMessage;
067:
068: /**
069: * A <code>ClientProperties</code> object describing the web browser
070: * client.
071: */
072: private ClientProperties clientProperties;
073:
074: /**
075: * The URI of the servlet.
076: */
077: private String servletUri;
078:
079: /**
080: * Reference to the <code>HttpSession</code> in which this
081: * <code>UserInstance</code> is stored.
082: */
083: private transient HttpSession session;
084:
085: /**
086: * Provides information about updated <code>UserInstance</code> properties.
087: */
088: private UserInstanceUpdateManager updateManager;
089:
090: /**
091: * The current transactionId. Used to ensure incoming ClientMessages reflect
092: * changes made by user against current server-side state of user interface.
093: * This is used to eliminate issues that could be encountered with two
094: * browser windows pointing at the same application instance.
095: */
096: private long transactionId = 0;
097:
098: /**
099: * Creates a new <code>UserInstance</code>.
100: *
101: * @param conn the client/server <code>Connection</code> for which the
102: * instance is being instantiated
103: */
104: public UserInstance(Connection conn) {
105: super ();
106: updateManager = new UserInstanceUpdateManager();
107: conn.initUserInstance(this );
108: }
109:
110: /**
111: * Returns the default character encoding in which responses should be
112: * rendered.
113: *
114: * @return the default character encoding in which responses should be
115: * rendered
116: */
117: public String getCharacterEncoding() {
118: return characterEncoding;
119: }
120:
121: /**the <code>ServerDelayMessage</code> displayed during
122: * client/server-interactions.
123: * Retrieves the <code>ClientConfiguration</code> information containing
124: * application-specific client behavior settings.
125: *
126: * @return the relevant <code>ClientProperties</code>
127: */
128: public ClientConfiguration getClientConfiguration() {
129: return clientConfiguration;
130: }
131:
132: /**
133: * Retrieves the <code>ClientProperties</code> object providing
134: * information about the client of this instance.
135: *
136: * @return the relevant <code>ClientProperties</code>
137: */
138: public ClientProperties getClientProperties() {
139: return clientProperties;
140: }
141:
142: /**
143: * Returns the current transaction id.
144: *
145: * @return the current transaction id
146: */
147: public long getCurrentTransactionId() {
148: return transactionId;
149: }
150:
151: /**
152: * Increments the current transaction id and returns it.
153: *
154: * @return the current transaction id, after an increment
155: */
156: public long getNextTransactionId() {
157: ++transactionId;
158: return transactionId;
159: }
160:
161: /**
162: * Retrieves the <code>ServerDelayMessage</code> displayed during
163: * client/server-interactions.
164: *
165: * @return the <code>ServerDelayMessage</code>
166: */
167: public ServerDelayMessage getServerDelayMessage() {
168: return serverDelayMessage;
169: }
170:
171: /**
172: * Determines the URI to invoke the specified <code>Service</code>.
173: *
174: * @param service the <code>Service</code>
175: * @return the URI
176: */
177: public String getServiceUri(Service service) {
178: return servletUri + "?serviceId=" + service.getId();
179: }
180:
181: /**
182: * Determines the URI to invoke the specified <code>Service</code> with
183: * additional request parameters. The additional parameters are provided by
184: * way of the <code>parameterNames</code> and <code>parameterValues</code>
185: * arrays. The value of a parameter at a specific index in the
186: * <code>parameterNames</code> array is provided in the
187: * <code>parameterValues</code> array at the same index. The arrays must
188: * thus be of equal length. Null values are allowed in the
189: * <code>parameterValues</code> array, and in such cases only the parameter
190: * name will be rendered in the returned URI.
191: *
192: * @param service the <code>Service</code>
193: * @param parameterNames the names of the additional URI parameters
194: * @param parameterValues the values of the additional URI parameters
195: * @return the URI
196: */
197: public String getServiceUri(Service service,
198: String[] parameterNames, String[] parameterValues) {
199: StringBuffer out = new StringBuffer(servletUri);
200: out.append("?serviceId=");
201: out.append(service.getId());
202: for (int i = 0; i < parameterNames.length; ++i) {
203: out.append("&");
204: out.append(parameterNames[i]);
205: if (parameterValues[i] != null) {
206: out.append("=");
207: out.append(parameterValues[i]);
208: }
209: }
210: return out.toString();
211: }
212:
213: /**
214: * Returns the URI of the servlet managing this <code>UserInstance</code>.
215: *
216: * @return the URI
217: */
218: public String getServletUri() {
219: return servletUri;
220: }
221:
222: /**
223: * Returns the <code>UserInstanceUpdateManager</code> providing information
224: * about updated <code>UserInstance</code> properties.
225: *
226: * @return the <code>UserInstanceUpdateManager</code>
227: */
228: public UserInstanceUpdateManager getUserInstanceUpdateManager() {
229: return updateManager;
230: }
231:
232: /**
233: * Returns the <code>HttpSession</code> containing this
234: * <code>UserInstance</code>.
235: *
236: * @return the <code>HttpSession</code>
237: */
238: public HttpSession getSession() {
239: return session;
240: }
241:
242: /**
243: * @see javax.servlet.http.HttpSessionActivationListener#sessionDidActivate(javax.servlet.http.HttpSessionEvent)
244: */
245: public void sessionDidActivate(HttpSessionEvent e) {
246: session = e.getSession();
247: }
248:
249: /**
250: * @see javax.servlet.http.HttpSessionActivationListener#sessionWillPassivate(javax.servlet.http.HttpSessionEvent)
251: */
252: public void sessionWillPassivate(HttpSessionEvent e) {
253: session = null;
254: }
255:
256: /**
257: * Sets the <code>ClientConfiguration</code> information containing
258: * application-specific client behavior settings.
259: *
260: * @param clientConfiguration the new <code>ClientConfiguration</code>
261: */
262: public void setClientConfiguration(
263: ClientConfiguration clientConfiguration) {
264: this .clientConfiguration = clientConfiguration;
265: updateManager
266: .processPropertyUpdate(PROPERTY_CLIENT_CONFIGURATION);
267: }
268:
269: /**
270: * Stores the <code>ClientProperties</code> object that provides
271: * information about the client of this instance.
272: *
273: * @param clientProperties the relevant <code>ClientProperties</code>
274: */
275: void setClientProperties(ClientProperties clientProperties) {
276: this .clientProperties = clientProperties;
277: }
278:
279: /**
280: * Sets the <code>ServerDelayMessage</code> displayed during
281: * client/server-interactions.
282: *
283: * @param serverDelayMessage the new <code>ServerDelayMessage</code>
284: */
285: public void setServerDelayMessage(
286: ServerDelayMessage serverDelayMessage) {
287: this .serverDelayMessage = serverDelayMessage;
288: updateManager
289: .processPropertyUpdate(PROPERTY_SERVER_DELAY_MESSAGE);
290: }
291:
292: /**
293: * Sets the URI of the servlet managing this <code>UserInstance</code>.
294: *
295: * @param servletUri the URI
296: */
297: void setServletUri(String servletUri) {
298: this .servletUri = servletUri;
299: }
300:
301: /**
302: * Listener implementation of <code>HttpSessionBindingListener</code>.
303: * Stores reference to session when invoked.
304: *
305: * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
306: */
307: public void valueBound(HttpSessionBindingEvent e) {
308: session = e.getSession();
309: }
310:
311: /**
312: * Listener implementation of <code>HttpSessionBindingListener</code>.
313: * Removes reference to session when invoked.
314: *
315: * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
316: */
317: public void valueUnbound(HttpSessionBindingEvent e) {
318: session = null;
319: }
320: }
|