if (outputFile.exists()) {
messageDialog.addMessage("Error when deleting old " + outputFile.getName(), MessageList.ERROR);
if (onFailure != null) {
onFailure.actionPerformed(null);
}
throw new MoteTypeCreationException("Error when deleting old " + outputFile.getName());
}
}
Thread readInput = new Thread(new Runnable() {
public void run() {
try {
String readLine;
while ((readLine = processNormal.readLine()) != null) {
if (messageDialog != null) {
messageDialog.addMessage(readLine, MessageList.NORMAL);
}
}
} catch (IOException e) {
logger.warn("Error while reading from process");
}
}
}, "read input stream thread");
Thread readError = new Thread(new Runnable() {
public void run() {
try {
String readLine;
while ((readLine = processError.readLine()) != null) {
if (messageDialog != null) {
messageDialog.addMessage(readLine, MessageList.ERROR);
}
}
} catch (IOException e) {
logger.warn("Error while reading from process");
}
}
}, "read error stream thread");
final MoteTypeCreationException syncException = new MoteTypeCreationException("");
Thread handleCompilationResultThread = new Thread(new Runnable() {
public void run() {
/* Wait for compilation to end */
try {
compileProcess.waitFor();
} catch (Exception e) {
messageDialog.addMessage(e.getMessage(), MessageList.ERROR);
syncException.setCompilationOutput(new MessageList());
syncException.fillInStackTrace();
return;
}
/* Check return value */
if (compileProcess.exitValue() != 0) {
messageDialog.addMessage("Process returned error code " + compileProcess.exitValue(), MessageList.ERROR);
if (onFailure != null) {
onFailure.actionPerformed(null);
}
syncException.setCompilationOutput(new MessageList());
syncException.fillInStackTrace();
return;
}
if (outputFile == null) {
/* No firmware to generate: OK */
if (onSuccess != null) {
onSuccess.actionPerformed(null);
}
return;
}
if (!outputFile.exists()) {
messageDialog.addMessage("No firmware file: " + outputFile, MessageList.ERROR);
if (onFailure != null) {
onFailure.actionPerformed(null);
}
syncException.setCompilationOutput(new MessageList());
syncException.fillInStackTrace();
return;
}
messageDialog.addMessage("", MessageList.NORMAL);
messageDialog.addMessage("Compilation succeded", MessageList.NORMAL);
if (onSuccess != null) {
onSuccess.actionPerformed(null);
}
}
}, "handle compilation results");
readInput.start();
readError.start();
handleCompilationResultThread.start();
if (synchronous) {
try {
handleCompilationResultThread.join();
} catch (Exception e) {
/* Make sure process has exited */
compileProcess.destroy();
String msg = e.getMessage();
if (e instanceof InterruptedException) {
msg = "Aborted by user";
}
throw (MoteTypeCreationException) new MoteTypeCreationException(
"Compilation error: " + msg).initCause(e);
}
/* Detect error manually */
if (syncException.hasCompilationOutput()) {
throw (MoteTypeCreationException) new MoteTypeCreationException(
"Bad return value").initCause(syncException);
}
}
} catch (IOException ex) {
if (onFailure != null) {
onFailure.actionPerformed(null);
}
throw (MoteTypeCreationException) new MoteTypeCreationException(
"Compilation error: " + ex.getMessage()).initCause(ex);
}
return compileProcess;
}