Author: jcompagner
Date: Thu May 17 08:04:04 2007
New Revision: 538958

URL: http://svn.apache.org/viewvc?view=rev&rev=538958
Log:
first fix for stack overflow in serializing pages
(not completely done yet)

Added:
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/FilePageStoreTest.java
   (with props)
Modified:
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Page.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/FilePageStore.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockServletContext.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageA.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageB.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/WicketOutputStreamTest.java

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Page.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Page.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Page.java 
(original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Page.java 
Thu May 17 08:04:04 2007
@@ -16,6 +16,7 @@
  */
 package org.apache.wicket;
 
+import java.io.IOException;
 import java.io.ObjectStreamException;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -257,6 +258,25 @@
                return this;
        }
        
+       private void writeObject(java.io.ObjectOutputStream s) throws 
IOException
+       {
+               s.writeShort(numericId);
+               s.writeObject(pageMapName);
+               s.defaultWriteObject();
+       }
+       
+       private void readObject(java.io.ObjectInputStream s) throws 
IOException, ClassNotFoundException
+       {
+               int id = s.readShort();
+               String name = (String)s.readObject();
+               
+               IPageSerializer ps = (IPageSerializer)serializer.get();
+               if (ps != null)
+               {
+                       ps.deserializePage(id, name, this);
+               }
+               s.defaultReadObject();
+       }
        
        /**
         * Called right after a component's listener method (the provided method
@@ -1391,5 +1411,12 @@
                 * @return The page or another Object that should be replaced 
for the page.
                 */
                public Object serializePage(Page page);
+
+               /**
+                * @param id TODO
+                * @param name TODO
+                * @param page
+                */
+               public void deserializePage(int id, String name, Page page);
        }
 }

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/FilePageStore.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/FilePageStore.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/FilePageStore.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/FilePageStore.java
 Thu May 17 08:04:04 2007
@@ -25,6 +25,7 @@
 import java.io.Serializable;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -35,6 +36,7 @@
 import org.apache.wicket.Page;
 import org.apache.wicket.Session;
 import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
+import org.apache.wicket.util.collections.IntHashMap;
 import org.apache.wicket.util.concurrent.ConcurrentHashMap;
 import org.apache.wicket.util.lang.Objects;
 import org.slf4j.Logger;
@@ -384,6 +386,8 @@
        private volatile int serialized;
 
        private volatile long totalSerializationTime = 0;
+       
+       private static final ThreadLocal restoredPages = new ThreadLocal();
 
        /**
         * Construct.
@@ -447,9 +451,7 @@
                byte[] bytes = testMap(currentKey);
                if (bytes != null)
                {
-                       Page page = (Page)Objects.byteArrayToObject(bytes);
-                       page = page.getVersion(versionNumber);
-                       return page;
+                       return readPage(versionNumber, bytes);
                }
                File sessionDir = new File(getWorkDir(), sessionId);
                if (sessionDir.exists())
@@ -475,19 +477,26 @@
                                                pageData = new byte[length];
                                                bb.get(pageData);
                                        }
-                                       long t2 = System.currentTimeMillis();
-                                       Page page = 
(Page)Objects.byteArrayToObject(pageData);
-                                       page = page.getVersion(versionNumber);
-                                       if (page != null && 
log.isDebugEnabled())
-                                       {
-                                               long t3 = 
System.currentTimeMillis();
-                                               log.debug("restoring page " + 
page.getClass() + "[" + page.getNumericId()
-                                                               + "," + 
page.getCurrentVersionNumber() + "] size: "
-                                                               + 
pageData.length + " for session " + sessionId + " took "
-                                                               + (t2 - t1) + " 
miliseconds to read in and " + (t3 - t2)
-                                                               + " miliseconds 
to deserialize");
+                                       try
+                                       {
+                                               long t2 = 
System.currentTimeMillis();
+                                               Page page = 
readPage(versionNumber, pageData);
+                                               if (page != null && 
log.isDebugEnabled())
+                                               {
+                                                       long t3 = 
System.currentTimeMillis();
+                                                       log.debug("restoring 
page " + page.getClass() + "[" + page.getNumericId()
+                                                                       + "," + 
page.getCurrentVersionNumber() + "] size: "
+                                                                       + 
pageData.length + " for session " + sessionId + " took "
+                                                                       + (t2 - 
t1) + " miliseconds to read in and " + (t3 - t2)
+                                                                       + " 
miliseconds to deserialize");
+                                               }
+                                               return page;
+                                       } 
+                                       finally
+                                       {
+                                               
                                        }
-                                       return page;
+
                                }
                                catch (Exception e)
                                {
@@ -514,6 +523,37 @@
        }
 
        /**
+        * @param versionNumber
+        * @param bytes
+        * @return
+        */
+       private Page readPage(int versionNumber, byte[] bytes)
+       {
+               Page page;
+               Map map = null;
+               try
+               {
+                       if (restoredPages.get() == null)
+                       {
+                               map = new HashMap();
+                               restoredPages.set(map);
+                               Page.serializer.set(new PageSerializer(null));
+                       }
+                       page = (Page)Objects.byteArrayToObject(bytes);
+                       page = page.getVersion(versionNumber);
+               } 
+               finally
+               {
+                       if (map != null)
+                       {
+                               Page.serializer.set(null);
+                               restoredPages.set(null);
+                       }
+               }
+               return page;
+       }
+
+       /**
         * @see 
org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#pageAccessed(java.lang.String,
         *      org.apache.wicket.Page)
         */
@@ -784,7 +824,7 @@
                                return page;
                        }
                        SessionPageKey spk = new 
SessionPageKey(current.sessionId,page);
-                       if (!completed.contains(spk))
+                       if (!completed.contains(spk) && !previous.contains(spk))
                        {
                                previous.add(current);
                                current = spk;
@@ -796,6 +836,25 @@
                        }
                        return new PageHolder(page);
                }
+
+               /**
+                * @see 
org.apache.wicket.Page.IPageSerializer#deserializePage(int, String, 
org.apache.wicket.Page)
+                */
+               public void deserializePage(int id, String name, Page page)
+               {
+                       HashMap map = (HashMap)restoredPages.get();
+                       if (map != null)
+                       {
+                               IntHashMap pagesMap = (IntHashMap)map.get(name);
+                               if (pagesMap == null)
+                               {
+                                       pagesMap = new IntHashMap();
+                                       map.put(name, pagesMap);
+                               }
+                               
+                               pagesMap.put(id, page);
+                       }
+               }
        }
        
        private static class PageHolder implements Serializable
@@ -813,7 +872,27 @@
                
                protected Object readResolve() throws ObjectStreamException
                {
-                       return Session.get().getPage(pagemap, 
Integer.toString(pageid), -1);
+                       IntHashMap intHashMap = null;
+                       Map map = (Map)restoredPages.get();
+                       if (map != null)
+                       {
+                               intHashMap = (IntHashMap)map.get(pagemap);
+                               if (intHashMap == null)
+                               {
+                                       intHashMap = new IntHashMap();
+                                       map.put(pagemap, intHashMap);
+                               }
+                       }
+                       Page page = (Page)intHashMap.get(pageid);
+                       if (page == null)
+                       {
+                               page = Session.get().getPage(pagemap, 
Integer.toString(pageid), -1);
+                               if (page != null)
+                               {
+                                       intHashMap.put(pageid, page);
+                               }
+                       }
+                       return page;
                }
        }
 }

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java
 Thu May 17 08:04:04 2007
@@ -43,7 +43,7 @@
 
        private final long creationTime = System.currentTimeMillis();
 
-       private final String id = (new UID()).toString();
+       private final String id = (new UID()).toString().replace(':', 
'_').replace('-','_');
 
        private long lastAccessedTime = 0;
 

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockServletContext.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockServletContext.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockServletContext.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockServletContext.java
 Thu May 17 08:04:04 2007
@@ -102,7 +102,9 @@
                // so the sessions directory will be created inside the target 
directory,
                // and will be cleaned up with a mvn clean
 
-               attributes.put("javax.servlet.context.tempdir", new 
File("target/work"));
+               File file = new File("target/work/");
+               file.mkdirs();
+               attributes.put("javax.servlet.context.tempdir", file);
 
                mimeTypes.put("html", "text/html");
                mimeTypes.put("htm", "text/html");

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
 Thu May 17 08:04:04 2007
@@ -165,6 +165,7 @@
                                {
                                        ajaxVersionNumber = pv.ajaxversionid;
                                }
+                               lastPage = null;
                                return getStore().getPage(sessionId, getName(), 
id, versionNumber, ajaxVersionNumber);
                        }
                        return null;

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/FilePageStoreTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/FilePageStoreTest.java?view=auto&rev=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/FilePageStoreTest.java
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/FilePageStoreTest.java
 Thu May 17 08:04:04 2007
@@ -0,0 +1,53 @@
+/*
+ * 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.wicket.protocol.http;
+
+import junit.framework.Assert;
+
+import org.apache.wicket.Session;
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.util.io.PageA;
+import org.apache.wicket.util.io.PageB;
+
+/**
+ * @author jcompagner
+ */
+public class FilePageStoreTest extends WicketTestCase
+{
+       /**
+        * @throws Exception
+        */
+       public void testPageSerialization() throws Exception
+       {
+               PageB b = new PageB("test");
+               PageA a = new PageA(b);
+               b.setA(a);
+               
+               tester.setupRequestAndResponse();
+               
+               Session session = Session.get();
+               
+               a.getPageMap().put(a);
+               a.getPageMap().put(b);
+               
+               PageA a2 = (PageA)a.getPageMap().get(a.getNumericId(), 
a.getCurrentVersionNumber());
+               
+               Assert.assertEquals(a, a2);
+               
+               Assert.assertSame(a2, a2.getB().getA());
+       }
+}

Propchange: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/FilePageStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageA.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageA.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageA.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageA.java
 Thu May 17 08:04:04 2007
@@ -16,8 +16,10 @@
  */
 package org.apache.wicket.util.io;
 
+import org.apache.wicket.AttributeModifier;
 import org.apache.wicket.Page;
 import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.model.Model;
 
 /**
  * @author jcompagner
@@ -28,11 +30,13 @@
         * 
         */
        private static final long serialVersionUID = 1L;
-       Page page;
        
-       public PageA(Page page)
+       private PageB page;
+       
+       public PageA(PageB page)
        {
                this.page = page;
+               //add(new AttributeModifier("test",new Model(page)));
        }
        
        /**
@@ -52,5 +56,13 @@
                        }
                }
                return false;
+       }
+
+       /**
+        * @return
+        */
+       public PageB getB()
+       {
+               return page;
        }
 }

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageB.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageB.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageB.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/PageB.java
 Thu May 17 08:04:04 2007
@@ -16,7 +16,9 @@
  */
 package org.apache.wicket.util.io;
 
+import org.apache.wicket.AttributeModifier;
 import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.model.Model;
 
 /**
  * @author jcompagner
@@ -27,13 +29,43 @@
         * 
         */
        private static final long serialVersionUID = 1L;
+       private PageA a;
+       private final String test;
+       
+       /**
+        * Construct.
+        * @param t
+        */
+       public PageB(String test)
+       {
+               this.test = test;
+               
+       }
 
        public boolean equals(Object obj)
        {
                if (obj instanceof PageB)
                {
-                       return getNumericId() == ((PageB)obj).getNumericId();
+                       return getNumericId() == ((PageB)obj).getNumericId() && 
test.equals( ((PageB)obj).test);
                }
                return false;
        }
+
+       /**
+        * @param a
+        */
+       public void setA(PageA a)
+       {
+               this.a = a;
+               //add(new AttributeModifier("test",new Model(a)));
+       }
+
+       /**
+        * @return
+        */
+       public PageA getA()
+       {
+               return a;
+       }
+       
 }

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/WicketOutputStreamTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/WicketOutputStreamTest.java?view=diff&rev=538958&r1=538957&r2=538958
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/WicketOutputStreamTest.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/util/io/WicketOutputStreamTest.java
 Thu May 17 08:04:04 2007
@@ -107,7 +107,9 @@
 
        public void testPageReference() throws Exception
        {
-               PageA a = new PageA(new PageB());
+               PageB b = new PageB("test");
+               PageA a = new PageA(b);
+               b.setA(a);
                
                woos.writeObject(a);
 
@@ -117,6 +119,8 @@
                PageA a2 = (PageA)wois.readObject();
 
                Assert.assertEquals(a, a2);
+               
+               Assert.assertSame(a2, a2.getB().getA());
 
        }
         


Reply via email to