Hello,
I'm looking for suggestions on the best way to integrate a suite of Java
commands into Taverna as local services.
The commands are implemented as typed instances of Callable<T>
ExampleCommand.java:
import java.io.InputStream;
public final class ExampleCommand
implements Callable<String>
{
private InputStream inputStream;
public ExampleCommand(final InputStream inputStream)
{
this.inputStream = inputStream;
}
@Override
public String call() throws Exception
{
return "hello world";
}
}
Ideally we'd use these as our services, but I'm not sure how to provide
streams as input in Taverna and how to handle casting the result of call()
to the appropriate class. It doesn't appear that the return type shows up
when using the ApiConsumer.
A second option is to use the commandline wrappers for our commands, which
handle file/stdio
ExampleCommandLine.java:
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.io.IOUtils;
public final class ExampleCommandLine
implements Runnable
{
private String input;
private String output;
public ExampleCommandLine(final String input, final String output)
{
this.input = input;
this.output = output;
}
@Override
public void run()
{
InputStream inputStream = null;
PrintWriter writer = null;
try
{
// read from input, if specified, otherwise from STDIN
inputStream = (input == null) ? System.in : new FileInputStream(new
File(input));
// write to output, if specified, otherwise to STDOUT
writer = (output == null) ? new PrintWriter(new
OutputStreamWriter(System.out)) : new PrintWriter(FileWriter(new File(output)));
ExampleCommand exampleCommand = new ExampleCommand(inputStream);
String example = exampleCommand.call();
writer.println(example);
writer.flush();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(-1);
}
finally
{
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(writer);
}
}
public static void main(final String[] args)
{
Options options = new Options();
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.setWidth(120);
Option input = new Option("i", "input", true, "input file name, default
read from STDIN");
input.setRequired(false);
Option output = new Option("o", "output", true, "output file name,
default write to STDOUT");
output.setRequired(false);
Option help = new Option("h", "help", false, "print help text to
STDOUT");
help.setRequired(false);
options.addOption(input);
options.addOption(output);
options.addOption(help);
try
{
CommandLineParser parser = new PosixParser();
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption(help.getOpt()))
{
helpFormatter.printHelp("example", options, true);
System.exit(0);
}
new ExampleCommandLine(input.getValue(), output.getValue()).run();
}
catch (ParseException e)
{
System.err.println(e.getMessage() + "\n");
helpFormatter.printHelp(new PrintWriter(System.err, true), 120,
"example", "", options, 1, 3, "", true);
System.exit(-2);
}
catch (IllegalArgumentException e)
{
System.err.println("Illegal argument: " + e.getMessage() + "\n");
helpFormatter.printHelp(new PrintWriter(System.err, true), 120,
"example", "", options, 1, 3, "", true);
System.exit(-2);
}
}
}
(Sorry about all the commons-cli junk)
Here the input is just a String file name, but the command output goes to
an output file.
Thank you,
michael
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
taverna-hackers mailing list
[email protected]
Web site: http://www.taverna.org.uk
Mailing lists: http://www.taverna.org.uk/taverna-mailing-lists/
Developers Guide: http://www.mygrid.org.uk/tools/developer-information