From e890020a1b558a5aacacf91487aea9b3e432a1c6 Mon Sep 17 00:00:00 2001 From: Stefan Reuter Date: Fri, 10 May 2013 14:24:34 +0200 Subject: [PATCH] INT-3011 Groovy Classloader and BeanFactory Fixes See: https://jira.springsource.org/browse/INT-3011 INT-3011 Pass BeanClassLoader and BeanFactory to GroovyScriptFactory INT-3011 Move 'implements BeanFactoryAware, BeanClassLoaderAware' to AbstractScriptExecutingMessageProcessor and GroovyControlBusFactory --- .../groovy/GroovyCommandMessageProcessor.java | 9 +++++- ...GroovyScriptExecutingMessageProcessor.java | 19 ++++++++++-- .../config/GroovyControlBusFactoryBean.java | 22 ++++++++++---- ...stractScriptExecutingMessageProcessor.java | 29 +++++++++++++++++-- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java index 4869be62d0..ce6196da45 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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 @@ -34,6 +34,7 @@ import org.springframework.util.CollectionUtils; * @author Mark Fisher * @author Oleg Zhurakousky * @author Artem Bilan + * @author Stefan Reuter * @since 2.0 */ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor { @@ -108,6 +109,12 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag customizerDecorator.setVariables(variables); } GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator); + if (getBeanClassLoader() != null) { + factory.setBeanClassLoader(getBeanClassLoader()); + } + if (getBeanFactory() != null) { + factory.setBeanFactory(getBeanFactory()); + } Object result = factory.getScriptedObject(scriptSource, null); return (result instanceof GString) ? result.toString() : result; } diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java index ca9ac15275..eb79bc172e 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -20,6 +20,11 @@ import groovy.lang.GString; import java.util.Map; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.InitializingBean; import org.springframework.integration.Message; import org.springframework.integration.scripting.AbstractScriptExecutingMessageProcessor; import org.springframework.integration.scripting.ScriptVariableGenerator; @@ -33,9 +38,10 @@ import org.springframework.util.CollectionUtils; * @author Dave Syer * @author Mark Fisher * @author Oleg Zhurakousky + * @author Stefan Reuter * @since 2.0 */ -public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor { +public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor implements InitializingBean { private final GroovyScriptFactory scriptFactory; @@ -90,4 +96,13 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti } } + @Override + public void afterPropertiesSet() throws Exception { + if (getBeanClassLoader() != null) { + this.scriptFactory.setBeanClassLoader(getBeanClassLoader()); + } + if (getBeanFactory() != null) { + this.scriptFactory.setBeanFactory(getBeanFactory()); + } + } } diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java index 9f06c8634f..9af9aeed24 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/config/GroovyControlBusFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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 @@ -18,9 +18,7 @@ import java.util.Map; 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.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.*; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.Lifecycle; import org.springframework.core.annotation.AnnotationUtils; @@ -41,14 +39,17 @@ import org.springframework.util.CustomizableThreadCreator; * @author Oleg Zhurakousky * @author Mark Fisher * @author Artem Bilan + * @author Stefan Reuter * @since 2.0 */ -public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFactoryBean { +public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFactoryBean implements BeanClassLoaderAware { private volatile Long sendTimeout; private volatile GroovyObjectCustomizer customizer; + private volatile ClassLoader beanClassLoader; + public void setSendTimeout(Long sendTimeout) { this.sendTimeout = sendTimeout; } @@ -57,6 +58,11 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac this.customizer = customizer; } + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + @Override protected MessageHandler createHandler() { Binding binding = new ManagedBeansBinding(this.getBeanFactory()); @@ -70,6 +76,12 @@ public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFac if (this.customizer != null) { processor.setCustomizer(this.customizer); } + if (this.beanClassLoader != null) { + processor.setBeanClassLoader(beanClassLoader); + } + if (getBeanFactory() != null) { + processor.setBeanFactory(getBeanFactory()); + } return this.configureHandler(new ServiceActivatingHandler(processor)); } diff --git a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java index 9446d2e2f7..06300b0554 100644 --- a/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java +++ b/spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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 @@ -15,6 +15,10 @@ package org.springframework.integration.scripting; import java.util.Map; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.integration.Message; import org.springframework.integration.MessageHandlingException; import org.springframework.integration.handler.MessageProcessor; @@ -25,12 +29,16 @@ import org.springframework.util.Assert; * Base {@link MessageProcessor} for scripting implementations to extend. * * @author Mark Fisher + * @author Stefan Reuter * @since 2.0 */ -public abstract class AbstractScriptExecutingMessageProcessor implements MessageProcessor { +public abstract class AbstractScriptExecutingMessageProcessor implements MessageProcessor, BeanClassLoaderAware, BeanFactoryAware { private final ScriptVariableGenerator scriptVariableGenerator; + private volatile ClassLoader beanClassLoader; + + private volatile BeanFactory beanFactory; protected AbstractScriptExecutingMessageProcessor() { this.scriptVariableGenerator = new DefaultScriptVariableGenerator(); @@ -56,6 +64,23 @@ public abstract class AbstractScriptExecutingMessageProcessor implements Mess } } + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + + protected ClassLoader getBeanClassLoader() { + return this.beanClassLoader; + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } + + protected BeanFactory getBeanFactory() { + return this.beanFactory; + } /** * Subclasses must implement this method to create a script source,