Example usage for org.eclipse.jgit.transport SshSessionFactory getSession

List of usage examples for org.eclipse.jgit.transport SshSessionFactory getSession

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport SshSessionFactory getSession.

Prototype

public abstract RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms)
        throws TransportException;

Source Link

Document

Open (or reuse) a session to a host.

Usage

From source file:com.google.gerrit.server.git.PushReplication.java

License:Apache License

private void replicateProject(final URIish replicateURI, final String head) {
    SshSessionFactory sshFactory = SshSessionFactory.getInstance();
    RemoteSession sshSession;//from  w w  w.java  2  s  .c om
    String projectPath = QuotedString.BOURNE.quote(replicateURI.getPath());

    if (!usingSSH(replicateURI)) {
        log.warn("Cannot create new project on remote site since the connection " + "method is not SSH: "
                + replicateURI.toString());
        return;
    }

    OutputStream errStream = createErrStream();
    String cmd = "mkdir -p " + projectPath + "&& cd " + projectPath + "&& git init --bare"
            + "&& git symbolic-ref HEAD " + QuotedString.BOURNE.quote(head);

    try {
        sshSession = sshFactory.getSession(replicateURI, null, FS.DETECTED, 0);
        Process proc = sshSession.exec(cmd, 0);
        proc.getOutputStream().close();
        StreamCopyThread out = new StreamCopyThread(proc.getInputStream(), errStream);
        StreamCopyThread err = new StreamCopyThread(proc.getErrorStream(), errStream);
        out.start();
        err.start();
        try {
            proc.waitFor();
            out.halt();
            err.halt();
        } catch (InterruptedException interrupted) {
            // Don't wait, drop out immediately.
        }
        sshSession.disconnect();
    } catch (IOException e) {
        log.error("Communication error when trying to replicate to: " + replicateURI.toString() + "\n"
                + "Error reported: " + e.getMessage() + "\n" + "Error in communication: "
                + errStream.toString());
    }
}

From source file:org.jboss.tools.openshift.express.internal.ui.command.TailFilesHandler.java

License:Open Source License

/**
 * Starting the tail process on the remote OpenShift Platform. This method
 * relies on the JGit SSH support (including JSch) to open a connection AND
 * execute a command in a single invocation. The connection establishement
 * requires an SSH key, and the passphrase is prompted to the user if
 * necessary.//  w  w w. j  a  v  a2  s  .co  m
         
 * @param sshUrl
 * @param filePattern
 * @param optionsAndFile
 * @param console
 * @return
 * @throws URISyntaxException 
 * @throws IOException 
 */
private TailServerLogWorker startTailProcess(final String sshUrl, final String optionsAndFile,
        final MessageConsole console) throws URISyntaxException, IOException {
    JSch.setLogger(new JschToEclipseLogger());
    final SshSessionFactory sshSessionFactory = SshSessionFactory.getInstance();
    URI uri = new URI(sshUrl);
    uri.getHost();
    final URIish urish = new URIish().setHost(uri.getHost()).setUser(uri.getUserInfo());
    RemoteSession remoteSession = sshSessionFactory.getSession(urish, CredentialsProvider.getDefault(),
            FS.DETECTED, 0);
    final String command = new TailCommandBuilder(optionsAndFile).build();

    Logger.debug("ssh command to execute: " + command);
    Process process = remoteSession.exec(command, 0);
    return new TailServerLogWorker(console, process, remoteSession);
}