wicket-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehillen...@apache.org
Subject svn commit: r492722 - /incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
Date Thu, 04 Jan 2007 20:26:50 GMT
Author: ehillenius
Date: Thu Jan  4 12:26:49 2007
New Revision: 492722

URL: http://svn.apache.org/viewvc?view=rev&rev=492722
Log:
improvement in error reporting

Modified:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java?view=diff&rev=492722&r1=492721&r2=492722
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
(original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
Thu Jan  4 12:26:49 2007
@@ -14,38 +14,40 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package wicket.protocol.http;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import wicket.Application;
-import wicket.Page;
-import wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
-import wicket.util.lang.Objects;
-
-/**
+package wicket.protocol.http;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import wicket.Application;
+import wicket.Page;
+import wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore;
+import wicket.util.lang.Objects;
+
+/**
  * Stores pages on disk.
  * <p>
- * Override {@link #getWorkDir()} to change the default directory
- * for pages, which is configured from the javax.servlet.context.tempdir
- * attribute in the servlet context.
+ * Override {@link #getWorkDir()} to change the default directory for pages,
+ * which is configured from the javax.servlet.context.tempdir attribute in the
+ * servlet context.
  * 
- * @author jcompagner
- */
-public class FilePageStore implements IPageStore
-{
-	/** log. */
+ * @author jcompagner
+ */
+public class FilePageStore implements IPageStore
+{
+	/** log. */
 	protected static Log log = LogFactory.getLog(FilePageStore.class);
-	
+
 	private File defaultWorkDir;
-	
+
 	/**
 	 * Construct.
 	 */
@@ -54,7 +56,7 @@
 		defaultWorkDir = (File)((WebApplication)Application.get()).getServletContext()
 				.getAttribute("javax.servlet.context.tempdir");
 	}
-	
+
 	/**
 	 * Returns the working directory for this disk-based PageStore. Override
 	 * this to configure a different location. The default is
@@ -65,155 +67,163 @@
 	protected File getWorkDir()
 	{
 		return defaultWorkDir;
-	}
-
+	}
+
 	/**
 	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#getPage(java.lang.String,
 	 *      int, int)
-	 */
-	public Page getPage(String sessionId, int id, int versionNumber)
-	{
-		File sessionDir = new File(getWorkDir(), sessionId);
-		if (sessionDir.exists())
-		{
-			File pageFile = getPageFile(id, versionNumber, sessionDir);
-			if (pageFile.exists())
-			{
-				FileInputStream fis = null;
-				try
-				{
-					byte[] pageData = null;
-					fis = new FileInputStream(pageFile);
-					int length = (int)pageFile.length();
-					ByteBuffer bb = ByteBuffer.allocate(length);
-					fis.getChannel().read(bb);
-					if (bb.hasArray())
-					{
-						pageData = bb.array();
-					}
-					else
-					{
-						pageData = new byte[length];
-						bb.get(pageData);
-					}
-
-					Page page = (Page)Objects.byteArrayToObject(pageData);
-					return page.getVersion(versionNumber);
-				}
-				catch (Exception e)
-				{
-					log.debug("Error loading page " + id + " with version " + versionNumber
-							+ " for the sessionid " + sessionId + " from disc", e);
-				}
-				finally
-				{
-					try
-					{
-						if (fis != null)
-						{
-							fis.close();
-						}
-					}
-					catch (IOException ex)
-					{
-						// ignore
-					}
-				}
-			}
-		}
-		return null;
-	}
-
-	/**
-	 * @param id
-	 * @param versionNumber
-	 * @param sessionDir
-	 * @return The file pointing to the page
-	 */
-	private File getPageFile(int id, int versionNumber, File sessionDir)
-	{
-		return new File(sessionDir, Application.get().getApplicationKey() + "-page-" + id
-				+ "-version-" + versionNumber);
-	}
-
-	/**
-	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#removePage(java.lang.String,
-	 *      wicket.Page)
-	 */
-	public void removePage(String sessionId, Page page)
-	{
-		File sessionDir = new File(getWorkDir(), sessionId);
-		if (sessionDir.exists())
-		{
-			File pageFile = getPageFile(page.getNumericId(), page.getCurrentVersionNumber(),
-					sessionDir);
-			if (pageFile.exists())
-			{
-				pageFile.delete();
-			}
-		}
-	}
-
-	/**
-	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#storePage(java.lang.String,
-	 *      wicket.Page)
-	 */
-	public void storePage(String sessionId, Page page)
-	{
-		File sessionDir = new File(getWorkDir(), sessionId);
-		sessionDir.mkdirs();
-		File pageFile = getPageFile(page.getNumericId(), page.getCurrentVersionNumber(), sessionDir);
-		// TODO check can this be called everytime at this place? Putting should
-		// be called after the rendering so it should be ok.
-		page.internalDetach();
-		byte[] bytes = Objects.objectToByteArray(page);
-		FileOutputStream fos = null;
-		try
-		{
-			fos = new FileOutputStream(pageFile);
-			ByteBuffer bb = ByteBuffer.wrap(bytes);
-			fos.getChannel().write(bb);
-		}
-		catch (Exception e)
-		{
-			log.debug("Error saving page " + page.getId() + " with version "
-					+ page.getCurrentVersionNumber() + " for the sessionid " + sessionId
-					+ " from disc", e);
-		}
-		finally
-		{
-			try
-			{
-				if (fos != null)
-				{
-					fos.close();
-				}
-			}
-			catch (IOException ex)
-			{
-				// ignore
-			}
-		}
-
-	}
-
-	/**
-	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#unbind(java.lang.String)
-	 */
-	public void unbind(String sessionId)
-	{
-		File sessionDir = new File(getWorkDir(), sessionId);
-		if (sessionDir.exists())
-		{
-			File[] files = sessionDir.listFiles();
-			if (files != null)
-			{
-				for (int i = 0; i < files.length; i++)
-				{
-					files[i].delete();
-				}
-			}
-			sessionDir.delete();
-		}
-	}
-}
+	 */
+	public Page getPage(String sessionId, int id, int versionNumber)
+	{
+		File sessionDir = new File(getWorkDir(), sessionId);
+		if (sessionDir.exists())
+		{
+			File pageFile = getPageFile(id, versionNumber, sessionDir);
+			if (pageFile.exists())
+			{
+				FileInputStream fis = null;
+				try
+				{
+					byte[] pageData = null;
+					fis = new FileInputStream(pageFile);
+					int length = (int)pageFile.length();
+					ByteBuffer bb = ByteBuffer.allocate(length);
+					fis.getChannel().read(bb);
+					if (bb.hasArray())
+					{
+						pageData = bb.array();
+					}
+					else
+					{
+						pageData = new byte[length];
+						bb.get(pageData);
+					}
+
+					Page page = (Page)Objects.byteArrayToObject(pageData);
+					return page.getVersion(versionNumber);
+				}
+				catch (Exception e)
+				{
+					log.debug("Error loading page " + id + " with version " + versionNumber
+							+ " for the sessionid " + sessionId + " from disc", e);
+				}
+				finally
+				{
+					try
+					{
+						if (fis != null)
+						{
+							fis.close();
+						}
+					}
+					catch (IOException ex)
+					{
+						// ignore
+					}
+				}
+			}
+		}
+		return null;
+	}
+
+	/**
+	 * @param id
+	 * @param versionNumber
+	 * @param sessionDir
+	 * @return The file pointing to the page
+	 */
+	private File getPageFile(int id, int versionNumber, File sessionDir)
+	{
+		return new File(sessionDir, Application.get().getApplicationKey() + "-page-" + id
+				+ "-version-" + versionNumber);
+	}
+
+	/**
+	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#removePage(java.lang.String,
+	 *      wicket.Page)
+	 */
+	public void removePage(String sessionId, Page page)
+	{
+		File sessionDir = new File(getWorkDir(), sessionId);
+		if (sessionDir.exists())
+		{
+			File pageFile = getPageFile(page.getNumericId(), page.getCurrentVersionNumber(),
+					sessionDir);
+			if (pageFile.exists())
+			{
+				pageFile.delete();
+			}
+		}
+	}
+
+	/**
+	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#storePage(java.lang.String,
+	 *      wicket.Page)
+	 */
+	public void storePage(String sessionId, Page page)
+	{
+		File sessionDir = new File(getWorkDir(), sessionId);
+		sessionDir.mkdirs();
+		File pageFile = getPageFile(page.getNumericId(), page.getCurrentVersionNumber(), sessionDir);
+		// TODO check can this be called everytime at this place? Putting should
+		// be called after the rendering so it should be ok.
+		page.internalDetach();
+		FileOutputStream fos = null;
+		try
+		{
+			final ByteArrayOutputStream out = new ByteArrayOutputStream();
+			try
+			{
+				new ObjectOutputStream(out).writeObject(page);
+			}
+			finally
+			{
+				out.close();
+			}
+			byte[] bytes = out.toByteArray();
+			fos = new FileOutputStream(pageFile);
+			ByteBuffer bb = ByteBuffer.wrap(bytes);
+			fos.getChannel().write(bb);
+		}
+		catch (Exception e)
+		{
+			log.error("Error saving page " + page.getId() + " with version "
+					+ page.getCurrentVersionNumber() + " for the sessionid " + sessionId, e);
+		}
+		finally
+		{
+			try
+			{
+				if (fos != null)
+				{
+					fos.close();
+				}
+			}
+			catch (IOException ex)
+			{
+				// ignore
+			}
+		}
+
+	}
+
+	/**
+	 * @see wicket.protocol.http.SecondLevelCacheSessionStore.IPageStore#unbind(java.lang.String)
+	 */
+	public void unbind(String sessionId)
+	{
+		File sessionDir = new File(getWorkDir(), sessionId);
+		if (sessionDir.exists())
+		{
+			File[] files = sessionDir.listFiles();
+			if (files != null)
+			{
+				for (int i = 0; i < files.length; i++)
+				{
+					files[i].delete();
+				}
+			}
+			sessionDir.delete();
+		}
+	}
+}



Mime
View raw message