⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializedpagescache.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
字号:
/*
 * 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 + "]";
		}
	};

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -