Author: knopp
Date: Wed Jan  9 03:51:56 2008
New Revision: 610351

URL: http://svn.apache.org/viewvc?rev=610351&view=rev
Log:
WICKET-1272

Added:
    
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/SerializedPagesCache.java
   (with props)
Modified:
    
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
    
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/DiskPageStore.java

Modified: 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java?rev=610351&r1=610350&r2=610351&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
 (original)
+++ 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/SecondLevelCacheSessionStore.java
 Wed Jan  9 03:51:56 2008
@@ -133,11 +133,6 @@
                boolean containsPage(String sessionId, String pageMapName, int 
pageId, int pageVersion);
        }
 
-       protected boolean isPageStoreClustered()
-       {
-               return pageStore instanceof IClusteredPageStore;
-       }
-
        /**
         * Marker interface for PageStores that support replication of 
serialized pages across cluster,
         * which means that the lastPage attribute of [EMAIL PROTECTED] 
SecondLevelCachePageMap} does not have to
@@ -161,20 +156,31 @@
        public static interface ISerializationAwarePageStore extends IPageStore
        {
                /**
-                * Process the page before the it gets serialized
+                * Process the page before the it gets serialized. The page can 
be either real page instance
+                * of object returned by [EMAIL PROTECTED] 
#restoreAfterSerialization(Serializable)}.
                 * 
+                * @param sessionId
                 * @param page
                 * @return The Page itself or a SerializedContainer for that 
page
                 */
-               public Serializable prepareForSerialization(Page page);
+               public Serializable prepareForSerialization(String sessionId, 
Object page);
 
                /**
-                * This method should restore the given object to the original 
page.
+                * This method should restore the serialized page to 
intermediate object that can be
+                * converted to real page instance using [EMAIL PROTECTED] 
#convertToPage(Object)}.
                 * 
+                * @param sessionId
                 * @param serializable
                 * @return Page
                 */
-               public Page restoreAfterSerialization(Serializable 
serializable);
+               public Object restoreAfterSerialization(Serializable 
serializable);
+
+               /**
+                * 
+                * @param page
+                * @return
+                */
+               public Page convertToPage(Object page);
        };
 
        /**
@@ -184,82 +190,69 @@
        {
                private static final long serialVersionUID = 1L;
 
-               private transient Page lastPage = null;
-
-               // whether the last page instance should be serialized together 
with the
-               // pagemap
-               private final boolean serializeLastPage;
-
-               // when the last page is deserialized, it's actually placed here
-               // then on first demand, it's postprocessed (if the session 
store
-               // implements
-               // ISerializationAwareSessionStore) and set as lastPage
-               private Serializable lastPageDeserialized;
+               private transient Object lastPage = null;
 
-               private transient SecondLevelCacheSessionStore sessionStore;
+               private final String applicationKey;
+               private String sessionId;
 
                private IPageStore getPageStore()
                {
-                       if (sessionStore == null)
-                       {
-                               Application app = Application.exists() ? 
Application.get() : null;
-                               if (app != null)
-                               {
-                                       sessionStore = 
(SecondLevelCacheSessionStore)app.getSessionStore();
-                               }
-                       }
-                       if (sessionStore != null)
+                       Application application = 
Application.get(applicationKey);
+
+                       if (application != null)
                        {
-                               return sessionStore.getStore();
+                               SecondLevelCacheSessionStore store = 
(SecondLevelCacheSessionStore)application.getSessionStore();
+
+                               return store.getStore();
                        }
                        else
                        {
+                               // may happen when getPageStore() is called 
before application is initialized
+                               // for example on session initialization when 
cluster node starts
                                return null;
                        }
                }
 
                private Page getLastPage()
                {
-                       if (lastPage == null && lastPageDeserialized != null)
+                       Page result = null;
+                       if (lastPage instanceof Page)
+                       {
+                               result = (Page)lastPage;
+                       }
+                       else if (lastPage != null)
                        {
                                IPageStore store = getPageStore();
-                               // initialize lastPage if necessary (we 
intentionally delay this
-                               // until the first demand)
                                if (store instanceof 
ISerializationAwarePageStore)
                                {
-                                       lastPage = 
((ISerializationAwarePageStore)store).restoreAfterSerialization(lastPageDeserialized);
+                                       lastPage = result = 
((ISerializationAwarePageStore)store).convertToPage(lastPage);
                                }
-                               else if (lastPageDeserialized instanceof Page)
-                               {
-                                       lastPage = (Page)lastPageDeserialized;
-                               }
-                               lastPageDeserialized = null;
                        }
-                       return lastPage;
+                       return result;
                }
 
                private void setLastPage(Page lastPage)
                {
                        this.lastPage = lastPage;
-                       lastPageDeserialized = null;
                }
 
                /**
                 * Construct.
                 * 
-                * @param sessionStore
+                * @param sessionId
+                * @param application
                 * @param name
                 */
-               private SecondLevelCachePageMap(SecondLevelCacheSessionStore 
sessionStore, String name)
+               private SecondLevelCachePageMap(String sessionId, Application 
application, String name)
                {
                        super(name);
-                       this.sessionStore = sessionStore;
-                       serializeLastPage = sessionStore.isPageStoreClustered() 
== false;
+                       applicationKey = application.getApplicationKey();
+                       this.sessionId = sessionId;
                }
 
                public boolean containsPage(int id, int versionNumber)
                {
-                       Page lastPage = this.lastPage;
+                       Page lastPage = this.lastPage instanceof Page ? 
(Page)this.lastPage : null;
                        if (lastPage != null && lastPage.getNumericId() == id &&
                                lastPage.getCurrentVersionNumber() == 
versionNumber)
                        {
@@ -334,6 +327,8 @@
                                String sessionId = session.getId();
                                if (sessionId != null && 
!session.isSessionInvalidated())
                                {
+                                       // the id could have changed from null 
during request
+                                       this.sessionId = sessionId;
                                        getStore().storePage(sessionId, page);
                                        setLastPage(page);
                                        dirty();
@@ -376,16 +371,16 @@
 
                        s.defaultWriteObject();
 
-                       // if the pagestore is not clustered, we need to 
serialize the
-                       // lastPage instance
-                       if (serializeLastPage)
-                       {
-                               Serializable page = lastPage;
+                       IPageStore store = getPageStore();
 
-                               IPageStore store = getPageStore();
-                               if (page != null && store instanceof 
ISerializationAwarePageStore)
+                       // for IClusteredPageStore we just skip serializing the 
page, pagestore takes care of it
+                       if (sessionId != null && store instanceof 
IClusteredPageStore == false)
+                       {
+                               Object page = lastPage;
+                               if (store instanceof 
ISerializationAwarePageStore)
                                {
-                                       page = 
((ISerializationAwarePageStore)store).prepareForSerialization(lastPage);
+                                       page = 
((ISerializationAwarePageStore)store).prepareForSerialization(sessionId,
+                                               page);
                                }
 
                                try
@@ -404,15 +399,16 @@
                {
                        s.defaultReadObject();
 
-                       // if the pagestore is not clustered, we need to read 
the lastPage
-                       // instance
-                       if (serializeLastPage)
+                       IPageStore store = getPageStore();
+
+                       if (sessionId != null && store instanceof 
IClusteredPageStore == false)
                        {
-                               Serializable page = 
(Serializable)s.readObject();
-                               if (page != null)
+                               Object lastPage = s.readObject();
+                               if (store instanceof 
ISerializationAwarePageStore)
                                {
-                                       lastPageDeserialized = page;
+                                       lastPage = 
((ISerializationAwarePageStore)store).restoreAfterSerialization((Serializable)lastPage);
                                }
+                               this.lastPage = lastPage;
                        }
                }
        }
@@ -675,7 +671,7 @@
         */
        public IPageMap createPageMap(String name)
        {
-               return new SecondLevelCachePageMap(this, name);
+               return new SecondLevelCachePageMap(Session.get().getId(), 
Application.get(), name);
        }
 
        /**

Modified: 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/DiskPageStore.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/DiskPageStore.java?rev=610351&r1=610350&r2=610351&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/DiskPageStore.java
 (original)
+++ 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/DiskPageStore.java
 Wed Jan  9 03:51:56 2008
@@ -26,7 +26,6 @@
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.io.Serializable;
-import java.lang.ref.WeakReference;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.util.ArrayList;
@@ -43,6 +42,7 @@
 import org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
 import 
org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.ISerializationAwarePageStore;
 import org.apache.wicket.protocol.http.pagestore.PageWindowManager.PageWindow;
+import 
org.apache.wicket.protocol.http.pagestore.SerializedPagesCache.SerializedPageWithSession;
 import org.apache.wicket.util.concurrent.ConcurrentHashMap;
 import org.apache.wicket.util.lang.Bytes;
 import org.slf4j.Logger;
@@ -805,7 +805,7 @@
        {
                List pages = serializePage(page);
 
-               cacheSerializedPage(sessionId, page, pages);
+               serializedPagesCache.storePage(sessionId, page, pages);
 
                onPagesSerialized(sessionId, pages);
 
@@ -1023,141 +1023,187 @@
        /**
         * @return
         */
-       public int getLastRecentlySerializedPagesCacheSize()
+       protected int getLastRecentlySerializedPagesCacheSize()
        {
                return lastRecentlySerializedPagesCacheSize;
        }
 
-       private final List /* SerializedPageWithSession 
*/lastRecentlySerializedPagesCache = new ArrayList(
-               lastRecentlySerializedPagesCacheSize);
+       private final SerializedPagesCache serializedPagesCache = new 
SerializedPagesCache(
+               getLastRecentlySerializedPagesCacheSize());
 
-       private SerializedPageWithSession 
removePageFromLastRecentlySerializedPagesCache(Page page)
+       /**
+        * Strips the actual serialized page data. This is used to store
+        * [EMAIL PROTECTED] SerializedPageWithSession} instance in http 
session to reduce the memory consumption.
+        * The data can be stripped because it's already stored on disk
+        * 
+        * @param page
+        * @return
+        */
+       private SerializedPageWithSession 
stripSerializedPage(SerializedPageWithSession page)
        {
-               for (Iterator i = lastRecentlySerializedPagesCache.iterator(); 
i.hasNext();)
+               List pages = new ArrayList(page.pages.size());
+               for (Iterator i = page.pages.iterator(); i.hasNext();)
                {
-                       SerializedPageWithSession entry = 
(SerializedPageWithSession)i.next();
-                       if (entry != null && entry.page.get() == page)
-                       {
-                               i.remove();
-                               return entry;
-                       }
+                       SerializedPage sp = (SerializedPage)i.next();
+                       pages.add(new SerializedPage(sp.getPageId(), 
sp.getPageMapName(),
+                               sp.getVersionNumber(), 
sp.getAjaxVersionNumber(), null));
                }
-               return null;
+               return new SerializedPageWithSession(page.sessionId, 
page.pageId, page.pageMapName,
+                       page.versionNumber, page.ajaxVersionNumber, pages);
        }
 
-       /**
-        * Store the serialized page in the request metadata,.
-        * 
-        * @param sessionId
-        * @param page
-        * @param pagesList
-        */
-       private void cacheSerializedPage(String sessionId, Page page,
-               List /* <SerializedPage> */pagesList)
+       private byte[] getPageData(String sessionId, int pageId, String 
pageMapName, int versionNumber,
+               int ajaxVersionNumber)
        {
-               if (getLastRecentlySerializedPagesCacheSize() > 0)
+               SessionEntry entry = getSessionEntry(sessionId, false);
+               if (entry != null)
                {
-                       SerializedPageWithSession entry = new 
SerializedPageWithSession(sessionId, page,
-                               pagesList);
+                       byte[] data;
 
-                       synchronized (lastRecentlySerializedPagesCache)
+                       if (isSynchronous())
+                       {
+                               data = entry.loadPage(pageMapName, pageId, 
versionNumber, ajaxVersionNumber);
+                       }
+                       else
                        {
-                               
removePageFromLastRecentlySerializedPagesCache(page);
-                               lastRecentlySerializedPagesCache.add(entry);
-                               if (lastRecentlySerializedPagesCache.size() > 
getLastRecentlySerializedPagesCacheSize())
+                               // we need to make sure that the there are no 
pending pages to
+                               // be saved before loading a page
+                               List pages = getPagesToSaveList(sessionId);
+                               synchronized (pages)
                                {
-                                       
lastRecentlySerializedPagesCache.remove(0);
+                                       flushPagesToSaveList(sessionId, pages);
+                                       data = entry.loadPage(pageMapName, 
pageId, versionNumber, ajaxVersionNumber);
                                }
                        }
+                       return data;
+               }
+               else
+               {
+                       return null;
                }
        }
 
        /**
+        * Loads the data stripped by
+        * [EMAIL PROTECTED] 
#stripSerializedPage(org.apache.wicket.protocol.http.pagestore.DiskPageStore.SerializedPageWithSession)}.
         * 
-        * @author Matej Knopp
+        * @param page
+        * @return
         */
-       private static class SerializedPageWithSession implements Serializable
+       private SerializedPageWithSession 
restoreStrippedSerializedPage(SerializedPageWithSession page)
        {
-               private static final long serialVersionUID = 1L;
+               List pages = new ArrayList(page.pages.size());
+               for (Iterator i = page.pages.iterator(); i.hasNext();)
+               {
+                       SerializedPage sp = (SerializedPage)i.next();
+                       byte data[] = getPageData(page.sessionId, 
sp.getPageId(), sp.getPageMapName(),
+                               sp.getVersionNumber(), 
sp.getAjaxVersionNumber());
 
-               // this is used for lookup on pagemap serialization. We don't 
have the
-               // session id at that point, because it can happen outside the 
request
-               // thread. We only have the page instance and we need to use it 
as a key
-               private final transient WeakReference /* <Page> */page;
-
-               // list of serialized pages
-               private final List pages;
-
-               private final String sessionId;
-
-               // after deserialization, we need to be able to know which page 
to load
-               private final int pageId;
-               private final String pageMapName;
-               private final int versionNumber;
-               private final int ajaxVersionNumber;
-
-               private SerializedPageWithSession(String sessionId, Page page,
-                       List /* <SerializablePage> */pages)
-               {
-                       this.sessionId = sessionId;
-                       pageId = page.getNumericId();
-                       pageMapName = page.getPageMapName();
-                       versionNumber = page.getCurrentVersionNumber();
-                       ajaxVersionNumber = page.getAjaxVersionNumber();
-                       this.pages = new ArrayList(pages);
-                       this.page = new WeakReference(page);
+                       pages.add(new SerializedPage(sp.getPageId(), 
sp.getPageMapName(),
+                               sp.getVersionNumber(), 
sp.getAjaxVersionNumber(), data));
                }
 
-               public String toString()
-               {
-                       return getClass().getName() + " [ pageId:" + pageId + 
", pageMapName: " + pageMapName +
-                               ", session: " + sessionId + "]";
-               }
-       };
+               return new SerializedPageWithSession(page.sessionId, 
page.pageId, page.pageMapName,
+                       page.versionNumber, page.ajaxVersionNumber, pages);
+       }
+
 
        /**
-        * @see 
org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.ISerializationAwarePageStore#prepareForSerialization(org.apache.wicket.Page)
+        * [EMAIL PROTECTED]
         */
-       public Serializable prepareForSerialization(Page page)
+       public Serializable prepareForSerialization(String sessionId, Object 
page)
        {
-               Serializable result = page;
-
-               if (getLastRecentlySerializedPagesCacheSize() > 0)
+               SerializedPageWithSession result = null;
+               if (page instanceof Page)
                {
-                       SerializedPageWithSession entry;
-                       synchronized (lastRecentlySerializedPagesCache)
+                       result = serializedPagesCache.getPage((Page)page);
+                       if (result == null)
                        {
-                               entry = 
removePageFromLastRecentlySerializedPagesCache(page);
+                               List serialized = serializePage((Page)page);
+                               result = 
serializedPagesCache.storePage(sessionId, (Page)page, serialized);
                        }
-
-                       if (entry != null)
+               }
+               else if (page instanceof SerializedPageWithSession)
+               {
+                       SerializedPageWithSession serialized = 
(SerializedPageWithSession)page;
+                       if (serialized.page.get() == 
SerializedPageWithSession.NO_PAGE)
                        {
-                               result = entry;
+                               // stripped page, need to restore it first
+                               result = 
restoreStrippedSerializedPage(serialized);
+                       }
+                       else
+                       {
+                               result = serialized;
                        }
                }
 
-               return result;
+               if (result != null)
+               {
+                       return result;
+               }
+               else
+               {
+                       return (Serializable)page;
+               }
+       }
+
+       protected boolean storeAfterSessionReplication()
+       {
+               return true;
        }
 
        /**
         * @see 
org.apache.wicket.protocol.http.SecondLevelCacheSessionStore.ISerializationAwarePageStore#restoreAfterSerialization(java.io.Serializable)
         */
-       public Page restoreAfterSerialization(Serializable serializable)
+       public Object restoreAfterSerialization(Serializable serializable)
        {
-               if (serializable instanceof Page)
+               if (!storeAfterSessionReplication() || serializable instanceof 
Page)
                {
-                       return (Page)serializable;
+                       return serializable;
                }
                else if (serializable instanceof SerializedPageWithSession)
                {
                        SerializedPageWithSession page = 
(SerializedPageWithSession)serializable;
-                       storeSerializedPages(page.sessionId, page.pages);
-                       return getPage(page.sessionId, page.pageMapName, 
page.pageId, page.versionNumber,
-                               page.ajaxVersionNumber);
+                       if (page.page == null || page.page.get() != 
SerializedPageWithSession.NO_PAGE)
+                       {
+                               storeSerializedPages(page.sessionId, 
page.pages);
+                               return stripSerializedPage(page);
+                       }
+                       else
+                       {
+                               return page;
+                       }
+               }
+               else
+               {
+                       String type = serializable != null ? 
serializable.getClass().getName() : null;
+                       throw new IllegalArgumentException("Unknown object type 
" + type);
+               }
+       }
+
+       public Page convertToPage(Object page)
+       {
+               if (page instanceof Page)
+               {
+                       return (Page)page;
+               }
+               else if (page instanceof SerializedPageWithSession)
+               {
+                       SerializedPageWithSession serialized = 
(SerializedPageWithSession)page;
+
+                       if (serialized.page == null ||
+                               serialized.page.get() != 
SerializedPageWithSession.NO_PAGE)
+                       {
+                               storeSerializedPages(serialized.sessionId, 
serialized.pages);
+                       }
+
+                       return getPage(serialized.sessionId, 
serialized.pageMapName, serialized.pageId,
+                               serialized.versionNumber, 
serialized.ajaxVersionNumber);
                }
                else
                {
-                       throw new IllegalArgumentException("Unknown object 
type");
+                       String type = page != null ? page.getClass().getName() 
: null;
+                       throw new IllegalArgumentException("Unknown object type 
+ type");
                }
        }
 

Added: 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/SerializedPagesCache.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/SerializedPagesCache.java?rev=610351&view=auto
==============================================================================
--- 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/SerializedPagesCache.java
 (added)
+++ 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/SerializedPagesCache.java
 Wed Jan  9 03:51:56 2008
@@ -0,0 +1,208 @@
+/*
+ * 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.pagestore;
+
+import java.io.Serializable;
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.wicket.Page;
+
+/**
+ * Cache that stores serialized pages. This is important to make sure that a 
single page is not
+ * serialized twice or more when not necessary.
+ * <p>
+ * For example a page is serialized during request, but it might be also later 
serialized on session
+ * replication. The purpose of this cache is to make sure that the data 
obtained from first
+ * serialization is reused on second serialization.
+ * 
+ * @author Matej Knopp
+ */
+class SerializedPagesCache
+{
+       /**
+        * Construct.
+        * 
+        * @param size
+        */
+       public SerializedPagesCache(final int size)
+       {
+               this.size = size;
+               cache = new ArrayList(size);
+       }
+
+       private final int size;
+
+       private final List /* SerializedPageWithSession */cache;
+
+       SerializedPageWithSession removePage(Page page)
+       {
+               if (size > 0)
+               {
+                       synchronized (cache)
+                       {
+                               for (Iterator i = cache.iterator(); 
i.hasNext();)
+                               {
+                                       SerializedPageWithSession entry = 
(SerializedPageWithSession)i.next();
+                                       if (entry != null && entry.page.get() 
== page)
+                                       {
+                                               i.remove();
+                                               return entry;
+                                       }
+                               }
+                       }
+               }
+               return null;
+       }
+
+       SerializedPageWithSession getPage(Page page)
+       {
+               SerializedPageWithSession result = null;
+               if (size > 0)
+               {
+                       synchronized (cache)
+                       {
+                               for (Iterator i = cache.iterator(); 
i.hasNext();)
+                               {
+                                       SerializedPageWithSession entry = 
(SerializedPageWithSession)i.next();
+                                       if (entry != null && entry.page.get() 
== page)
+                                       {
+                                               i.remove();
+                                               result = entry;
+                                               break;
+                                       }
+                               }
+
+                               if (result != null)
+                               {
+                                       cache.add(result);
+                               }
+                       }
+               }
+               return result;
+       }
+
+       SerializedPageWithSession getPage(String sessionId, int pageId, String 
pageMapName,
+               int version, int ajaxVersion)
+       {
+               if (size > 0)
+               {
+                       synchronized (cache)
+                       {
+                               for (Iterator i = cache.iterator(); 
i.hasNext();)
+                               {
+                                       SerializedPageWithSession entry = 
(SerializedPageWithSession)i.next();
+                                       if (entry.sessionId.equals(sessionId) 
&& entry.pageId == pageId &&
+                                               
entry.pageMapName.equals(pageMapName) && entry.versionNumber == version &&
+                                               entry.ajaxVersionNumber == 
ajaxVersion)
+                                       {
+                                               return entry;
+                                       }
+                               }
+                       }
+               }
+               return null;
+       }
+
+       /**
+        * Store the serialized page in cache
+        * 
+        * @return
+        * @param sessionId
+        * @param page
+        * @param pagesList
+        */
+       SerializedPageWithSession storePage(String sessionId, Page page,
+               List /* <SerializedPage> */pagesList)
+       {
+               SerializedPageWithSession entry = new 
SerializedPageWithSession(sessionId, page, pagesList);
+
+               if (size > 0)
+               {
+                       synchronized (cache)
+                       {
+                               removePage(page);
+                               cache.add(entry);
+                               if (cache.size() > size)
+                               {
+                                       cache.remove(0);
+                               }
+                       }
+               }
+
+               return entry;
+       }
+
+       /**
+        * 
+        * @author Matej Knopp
+        */
+       static class SerializedPageWithSession implements Serializable
+       {
+               private static final long serialVersionUID = 1L;
+
+               // this is used for lookup on pagemap serialization. We don't 
have the
+               // session id at that point, because it can happen outside the 
request
+               // thread. We only have the page instance and we need to use it 
as a key
+               final transient WeakReference /* <Page> */page;
+
+               // list of serialized pages
+               final List pages;
+
+               final String sessionId;
+
+               // after deserialization, we need to be able to know which page 
to load
+               final int pageId;
+               final String pageMapName;
+               final int versionNumber;
+               final int ajaxVersionNumber;
+
+               SerializedPageWithSession(String sessionId, Page page, List /* 
<SerializablePage> */pages)
+               {
+                       this.sessionId = sessionId;
+                       pageId = page.getNumericId();
+                       pageMapName = page.getPageMapName();
+                       versionNumber = page.getCurrentVersionNumber();
+                       ajaxVersionNumber = page.getAjaxVersionNumber();
+                       this.pages = new ArrayList(pages);
+                       this.page = new WeakReference(page);
+               }
+
+               SerializedPageWithSession(String sessionId, int pageId, String 
pageMapName,
+                       int versionNumber, int ajaxVersionNumber, List /* 
<SerializablePage> */pages)
+               {
+                       this.sessionId = sessionId;
+                       page = new WeakReference(NO_PAGE);
+                       this.pageId = pageId;
+                       this.pageMapName = pageMapName;
+                       this.versionNumber = versionNumber;
+                       this.ajaxVersionNumber = ajaxVersionNumber;
+                       this.pages = pages;
+               }
+
+               static Object NO_PAGE = new Object();
+
+               public String toString()
+               {
+                       return getClass().getName() + " [ pageId:" + pageId + 
", pageMapName: " + pageMapName +
+                               ", session: " + sessionId + "]";
+               }
+       };
+
+}

Propchange: 
wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/pagestore/SerializedPagesCache.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to