– This blog shows how to Integrate Groovy into Java
Java is powerful and it can do almost everything.
However, it feels tired sometimes to spend hours on the setting before seeing a ‘Hello World’ to print out.
On the contrast, Groovy is easy to use as a scripting language on top of Java.
Sometimes it gives me an idea if we can merge these 2 together to make the life easier.
This blog shows a way to run Groovy Scripts dynamically inside Java – just do some simple change, we can make it a simple and powerful tool that can be embedded into existing Java projects as a scripting addon.
Following codes uses 2 different kinds of Groovy scripting running method, please refer to Standard Groovy documentation “Integrating Groovy into applications” for the difference.
Transformer1 uses GroovyScriptEngine
Transformer2 uses GroovyShell
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.util.GroovyScriptEngine;
import java.io.File;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
public class GroovyTransformer {
public static void main(String[] args) throws Exception {
groovyScriptTransformer1();
// groovyScriptTransformer2();
}
public static void groovyScriptTransformer1() throws Exception {
byte[] input = Files.readAllBytes(new File("c:/test/order_sample.csv").toPath());
Binding sharedData = new Binding();
sharedData.setVariable("input", input);
String[] roots = new String[] { "c:/test" };
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
gse.run("csvParser.groovy", sharedData);
byte[] output = (byte[]) sharedData.getVariable("output");
System.out.println(new String(output));
}
public static void groovyScriptTransformer2() throws Exception {
// get the source file bytes
byte[] input = Files.readAllBytes(new File("c:/test/order_sample.csv").toPath());
Binding sharedData = new Binding();
sharedData.setVariable("input", input);
// get the groovy script as a String
String script = FileUtils.readFileToString(new File("c:/test/csvParser.groovy"), "UTF-8");
System.out.println(script);
// create a groovy shell and run the script
GroovyShell shell = new GroovyShell(sharedData);
shell.evaluate(script);
// get the output of the transformation
byte[] output = (byte[]) sharedData.getVariable("output");
System.out.println(new String(output));
}
}
Following is the sample file “order_sample.csv”
Order Number,Date,To Name, 10090,29/10/15,Sharen Shirley, 10091,29/10/15,Justina Jump, 10092,29/10/15,Macie Maxon, 10093,29/10/15,Shamika Solt, 10094,29/10/15,Marcos Mccabe,
Following is the sample Groovy script “csvParser.groovy”, it did nothing but parsed the csv file into lines, it can easily be changed to transform to other file formats – xml, json or marshaling to groovy/java objects.
import java.io.ByteArrayInputStream
import java.io.Reader
import java.io.InputStreamReader
import static com.xlson.groovycsv.CsvParser.parseCsv
def sb = StringBuilder.newInstance()
Reader inputReader = new InputStreamReader(new ByteArrayInputStream(input));
for(line in parseCsv(inputReader,separator:',')){
// println line
sb.append(line)
sb.append("\n")
}
output = sb.toString().getBytes()