Merge pull request #803 from srt/INT-3011

* INT-3011:
  INT-3011 Groovy Classloader and BeanFactory Fixes
This commit is contained in:
Gary Russell
2013-05-20 17:50:02 -04:00
4 changed files with 69 additions and 10 deletions

View File

@@ -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<Object> {
@@ -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;
}

View File

@@ -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<Object> {
public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> 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());
}
}
}

View File

@@ -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<MessageHandler> {
public class GroovyControlBusFactoryBean extends AbstractSimpleMessageHandlerFactoryBean<MessageHandler> 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));
}

View File

@@ -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<T> implements MessageProcessor<T> {
public abstract class AbstractScriptExecutingMessageProcessor<T> implements MessageProcessor<T>, 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<T> 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,