[jboss-svn-commits] JBL Code SVN: r11830 - labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/naming/strategy.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu May 10 19:35:12 EDT 2007
Author: derek.adams
Date: 2007-05-10 19:35:12 -0400 (Thu, 10 May 2007)
New Revision: 11830
Added:
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/naming/strategy/GroovyNamingStrategy.java
Log:
Added naming strategy that uses a groovy script to manipulate the filename.
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/naming/strategy/GroovyNamingStrategy.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/naming/strategy/GroovyNamingStrategy.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/naming/strategy/GroovyNamingStrategy.java 2007-05-10 23:35:12 UTC (rev 11830)
@@ -0,0 +1,110 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY 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 along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.soa.esb.actions.naming.strategy;
+
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+
+import java.io.IOException;
+
+import org.jboss.soa.esb.actions.scripting.GroovyActionProcessor;
+import org.jboss.soa.esb.message.Message;
+import org.w3c.dom.Node;
+
+/**
+ * Naming strategy that executes a Groovy script to manipulate a filename.
+ *
+ * @author Derek Adams
+ */
+public class GroovyNamingStrategy extends AbstractFileNamingStrategy {
+
+ /** Path to the groovy script that will be executed */
+ private String scriptPath = DEFAULT_SCRIPT_PATH;
+
+ /** Default path for the groovy script */
+ public static final String DEFAULT_SCRIPT_PATH = "naming.groovy";
+
+ /** XML attribute that holds the script pathx */
+ public static final String ATTR_SCRIPT_PATH = "scriptPath";
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.actions.naming.strategy.FileNamingStrategy#configure(org.w3c.dom.Node)
+ */
+ public void configure(Node xml) {
+ Node scriptPathNode = xml.getAttributes().getNamedItem(ATTR_SCRIPT_PATH);
+ if (scriptPathNode != null) {
+ setScriptPath(scriptPathNode.getNodeValue());
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.soa.esb.actions.naming.strategy.AbstractFileNamingStrategy#process(java.lang.String,
+ * org.jboss.soa.esb.message.Message)
+ */
+ @Override
+ public String process(String filename, Message message) {
+ try {
+ // Read the script into a string.
+ String script = GroovyActionProcessor.getScriptFromClasspath(getScriptPath());
+
+ // Bind objects into the Groovy context.
+ Binding binding = new Binding();
+ binding.setVariable("filename", filename);
+ binding.setVariable("message", message);
+
+ // Create a shell and evaluate
+ GroovyShell shell = new GroovyShell(Thread.currentThread().getContextClassLoader(),
+ binding);
+ Object result = shell.evaluate(script);
+ if (!(result instanceof String)) {
+ throw new RuntimeException(
+ "Groovy script should return a String indicating processed filename");
+ }
+ return (String) result;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Set the path to the script.
+ *
+ * @param scriptPath
+ */
+ public void setScriptPath(String scriptPath) {
+ this.scriptPath = scriptPath;
+ }
+
+ /**
+ * Get the path to the script.
+ *
+ * @return
+ */
+ public String getScriptPath() {
+ return scriptPath;
+ }
+}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list