[jboss-svn-commits] JBL Code SVN: r10160 - in labs/jbossesb/trunk/product/core/listeners: src/org/jboss/soa/esb/actions/scripting and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Mar 13 13:12:19 EDT 2007
Author: tfennelly
Date: 2007-03-13 13:12:18 -0400 (Tue, 13 Mar 2007)
New Revision: 10160
Added:
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/scripting/
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessor.java
labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/
labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessorUnitTest.java
labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/test.groovy
Log:
Added Groovy action
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessor.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessor.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessor.java 2007-03-13 17:12:18 UTC (rev 10160)
@@ -0,0 +1,108 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.soa.esb.actions.scripting;
+
+import groovy.lang.Binding;
+import groovy.util.GroovyScriptEngine;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionProcessor;
+import org.apache.log4j.Logger;
+
+import java.io.IOException;
+import java.io.Serializable;
+
+/**
+ * <a href="http://groovy.codehaus.org">Groovy</a> Scripting action processor.
+ * <p/>
+ * <pre>
+ * <action name="groovy" class="org.jboss.soa.esb.actions.scripting.GroovyActionProcessor">
+ * <property name="scriptPath" value="./scripts" />
+ * <property name="script" value="helloWorld.groovy" />
+ * </action>
+ * </pre>
+ * <p/>
+ * The {@link Message message} is bound on the script under the name "message" and the
+ * {@link ConfigTree configuration} is bould under the name "config".
+ *
+ * @author Gregory Pierce.
+ */
+public class GroovyActionProcessor implements ActionProcessor {
+
+ private static Logger logger = Logger.getLogger(GroovyActionProcessor.class);
+ protected ConfigTree configTree;
+ protected GroovyScriptEngine scriptEngine;
+ private String scriptName;
+
+ public GroovyActionProcessor(ConfigTree config) throws ConfigurationException {
+ this.configTree = config;
+ String[] scriptPaths = new String[]{new String(configTree.getAttribute("scriptPath"))};
+
+ scriptName = configTree.getAttribute("script");
+ // create a script engine which uses the scriptPaths to look up scripts
+ // and the messages class loader to load any special files that may not
+ // be available to groovy's class loader
+ //
+ try {
+ scriptEngine = new GroovyScriptEngine(scriptPaths, getClass().getClassLoader());
+ } catch (IOException e) {
+ throw new ConfigurationException("Failed to initialise a Groovy scripting engine instance.", e);
+ }
+ }
+
+ /**
+ * Processes an ESB message. Configuration is via two properties of this ESB element
+ * scriptPath - The path to the script that will be run
+ * script - the name of the script that will be run (include .groovy exception)
+ */
+ public Message process(Message message) {
+ try {
+ // create a Java/Groovy binding for the message object
+ //
+ Binding binding = new Binding();
+
+ // binds the message object to the script with a variable name of 'message'
+ //
+ binding.setVariable("message", message);
+ binding.setVariable("config", configTree);
+
+ // run the script specified in the scriptName
+ //
+ scriptEngine.run(scriptName, binding);
+
+ // send the modified message back into the channel
+ //
+ return (Message) binding.getVariable("message");
+ }
+ catch (Exception e) {
+ logger.error("Error executing Groovy script.", e);
+ return message;
+ }
+ }
+
+ public Serializable getOkNotification(Message message) {
+ return null;
+ }
+
+ public Serializable getErrorNotification(Message message) {
+ return null;
+ }
+}
\ No newline at end of file
Property changes on: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessor.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessorUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessorUnitTest.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessorUnitTest.java 2007-03-13 17:12:18 UTC (rev 10160)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.soa.esb.actions.scripting;
+
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for GroovyActionProcessor.
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class GroovyActionProcessorUnitTest extends TestCase {
+
+ // Of course, this is going to cause trouble for anyone running the build through
+ // an IDE that's not setup properly (to match the ant build). Sorry about that!!
+ // This test assumes your cwd is the "product" dir, as per the ant build - will need
+ // changing when we move to maven (sorry Kev :-) ).
+ private static final String scriptPath = "./core/listeners/tests/src/org/jboss/soa/esb/actions/scripting";
+
+ public void test() throws ConfigurationException {
+ ConfigTree config = getConfig("test.groovy");
+ String messageContents = "Hello World!";
+
+ config.setAttribute("messageContents", messageContents);
+
+ GroovyActionProcessor processor = new GroovyActionProcessor(config);
+ Message message = MessageFactory.getInstance().getMessage();
+
+ processor.process(message);
+ byte[] bodyContents = message.getBody().getContents();
+ assertNotNull("Expected body contents to be set.", bodyContents);
+ assertEquals(messageContents, new String(bodyContents));
+ }
+
+ private ConfigTree getConfig(String script) {
+ ConfigTree config = new ConfigTree("<config/>");
+
+ config.setAttribute("scriptPath", scriptPath);
+ config.setAttribute("script", script);
+
+ return config;
+ }
+}
Property changes on: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/GroovyActionProcessorUnitTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/test.groovy
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/test.groovy (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/scripting/test.groovy 2007-03-13 17:12:18 UTC (rev 10160)
@@ -0,0 +1,3 @@
+import org.jboss.soa.esb.message.*
+
+message.getBody().setContents(config.getAttribute("messageContents").getBytes());
\ No newline at end of file
More information about the jboss-svn-commits
mailing list