}
} else {
logger.error("Cannot find errorlevel in Windows output: " + outputBuf);
}
} catch (IOException exc) {
throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, CifsTelnetConnection.this), exc);
} finally {
closeQuietly(toCallersStdout);
}
}
};
outputReaderThread.setDaemon(true);
outputReaderThread.start();
return new OverthereProcess() {
@Override
public synchronized OutputStream getStdin() {
return stdin;
}
@Override
public synchronized InputStream getStdout() {
return callersStdout;
}
@Override
public synchronized InputStream getStderr() {
return new ByteArrayInputStream(new byte[0]);
}
@Override
public synchronized int waitFor() {
if (!tc.isConnected()) {
return exitValue[0];
}
try {
try {
outputReaderThread.join();
} finally {
disconnect();
}
return exitValue[0];
} catch (InterruptedException exc) {
throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, CifsTelnetConnection.this), exc);
}
}
@Override
public synchronized void destroy() {
if (!tc.isConnected()) {
return;
}
disconnect();
}
private synchronized void disconnect() {
try {
tc.disconnect();
logger.info("Disconnected from {}", CifsTelnetConnection.this);
closeQuietly(toCallersStdout);
} catch (IOException exc) {
throw new RuntimeIOException(format("Cannot disconnect from %s", CifsTelnetConnection.this), exc);
}
}
@Override
public synchronized int exitValue() {
if (tc.isConnected()) {
throw new IllegalThreadStateException(format("Process for command [%s] on %s is still running", obfuscatedCmd, CifsTelnetConnection.this));
}
synchronized (exitValue) {
return exitValue[0];
}
}
};
} catch (InvalidTelnetOptionException exc) {
throw new RuntimeIOException("Cannot execute command " + cmd + " at telnet://" + username + "@" + address, exc);
} catch (IOException exc) {
throw new RuntimeIOException("Cannot execute command " + cmd + " at telnet://" + username + "@" + address, exc);
}
}