INT-2631: remove BD logic from GroovyControlBus
* make 'Groovy Control Bus' similar to 'Expression Control Bus' * add tests about `abstract` & `prototype` Beans * fix about lost `groovy.lang.Script#println()` functionality in the `GroovyControlBusFactoryBean$ManagedBeansBinding` * polishing `GroovyControlBusDelayerManagementTest` to show abilities & fixes with this issue JIRA: https://jira.springsource.org/browse/INT-2631 INT-2631: polishing according to PR comments
This commit is contained in:
committed by
Gary Russell
parent
f54bb340d6
commit
3757592f2a
@@ -20,7 +20,7 @@ import groovy.lang.Binding;
|
||||
import groovy.lang.MissingPropertyException;
|
||||
import org.springframework.beans.factory.BeanCreationNotAllowedException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
@@ -86,7 +86,7 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
|
||||
* in the original Groovy script {@link Binding}.
|
||||
* In additionally beans should be 'managed' with specific properties which
|
||||
* are allowed in the Control Bus operations.
|
||||
*/
|
||||
*/
|
||||
private static class ManagedBeansBinding extends Binding {
|
||||
|
||||
private final ConfigurableListableBeanFactory beanFactory;
|
||||
@@ -108,14 +108,19 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac
|
||||
if (this.beanFactory == null) {
|
||||
throw new MissingPropertyException(name, this.getClass());
|
||||
}
|
||||
BeanDefinition def = this.beanFactory.getBeanDefinition(name);
|
||||
if (!def.isAbstract() && !def.isPrototype()) {
|
||||
Object bean = this.beanFactory.getBean(name);
|
||||
if (bean instanceof Lifecycle ||
|
||||
bean instanceof CustomizableThreadCreator ||
|
||||
(AnnotationUtils.findAnnotation(bean.getClass(), ManagedResource.class) != null)) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
Object bean = null;
|
||||
try {
|
||||
bean = this.beanFactory.getBean(name);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException e) {
|
||||
throw new MissingPropertyException(name, this.getClass(), e);
|
||||
}
|
||||
|
||||
if (bean instanceof Lifecycle ||
|
||||
bean instanceof CustomizableThreadCreator ||
|
||||
(AnnotationUtils.findAnnotation(bean.getClass(), ManagedResource.class) != null)) {
|
||||
return bean;
|
||||
}
|
||||
throw new BeanCreationNotAllowedException(name, "Only beans with @ManagedResource or beans which implement " +
|
||||
"org.springframework.context.Lifecycle or org.springframework.util.CustomizableThreadCreator " +
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import org.springframework.integration.test.util.TestUtils
|
||||
import org.springframework.integration.handler.DelayHandler
|
||||
|
||||
def delayHandler = TestUtils.getPropertyValue(testDelayer, 'handler', DelayHandler)
|
||||
|
||||
def delayHandler = this.'testDelayer.handler'
|
||||
def delayedMessageCount = delayHandler.delayedMessageCount
|
||||
|
||||
println delayedMessageCount
|
||||
|
||||
assert 2 == delayedMessageCount
|
||||
|
||||
delayHandler.reschedulePersistedMessages()
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
package org.springframework.integration.groovy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
|
||||
@@ -35,6 +35,12 @@
|
||||
|
||||
<beans:bean id="nonManagedService" class="org.springframework.integration.groovy.config.GroovyControlBusTests$NonManagedService" />
|
||||
|
||||
<beans:bean id="abstractService" class="org.springframework.integration.groovy.config.GroovyControlBusTests$Service"
|
||||
abstract="true"/>
|
||||
|
||||
<beans:bean id="prototypeService" class="org.springframework.integration.groovy.config.GroovyControlBusTests$Service"
|
||||
scope="prototype"/>
|
||||
|
||||
<beans:bean id="groovyCustomizer"
|
||||
class="org.springframework.integration.groovy.config.GroovyControlBusTests$MyGroovyCustomizer"/>
|
||||
|
||||
|
||||
@@ -21,12 +21,16 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import groovy.lang.GroovyObject;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanCreationNotAllowedException;
|
||||
import org.springframework.beans.factory.BeanIsAbstractException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
@@ -41,8 +45,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -115,6 +117,27 @@ public class GroovyControlBusTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test //INT-2631
|
||||
public void testFailOperationOnAbstractBean() {
|
||||
try {
|
||||
Message<?> message = MessageBuilder.withPayload("abstractService.convert('testString')").build();
|
||||
this.input.send(message);
|
||||
fail("Expected BeanIsAbstractException");
|
||||
}
|
||||
catch (MessageHandlingException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertTrue("Expected BeanIsAbstractException, got " + cause.getClass() + ":" + cause.getMessage(), cause instanceof BeanIsAbstractException);
|
||||
assertTrue(cause.getMessage().contains("abstractService"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test //INT-2631
|
||||
public void testOperationOnPrototypeBean() {
|
||||
Message<?> message = MessageBuilder.withPayload("def result = prototypeService.convert('testString')").build();
|
||||
this.input.send(message);
|
||||
assertEquals("cat", output.receive(0).getPayload());
|
||||
}
|
||||
|
||||
@ManagedResource
|
||||
public static class Service {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user