INT-1727 polishing

This commit is contained in:
Mark Fisher
2011-02-04 10:20:17 -05:00
parent 1b44da28c2
commit 8c50f02555
3 changed files with 42 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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
@@ -29,13 +29,15 @@ import org.springframework.util.Assert;
* @since 2.0
*/
public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
private final ScriptVariableGenerator scriptVariableSource;
public GroovyCommandMessageProcessor(ScriptVariableGenerator scriptVariableSource) {
this.scriptVariableSource = scriptVariableSource;
private final ScriptVariableGenerator scriptVariableGenerator;
public GroovyCommandMessageProcessor(ScriptVariableGenerator scriptVariableGenerator) {
this.scriptVariableGenerator = scriptVariableGenerator;
}
@Override
protected ScriptSource getScriptSource(Message<?> message) {
Object payload = message.getPayload();
@@ -47,10 +49,10 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
@Override
protected Object executeScript(ScriptSource scriptSource, Message<?> message) throws Exception {
Assert.notNull(scriptSource, "scriptSource must not be null");
MapResolvingBindingCustomizer bindingCustomizer = new MapResolvingBindingCustomizer();
GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), bindingCustomizer);
if (this.scriptVariableSource != null){
bindingCustomizer.setResolvedScriptVariables(this.scriptVariableSource.generateScriptVariables(message));
VariableBindingGroovyObjectCustomizer customizer = new VariableBindingGroovyObjectCustomizer();
GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizer);
if (this.scriptVariableGenerator != null) {
customizer.setVariables(this.scriptVariableGenerator.generateScriptVariables(message));
}
Object result = factory.getScriptedObject(scriptSource, null);
return (result instanceof GString) ? result.toString() : result;
@@ -60,4 +62,5 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
// Don't use the same script (class) name for all invocations by default
return getClass().getSimpleName() + message.getHeaders().getId().toString().replaceAll("-", "");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -18,11 +18,14 @@ package org.springframework.integration.groovy;
import groovy.lang.GString;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.handler.AbstractScriptExecutingMessageProcessor;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.groovy.GroovyScriptFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* @author Dave Syer
@@ -34,11 +37,12 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
private final GroovyScriptFactory scriptFactory;
private final MapResolvingBindingCustomizer customizer = new MapResolvingBindingCustomizer();
private final VariableBindingGroovyObjectCustomizer customizer = new VariableBindingGroovyObjectCustomizer();
private volatile ScriptSource scriptSource;
protected final ScriptVariableGenerator scriptVariableSource;
private final ScriptVariableGenerator scriptVariableGenerator;
/**
* Create a processor for the given {@link ScriptSource}.
@@ -46,13 +50,15 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource) {
this(scriptSource, new DefaultScriptVariableGenerator());
}
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableSource) {
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator) {
this.scriptSource = scriptSource;
this.scriptVariableSource = scriptVariableSource;
this.scriptVariableGenerator = (scriptVariableGenerator != null) ? scriptVariableGenerator
: new DefaultScriptVariableGenerator();
this.scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), this.customizer);
}
@Override
protected ScriptSource getScriptSource(Message<?> message) {
return this.scriptSource;
@@ -61,12 +67,14 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
@Override
protected Object executeScript(ScriptSource scriptSource, Message<?> message) throws Exception {
Assert.notNull(scriptSource, "scriptSource must not be null");
Map<String, Object> scriptVariables = this.scriptVariableGenerator.generateScriptVariables(message);
synchronized (this) {
if (this.scriptVariableSource != null){
this.customizer.setResolvedScriptVariables(this.scriptVariableSource.generateScriptVariables(message));
}
if (!CollectionUtils.isEmpty(scriptVariables)) {
this.customizer.setVariables(scriptVariables);
}
Object result = this.scriptFactory.getScriptedObject(scriptSource, null);
return (result instanceof GString) ? result.toString() : result;
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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
@@ -25,25 +25,26 @@ import org.springframework.util.Assert;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.0
*/
class MapResolvingBindingCustomizer implements GroovyObjectCustomizer {
class VariableBindingGroovyObjectCustomizer implements GroovyObjectCustomizer {
private volatile Map<String, ?> resolvedScriptVariables;
private volatile Map<String, ?> variables;
public void setResolvedScriptVariables(Map<String, ?> resolvedScriptVariables) {
this.resolvedScriptVariables = resolvedScriptVariables;
public void setVariables(Map<String, ?> variables) {
this.variables = variables;
}
public void customize(GroovyObject goo) {
Assert.state(goo instanceof Script, "Expected a Script");
if (this.resolvedScriptVariables != null) {
if (this.variables != null) {
Binding binding = ((Script) goo).getBinding();
for (String key : this.resolvedScriptVariables.keySet()) {
binding.setVariable(key, this.resolvedScriptVariables.get(key));
for (Map.Entry<String, ?> entry : this.variables.entrySet()) {
binding.setVariable(entry.getKey(), entry.getValue());
}
}
}
}
}