Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Tool operation refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Feb 12, 2016
1 parent 6326cea commit 4e1ebc5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,14 @@ public ExternalProcessResult runEngineTool(ProcessBuilder pb, String processInpu
*/
public class ToolManagerEngineToolRunner2 implements IOperationService {

protected final boolean throwOnNonZeroStatus;

public ToolManagerEngineToolRunner2() {
this(false);
}

@Deprecated
protected ToolManagerEngineToolRunner2(boolean throwOnNonZeroStatus) {
this.throwOnNonZeroStatus = throwOnNonZeroStatus;
}

@Override
public ExternalProcessResult runProcess(ProcessBuilder pb, String input, ICancelMonitor cm)
throws CommonException, OperationCancellation {
IOperationConsoleHandler handler = startNewOperation(ProcessStartKind.ENGINE_TOOLS, false, false);
return new RunToolTask(handler, pb, cm).runProcess(input, throwOnNonZeroStatus);
return new RunToolTask(handler, pb, cm).runProcess(input, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public ExternalProcessResult runProcess(String input, boolean throwOnNonZeroStat
public ExternalProcessResult doRunProcess(String input, boolean throwOnNonZeroStatus)
throws CommonException, OperationCancellation
{
ICancelMonitor._Util.checkCancelation(cancelMonitor);
ICancelMonitor.checkCancelation(cancelMonitor);

ExternalProcessNotifyingHelper processHelper = startProcess(cancelMonitor);
processHelper.writeInput_(input, StringUtil.UTF8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;


public abstract class AbstractToolOperation {
public abstract class AbstractToolOperation2<RESULT> extends AbstractToolOutputParser2<RESULT> {

protected final IOperationService operationHelper;

public AbstractToolOperation(IOperationService opHelper) {
public AbstractToolOperation2(IOperationService opHelper, String toolPath) {
super(toolPath);
this.operationHelper = assertNotNull(opHelper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,40 @@
import melnorme.lang.tooling.ToolingMessages;
import melnorme.lang.utils.parse.StringParseSource;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.StringUtil;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;

public abstract class AbstractToolOutputParser<RESULT> extends ToolOutputParseHelper {
public abstract class AbstractToolOutputParser<RESULT> extends AbstractToolOutputParser2<RESULT> {

public AbstractToolOutputParser() {
super();
public AbstractToolOutputParser(String toolPath) {
super(toolPath, true);
}

public RESULT parse(ExternalProcessResult result) throws CommonException {
int exitValue = result.exitValue;
if(exitValue != 0) {
throw new CommonException(
ToolingMessages.PROCESS_CompletedWithNonZeroVAlue(getToolProcessName(), exitValue));
handleNonZeroExitValue(result);
}

return doParse(result);
}

protected abstract String getToolProcessName();
@Override
protected void handleNonZeroExitValue(ExternalProcessResult result) throws CommonException {
throw new CommonException(
ToolingMessages.PROCESS_CompletedWithNonZeroVAlue(getToolName(), result.exitValue));
}

protected final RESULT doParse(ExternalProcessResult result) throws CommonException {
return parseToolResult(result);
}

protected RESULT doParse(ExternalProcessResult result) throws CommonException {
return parse(result.getStdOutBytes().toString(StringUtil.UTF8));
public final RESULT parse(String outputSource) throws CommonException {
return parseToolResult(outputSource);
}

public RESULT parse(String outputSource) throws CommonException {
return parse(new StringParseSource(outputSource));
@Override
public RESULT parseToolResult(String output) throws CommonException {
return parse(new StringParseSource(output));
}

protected abstract RESULT parse(StringParseSource outputParseSource) throws CommonException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,41 @@
import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.StringUtil;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;

public abstract class AbstractToolOutputParser2<RESULT> extends ToolOutputParseHelper {

protected final String toolPath;
protected final boolean nonZeroExitIsFatal;

public AbstractToolOutputParser2(String toolPath) {
this(toolPath, false);
}

public AbstractToolOutputParser2(String toolPath, boolean nonZeroResultIsFatal) {
super();
this.toolPath = toolPath;
this.nonZeroExitIsFatal = nonZeroResultIsFatal;
}

public FindDefinitionResult execute(IProcessRunner opRunner,
public RESULT execute(IProcessRunner opRunner,
ICancelMonitor cm) throws OperationCancellation, CommonException, InfoResult {
ProcessBuilder pb = createProcessBuilder();

ExternalProcessResult result = opRunner.runProcess(pb, null, cm);
return handleResult(result);
}

protected abstract ProcessBuilder createProcessBuilder() throws CommonException;

protected RESULT handleResult(ExternalProcessResult result) throws CommonException, InfoResult {
if(result.exitValue != 0) {
handleNonZeroExitValue(result);
}

return parseToolResult(result);
}

protected abstract ProcessBuilder createProcessBuilder() throws CommonException;

@SuppressWarnings("unused")
protected void handleNonZeroExitValue( ExternalProcessResult result) throws CommonException, InfoResult {
String nonZeroExitMsg = getToolName() + " did not complete successfully.";
Expand All @@ -54,10 +62,10 @@ protected void handleNonZeroExitValue( ExternalProcessResult result) throws Comm

protected abstract String getToolName();

public FindDefinitionResult parseToolResult(ExternalProcessResult result) throws CommonException {
return parseToolResult(result.getStdOutBytes().toString());
public RESULT parseToolResult(ExternalProcessResult result) throws CommonException {
return parseToolResult(result.getStdOutBytes().toString(StringUtil.UTF8));
}

public abstract FindDefinitionResult parseToolResult(String output) throws CommonException;
public abstract RESULT parseToolResult(String output) throws CommonException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;


public abstract class BuildOutputParser extends AbstractToolOutputParser<ArrayList<ToolSourceMessage>> {
public abstract class BuildOutputParser2 extends AbstractToolOutputParser<ArrayList<ToolSourceMessage>> {

protected ArrayList2<ToolSourceMessage> buildMessages;

public BuildOutputParser() {
public BuildOutputParser2(String toolPath) {
super(toolPath);
}

public ArrayList2<ToolSourceMessage> getBuildMessages() {
Expand All @@ -38,8 +39,9 @@ public ArrayList2<ToolSourceMessage> getBuildMessages() {
public ArrayList<ToolSourceMessage> parseOutput(ExternalProcessResult buildResult) throws CommonException {
return doParse(buildResult);
}

@Override
protected ArrayList<ToolSourceMessage> doParse(ExternalProcessResult result) throws CommonException {
public ArrayList<ToolSourceMessage> parseToolResult(ExternalProcessResult result) throws CommonException {
return parse(result.getStdErrBytes().toString(StringUtil.UTF8));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@ public boolean isCanceled() {

}

// TODO: remove this unnecessary class wrapper in Java8
public class _Util {

public static final NullCancelMonitor NULL_MONITOR = new NullCancelMonitor();

public static void checkCancelation(ICancelMonitor cm) throws OperationCancellation {
if(cm.isCanceled()) {
throw new OperationCancellation();
}
public static final NullCancelMonitor NULL_MONITOR = new NullCancelMonitor();

public static void checkCancelation(ICancelMonitor cm) throws OperationCancellation {
if(cm.isCanceled()) {
throw new OperationCancellation();
}

}

}

0 comments on commit 4e1ebc5

Please sign in to comment.