Java Code Examples for com.jcraft.jsch.JSchException#getCause()

The following examples show how to use com.jcraft.jsch.JSchException#getCause() . 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: JschBuilder.java    From jwala with Apache License 2.0 6 votes vote down vote up
public JSch build() throws JSchException {
    LOGGER.debug("Initializing JSch Logger");
    JSch.setLogger(new JschLogger());
    final JSch jsch = new JSch();
    try {
        if (null != knownHostsFileName && new File(knownHostsFileName).exists()) {
            jsch.setKnownHosts(knownHostsFileName);
        }
        if (null != privateKeyFileName && new File(privateKeyFileName).exists()) {
            jsch.addIdentity(privateKeyFileName);
        }
    } catch (JSchException e) {
        LOGGER.error("Could not access known hosts or private key file.", e);
        if (!(e.getCause() instanceof FileNotFoundException)) {
            throw new JSchException();
        }
    }
    return jsch;
}
 
Example 2
Source File: JschSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private synchronized static ChannelStreams start(final JSchWorker<ChannelStreams> worker, final ExecutionEnvironment env, final int attempts) throws IOException, JSchException, InterruptedException {
    int retry = attempts;

    while (retry-- > 0) {
        try {
            return worker.call();
        } catch (JSchException ex) {
            String message = ex.getMessage();
            Throwable cause = ex.getCause();
            if (cause != null && cause instanceof NullPointerException) {
                // Jsch bug... retry?
                log.log(Level.INFO, "JSch exception opening channel to " + env + ". Retrying", ex); // NOI18N
            } else if ("java.io.InterruptedIOException".equals(message)) { // NOI18N
                log.log(Level.INFO, "JSch exception opening channel to " + env + ". Retrying in 0.5 seconds", ex); // NOI18N
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex1) {
                    Thread.currentThread().interrupt();
                    break;
                }
            } else if ("channel is not opened.".equals(message)) { // NOI18N
                log.log(Level.INFO, "JSch exception opening channel to " + env + ". Reconnecting and retrying", ex); // NOI18N
                // Now reconnect disconnects old session and creates new, so this might help
                ConnectionManagerAccessor.getDefault().reconnect(env);

            } else {
                throw ex;
            }
        } catch (NullPointerException npe) {
            // Jsch bug... retry? ;)
            log.log(Level.FINE, "Exception from JSch", npe); // NOI18N
        }
    }

    throw new IOException("Failed to execute " + worker.toString()); // NOI18N
}
 
Example 3
Source File: MultiUserSshSessionFactory.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public synchronized RemoteSession getSession(URIish uri,
                                             CredentialsProvider credentialsProvider, FS fs, int tms)
        throws TransportException {

    String user = uri.getUser();
    final String pass = uri.getPass();
    String host = uri.getHost();
    int port = uri.getPort();

    try {
        if (config == null)
            config = OpenSshConfig.get(fs);

        final OpenSshConfig.Host hc = config.lookup(host);
        host = hc.getHostName();
        if (port <= 0)
            port = hc.getPort();
        if (user == null)
            user = hc.getUser();

        Session session = createSession(credentialsProvider, fs, user,
                pass, host, port, hc);

        int retries = 0;
        while (!session.isConnected()) {
            try {
                retries++;
                session.connect(tms);
            } catch (JSchException e) {
                session.disconnect();
                session = null;
                // Make sure our known_hosts is not outdated
                knownHosts(getJSch(credentialsProvider, hc, fs), fs);

                if (isAuthenticationCanceled(e)) {
                    throw e;
                } else if (isAuthenticationFailed(e)
                        && credentialsProvider != null) {
                    // if authentication failed maybe credentials changed at
                    // the remote end therefore reset credentials and retry
                    if (retries < 3) {
                        credentialsProvider.reset(uri);
                        session = createSession(credentialsProvider, fs,
                                user, pass, host, port, hc);
                    } else
                        throw e;
                } else if (retries >= hc.getConnectionAttempts()) {
                    throw e;
                } else {
                    try {
                        Thread.sleep(1000);
                        session = createSession(credentialsProvider, fs,
                                user, pass, host, port, hc);
                    } catch (InterruptedException e1) {
                        throw new TransportException(
                                JGitText.get().transportSSHRetryInterrupt,
                                e1);
                    }
                }
            }
        }

        return new JschSession(session, uri);

    } catch (JSchException je) {
        final Throwable c = je.getCause();
        if (c instanceof UnknownHostException)
            throw new TransportException(uri, JGitText.get().unknownHost);
        if (c instanceof ConnectException)
            throw new TransportException(uri, c.getMessage());
        throw new TransportException(uri, je.getMessage(), je);
    }

}
 
Example 4
Source File: MultiUserSshSessionFactory.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isAuthenticationFailed(JSchException e) {
    return e.getCause() == null && e.getMessage().equals("Auth fail"); //$NON-NLS-1$
}
 
Example 5
Source File: MultiUserSshSessionFactory.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isAuthenticationCanceled(JSchException e) {
    return e.getCause() == null && e.getMessage().equals("Auth cancel"); //$NON-NLS-1$
}