use of org.apache.ivy.core.IvyThread in project ant-ivy by apache.
the class VsftpRepository method readResponse.
@SuppressWarnings("deprecation")
protected synchronized String readResponse(final boolean sendErrorAsResponse, long timeout) throws IOException {
final StringBuilder response = new StringBuilder();
final IOException[] exc = new IOException[1];
final boolean[] done = new boolean[1];
Runnable r = new Runnable() {
public void run() {
synchronized (VsftpRepository.this) {
try {
int c;
boolean getPrompt = false;
// the reading is done in a loop making five attempts to read the stream
// if we do not reach the next prompt
int attempt = 0;
while (!getPrompt && attempt < MAX_READ_PROMPT_ATTEMPT) {
while ((c = in.read()) != -1) {
// we managed to read something, reset number of attempts
attempt = 0;
response.append((char) c);
if (response.length() >= PROMPT.length() && response.substring(response.length() - PROMPT.length(), response.length()).equals(PROMPT)) {
response.setLength(response.length() - PROMPT.length());
getPrompt = true;
break;
}
}
if (!getPrompt) {
try {
Thread.sleep(PROMPT_SLEEP_TIME);
} catch (InterruptedException e) {
break;
}
}
attempt++;
}
if (getPrompt) {
// wait enough for error stream to be fully read
if (errorsLastUpdateTime == 0) {
// no error written yet, but it may be pending...
errorsLastUpdateTime = lastCommand;
}
while ((System.currentTimeMillis() - errorsLastUpdateTime) < PROMPT_SLEEP_TIME) {
try {
Thread.sleep(ERROR_SLEEP_TIME);
} catch (InterruptedException e) {
break;
}
}
}
if (errors.length() > 0) {
if (sendErrorAsResponse) {
response.append(errors);
errors.setLength(0);
} else {
throw new IOException(chomp(errors).toString());
}
}
chomp(response);
done[0] = true;
} catch (IOException e) {
exc[0] = e;
} finally {
VsftpRepository.this.notify();
}
}
}
};
Thread reader = null;
if (timeout == 0) {
r.run();
} else {
reader = new IvyThread(r);
reader.start();
try {
wait(timeout);
} catch (InterruptedException e) {
// nothing to do
}
}
updateLastCommandTime();
if (exc[0] != null) {
throw exc[0];
} else if (!done[0]) {
if (reader != null && reader.isAlive()) {
reader.interrupt();
int attempt = 0;
while (attempt < MAX_READER_ALIVE_ATTEMPT && reader.isAlive()) {
try {
Thread.sleep(READER_ALIVE_SLEEP_TIME);
} catch (InterruptedException e) {
break;
}
attempt++;
}
if (reader.isAlive()) {
// no way to interrupt it non abruptly
reader.stop();
}
}
throw new IOException("connection timeout to " + getHost());
} else {
if ("Not connected.".equals(response.toString())) {
Message.info("vsftp connection to " + getHost() + " reset");
closeConnection();
throw new IOException("not connected to " + getHost());
}
Message.debug("received response '" + response + "' from " + getHost());
return response.toString();
}
}
use of org.apache.ivy.core.IvyThread in project ant-ivy by apache.
the class VsftpRepository method get.
public void get(final String source, File destination) throws IOException {
initIvy();
try {
fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
File destDir = destination.getParentFile();
if (destDir != null) {
sendCommand("lcd " + destDir.getAbsolutePath());
}
if (destination.exists()) {
destination.delete();
}
int index = source.lastIndexOf('/');
String srcName = (index == -1) ? source : source.substring(index + 1);
final File to = (destDir == null) ? Checks.checkAbsolute(srcName, "source") : new File(destDir, srcName);
final IOException[] ex = new IOException[1];
Thread get = new IvyThread() {
public void run() {
initContext();
try {
sendCommand("get " + source, getExpectedDownloadMessage(source, to), 0);
} catch (IOException e) {
ex[0] = e;
}
}
};
get.start();
long prevLength = 0;
long lastUpdate = System.currentTimeMillis();
long timeout = readTimeout;
while (get.isAlive()) {
checkInterrupted();
long length = to.exists() ? to.length() : 0;
if (length > prevLength) {
fireTransferProgress(length - prevLength);
lastUpdate = System.currentTimeMillis();
prevLength = length;
} else {
if (System.currentTimeMillis() - lastUpdate > timeout) {
Message.verbose("download hang for more than " + timeout + "ms. Interrupting.");
get.interrupt();
if (to.exists()) {
to.delete();
}
throw new IOException(source + " download timeout from " + getHost());
}
}
try {
get.join(GET_JOIN_MAX_TIME);
} catch (InterruptedException e) {
if (to.exists()) {
to.delete();
}
return;
}
}
if (ex[0] != null) {
if (to.exists()) {
to.delete();
}
throw ex[0];
}
to.renameTo(destination);
fireTransferCompleted(destination.length());
} catch (IOException ex) {
fireTransferError(ex);
cleanup(ex);
throw ex;
} finally {
cleanup();
}
}
use of org.apache.ivy.core.IvyThread in project ant-ivy by apache.
the class VsftpRepository method ensureConnectionOpened.
protected synchronized void ensureConnectionOpened() throws IOException {
if (in == null) {
Message.verbose("connecting to " + getUsername() + "@" + getHost() + "... ");
String connectionCommand = getConnectionCommand();
exec(connectionCommand);
try {
// waits for first prompt
readResponse(false);
if (reuseConnection > 0) {
connectionCleaner = new IvyThread() {
public void run() {
initContext();
try {
long sleep = REUSE_CONNECTION_SLEEP_TIME;
while (in != null && sleep > 0) {
sleep(sleep);
sleep = reuseConnection - (System.currentTimeMillis() - lastCommand);
if (inCommand && sleep <= 0) {
sleep = reuseConnection;
}
}
} catch (InterruptedException e) {
// nothing to do
}
disconnect();
}
};
connectionCleaner.start();
}
if (ivy != null) {
ivy.getEventManager().addIvyListener(new IvyListener() {
public void progress(IvyEvent event) {
disconnect();
event.getSource().removeIvyListener(this);
}
}, EndResolveEvent.NAME);
}
} catch (IOException ex) {
closeConnection();
throw new IOException("impossible to connect to " + getUsername() + "@" + getHost() + " using " + getAuthentication() + ": " + ex.getMessage());
}
Message.verbose("connected to " + getHost());
}
}
use of org.apache.ivy.core.IvyThread in project ant-ivy by apache.
the class VsftpRepository method exec.
private void exec(String command) throws IOException {
Message.debug("launching '" + command + "'");
process = Runtime.getRuntime().exec(command);
in = new InputStreamReader(process.getInputStream());
err = new InputStreamReader(process.getErrorStream());
out = new PrintWriter(process.getOutputStream());
errorsReader = new IvyThread() {
public void run() {
initContext();
int c;
try {
// CheckStyle:InnerAssignment OFF
while (err != null && (c = err.read()) != -1) {
errors.append((char) c);
errorsLastUpdateTime = System.currentTimeMillis();
}
// CheckStyle:InnerAssignment ON
} catch (IOException e) {
// nothing to do
}
}
};
errorsReader.start();
}
Aggregations