From 5873ee98eb24929c40950b81dcf4889ce7385129 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 22 Nov 2010 14:02:00 -0500 Subject: [PATCH] INT-1552 doc polishing, also updated Groovy-based Control Bus to be more consistent with the SpEL version --- .../groovy/GroovyCommandMessageProcessor.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java 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 new file mode 100644 index 0000000000..04433fa5b5 --- /dev/null +++ b/spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2010 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +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.GroovyObjectCustomizer; +import org.springframework.scripting.groovy.GroovyScriptFactory; +import org.springframework.scripting.support.StaticScriptSource; +import org.springframework.util.Assert; + +/** + * @author Dave Syer + * @author Mark Fisher + * @since 2.0 + */ +public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor { + + private final GroovyObjectCustomizer customizer; + + + public GroovyCommandMessageProcessor() { + this((GroovyObjectCustomizer) null); + } + + public GroovyCommandMessageProcessor(Map map) { + this(new MapContextBindingCustomizer(map)); + } + + public GroovyCommandMessageProcessor(GroovyObjectCustomizer customizer) { + this.customizer = customizer; + } + + + @Override + protected ScriptSource getScriptSource(Message message) { + Object payload = message.getPayload(); + Assert.isInstanceOf(String.class, payload, "Payload must be a String containing a Groovy script."); + String className = generateScriptName(message); + return new StaticScriptSource((String) payload, className); + } + + @Override + protected Object executeScript(ScriptSource scriptSource, Message message) throws Exception { + // Keeping everything local prevents PermGen (class instances) leaks... + MessageContextBindingCustomizer bindingCustomizer = new MessageContextBindingCustomizer(this.customizer); + bindingCustomizer.setMessage(message); + GroovyScriptFactory scriptFactory = new GroovyScriptFactory(this.getClass().getSimpleName(), bindingCustomizer); + Object result = scriptFactory.getScriptedObject(scriptSource, null); + return (result instanceof GString) ? result.toString() : result; + } + + protected String generateScriptName(Message message) { + // Don't use the same script (class) name for all invocations by default + return getClass().getSimpleName() + message.getHeaders().getId().toString().replaceAll("-", ""); + } + +}