myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r991420 - in /myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior: BehaviorBase.java ClientBehaviorHolder.java _AttachedListStateWrapper.java _AttachedStateWrapper.java
Date Wed, 01 Sep 2010 02:42:51 GMT
Author: lu4242
Date: Wed Sep  1 02:42:51 2010
New Revision: 991420

URL: http://svn.apache.org/viewvc?rev=991420&view=rev
Log:
MYFACES-2887 IllegalAccessException restoring custom behavior state

Added:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedListStateWrapper.java
    myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedStateWrapper.java
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/BehaviorBase.java
    myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/ClientBehaviorHolder.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/BehaviorBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/BehaviorBase.java?rev=991420&r1=991419&r2=991420&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/BehaviorBase.java
(original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/BehaviorBase.java
Wed Sep  1 02:42:51 2010
@@ -18,12 +18,12 @@
  */
 package javax.faces.component.behavior;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.faces.component.PartialStateHolder;
 import javax.faces.component.StateHolder;
-import javax.faces.component.UIComponentBase;
 import javax.faces.context.FacesContext;
 import javax.faces.event.BehaviorEvent;
 import javax.faces.event.BehaviorListener;
@@ -124,7 +124,7 @@ public class BehaviorBase implements Beh
         {
             //Full
             _behaviorListeners = (_DeltaList<BehaviorListener>)
-                UIComponentBase.restoreAttachedState(context, state);
+                restoreAttachedState(context, state);
         }
     }
 
@@ -149,7 +149,100 @@ public class BehaviorBase implements Beh
         }
         else
         {
-            return UIComponentBase.saveAttachedState(facesContext,_behaviorListeners);
+            return saveAttachedState(facesContext,_behaviorListeners);
+        }
+    }
+
+    private static Object saveAttachedState(FacesContext context, Object attachedObject)
+    {
+        if (context == null)
+        {
+            throw new NullPointerException ("context");
+        }
+        
+        if (attachedObject == null)
+            return null;
+        // StateHolder interface should take precedence over
+        // List children
+        if (attachedObject instanceof StateHolder)
+        {
+            StateHolder holder = (StateHolder) attachedObject;
+            if (holder.isTransient())
+            {
+                return null;
+            }
+
+            return new _AttachedStateWrapper(attachedObject.getClass(), holder.saveState(context));
+        }        
+        else if (attachedObject instanceof List)
+        {
+            List<Object> lst = new ArrayList<Object>(((List<?>) attachedObject).size());
+            for (Object item : (List<?>) attachedObject)
+            {
+                if (item != null)
+                {
+                    lst.add(saveAttachedState(context, item));
+                }
+            }
+
+            return new _AttachedListStateWrapper(lst);
+        }
+        else if (attachedObject instanceof Serializable)
+        {
+            return attachedObject;
+        }
+        else
+        {
+            return new _AttachedStateWrapper(attachedObject.getClass(), null);
+        }
+    }
+
+    private static Object restoreAttachedState(FacesContext context, Object stateObj) throws
IllegalStateException
+    {
+        if (context == null)
+            throw new NullPointerException("context");
+        if (stateObj == null)
+            return null;
+        if (stateObj instanceof _AttachedListStateWrapper)
+        {
+            List<Object> lst = ((_AttachedListStateWrapper) stateObj).getWrappedStateList();
+            List<Object> restoredList = new ArrayList<Object>(lst.size());
+            for (Object item : lst)
+            {
+                restoredList.add(restoreAttachedState(context, item));
+            }
+            return restoredList;
+        }
+        else if (stateObj instanceof _AttachedStateWrapper)
+        {
+            Class<?> clazz = ((_AttachedStateWrapper) stateObj).getClazz();
+            Object restoredObject;
+            try
+            {
+                restoredObject = clazz.newInstance();
+            }
+            catch (InstantiationException e)
+            {
+                throw new RuntimeException("Could not restore StateHolder of type " + clazz.getName()
+                        + " (missing no-args constructor?)", e);
+            }
+            catch (IllegalAccessException e)
+            {
+                throw new RuntimeException(e);
+            }
+            if (restoredObject instanceof StateHolder)
+            {
+                _AttachedStateWrapper wrapper = (_AttachedStateWrapper) stateObj;
+                Object wrappedState = wrapper.getWrappedStateObject();
+
+                StateHolder holder = (StateHolder) restoredObject;
+                holder.restoreState(context, wrappedState);
+            }
+            return restoredObject;
+        }
+        else
+        {
+            return stateObj;
         }
     }
 

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/ClientBehaviorHolder.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/ClientBehaviorHolder.java?rev=991420&r1=991419&r2=991420&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/ClientBehaviorHolder.java
(original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/ClientBehaviorHolder.java
Wed Sep  1 02:42:51 2010
@@ -19,7 +19,6 @@
 package javax.faces.component.behavior;
 
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 

Added: myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedListStateWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedListStateWrapper.java?rev=991420&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedListStateWrapper.java
(added)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedListStateWrapper.java
Wed Sep  1 02:42:51 2010
@@ -0,0 +1,43 @@
+/*
+ * 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 javax.faces.component.behavior;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author: skitching $)
+ * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (dom, 13 jul 2008) $
+ */
+class _AttachedListStateWrapper
+        implements Serializable
+{
+    private static final long serialVersionUID = -3958718149793179776L;
+    private List<Object> _wrappedStateList;
+
+    public _AttachedListStateWrapper(List<Object> wrappedStateList)
+    {
+        _wrappedStateList = wrappedStateList;
+    }
+
+    public List<Object> getWrappedStateList()
+    {
+        return _wrappedStateList;
+    }
+}

Added: myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedStateWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedStateWrapper.java?rev=991420&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedStateWrapper.java
(added)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/behavior/_AttachedStateWrapper.java
Wed Sep  1 02:42:51 2010
@@ -0,0 +1,58 @@
+/*
+ * 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 javax.faces.component.behavior;
+
+import java.io.Serializable;
+
+/**
+ * @author Manfred Geiler (latest modification by $Author: slessard $)
+ * @version $Revision: 701829 $ $Date: 2008-10-05 12:06:02 -0500 (Dom, 05 Oct 2008) $
+ */
+class _AttachedStateWrapper implements Serializable
+{
+    private static final long serialVersionUID = 4948301780259917764L;
+    private Class<?> _class;
+    private Object _wrappedStateObject;
+
+    /**
+     * @param clazz
+     *            null means wrappedStateObject is a List of state objects
+     * @param wrappedStateObject
+     */
+    public _AttachedStateWrapper(Class<?> clazz, Object wrappedStateObject)
+    {
+        if (wrappedStateObject != null && !(wrappedStateObject instanceof Serializable))
+        {
+            throw new IllegalArgumentException("Attached state for Object of type " + clazz
+ " (Class "
+                    + wrappedStateObject.getClass().getName() + ") is not serializable");
+        }
+        _class = clazz;
+        _wrappedStateObject = wrappedStateObject;
+    }
+
+    public Class<?> getClazz()
+    {
+        return _class;
+    }
+
+    public Object getWrappedStateObject()
+    {
+        return _wrappedStateObject;
+    }
+}



Mime
View raw message