public Result execute(UIExecutionContext context) throws Exception
{
Result result = Results.fail("Error executing script.");
UIContext uiContext = context.getUIContext();
Resource<?> currentResource = (Resource<?>) uiContext.getInitialSelection().get();
final UIOutput output = uiContext.getProvider().getOutput();
if (command.hasValue())
{
String[] commands = command.getValue().split(" ");
ProcessBuilder processBuilder = new ProcessBuilder(commands);
Object currentDir = currentResource.getUnderlyingResourceObject();
if (currentDir instanceof File)
{
processBuilder.directory((File) currentDir);
}
final Process process = processBuilder.start();
ExecutorService executor = Executors.newFixedThreadPool(2);
// Read std out
executor.submit(new Runnable()
{
@Override
public void run()
{
Streams.write(process.getInputStream(), output.out());
}
});
// Read std err
executor.submit(new Runnable()
{
@Override
public void run()
{
Streams.write(process.getErrorStream(), output.err());
}
});
executor.shutdown();
int returnCode = process.waitFor();
if (returnCode == 0)
{
result = Results.success();
}
else
{
result = Results.fail("Error while executing native command. See output for more details");
}
}
else
{
Resource<?> selectedResource = currentResource;
ALL: for (String path : arguments.getValue())
{
List<Resource<?>> resources = new ResourcePathResolver(resourceFactory, currentResource, path).resolve();
for (Resource<?> resource : resources)
{
if (resource.exists())
{
final PipedOutputStream stdin = new PipedOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
PrintStream stdout = new UncloseablePrintStream(output.out());
PrintStream stderr = new UncloseablePrintStream(output.err());
Settings settings = new SettingsBuilder()
.inputStream(new PipedInputStream(stdin))
.outputStream(stdout)
.outputStreamError(stderr)