KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > connection > ExtConnection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.connection;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.OutputStream JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
20 import org.eclipse.team.internal.ccvs.core.IServerConnection;
21 import org.eclipse.team.internal.ccvs.core.util.Util;
22 import org.eclipse.team.internal.core.streams.PollingInputStream;
23 import org.eclipse.team.internal.core.streams.PollingOutputStream;
24 import org.eclipse.team.internal.core.streams.TimeoutInputStream;
25 import org.eclipse.team.internal.core.streams.TimeoutOutputStream;
26
27 /**
28  * Implements a connection method which invokes an external tool to
29  * establish the connection to the cvs server. Authentication and starting
30  * of the cvs server are the responsibility of the external connection
31  * tool.
32  */

33 public class ExtConnection implements IServerConnection {
34
35     // cvs format for the repository (e.g. :extssh:user@host:/home/cvs/repo)
36
private ICVSRepositoryLocation location;
37     private String JavaDoc password;
38
39     // incoming from remote host
40
InputStream JavaDoc inputStream;
41
42     // outgoing to remote host
43
OutputStream JavaDoc outputStream;
44     
45     // Process spawn to run the command
46
Process JavaDoc process;
47     
48     protected ExtConnection(ICVSRepositoryLocation location, String JavaDoc password) {
49         this.location = location;
50         this.password = password;
51     }
52     
53     /**
54      * Closes the connection.
55      */

56     public void close() throws IOException JavaDoc {
57         try {
58             if (inputStream != null) inputStream.close();
59         } finally {
60             inputStream = null;
61             try {
62                 if (outputStream != null) outputStream.close();
63             } finally {
64                 outputStream = null;
65                 if (process != null) process.destroy();
66             }
67         }
68     }
69     
70     /**
71      * Returns the <code>InputStream</code> used to read data from the
72      * server.
73      */

74     public InputStream JavaDoc getInputStream() {
75         return inputStream;
76     }
77     
78     /**
79      * Returns the <code>OutputStream</code> used to send data to the
80      * server.
81      */

82     public OutputStream JavaDoc getOutputStream() {
83         return outputStream;
84     }
85     
86     /**
87      * Opens the connection and invokes cvs in server mode.
88      *
89      * @see Connection.open()
90      */

91     public void open(IProgressMonitor monitor) throws IOException JavaDoc {
92         String JavaDoc[] command = ((CVSRepositoryLocation)location).getExtCommand(password);
93         boolean connected = false;
94         try {
95             process = Util.createProcess(command, monitor);
96
97             inputStream = new PollingInputStream(new TimeoutInputStream(process.getInputStream(),
98                 8192 /*bufferSize*/, 1000 /*readTimeout*/, -1 /*closeTimeout*/), location.getTimeout(), monitor);
99             outputStream = new PollingOutputStream(new TimeoutOutputStream(process.getOutputStream(),
100                 8192 /*buffersize*/, 1000 /*writeTimeout*/, 1000 /*closeTimeout*/), location.getTimeout(), monitor);
101
102             // XXX need to do something more useful with stderr
103
connected = true;
104         } finally {
105             if (! connected) {
106                 try {
107                     close();
108                 } finally {
109                     // Ignore any exceptions during close
110
}
111             }
112         }
113     }
114 }
115
Popular Tags