Java Code Examples for com.jcraft.jsch.ChannelSftp#quit()

The following examples show how to use com.jcraft.jsch.ChannelSftp#quit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SftpSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected void work() throws IOException, CancellationException, JSchException, SftpException, ExecutionException, InterruptedException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} started", getTraceName());
    }
    ChannelSftp cftp = getChannel();
    RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("download", srcFileName); // NOI18N
    try {
        cftp.get(srcFileName, dstFileName);
    } catch (SftpException e) {
        if (MiscUtils.mightBrokeSftpChannel(e)) {
            cftp.quit();
        }
        throw decorateSftpException(e, srcFileName);
    } finally {
        releaseChannel(cftp);
        RemoteStatistics.stopChannelActivity(activityID, new File(dstFileName).length());
    }
}
 
Example 2
Source File: SftpSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public StatInfo call() throws Exception {
    StatInfo result = null;
    SftpException exception = null;
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} started", getTraceName());
    }
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(PREFIX + ": " + getTraceName()); // NOI18N
    RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("move", from); // NOI18N
    ChannelSftp cftp = null;
    try {
        cftp = getChannel();
        cftp.rename(from, to);
    } catch (SftpException e) {
        exception = e;
        if (e.id == SftpIOException.SSH_FX_FAILURE) {
            if (MiscUtils.mightBrokeSftpChannel(e)) {
                cftp.quit();
            }
        }
    } finally {
        RemoteStatistics.stopChannelActivity(activityID);
        releaseChannel(cftp);
        Thread.currentThread().setName(threadName);
    }
    if (exception != null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "{0} failed", getTraceName());
        }
        throw decorateSftpException(exception, from + " to " + to); //NOI18N
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} finished", new Object[] {getTraceName()});
    }
    return result;
}
 
Example 3
Source File: SftpSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public StatInfo call() throws IOException, CancellationException, JSchException, ExecutionException, InterruptedException, SftpException {
    if (!path.startsWith("/")) { //NOI18N
        throw new FileNotFoundException("Path is not absolute: " + path); //NOI18N
    }
    StatInfo result = null;
    SftpException exception = null;
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} started", getTraceName());
    }
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(PREFIX + ": " + getTraceName()); // NOI18N
    int attempt = 1;
    try {
        for (; attempt <= LS_RETRY_COUNT; attempt++) {
            ChannelSftp cftp = getChannel();
            RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("statload", path); // NOI18N
            try {
                SftpATTRS attrs = lstat ? cftp.lstat(path) : cftp.stat(path);
                String dirName, baseName;
                int slashPos = path.lastIndexOf('/');
                if (slashPos == 0) {
                    dirName = "";
                    baseName = path.substring(1);
                } else {
                    dirName = path.substring(0, slashPos);
                    baseName = path.substring(slashPos + 1);
                }
                result = createStatInfo(dirName, baseName, attrs, cftp);
                exception = null;
                break;
            } catch (SftpException e) {
                exception = e;
                if (e.id == SftpIOException.SSH_FX_FAILURE) {
                    if (LOG.isLoggable(Level.FINE)) {
                        LOG.log(Level.FINE, "{0} - exception while attempt {1}", new Object[]{getTraceName(), attempt});
                    }
                    if (MiscUtils.mightBrokeSftpChannel(e)) {
                        cftp.quit();
                    }
                } else {
                    // re-try in case of failure only
                    // otherwise consider this exception as unrecoverable
                    break;
                }
            } finally {
                RemoteStatistics.stopChannelActivity(activityID);
                releaseChannel(cftp);
            }
        }
    } finally {
        Thread.currentThread().setName(threadName);
    }

    if (exception != null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "{0} failed", getTraceName());
        }
        throw decorateSftpException(exception, path);
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} finished in {1} attempt(s)", new Object[]{getTraceName(), attempt});
    }
    return result;
}
 
Example 4
Source File: SftpSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public StatInfo[] call() throws IOException, CancellationException, JSchException, ExecutionException, InterruptedException, SftpException {
    if (!path.startsWith("/")) { //NOI18N
        throw new FileNotFoundException("Path is not absolute: " + path); //NOI18N
    }
    List<StatInfo> result = null;
    SftpException exception = null;
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} started", getTraceName());
    }
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(PREFIX + ": " + getTraceName()); // NOI18N
    int attempt = 1;
    try {
        for (; attempt <= LS_RETRY_COUNT; attempt++) {
            ChannelSftp cftp = getChannel();
            RemoteStatistics.ActivityID lsLoadID = RemoteStatistics.startChannelActivity("lsload", path); // NOI18N
            try {
                List<LsEntry> entries = (List<LsEntry>) cftp.ls(path);
                result = new ArrayList<>(Math.max(1, entries.size() - 2));
                for (LsEntry entry : entries) {
                    String name = entry.getFilename();
                    if (!".".equals(name) && !"..".equals(name)) { //NOI18N
                        SftpATTRS attrs = entry.getAttrs();
                        //if (!(attrs.isDir() || attrs.isLink())) {
                        //    if ( (attrs.getPermissions() & S_IFMT) != S_IFREG) {
                        //        // skip not regular files
                        //        continue;
                        //    }
                        //}
                        result.add(createStatInfo(path, name, attrs, cftp));
                    }
                }
                exception = null;
                break;
            } catch (SftpException e) {
                exception = e;
                if (e.id == SftpIOException.SSH_FX_FAILURE) {
                    if (LOG.isLoggable(Level.FINE)) {
                        LOG.log(Level.FINE, "{0} - exception while attempt {1}", new Object[]{getTraceName(), attempt});
                    }
                    if (MiscUtils.mightBrokeSftpChannel(e)) {
                        cftp.quit();
                    }
                } else {
                    // re-try in case of failure only
                    // otherwise consider this exception as unrecoverable
                    break;
                }
            } finally {
                RemoteStatistics.stopChannelActivity(lsLoadID);
                releaseChannel(cftp);
            }
        }
    } finally {
        Thread.currentThread().setName(threadName);
    }

    if (exception != null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "{0} failed", getTraceName());
        }
        throw decorateSftpException(exception, path);
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} finished in {1} attempt(s)", new Object[]{getTraceName(), attempt});
    }

    return result == null ? new StatInfo[0] : result.toArray(new StatInfo[result.size()]);
}