You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2021/11/22 20:33:27 UTC

[wicket] branch wicket-9.x updated: WICKET-6936 allow alternative read and write

This is an automated email from the ASF dual-hosted git repository.

svenmeier pushed a commit to branch wicket-9.x
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/wicket-9.x by this push:
     new e7dc93b  WICKET-6936 allow alternative read and write
e7dc93b is described below

commit e7dc93b4997faaba9cdbe49399ff856e00f8b0c5
Author: Sven Meier <sv...@apache.org>
AuthorDate: Mon Nov 22 20:43:50 2021 +0100

    WICKET-6936 allow alternative read and write
---
 .../org/apache/wicket/pageStore/FilePageStore.java | 114 ++++++++++++---------
 .../apache/wicket/pageStore/FilePageStoreTest.java |   1 -
 2 files changed, 68 insertions(+), 47 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/pageStore/FilePageStore.java b/wicket-core/src/main/java/org/apache/wicket/pageStore/FilePageStore.java
index 4462b1b..38a8d0a 100644
--- a/wicket-core/src/main/java/org/apache/wicket/pageStore/FilePageStore.java
+++ b/wicket-core/src/main/java/org/apache/wicket/pageStore/FilePageStore.java
@@ -102,43 +102,50 @@ public class FilePageStore extends AbstractPersistentPageStore implements IPersi
 	@Override
 	protected IManageablePage getPersistedPage(String sessionIdentifier, int id)
 	{
-		byte[] data = readFile(sessionIdentifier, id);
-		if (data == null)
+		File file = getPageFile(sessionIdentifier, id, false);
+		if (file.exists() == false)
 		{
 			return null;
 		}
-		
-		return new SerializedPage(id, "unknown", data);
-	}
 
-	private byte[] readFile(String sessionIdentifier, int id)
-	{
-		File file = getPageFile(sessionIdentifier, id, false);
-		if (file.exists() == false)
+		byte[] data;
+		try
+		{
+			data = readFile(file);
+		}
+		catch (IOException ex)
 		{
+			log.warn("cannot read page data for session {} page {}", sessionIdentifier, id, ex);
 			return null;
 		}
 
-		byte[] data = null;
+		return new SerializedPage(id, "unknown", data);
+	}
+
+	/**
+	 * Read a file.
+	 * <p>
+	 * Note: This implementation uses a {@link FileChannel}.
+	 * 
+	 * @param file
+	 *            file to read
+	 * @throws IOException
+	 */
+	protected byte[] readFile(File file) throws IOException
+	{
+		byte[] data;
 
+		FileChannel channel = FileChannel.open(file.toPath());
 		try
 		{
-			FileChannel channel = FileChannel.open(file.toPath());
-			try
-			{
-				int size = (int)channel.size();
-				MappedByteBuffer buf = channel.map(MapMode.READ_ONLY, 0, size);
-				data = new byte[size];
-				buf.get(data);
-			}
-			finally
-			{
-				IOUtils.closeQuietly(channel);
-			}
+			int size = (int)channel.size();
+			MappedByteBuffer buf = channel.map(MapMode.READ_ONLY, 0, size);
+			data = new byte[size];
+			buf.get(data);
 		}
-		catch (IOException ex)
+		finally
 		{
-			log.warn("cannot read page data for session {} page {}", sessionIdentifier, id, ex);
+			IOUtils.closeQuietly(channel);
 		}
 
 		return data;
@@ -170,39 +177,54 @@ public class FilePageStore extends AbstractPersistentPageStore implements IPersi
 		{
 			throw new WicketRuntimeException("FilePageStore works with serialized pages only");
 		}
-		SerializedPage serializedPage = (SerializedPage) page;
+		SerializedPage serializedPage = (SerializedPage)page;
 
-		String type = serializedPage.getPageType();
 		byte[] data = serializedPage.getData();
 
-		writeFile(sessionIdentifier, serializedPage.getPageId(), type, data);
+		File file = getPageFile(sessionIdentifier, serializedPage.getPageId(), true);
+		try
+		{
+			writeFile(file, data);
+		}
+		catch (IOException ex)
+		{
+			log.warn("cannot store page data for session {} page {}", sessionIdentifier,
+				serializedPage.getPageId(), ex);
+		}
+
+		setPageType(file, serializedPage.getPageType());
 
 		checkMaxSize(sessionIdentifier);
 	}
 
-	private void writeFile(String sessionIdentifier, int pageId, String pageType, byte[] data)
+	/**
+	 * Write a file with given data.
+	 * <p>
+	 * Note: This implementation uses a {@link FileChannel} with
+	 * {@link StandardOpenOption#TRUNCATE_EXISTING}. This might fail on Windows systems with
+	 * "The requested operation cannot be performed on a file with a user-mapped section open", so subclasses
+	 * can omit this option to circumvent this error, although this prevents files from shrinking when pages become smaller.
+	 * Alternatively a completely different implementation can be chosen.
+	 * 
+	 * @param file
+	 *            file to write
+	 * @param data
+	 *            data to write
+	 * @throws IOException
+	 */
+	protected void writeFile(File file, byte[] data) throws IOException
 	{
-		File file = getPageFile(sessionIdentifier, pageId, true);
+		FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE,
+			StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
 		try
 		{
-			FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE,
-				StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
-			try
-			{
-				ByteBuffer buffer = ByteBuffer.wrap(data);
-				channel.write(buffer);
-			}
-			finally
-			{
-				IOUtils.closeQuietly(channel);
-			}
+			ByteBuffer buffer = ByteBuffer.wrap(data);
+			channel.write(buffer);
 		}
-		catch (IOException ex)
+		finally
 		{
-			log.warn("cannot store page data for session {} page {}", sessionIdentifier, pageId, ex);
+			IOUtils.closeQuietly(channel);
 		}
-
-		setPageType(file, pageType);
 	}
 
 	private void checkMaxSize(String sessionIdentifier)
@@ -320,7 +342,7 @@ public class FilePageStore extends AbstractPersistentPageStore implements IPersi
 	/**
 	 * Set the type of page on the given file.
 	 * <p>
-	 * This is an optional operation that silently fails in case of an error. 
+	 * This is an optional operation that silently fails in case of an error.
 	 * 
 	 * @param file
 	 * @param pageType
@@ -330,7 +352,7 @@ public class FilePageStore extends AbstractPersistentPageStore implements IPersi
 		try
 		{
 			UserDefinedFileAttributeView view = getAttributeView(file);
-			
+
 			view.write(ATTRIBUTE_PAGE_TYPE, Charset.defaultCharset().encode(pageType));
 		}
 		catch (Exception ex)
diff --git a/wicket-core/src/test/java/org/apache/wicket/pageStore/FilePageStoreTest.java b/wicket-core/src/test/java/org/apache/wicket/pageStore/FilePageStoreTest.java
index 777a1d5..2988649 100644
--- a/wicket-core/src/test/java/org/apache/wicket/pageStore/FilePageStoreTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/pageStore/FilePageStoreTest.java
@@ -20,7 +20,6 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 
-import org.apache.wicket.serialize.java.JavaSerializer;
 import org.apache.wicket.util.WicketTestTag;
 import org.apache.wicket.util.lang.Bytes;
 import org.junit.jupiter.api.Tag;