You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2020/12/15 09:26:44 UTC

[wicket] branch master updated (b269b6d -> b68a955)

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

mgrigorov pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git.


    from b269b6d  Add java.io to the opens for running the tests
     new 2bd6d7c  Replace usage of Reflection with StackWalker when resolving the latest user defined class loader in JavaSerializer
     new b68a955  Improve logging in JavaSerializer

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../wicket/serialize/java/JavaSerializer.java      | 63 ++++++++++++----------
 1 file changed, 36 insertions(+), 27 deletions(-)


[wicket] 01/02: Replace usage of Reflection with StackWalker when resolving the latest user defined class loader in JavaSerializer

Posted by mg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 2bd6d7c54ff0cdac9f19fb9709a6e1eaecea06c1
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Tue Dec 15 11:25:29 2020 +0200

    Replace usage of Reflection with StackWalker when resolving the latest user defined class loader in JavaSerializer
---
 .../wicket/serialize/java/JavaSerializer.java      | 36 ++++++++++++++--------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java b/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
index 7f8dbce..4628999 100644
--- a/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
@@ -26,10 +26,11 @@ import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.OutputStream;
 import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Proxy;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Objects;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.ThreadContext;
@@ -53,6 +54,19 @@ import org.slf4j.LoggerFactory;
 public class JavaSerializer implements ISerializer
 {
 	private static final Logger log = LoggerFactory.getLogger(JavaSerializer.class);
+
+	private static final StackWalker STACKWALKER;
+	private static final ClassLoader PLATFORM_CLASS_LOADER;
+
+	static {
+		PrivilegedAction<StackWalker> pa1 =
+				() -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
+		PrivilegedAction<ClassLoader> pa2 = ClassLoader::getPlatformClassLoader;
+		STACKWALKER = AccessController.doPrivileged(pa1);
+		PLATFORM_CLASS_LOADER = AccessController.doPrivileged(pa2);
+	}
+
+
 	/**
 	 * The key of the application which can be used later to find the proper {@link IClassResolver}
 	 */
@@ -321,22 +335,20 @@ public class JavaSerializer implements ISerializer
 			}
 		}
 
-		/*
-		 * Method in the superclass is private, call it via reflection.
-		 */
 		private static ClassLoader latestUserDefinedLoader()
 		{
 			try
 			{
-				Method originalMethod =
-					ObjectInputStream.class.getDeclaredMethod("latestUserDefinedLoader");
-				originalMethod.setAccessible(true);
-				return (ClassLoader) originalMethod.invoke(null);
+				return STACKWALKER.walk(s ->
+                    s.map(StackWalker.StackFrame::getDeclaringClass)
+                     .map(Class::getClassLoader)
+                     .filter(Objects::nonNull)
+                     .filter(cl -> !PLATFORM_CLASS_LOADER.equals(cl))
+                     .findFirst()
+                     .orElse(PLATFORM_CLASS_LOADER));
 			}
-			catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
-					| NoSuchMethodException | SecurityException e)
+			catch (IllegalArgumentException | SecurityException e)
 			{
-				// should not happen
 				throw new WicketRuntimeException(e);
 			}
 		}


[wicket] 02/02: Improve logging in JavaSerializer

Posted by mg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit b68a9554c6ca54bb5bdd1f54a2b60a71b869a32a
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Tue Dec 15 11:26:15 2020 +0200

    Improve logging in JavaSerializer
---
 .../wicket/serialize/java/JavaSerializer.java      | 27 ++++++++++------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java b/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
index 4628999..62ded9a 100644
--- a/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/serialize/java/JavaSerializer.java
@@ -111,8 +111,8 @@ public class JavaSerializer implements ISerializer
 		}
 		catch (Exception e)
 		{
-			log.error("Error serializing object " + object.getClass() + " [object=" + object + "]",
-				e);
+			log.error("Error serializing object {} [object={}]",
+			          object.getClass(), object, e);
 		}
 		return null;
 	}
@@ -155,7 +155,7 @@ public class JavaSerializer implements ISerializer
 		}
 		catch (ClassNotFoundException | IOException cnfx)
 		{
-			throw new RuntimeException("Could not deserialize object from byte[]", cnfx);
+			throw new WicketRuntimeException("Could not deserialize object from byte[]", cnfx);
 		}
 		finally
 		{
@@ -292,7 +292,7 @@ public class JavaSerializer implements ISerializer
 			{
 				// ignore this exception.
 				log.debug(
-					"Proxy Class not found by the object outputstream itself, trying the IClassResolver");
+					"Proxy Class not found by the ObjectOutputStream itself, trying the IClassResolver");
 
 				ClassLoader latestLoader = latestUserDefinedLoader();
 				ClassLoader nonPublicLoader = null;
@@ -392,18 +392,15 @@ public class JavaSerializer implements ISerializer
 						CheckingObjectOutputStream checkingObjectOutputStream =
 							new CheckingObjectOutputStream(outputStream, new ObjectSerializationChecker(nsx));
 						checkingObjectOutputStream.writeObject(obj);
-					} 
+					}
+					catch (CheckingObjectOutputStream.ObjectCheckException x)
+					{
+						throw x;
+					}
 					catch (Exception x)
 					{
-						if (x instanceof CheckingObjectOutputStream.ObjectCheckException)
-						{
-							throw (CheckingObjectOutputStream.ObjectCheckException) x;
-						}
-						else
-						{
-							x.initCause(nsx);
-							throw new WicketRuntimeException("A problem occurred while trying to collect debug information about not serializable object", x);
-						}
+						x.initCause(nsx);
+						throw new WicketRuntimeException("A problem occurred while trying to collect debug information about not serializable object", x);
 					}
 
 					// if we get here, we didn't fail, while we should
@@ -413,7 +410,7 @@ public class JavaSerializer implements ISerializer
 			}
 			catch (Exception e)
 			{
-				log.error("error writing object " + obj + ": " + e.getMessage(), e);
+				log.error("error writing object {} : {}", obj, e.getMessage(), e);
 				throw new WicketRuntimeException(e);
 			}
 		}