Skip to main content
summaryrefslogtreecommitdiffstats
blob: cec941c1c25384af7bc1c5d20d16f87f2ed3f0ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* -*-mode:java; c-basic-offset:2; -*- */
/**********************************************************************
Copyright (c) 2003, Atsuhiko Yamanaka, JCraft,Inc. and others.
All rights reserved.   This program and the accompanying materials
are made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html

Contributors:
    Atsuhiko Yamanaka, JCraft,Inc. - initial API and implementation.
**********************************************************************/
package org.eclipse.team.cvs.ssh2;

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

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.IServerConnection;
import org.eclipse.team.internal.ccvs.core.connection.CVSAuthenticationException;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import com.jcraft.jsch.*;

public class CVSSSH2ServerConnection implements IServerConnection {
	
  private static final String COMMAND="cvs server";
	
  private ICVSRepositoryLocation location;
  private String password;

  private InputStream inputStream;
  private OutputStream outputStream;

  private Session session;
  private Channel channel;

  protected CVSSSH2ServerConnection(ICVSRepositoryLocation location, 
				    String password) {
    this.location=location;
    this.password=password;
  }

  public void close() throws IOException {
    if(channel!=null)
      channel.disconnect();
  }

  public InputStream getInputStream() { return inputStream; }
  public OutputStream getOutputStream() { return outputStream; }

  public void open(IProgressMonitor monitor) throws IOException, 
						    CVSAuthenticationException {
    try{
      String hostname=location.getHost();
      String username=location.getUsername();
      int port=location.getPort();
      if(port==ICVSRepositoryLocation.USE_DEFAULT_PORT)
	port=0;

      int retry=1;
      OutputStream channel_out;
      InputStream channel_in;

      while(true){
	session=JSchSession.getSession(username, password, hostname, port);
	channel=session.openChannel("exec");
	((ChannelExec)channel).setCommand(COMMAND);

	channel_out=channel.getOutputStream();
	channel_in=channel.getInputStream();

	try{ channel.connect(); }
	catch(JSchException ee){
          if(!session.isConnected()){
	    //System.out.println("sesssion is down");
	    //channel.disconnect();
	    retry--;
	    if(retry<0){
	      throw new CVSAuthenticationException("session is down");
	    }
	    continue;
	  }
	  throw ee;
	}
	break;
      }

      inputStream=channel_in;
      outputStream=channel_out;
    }
    catch(JSchException e){
      //e.printStackTrace();
      throw new CVSAuthenticationException(e.toString());
    }
  }
}

Back to the top