From 84310c8a11d010abb5671692028891e4b61ef7b3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 21 Jan 2014 12:49:16 +0100 Subject: [PATCH] RemoteInvocation(Result) explicitly designed for JavaBean-style deserialization Issue: SPR-11337 --- .../remoting/support/RemoteInvocation.java | 25 +++++++----- .../support/RemoteInvocationResult.java | 38 ++++++++++++++++++- .../remoting/rmi/RmiSupportTests.java | 18 ++++----- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocation.java b/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocation.java index 677438f0623..f0ec57a72ae 100644 --- a/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocation.java +++ b/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocation.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,9 @@ import org.springframework.util.ClassUtils; *

This is an SPI class, typically not used directly by applications. * Can be subclassed for additional invocation parameters. * + *

Both {@link RemoteInvocation} and {@link RemoteInvocationResult} are designed + * for use with standard Java serialization as well as JavaBean-style serialization. + * * @author Juergen Hoeller * @since 25.02.2004 * @see RemoteInvocationResult @@ -59,9 +62,13 @@ public class RemoteInvocation implements Serializable { /** - * Create a new RemoteInvocation for use as JavaBean. + * Create a new RemoteInvocation for the given AOP method invocation. + * @param methodInvocation the AOP invocation to convert */ - public RemoteInvocation() { + public RemoteInvocation(MethodInvocation methodInvocation) { + this.methodName = methodInvocation.getMethod().getName(); + this.parameterTypes = methodInvocation.getMethod().getParameterTypes(); + this.arguments = methodInvocation.getArguments(); } /** @@ -77,18 +84,16 @@ public class RemoteInvocation implements Serializable { } /** - * Create a new RemoteInvocation for the given AOP method invocation. - * @param methodInvocation the AOP invocation to convert + * Create a new RemoteInvocation for JavaBean-style deserialization + * (e.g. with Jackson). */ - public RemoteInvocation(MethodInvocation methodInvocation) { - this.methodName = methodInvocation.getMethod().getName(); - this.parameterTypes = methodInvocation.getMethod().getParameterTypes(); - this.arguments = methodInvocation.getArguments(); + public RemoteInvocation() { } /** * Set the name of the target method. + *

This setter is intended for JavaBean-style deserialization. */ public void setMethodName(String methodName) { this.methodName = methodName; @@ -103,6 +108,7 @@ public class RemoteInvocation implements Serializable { /** * Set the parameter types of the target method. + *

This setter is intended for JavaBean-style deserialization. */ public void setParameterTypes(Class[] parameterTypes) { this.parameterTypes = parameterTypes; @@ -117,6 +123,7 @@ public class RemoteInvocation implements Serializable { /** * Set the arguments for the target method call. + *

This setter is intended for JavaBean-style deserialization. */ public void setArguments(Object[] arguments) { this.arguments = arguments; diff --git a/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocationResult.java b/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocationResult.java index 3348b7a839f..e9dee2f72bd 100644 --- a/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocationResult.java +++ b/spring-context/src/main/java/org/springframework/remoting/support/RemoteInvocationResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,9 @@ import java.lang.reflect.InvocationTargetException; *

This is an SPI class, typically not used directly by applications. * Can be subclassed for additional invocation parameters. * + *

Both {@link RemoteInvocation} and {@link RemoteInvocationResult} are designed + * for use with standard Java serialization as well as JavaBean-style serialization. + * * @author Juergen Hoeller * @since 1.1 * @see RemoteInvocation @@ -59,6 +62,26 @@ public class RemoteInvocationResult implements Serializable { this.exception = exception; } + /** + * Create a new RemoteInvocationResult for JavaBean-style deserialization + * (e.g. with Jackson). + * @see #setValue + * @see #setException + */ + public RemoteInvocationResult() { + } + + + /** + * Set the result value returned by a successful invocation of the + * target method, if any. + *

This setter is intended for JavaBean-style deserialization. + * Use {@link #RemoteInvocationResult(Object)} otherwise. + * @see #RemoteInvocationResult() + */ + public void setValue(Object value) { + this.value = value; + } /** * Return the result value returned by a successful invocation @@ -69,6 +92,17 @@ public class RemoteInvocationResult implements Serializable { return this.value; } + /** + * Set the exception thrown by an unsuccessful invocation of the + * target method, if any. + *

This setter is intended for JavaBean-style deserialization. + * Use {@link #RemoteInvocationResult(Throwable)} otherwise. + * @see #RemoteInvocationResult() + */ + public void setException(Throwable exception) { + this.exception = exception; + } + /** * Return the exception thrown by an unsuccessful invocation * of the target method, if any. @@ -81,7 +115,7 @@ public class RemoteInvocationResult implements Serializable { /** * Return whether this invocation result holds an exception. * If this returns {@code false}, the result value applies - * (even if {@code null}). + * (even if it is {@code null}). * @see #getValue * @see #getException */ diff --git a/spring-context/src/test/java/org/springframework/remoting/rmi/RmiSupportTests.java b/spring-context/src/test/java/org/springframework/remoting/rmi/RmiSupportTests.java index 1c492f3759a..d4e98e01da8 100644 --- a/spring-context/src/test/java/org/springframework/remoting/rmi/RmiSupportTests.java +++ b/spring-context/src/test/java/org/springframework/remoting/rmi/RmiSupportTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -180,8 +180,8 @@ public class RmiSupportTests extends TestCase { } catch (RemoteProxyFailureException ex) { assertTrue(ex.getCause() instanceof NoSuchMethodException); - assertTrue(ex.getMessage().indexOf("setOtherName") != -1); - assertTrue(ex.getMessage().indexOf("IWrongBusinessBean") != -1); + assertTrue(ex.getMessage().contains("setOtherName")); + assertTrue(ex.getMessage().contains("IWrongBusinessBean")); } assertEquals(1, factory.counter); } @@ -319,7 +319,7 @@ public class RmiSupportTests extends TestCase { // let's see if the remote invocation object works final RemoteBean rb = new RemoteBean(); - final Method setNameMethod = rb.getClass().getDeclaredMethod("setName", new Class[] {String.class}); + final Method setNameMethod = rb.getClass().getDeclaredMethod("setName", String.class); MethodInvocation mi = new MethodInvocation() { @Override @@ -388,8 +388,8 @@ public class RmiSupportTests extends TestCase { IBusinessBean proxy = (IBusinessBean) factory.getObject(); // shouldn't go through to remote service - assertTrue(proxy.toString().indexOf("RMI invoker") != -1); - assertTrue(proxy.toString().indexOf(serviceUrl) != -1); + assertTrue(proxy.toString().contains("RMI invoker")); + assertTrue(proxy.toString().contains(serviceUrl)); assertEquals(proxy.hashCode(), proxy.hashCode()); assertTrue(proxy.equals(proxy)); @@ -444,11 +444,11 @@ public class RmiSupportTests extends TestCase { @Override public void setName(String nam) throws RemoteException { if (nam != null && nam.endsWith("Exception")) { - RemoteException rex = null; + RemoteException rex; try { Class exClass = Class.forName(nam); - Constructor ctor = exClass.getConstructor(new Class[] {String.class}); - rex = (RemoteException) ctor.newInstance(new Object[] {"myMessage"}); + Constructor ctor = exClass.getConstructor(String.class); + rex = (RemoteException) ctor.newInstance("myMessage"); } catch (Exception ex) { throw new RemoteException("Illegal exception class name: " + nam, ex);