Author: nbelaevski
Date: 2008-06-10 16:57:23 -0400 (Tue, 10 Jun 2008)
New Revision: 8988
Added:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java
Modified:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java
Log:
http://jira.jboss.com/jira/browse/RF-1923
Modified:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java
===================================================================
---
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java 2008-06-10
17:47:11 UTC (rev 8987)
+++
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java 2008-06-10
20:57:23 UTC (rev 8988)
@@ -21,10 +21,14 @@
package org.ajax4jsf.application;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
+import java.io.ObjectInputStream;
import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
@@ -36,6 +40,7 @@
import java.util.Set;
import java.util.Map.Entry;
+import javax.faces.FacesException;
import javax.faces.FactoryFinder;
import javax.faces.application.StateManager;
import javax.faces.component.UIComponent;
@@ -47,6 +52,7 @@
import javax.faces.render.ResponseStateManager;
import org.ajax4jsf.context.AjaxContext;
+import org.ajax4jsf.context.ContextInitParameters;
import org.ajax4jsf.event.AjaxPhaseListener;
import org.ajax4jsf.util.LRUMap;
import org.apache.commons.logging.Log;
@@ -225,10 +231,10 @@
UIViewRoot viewRoot = null;
ResponseStateManager responseStateManager = getRenderKit(context,
renderKitId).getResponseStateManager();
- TreeStrutureNode treeStructure = null;
+ TreeStructureNode treeStructure = null;
Object[] state = null;
if (isSavingStateInClient(context)) {
- treeStructure = (TreeStrutureNode) responseStateManager
+ treeStructure = (TreeStructureNode) responseStateManager
.getTreeStructureToRestore(context, viewId);
// viewRoot = parent.restoreView(context, viewId, renderKitId);
state = (Object[]) responseStateManager
@@ -237,8 +243,8 @@
Object[] serializedView = restoreStateFromSession(context, viewId,
renderKitId);
if (null != serializedView) {
- treeStructure = (TreeStrutureNode) serializedView[0];
- state = (Object[]) serializedView[1];
+ treeStructure = (TreeStructureNode) serializedView[0];
+ state = (Object[]) handleRestoreState(context, serializedView[1]);
}
}
if (null != treeStructure) {
@@ -252,6 +258,49 @@
}
+ private static final Object handleRestoreState(FacesContext context, Object state) {
+ if (ContextInitParameters.isSerializeServerState(context)) {
+ ObjectInputStream ois = null;
+ try {
+ ois = new ObjectInputStream(new ByteArrayInputStream((byte[]) state));
+ return ois.readObject();
+ } catch (Exception e) {
+ throw new FacesException(e);
+ } finally {
+ if (ois != null) {
+ try {
+ ois.close();
+ } catch (IOException ignored) { }
+ }
+ }
+ } else {
+ return state;
+ }
+ }
+
+ private static final Object handleSaveState(FacesContext context, Object state) {
+ if (ContextInitParameters.isSerializeServerState(context)) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+ ObjectOutputStream oas = null;
+ try {
+ oas = new ObjectOutputStream(baos);
+ oas.writeObject(state);
+ oas.flush();
+ } catch (Exception e) {
+ throw new FacesException(e);
+ } finally {
+ if (oas != null) {
+ try {
+ oas.close();
+ } catch (IOException ignored) { }
+ }
+ }
+ return baos.toByteArray();
+ } else {
+ return state;
+ }
+ }
+
protected Object[] restoreStateFromSession(FacesContext context,
String viewId, String renderKitId) {
Object[] restoredState = null;
@@ -331,7 +380,7 @@
SerializedView serializedView = null;
UIViewRoot viewRoot = context.getViewRoot();
if (null != viewRoot && (!viewRoot.isTransient()) ) {
- TreeStrutureNode treeStructure = new TreeStrutureNode();
+ TreeStructureNode treeStructure = new TreeStructureNode();
treeStructure.apply(context, viewRoot, new HashSet());
Object treeState = viewRoot.processSaveState(context);
Object state[] = { treeState, getAdditionalState(context) };
@@ -339,7 +388,7 @@
serializedView = new SerializedView(treeStructure, state);
} else {
serializedView = saveStateInSession(context, treeStructure,
- state);
+ handleSaveState(context, state));
}
}
@@ -456,7 +505,7 @@
}
- protected static final class TreeStrutureNode implements Externalizable {
+ protected static final class TreeStructureNode implements Externalizable {
/**
* TODO - implement Externalizable to reduce serialized state.
*/
@@ -472,7 +521,7 @@
private String id;
- public TreeStrutureNode() {
+ public TreeStructureNode() {
}
public void apply(FacesContext context, UIComponent component,
@@ -490,7 +539,7 @@
Entry element = (Entry) i.next();
UIComponent f = (UIComponent) element.getValue();
if (!f.isTransient()) {
- TreeStrutureNode facet = new TreeStrutureNode();
+ TreeStructureNode facet = new TreeStructureNode();
facet.apply(context, f, uniqueIds);
if (null == facets) {
facets = new HashMap();
@@ -502,7 +551,7 @@
for (Iterator i = component.getChildren().iterator(); i.hasNext();) {
UIComponent child = (UIComponent) i.next();
if (!child.isTransient()) {
- TreeStrutureNode t = new TreeStrutureNode();
+ TreeStructureNode t = new TreeStructureNode();
t.apply(context, child, uniqueIds);
if (null == children) {
children = new ArrayList();
@@ -520,7 +569,7 @@
if (null != facets) {
for (Iterator i = facets.entrySet().iterator(); i.hasNext();) {
Entry element = (Entry) i.next();
- UIComponent facet = ((TreeStrutureNode) element.getValue())
+ UIComponent facet = ((TreeStructureNode) element.getValue())
.restore(loader);
component.getFacets().put(element.getKey(), facet);
}
@@ -528,7 +577,7 @@
}
if (null != children) {
for (Iterator i = children.iterator(); i.hasNext();) {
- TreeStrutureNode node = (TreeStrutureNode) i.next();
+ TreeStructureNode node = (TreeStructureNode) i.next();
UIComponent child = node.restore(loader);
component.getChildren().add(child);
}
@@ -609,7 +658,7 @@
facets = new HashMap(facetsSize);
for (int i = 0; i < facetsSize; i++) {
String facetName = in.readUTF();
- TreeStrutureNode facet = new TreeStrutureNode();
+ TreeStructureNode facet = new TreeStructureNode();
facet.readExternal(in);
facets.put(facetName, facet);
}
@@ -618,7 +667,7 @@
if (childrenSize > 0) {
children = new ArrayList(childrenSize);
for (int i = 0; i < childrenSize; i++) {
- TreeStrutureNode child = new TreeStrutureNode();
+ TreeStructureNode child = new TreeStructureNode();
child.readExternal(in);
children.add(child);
}
@@ -633,7 +682,7 @@
for (Iterator i = facets.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
out.writeUTF((String) entry.getKey());
- TreeStrutureNode node = (TreeStrutureNode) entry.getValue();
+ TreeStructureNode node = (TreeStructureNode) entry.getValue();
node.writeExternal(out);
}
@@ -643,7 +692,7 @@
if (null != children) {
out.writeInt(children.size());
for (Iterator i = children.iterator(); i.hasNext();) {
- TreeStrutureNode child = (TreeStrutureNode) i.next();
+ TreeStructureNode child = (TreeStructureNode) i.next();
child.writeExternal(out);
}
Added:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java
===================================================================
---
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java
(rev 0)
+++
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/context/ContextInitParameters.java 2008-06-10
20:57:23 UTC (rev 8988)
@@ -0,0 +1,90 @@
+/**
+ *
+ */
+package org.ajax4jsf.context;
+
+import javax.faces.FacesException;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+
+/**
+ * This class hold all methods for get application init parameters. Created for
+ * single access point to all parameters - simplest for a documentation.
+ *
+ * @author asmirnov
+ *
+ */
+public class ContextInitParameters {
+
+ /**
+ *
+ */
+ private ContextInitParameters() {
+ // this is a only static methods for a access to Web app Init
+ // parameters. Do not Instantiate !
+ }
+
+ public static final String[] SERIALIZE_SERVER_STATE = new String[] {
+ "org.ajax4jsf.SERIALIZE_SERVER_STATE",
+
+ /* detect MyFaces vs. RI */
+ "com.sun.faces.serializeServerState",
+ "org.apache.myfaces.SERIALIZE_STATE_IN_SESSION"
+ };
+
+ public static final boolean isSerializeServerState(FacesContext context) {
+ return getBoolean(context, SERIALIZE_SERVER_STATE, false);
+ }
+
+ static int getInteger(FacesContext context, String[] paramNames,
+ int defaulValue) {
+ String initParameter = getInitParameter(context,paramNames);
+ if (null == initParameter) {
+ return defaulValue;
+ } else {
+ try {
+ return Integer.parseInt(initParameter);
+
+ } catch (NumberFormatException e) {
+ throw new FacesException("Context parameter " + paramNames
+ + " must have integer value");
+ }
+ }
+ }
+
+ static String getString(FacesContext context, String[] paramNames,
+ String defaulValue) {
+ String initParameter = getInitParameter(context,paramNames);
+ if (null == initParameter) {
+ return defaulValue;
+ } else {
+ return initParameter;
+ }
+ }
+
+ static boolean getBoolean(FacesContext context, String[] paramNames,
+ boolean defaulValue) {
+ String initParameter = getInitParameter(context,paramNames);
+ if (null == initParameter) {
+ return defaulValue;
+ } else if("true".equalsIgnoreCase(initParameter) ||
"yes".equalsIgnoreCase(initParameter)) {
+ return true;
+ } else if("false".equalsIgnoreCase(initParameter) ||
"no".equalsIgnoreCase(initParameter)) {
+ return false;
+ } else {
+ throw new FacesException("Illegal value ["+initParameter+"] for a init
parameter +"+paramNames+", only logical values 'true' or 'false'
is allowed");
+ }
+ }
+
+ static String getInitParameter(FacesContext context,
+ String[] paramNames) {
+ ExternalContext externalContext = context.getExternalContext();
+ String value = null;
+ for (int i = 0; i < paramNames.length && null == value; i++) {
+ value = externalContext.getInitParameter(paramNames[i]);
+ }
+ return value;
+ }
+
+
+}
\ No newline at end of file