KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ssh2 > PServerSSH2ServerConnection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Atsuhiko Yamanaka, JCraft,Inc. - initial API and implementation.
10  * IBM Corporation - ongoing maintenance
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ssh2;
13
14 import java.io.*;
15 import java.util.Properties JavaDoc;
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 JavaDoc password;
28     private Session session;
29     private static int localport = 2403;
30     private IServerConnection psc = null;
31
32     protected PServerSSH2ServerConnection(ICVSRepositoryLocation location, String JavaDoc 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"); //$NON-NLS-1$
50
monitor.worked(1);
51         String JavaDoc cvs_root = location.getRootDirectory();
52         int cvs_port = location.getPort();
53         if (cvs_port == 0)
54             cvs_port = 2401;
55         String JavaDoc cvs_host = location.getHost();
56         String JavaDoc ssh_host = cvs_host;
57         String JavaDoc ssh_user = location.getUsername();
58
59         String JavaDoc 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 JavaDoc e) {
79                 // Ignore
80
}
81         }
82
83         int lport = cvs_port;
84         String JavaDoc rhost = (cvs_host.equals(ssh_host) ? "localhost" : cvs_host); //$NON-NLS-1$
85
int rport = cvs_port;
86
87         // ssh -L lport:rhost:rport ssh_user@ssh_host
88
int retry = 1;
89         while (true) {
90             try {
91                 session = JSchSession.getSession(location, ssh_user, null, ssh_host, ssh_port, monitor).getSession();
92                 String JavaDoc[] list = session.getPortForwardingL();
93                 String JavaDoc name = ":" + rhost + ":" + rport; //$NON-NLS-1$ //$NON-NLS-2$
94
boolean done = false;
95                 for (int i = 0; i < list.length; i++) {
96                     if (list[i].endsWith(name)) {
97                         try {
98                             String JavaDoc foo = list[i].substring(0, list[i].indexOf(':'));
99                             lport = Integer.parseInt(foo);
100                         } catch (Exception JavaDoc ee) {
101                             // Ignore
102
}
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         // password for location will be over-written in JSchSession ;-<
124
((CVSRepositoryLocation)location).setPassword(password);
125         
126         // CVSROOT=":pserver:localhost:"+lport+""cvs_root
127
try {
128             // If user does not give a password, it must be null.
129
String JavaDoc _password = ""; //$NON-NLS-1$
130
if (password != null)
131                 _password = password;
132             Properties JavaDoc prop = new Properties JavaDoc();
133             prop.put("connection", "pserver"); //$NON-NLS-1$ //$NON-NLS-2$
134
prop.put("user", location.getUsername()); //$NON-NLS-1$
135
prop.put("password", _password); //$NON-NLS-1$
136
prop.put("host", "localhost"); //$NON-NLS-1$ //$NON-NLS-2$
137
prop.put("port", Integer.toString(lport)); //$NON-NLS-1$
138
prop.put("root", cvs_root); //$NON-NLS-1$
139

140             CVSRepositoryLocation cvsrl = CVSRepositoryLocation.fromProperties(prop);
141
142             IConnectionMethod method = cvsrl.getMethod();
143             psc = method.createConnection(cvsrl, _password);
144         } catch (Exception JavaDoc e) {
145             throw new CVSAuthenticationException(e.toString(), CVSAuthenticationException.NO_RETRY, location);
146         }
147         psc.open(monitor);
148     }
149 }
150
Popular Tags