Hi,
I have some code that executes a command and returns exit code and errorMessage ( if execution fails). The code that makes this call is the following (simplified):
public Status twoWayMerge(String file1, String file2) {
String [] errMsg = null;
String[] command = new String[] { getExec(), file1, file2 };
CommandLauncher launcher = new CommandLauncher();
launcher.execute(command, null, null, null);
//Check if command was ok.
if(launcher.getExitValue() != 0){
errMsg = launcher.getErrorOutput();
if(errMsg == null){
return new Status(IStatus.ERROR, "Plugin id here", "An unknown error occurred!");
}else{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < errMsg.length; i++) {
sb.append(errMsg[i]);
sb.append('\n');
}
return new Status(IStatus.ERROR, "Plugin id here",sb.toString());
}
}
//everything was ok!
return new Status(IStatus.OK, "Plugin id here", "Merge was ok");
}
Since it is a long running Job I use the following to make the call.
public void execute() throws TeamException{
// execute
Job job = new Job("Merge") {
protected IStatus run(IProgressMonitor monitor) {
IStatus status = null;
monitor.beginTask("Merge started...", 10);
// Run long running task here
// Add a factory here that can decide which launcher to use.
AbstractDifference merge = MergeFactory.getMergeTool(ClearCasePreferences.getExtMergeTool());
String vExtPath1 = resource.getLocation().toOSString()+"@@"+comparableVersion;
String path = resource.getLocation().toOSString();//Dont use version extended path. Since view selects current version.
if(base == null){
status = merge.twoWayMerge(file1, file2);
}else{
status = merge.threeWayMerge(file1, file2, path);
}
if(!status.isOK()){
//Throw exception
throw new TeamException(status);
}
monitor.done();
return status;
}
};
job.setUser(true);
job.schedule();
}
Then I need to provide the user with a message if I have a failure of the command. I use the following code:
ExternalMergeOperation extMergeOp = new ExternalMergeOperation(resource, comparableVersion, base);
try {
extMergeOp.execute();
} catch (TeamException te) {
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "External Merge title", te.getMessage(), te.getStatus());
}
Is this the recommend way to do it or is there an easier way to do it? Is there anything I can improve in code? I would be very happy if you give your opinion.
br,
//mike