Author: craigmcc
Date: Wed Jan 24 18:48:23 2007
New Revision: 499653
URL: http://svn.apache.org/viewvc?view=rev&rev=499653
Log:
First crack at a DialogContextListener implementation for saving and restoring
arbitrary state information (accessed via value binding expressions) across
HTTP requests. See the class level javadocs for usage info.
SHALE-399
Added:
shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java
(with props)
Added:
shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java?view=auto&rev=499653
==============================================================================
---
shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java
(added)
+++
shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java
Wed Jan 24 18:48:23 2007
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.dialog.base;
+
+import java.io.Serializable;
+import javax.faces.application.Application;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+import org.apache.shale.dialog.DialogContext;
+import org.apache.shale.dialog.DialogContextListener;
+
+/**
+ * <p>Convenience [EMAIL PROTECTED] DialogContextListener} implementation that
supports
+ * saving and restoring pre-request state information that is accessed via
+ * a configurable set of value binding expressions. Whenever the associated
+ * [EMAIL PROTECTED] DialogContext} is passivated (such as at the end of a
request), the
+ * <code>getValue()</code> method will be called on each value binding, to
+ * save away the retrieved information internally to this instance. Later,
+ * when a POST causes our [EMAIL PROTECTED] DialogContext} instance to be
activated again,
+ * <code>setValue()</code> will be called on each value binding, to restore the
+ * saved data to its original location.</p>
+ *
+ * <p>The most common use case for this class will be as the base class for
+ * a class that is registered as the <code>data</code> class for a particular
+ * dialog. When used in this manner:</p>
+ * <ul>
+ * <li>Whenever a new [EMAIL PROTECTED] DialogContext} instance is created, a
new instance
+ * of the registered class will also be instantiated, and assigned to the
+ * <code>data</code> property of the [EMAIL PROTECTED] DialogContext}
instance.</li>
+ * <li>Because this class implements [EMAIL PROTECTED] DialogContextListener},
it will
+ * automatically be registered as a listener on the [EMAIL PROTECTED]
DialogContext}
+ * instance, causing our <code>onActivate()</code> and
<code>onPassivate</code>
+ * methods to be called at the appropriate times.</li>
+ * <li>The default implementation of these event handlers will save and restore
+ * state infomation specified by the value binding expressions set on the
+ * <code>expressions</code> property, as described above.</li>
+ * </ul>
+ *
+ * <p>So, if you wish to use this mechanism to save and restore the
+ * <code>foo</code> and <code>bar</code> request scoped variables, create a
+ * subclass like this:</p>
+ * <pre>
+ * public class MyStateSavingListener extends StateSavingListener {
+ *
+ * public MyStateSavingListener() {
+ * setExpressions(new String[] { "#{foo}", "#{bar}" });
+ * }
+ *
+ * }
+ * </pre>
+ *
+ * <p>Next, cause this class to be registered as the standard <code>data</code>
+ * class in the configuraiton for this dialog. The details will vary depending
+ * on which dialog implementation you are using. Now, the specified state
+ * information will be saved and restored for you.</p>
+ *
+ * <p><strong>IMPLEMENTATION NOTE</strong> - Because this instance will
+ * be stored in session scope, all of the data saved via the configured
+ * value binding expressions is expected to be serializable.</p>
+ *
+ * @since 1.1.0
+ */
+public class StateSavingListener extends AbstractDialogContextListener
+ implements Serializable {
+
+
+ //---------------------------------------------------------------
Properties
+
+
+ /**
+ * <p>Array of value binding expression strings that identify the state
+ * information we need to save and restore.</p>
+ */
+ private String[] expressions = new String[0];
+
+
+ /**
+ * <p>The saved values corresponding to the specified expressions.</p>
+ */
+ private Object[] values = new Object[0];
+
+
+ /**
+ * <p>Return an array of value binding expressions that identify state
+ * information to be saved and restored.</p>
+ */
+ public String[] getExpressions() {
+
+ return this.expressions;
+
+ }
+
+
+ /**
+ * <p>Set the array of value binding expressions that identify state
+ * information to be saved and restored.</p>
+ *
+ * @param expressions The new expressions
+ */
+ public void setExpressions(String[] expressions) {
+
+ if (expressions == null) {
+ expressions = new String[0];
+ }
+ this.expressions = expressions;
+ this.values = new Object[this.expressions.length];
+
+ }
+
+
+ //-------------------------------------------- DialogContextListener
Methods
+
+
+ /**
+ * <p>Restore the values specified by our configured expressions.</p>
+ */
+ public void onActivate() {
+
+ if (expressions.length < 1) {
+ return;
+ }
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application application = context.getApplication();
+ ValueBinding vb = null;
+ for (int i = 0; i < expressions.length; i++) {
+ vb = application.createValueBinding(expressions[i]);
+ vb.setValue(context, values[i]);
+ values[i] = null;
+ }
+
+ }
+
+
+ /**
+ * <p>Save the values specified by our configured expressions.</p>
+ */
+ public void onPassivate() {
+
+ if (expressions.length < 1) {
+ return;
+ }
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application application = context.getApplication();
+ ValueBinding vb = null;
+ for (int i = 0; i < expressions.length; i++) {
+ vb = application.createValueBinding(expressions[i]);
+ values[i] = vb.getValue(context);
+ }
+
+ }
+
+
+}
Propchange:
shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-dialog/src/main/java/org/apache/shale/dialog/base/StateSavingListener.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL