dev@javaserverfaces.java.net

Re: [REVIEW] SerializedView Not Serializeable (Session)

From: Roger Kitain <Roger.Kitain_at_Sun.COM>
Date: Thu, 10 Nov 2005 15:31:28 -0500

No need to have decorator class public.

-roger

Roger Kitain wrote:

> This was identified externally. I've incorporated the patch for review.
>
> -roger
>
>------------------------------------------------------------------------
>
>
><< ADD DESCRIPTION HERE >>
>
>
>SECTION: Modified Files
>----------------------------
>M jsf-ri/src/com/sun/faces/application/StateManagerImpl.java
>M jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java
>
>
>SECTION: Diffs
>----------------------------
>Index: jsf-ri/src/com/sun/faces/application/StateManagerImpl.java
>===================================================================
>RCS file: /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/application/StateManagerImpl.java,v
>retrieving revision 1.24
>diff -u -r1.24 StateManagerImpl.java
>--- jsf-ri/src/com/sun/faces/application/StateManagerImpl.java 5 Aug 2004 20:02:19 -0000 1.24
>+++ jsf-ri/src/com/sun/faces/application/StateManagerImpl.java 10 Nov 2005 18:51:58 -0000
>@@ -25,6 +25,7 @@
> import javax.faces.context.FacesContext;
>
> import java.io.IOException;
>+import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.HashSet;
> import java.util.Iterator;
>@@ -132,7 +133,7 @@
> log.debug("End creating serialized view " +
> viewRoot.getViewId());
> }
>- sessionMap.put(viewRoot.getViewId(), result);
>+ sessionMap.put(viewRoot.getViewId(), new SerializedViewDecorator(result));
>
> } else {
> sessionMap.put(viewRoot.getViewId(), viewRoot);
>@@ -261,8 +262,12 @@
> log.debug("High available state saving option enabled");
> log.debug("Begin restoring serialized view for "+viewId);
> }
>- SerializedView serializedView = (SerializedView)
>- sessionMap.get(viewId);
>+ SerializedView serializedView = null;
>+ SerializedViewDecorator serializedViewDecorator =
>+ (SerializedViewDecorator) sessionMap.get(viewId);
>+ if (serializedViewDecorator != null) {
>+ serializedView = serializedViewDecorator.getSerializedView(context);
>+ }
> viewRoot = restoreSerializedView(context, serializedView,
> viewId);
> if (log.isDebugEnabled()) {
>@@ -535,5 +540,27 @@
> viewRoot.processRestoreState(context, state);
> }
> return ((UIViewRoot)viewRoot);
>+ }
>+
>+
>+ /**
>+ * Since StateManager.SerializedView does not implement Serializable, it is
>+ * decorated with this SerializedViewDecorator class before it is saved into
>+ * session.
>+ *
>+ * @author Markus Hjort
>+ */
>+ public static class SerializedViewDecorator implements Serializable {
>+ private Object structure;
>+ private Object state;
>+
>+ public SerializedViewDecorator(SerializedView originalView) {
>+ this.state = originalView.getState();
>+ this.structure = originalView.getStructure();
>+ }
>+
>+ public SerializedView getSerializedView(FacesContext context) {
>+ return context.getApplication().getStateManager().new SerializedView(structure, state);
>+ }
> }
> }
>Index: jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java
>===================================================================
>RCS file: /cvs/javaserverfaces-sources/jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java,v
>retrieving revision 1.1
>diff -u -r1.1 TestHAStateManagerImpl.java
>--- jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java 5 Aug 2004 20:02:20 -0000 1.1
>+++ jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java 10 Nov 2005 18:51:58 -0000
>@@ -31,6 +31,9 @@
> import javax.faces.FactoryFinder;
> import javax.faces.application.Application;
> import javax.faces.application.ApplicationFactory;
>+import java.io.ByteArrayOutputStream;
>+import java.io.IOException;
>+import java.io.ObjectOutputStream;
> import java.util.ArrayList;
>
>
>@@ -100,10 +103,23 @@
> getFacesContext().getApplication().getStateManager();
> stateManager.saveSerializedView(getFacesContext());
>
>+ // make sure the saved view stored in session is serializeable
>+ Object obj = getFacesContext().getExternalContext().getSessionMap().get(TEST_URI);
>+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
>+ try {
>+ ObjectOutputStream objStream = new ObjectOutputStream(outputStream);
>+ objStream.writeObject(obj);
>+ } catch (IOException e) {
>+ assertTrue(false);
>+ }
>+
>+
>+
> // make sure that the value of viewId attribute in session is an
> // instance of SerializedView.
> Object result = session.getAttribute(TEST_URI);
>- assertTrue(result instanceof SerializedView);
>+ assertTrue(result.getClass().getName().equals(
>+ "com.sun.faces.application.StateManagerImpl$SerializedViewDecorator"));
>
> root = stateManager.restoreView(getFacesContext(), TEST_URI,
> RenderKitFactory.HTML_BASIC_RENDER_KIT);
>
>
>
>
>
>------------------------------------------------------------------------
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
>For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
>
>




<< ADD DESCRIPTION HERE >>


SECTION: Modified Files
----------------------------
M jsf-ri/src/com/sun/faces/application/StateManagerImpl.java
M jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java


SECTION: Diffs
----------------------------
Index: jsf-ri/src/com/sun/faces/application/StateManagerImpl.java
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/application/StateManagerImpl.java,v
retrieving revision 1.24
diff -u -r1.24 StateManagerImpl.java
--- jsf-ri/src/com/sun/faces/application/StateManagerImpl.java 5 Aug 2004 20:02:19 -0000 1.24
+++ jsf-ri/src/com/sun/faces/application/StateManagerImpl.java 10 Nov 2005 20:20:01 -0000
@@ -25,6 +25,7 @@
 import javax.faces.context.FacesContext;
 
 import java.io.IOException;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -132,7 +133,7 @@
                         log.debug("End creating serialized view " +
                           viewRoot.getViewId());
                     }
- sessionMap.put(viewRoot.getViewId(), result);
+ sessionMap.put(viewRoot.getViewId(), new SerializedViewDecorator(result));
 
                 } else {
                     sessionMap.put(viewRoot.getViewId(), viewRoot);
@@ -261,8 +262,12 @@
                         log.debug("High available state saving option enabled");
                         log.debug("Begin restoring serialized view for "+viewId);
                     }
- SerializedView serializedView = (SerializedView)
- sessionMap.get(viewId);
+ SerializedView serializedView = null;
+ SerializedViewDecorator serializedViewDecorator =
+ (SerializedViewDecorator) sessionMap.get(viewId);
+ if (serializedViewDecorator != null) {
+ serializedView = serializedViewDecorator.getSerializedView(context);
+ }
                     viewRoot = restoreSerializedView(context, serializedView,
                         viewId);
                     if (log.isDebugEnabled()) {
@@ -535,5 +540,27 @@
              viewRoot.processRestoreState(context, state);
         }
         return ((UIViewRoot)viewRoot);
+ }
+
+
+ /**
+ * Since StateManager.SerializedView does not implement Serializable, it is
+ * decorated with this SerializedViewDecorator class before it is saved into
+ * session.
+ *
+ * @author Markus Hjort
+ */
+ private static class SerializedViewDecorator implements Serializable {
+ private Object structure;
+ private Object state;
+
+ public SerializedViewDecorator(SerializedView originalView) {
+ this.state = originalView.getState();
+ this.structure = originalView.getStructure();
+ }
+
+ public SerializedView getSerializedView(FacesContext context) {
+ return context.getApplication().getStateManager().new SerializedView(structure, state);
+ }
     }
 }
Index: jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java,v
retrieving revision 1.1
diff -u -r1.1 TestHAStateManagerImpl.java
--- jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java 5 Aug 2004 20:02:20 -0000 1.1
+++ jsf-ri/test/com/sun/faces/application/TestHAStateManagerImpl.java 10 Nov 2005 20:20:01 -0000
@@ -31,6 +31,9 @@
 import javax.faces.FactoryFinder;
 import javax.faces.application.Application;
 import javax.faces.application.ApplicationFactory;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 
 
@@ -100,10 +103,23 @@
             getFacesContext().getApplication().getStateManager();
         stateManager.saveSerializedView(getFacesContext());
 
+ // make sure the saved view stored in session is serializeable
+ Object obj = getFacesContext().getExternalContext().getSessionMap().get(TEST_URI);
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ try {
+ ObjectOutputStream objStream = new ObjectOutputStream(outputStream);
+ objStream.writeObject(obj);
+ } catch (IOException e) {
+ assertTrue(false);
+ }
+
+
+
         // make sure that the value of viewId attribute in session is an
         // instance of SerializedView.
         Object result = session.getAttribute(TEST_URI);
- assertTrue(result instanceof SerializedView);
+ assertTrue(result.getClass().getName().equals(
+ "com.sun.faces.application.StateManagerImpl$SerializedViewDecorator"));
         
         root = stateManager.restoreView(getFacesContext(), TEST_URI,
                                  RenderKitFactory.HTML_BASIC_RENDER_KIT);