1 12 package org.eclipse.team.internal.ccvs.ssh2; 13 14 import java.io.*; 15 import java.util.Properties ; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.team.internal.ccvs.core.*; 19 import org.eclipse.team.internal.ccvs.core.connection.CVSAuthenticationException; 20 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 21 22 import com.jcraft.jsch.*; 23 24 public class PServerSSH2ServerConnection implements IServerConnection { 25 26 private ICVSRepositoryLocation location; 27 private String password; 28 private Session session; 29 private static int localport = 2403; 30 private IServerConnection psc = null; 31 32 protected PServerSSH2ServerConnection(ICVSRepositoryLocation location, String password) { 33 this.location = location; 34 this.password = password; 35 } 36 37 public void close() throws IOException { 38 psc.close(); 39 } 40 41 public InputStream getInputStream() { 42 return psc.getInputStream(); 43 } 44 public OutputStream getOutputStream() { 45 return psc.getOutputStream(); 46 } 47 48 public void open(IProgressMonitor monitor) throws IOException, CVSAuthenticationException { 49 monitor.subTask("PServerSSH2ServerConnection.open"); monitor.worked(1); 51 String cvs_root = location.getRootDirectory(); 52 int cvs_port = location.getPort(); 53 if (cvs_port == 0) 54 cvs_port = 2401; 55 String cvs_host = location.getHost(); 56 String ssh_host = cvs_host; 57 String ssh_user = location.getUsername(); 58 59 String host = cvs_host; 60 if (host.indexOf('@') != -1) { 61 cvs_host = host.substring(host.lastIndexOf('@') + 1); 62 host = host.substring(0, host.lastIndexOf('@')); 63 if (host.indexOf('@') != -1) { 64 ssh_host = host.substring(host.lastIndexOf('@') + 1); 65 if (ssh_host.length() == 0) 66 ssh_host = cvs_host; 67 ssh_user = host.substring(0, host.lastIndexOf('@')); 68 } else { 69 ssh_host = host; 70 } 71 } 72 73 int ssh_port = 0; 74 if (ssh_host.indexOf('#') != -1) { 75 try { 76 ssh_port = Integer.parseInt(ssh_host.substring(ssh_host.lastIndexOf('#') + 1)); 77 ssh_host = ssh_host.substring(0, ssh_host.lastIndexOf('#')); 78 } catch (Exception e) { 79 } 81 } 82 83 int lport = cvs_port; 84 String rhost = (cvs_host.equals(ssh_host) ? "localhost" : cvs_host); int rport = cvs_port; 86 87 int retry = 1; 89 while (true) { 90 try { 91 session = JSchSession.getSession(location, ssh_user, null, ssh_host, ssh_port, monitor).getSession(); 92 String [] list = session.getPortForwardingL(); 93 String name = ":" + rhost + ":" + rport; boolean done = false; 95 for (int i = 0; i < list.length; i++) { 96 if (list[i].endsWith(name)) { 97 try { 98 String foo = list[i].substring(0, list[i].indexOf(':')); 99 lport = Integer.parseInt(foo); 100 } catch (Exception ee) { 101 } 103 done = true; 104 break; 105 } 106 } 107 if (!done) { 108 lport = localport++; 109 session.setPortForwardingL(lport, rhost, rport); 110 } 111 } catch (JSchException ee) { 112 retry--; 113 if(retry<0){ 114 throw new CVSAuthenticationException(CVSSSH2Messages.CVSSSH2ServerConnection_3, CVSAuthenticationException.NO_RETRY, location); 115 } 116 if(session != null && session.isConnected()){ 117 session.disconnect(); 118 } 119 continue; 120 } 121 break; 122 } 123 ((CVSRepositoryLocation)location).setPassword(password); 125 126 try { 128 String _password = ""; if (password != null) 131 _password = password; 132 Properties prop = new Properties (); 133 prop.put("connection", "pserver"); prop.put("user", location.getUsername()); prop.put("password", _password); prop.put("host", "localhost"); prop.put("port", Integer.toString(lport)); prop.put("root", cvs_root); 140 CVSRepositoryLocation cvsrl = CVSRepositoryLocation.fromProperties(prop); 141 142 IConnectionMethod method = cvsrl.getMethod(); 143 psc = method.createConnection(cvsrl, _password); 144 } catch (Exception e) { 145 throw new CVSAuthenticationException(e.toString(), CVSAuthenticationException.NO_RETRY, location); 146 } 147 psc.open(monitor); 148 } 149 } 150 | Popular Tags |