Skip to content

Commit

Permalink
Fixed FORGE-1631 - script output is out of sync and duplicated. Also …
Browse files Browse the repository at this point in the history
…improved script output streaming.
  • Loading branch information
lincolnthree committed Mar 5, 2014
1 parent c59d97f commit 0b42c3b
Showing 1 changed file with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.inject.Inject;

import org.jboss.aesh.console.Prompt;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.console.settings.SettingsBuilder;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
Expand Down Expand Up @@ -85,13 +85,13 @@ public UICommandMetadata getMetadata(UIContext context)
@Override
public void initializeUI(UIBuilder builder) throws Exception
{
builder.add(arguments);
builder.add(timeout).add(arguments);
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
List<Result> results = new ArrayList<>();
Result result = Results.fail("Error executing script.");
Resource<?> currentResource = (Resource<?>) context.getUIContext().getInitialSelection().get();

ALL: for (String path : arguments.getValue())
Expand All @@ -102,15 +102,21 @@ public Result execute(UIExecutionContext context) throws Exception
if (resource.exists())
{
final PipedOutputStream stdin = new PipedOutputStream();
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

PrintStream stdout = new UncloseablePrintStream(context.getUIContext().getProvider().getOutput().out());
PrintStream stderr = new UncloseablePrintStream(context.getUIContext().getProvider().getOutput().err());

Settings settings = new SettingsBuilder()
.inputStream(new PipedInputStream(stdin))
.outputStream(stdout)
.outputStreamError(stderr)
.create();

Shell scriptShell = shellFactory.createShell(((FileResource<?>) context.getUIContext()
.getInitialSelection().get()).getUnderlyingResourceObject(),
new SettingsBuilder().inputStream(new PipedInputStream(stdin))
.outputStream(new PrintStream(stdout))
.outputStreamError(new PrintStream(stderr)).create());
.getInitialSelection().get()).getUnderlyingResourceObject(), settings);

scriptShell.getConsole().setPrompt(new Prompt(""));

try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getResourceInputStream())))
{
Expand All @@ -119,23 +125,20 @@ public Result execute(UIExecutionContext context) throws Exception
{
try
{
Result result = execute(scriptShell, writer, reader.readLine(), timeout.getValue(),
String line = reader.readLine();
Integer timeoutValue = timeout.getValue();
result = execute(scriptShell, writer, line, timeoutValue,
TimeUnit.SECONDS, startTime);

if (result != null)
{
results.add(result);

context.getUIContext().getProvider().getOutput().out().write(stdout.toByteArray());
context.getUIContext().getProvider().getOutput().err().write(stderr.toByteArray());

if (result instanceof Failed)
break ALL;
}
}
catch (TimeoutException e)
{
results.add(Results.fail(path + ": timed out.", e));
result = Results.fail(path + ": timed out.");
break ALL;
}
}
Expand All @@ -147,13 +150,13 @@ public Result execute(UIExecutionContext context) throws Exception
}
else
{
results.add(Results.fail(path + ": not found."));
result = Results.fail(path + ": not found.");
break ALL;
}
}
}

return Results.aggregate(results);
return result;
}

public Result execute(Shell shell, BufferedWriter stdin, String line, int quantity, TimeUnit unit, long startTime)
Expand Down Expand Up @@ -212,13 +215,15 @@ public class ScriptCommandListener extends AbstractCommandExecutionListener
@Override
public void preCommandExecuted(UICommand command, UIExecutionContext context)
{
System.out.println("before: " + command);
}

@Override
public void postCommandExecuted(UICommand command, UIExecutionContext context, Result result)
{
synchronized (this)
{
System.out.println("after: " + command);
this.result = result;
}
}
Expand All @@ -228,6 +233,7 @@ public void postCommandFailure(UICommand command, UIExecutionContext context, Th
{
synchronized (this)
{
System.out.println("failed: " + command);
this.result = Results.fail("Error encountered during command execution.", failure);
}
}
Expand Down Expand Up @@ -262,4 +268,18 @@ public boolean isEnabled(ShellContext context)
{
return super.isEnabled(context) && context.getInitialSelection().get() instanceof DirectoryResource;
}

private static class UncloseablePrintStream extends PrintStream
{
public UncloseablePrintStream(PrintStream stream)
{
super(stream, true);
}

@Override
public void close()
{
// Uncloseable
}
}
}

0 comments on commit 0b42c3b

Please sign in to comment.