package org.jenkinsci.plugins.gitclient.trilead;

import com.trilead.ssh2.Connection;
import com.trilead.ssh2.Session;
import org.eclipse.jgit.transport.RemoteSession;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static com.trilead.ssh2.ChannelCondition.*;

/**
 * TrileadSession class.
 *
 * @author Kohsuke Kawaguchi
 */
public class TrileadSession implements RemoteSession {
    protected final Connection con;

    /**
     * Constructor for TrileadSession.
     *
     * @param con a {@link com.trilead.ssh2.Connection} object for this session's connection.
     */
    public TrileadSession(Connection con) {
        this.con = con;
    }

    /** {@inheritDoc} */
    public Process exec(String commandName, final int timeout) throws IOException {
        final Session s = con.openSession();
        s.execCommand(commandName);

        return new Process() {
            @Override
            public OutputStream getOutputStream() {
                return s.getStdin();
            }

            @Override
            public InputStream getInputStream() {
                return s.getStdout();
            }

            @Override
            public InputStream getErrorStream() {
                return s.getStderr();
            }

            @Override
            public int waitFor() throws InterruptedException {
                int r = s.waitForCondition(EXIT_STATUS, timeout * 1000L);
                if ((r&EXIT_STATUS)!=0)
                    return exitValue();

                // not sure what exception jgit expects
                throw new InterruptedException("Timed out: "+r);
            }

            @Override
            public int exitValue() {
                Integer i = s.getExitStatus();
                if (i==null)    throw new IllegalThreadStateException(); // hasn't finished
                return i;
            }

            @Override
            public void destroy() {
                s.close();
            }
        };
    }

    public void disconnect() {
        con.close();
    }
}

@@ -709,24 +709,38 @@
 eritDoc%7D */%0A
+    @Override%0A
     public P
@@ -823,21 +823,341 @@
     
-final Session
+return new ProcessImpl(con, commandName, timeout);%0A    %7D%0A%0A    private static class ProcessImpl extends Process %7B%0A%0A        private final int timeout;%0A        private final Session s;%0A%0A        public ProcessImpl(Connection con, String commandName, final int timeout) throws IOException %7B%0A            this.timeout = timeout;%0A           
  s =
@@ -1172,24 +1172,28 @@
 nSession();%0A
+    
         s.ex
@@ -1216,25 +1216,24 @@
 dName);%0A
-%0A
         
 return n
@@ -1228,35 +1228,11 @@
     
-return new Process() %7B%0A    
+%7D%0A%0A
     
@@ -1237,36 +1237,32 @@
       @Override%0A
-    
         public O
@@ -1297,36 +1297,32 @@
 ) %7B%0A            
-    
 return s.getStdi
@@ -1330,35 +1330,27 @@
 ();%0A        
-  
-  
 %7D%0A%0A
-    
         @Ove
@@ -1355,36 +1355,32 @@
 verride%0A        
-  
-  
 public InputStre
@@ -1405,36 +1405,32 @@
 ) %7B%0A            
-    
 return s.getStdo
@@ -1439,35 +1439,27 @@
 ();%0A        
-  
-  
 %7D%0A%0A
-    
         @Ove
@@ -1468,28 +1468,24 @@
 ide%0A        
-  
-  
 public Input
@@ -1514,36 +1514,32 @@
 ) %7B%0A            
-    
 return s.getStde
@@ -1548,35 +1548,27 @@
 ();%0A        
-  
-  
 %7D%0A%0A
-    
         @Ove
@@ -1573,36 +1573,32 @@
 verride%0A        
-  
-  
 public int waitF
@@ -1624,36 +1624,32 @@
 ptedException %7B%0A
-    
             int 
@@ -1710,28 +1710,24 @@
             
-  
-  
 if ((r&EXIT_
@@ -1750,28 +1750,24 @@
             
-    
 return exitV
@@ -1787,20 +1787,16 @@
         
-  
-  
 // not s
@@ -1827,20 +1827,16 @@
 expects%0A
-    
         
@@ -1887,9 +1887,11 @@
 t: %22
-+
+ + 
 r);%0A
@@ -1898,34 +1898,27 @@
         
-    %7D%0A%0A   
+%7D%0A%0A
         
  @Overri
@@ -1901,33 +1901,32 @@
      %7D%0A%0A        
- 
 @Override%0A      
@@ -1911,36 +1911,32 @@
       @Override%0A
-    
         public i
@@ -1964,20 +1964,16 @@
         
-   
- 
 Integer 
@@ -1991,28 +1991,24 @@
 itStatus();%0A
-    
             
@@ -2091,28 +2091,24 @@
             
-    
 return i;%0A  
@@ -2109,28 +2109,24 @@
  i;%0A        
-    
 %7D%0A%0A         
@@ -2120,28 +2120,24 @@
  %7D%0A%0A        
-  
-  
 @Override%0A  
@@ -2130,28 +2130,24 @@
   @Override%0A
-    
         publ
@@ -2178,20 +2178,16 @@
         
-  
-  
 s.close(
@@ -2201,31 +2201,30 @@
     
-    %7D%0A        %7D;%0A    %7D%0A
+%7D%0A    %7D%0A%0A    @Override
 %0A   

Use private static inner class in TrileadSession