From 8c50f02555e376474e04d69cf29f5b844a667d17 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 4 Feb 2011 10:20:17 -0500 Subject: [PATCH] INT-1727 polishing --- .../groovy/GroovyCommandMessageProcessor.java | 23 +++++++------- ...GroovyScriptExecutingMessageProcessor.java | 30 ++++++++++++------- ...ariableBindingGroovyObjectCustomizer.java} | 19 ++++++------ 3 files changed, 42 insertions(+), 30 deletions(-) rename spring-integration-groovy/src/main/java/org/springframework/integration/groovy/{MapResolvingBindingCustomizer.java => VariableBindingGroovyObjectCustomizer.java} (66%) 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 da0b7ce62c..39faaa5989 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-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 { - - 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("-", ""); } + } 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 6449b34f13..e1c53fcf9a 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-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 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; } - } + } + } diff --git a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/MapResolvingBindingCustomizer.java b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/VariableBindingGroovyObjectCustomizer.java similarity index 66% rename from spring-integration-groovy/src/main/java/org/springframework/integration/groovy/MapResolvingBindingCustomizer.java rename to spring-integration-groovy/src/main/java/org/springframework/integration/groovy/VariableBindingGroovyObjectCustomizer.java index 26a1da4d0c..89582372cd 100644 --- a/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/MapResolvingBindingCustomizer.java +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/VariableBindingGroovyObjectCustomizer.java @@ -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 resolvedScriptVariables; + private volatile Map variables; - public void setResolvedScriptVariables(Map resolvedScriptVariables) { - this.resolvedScriptVariables = resolvedScriptVariables; + public void setVariables(Map 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 entry : this.variables.entrySet()) { + binding.setVariable(entry.getKey(), entry.getValue()); } } } -} +}