From e9df7252195e4da33138ee4da0c724154646a9f0 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 28 Dec 2007 16:43:15 +0000 Subject: [PATCH] Added MethodInvokingSource. --- .../adapter/MethodInvokingSource.java | 76 +++++++++++++++++++ .../endpoint/SimpleMethodInvoker.java | 1 + .../adapter/MethodInvokingSourceTests.java | 59 ++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingSource.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingSourceTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingSource.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingSource.java new file mode 100644 index 0000000000..e7b6d23d72 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/MethodInvokingSource.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.adapter; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.integration.MessagingConfigurationException; +import org.springframework.integration.endpoint.MethodValidator; +import org.springframework.integration.endpoint.SimpleMethodInvoker; +import org.springframework.util.Assert; + +/** + * A pollable source that invokes a no-argument method so that its return value + * may be sent to a channel. + * + * @author Mark Fisher + */ +public class MethodInvokingSource implements PollableSource, InitializingBean { + + private T object; + + private String method; + + private SimpleMethodInvoker invoker; + + + public void setObject(T object) { + Assert.notNull(object, "'object' must not be null"); + this.object = object; + } + + public void setMethod(String method) { + Assert.notNull(method, "'method' must not be null"); + this.method = method; + } + + public void afterPropertiesSet() { + this.invoker = new SimpleMethodInvoker(this.object, this.method); + this.invoker.setMethodValidator(new MessageReceivingMethodValidator()); + } + + public Collection poll(int limit) { + if (this.invoker == null) { + this.afterPropertiesSet(); + } + return Arrays.asList(this.invoker.invokeMethod(new Object[] {})); + } + + + private static class MessageReceivingMethodValidator implements MethodValidator { + + public void validate(Method method) { + if (method.getReturnType().equals(void.class)) { + throw new MessagingConfigurationException("MethodInvokingSource requires a non-void returning method."); + } + } + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/SimpleMethodInvoker.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/SimpleMethodInvoker.java index c791613a3e..0bcd3ac724 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/SimpleMethodInvoker.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/SimpleMethodInvoker.java @@ -55,6 +55,7 @@ public class SimpleMethodInvoker { methodInvoker.setTargetMethod(this.method); methodInvoker.setArguments(args); methodInvoker.prepare(); + methodInvoker.getPreparedMethod().setAccessible(true); if (this.methodValidator != null) { this.methodValidator.validate(methodInvoker.getPreparedMethod()); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingSourceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingSourceTests.java new file mode 100644 index 0000000000..c4f1c54306 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/MethodInvokingSourceTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.adapter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Collection; + +import org.junit.Test; + +import org.springframework.integration.MessageDeliveryException; + +/** + * @author Mark Fisher + */ +public class MethodInvokingSourceTests { + + @Test + public void testSingleObjectResult() { + MethodInvokingSource source = new MethodInvokingSource(); + source.setObject(new TestBean()); + source.setMethod("foo"); + Collection result = source.poll(5); + assertNotNull(result); + assertEquals("bar", result.iterator().next()); + } + + @Test(expected=MessageDeliveryException.class) + public void testInvalidMethod() { + MethodInvokingSource source = new MethodInvokingSource(); + source.setObject(new TestBean()); + source.setMethod("boo"); + source.poll(5); + } + + + private static class TestBean { + + public String foo() { + return "bar"; + } + } + +}