From c313b8bd78c66e678d871952b9ab4fcc1f6d3796 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 2 Sep 2010 17:10:27 +0000 Subject: [PATCH] INT-1283 simplifying MethodInvokingMessageSource --- .../endpoint/MethodInvokingMessageSource.java | 61 ++++++++----------- ... => MethodInvokingMessageSourceTests.java} | 2 +- 2 files changed, 25 insertions(+), 38 deletions(-) rename spring-integration-core/src/test/java/org/springframework/integration/message/{MethodInvokingSourceTests.java => MethodInvokingMessageSourceTests.java} (98%) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MethodInvokingMessageSource.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MethodInvokingMessageSource.java index 32fd077c80..9f8f1cca89 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MethodInvokingMessageSource.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MethodInvokingMessageSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -16,7 +16,6 @@ package org.springframework.integration.endpoint; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.springframework.beans.factory.InitializingBean; @@ -24,11 +23,8 @@ import org.springframework.integration.Message; import org.springframework.integration.MessagingException; import org.springframework.integration.core.MessageSource; import org.springframework.integration.message.GenericMessage; -import org.springframework.integration.util.DefaultMethodInvoker; -import org.springframework.integration.util.MethodInvoker; -import org.springframework.integration.util.MethodValidator; -import org.springframework.integration.util.NameResolvingMethodInvoker; import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; /** * A {@link MessageSource} implementation that invokes a no-argument method so @@ -44,7 +40,9 @@ public class MethodInvokingMessageSource implements MessageSource, Initi private volatile String methodName; - private volatile MethodInvoker invoker; + private volatile boolean initialized; + + private final Object initializationMonitor = new Object(); public void setObject(Object object) { @@ -55,7 +53,6 @@ public class MethodInvokingMessageSource implements MessageSource, Initi public void setMethod(Method method) { Assert.notNull(method, "'method' must not be null"); this.method = method; - this.methodName = method.getName(); } public void setMethodName(String methodName) { @@ -64,26 +61,29 @@ public class MethodInvokingMessageSource implements MessageSource, Initi } public void afterPropertiesSet() { - if (this.method != null) { - this.invoker = new DefaultMethodInvoker(this.object, this.method); - } - else if (this.methodName != null) { - NameResolvingMethodInvoker nrmi = new NameResolvingMethodInvoker(this.object, this.methodName); - nrmi.setMethodValidator(new MessageReceivingMethodValidator()); - this.invoker = nrmi; - } - else { - throw new IllegalArgumentException("either 'method' or 'methodName' is required"); + synchronized (this.initializationMonitor) { + if (this.initialized) { + return; + } + Assert.notNull(this.object, "object is required"); + Assert.isTrue(this.method != null || this.methodName != null, "method or methodName is required"); + if (this.method == null) { + this.method = ReflectionUtils.findMethod(this.object.getClass(), this.methodName); + } + Assert.isTrue(!void.class.equals(this.method.getReturnType()), + "invalid MessageSource method '"+ this.method.getName() + "', a non-void return is required"); + this.method.setAccessible(true); + this.initialized = true; } } - @SuppressWarnings("unchecked") + @SuppressWarnings({"rawtypes", "unchecked"}) public Message receive() { - if (this.invoker == null) { - this.afterPropertiesSet(); - } try { - Object result = this.invoker.invokeMethod(new Object[] {}); + if (!this.initialized) { + this.afterPropertiesSet(); + } + Object result = ReflectionUtils.invokeMethod(this.method, this.object); if (result == null) { return null; } @@ -92,21 +92,8 @@ public class MethodInvokingMessageSource implements MessageSource, Initi } return new GenericMessage(result); } - catch (InvocationTargetException e) { - throw new MessagingException( - "Source method '" + this.methodName + "' threw an Exception.", e.getTargetException()); - } catch (Throwable e) { - throw new MessagingException("Failed to invoke source method '" + this.methodName + "'."); - } - } - - - private static class MessageReceivingMethodValidator implements MethodValidator { - - public void validate(Method method) { - Assert.isTrue(!method.getReturnType().equals(void.class), - "MethodInvokingSource requires a non-void returning method."); + throw new MessagingException("Failed to invoke MessageSource", e); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingSourceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java similarity index 98% rename from spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingSourceTests.java rename to spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java index ce3ab429f5..283a3027a7 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingSourceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java @@ -28,7 +28,7 @@ import org.springframework.integration.endpoint.MethodInvokingMessageSource; /** * @author Mark Fisher */ -public class MethodInvokingSourceTests { +public class MethodInvokingMessageSourceTests { @Test public void testValidMethod() {