KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IBM Corporation - initial API and implementation
10  * Brock Janiczak (brockj@tpg.com.au) - Bug 144419 Avoid calculating encoding for each line read/written
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.core.connection;
13
14  
15 import java.io.*;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.team.internal.ccvs.core.*;
21
22 /**
23  * A connection to talk to a cvs server. The life cycle of a connection is
24  * as follows:
25  * <ul>
26  * <li> constructor: creates a new connection object that wraps the given
27  * repository location and connection method.
28  * <li> open: opens a connection.
29  * <li> send a request: use write* method or use the request stream directly.
30  * <code>GetRequestStream</code> returns an output stream to directly
31  * talk to the server.
32  * <li> read responses: use read* methods or use the response stream directly.
33  * <code>GetResponseStream</code> returns an input stream to directly
34  * read output from the server.
35  * <li> close: closes the connection. A closed connection can be reopened by
36  * calling open again.
37  * </ul>
38  */

39 public class Connection {
40     private static final byte NEWLINE= 0xA;
41     
42     private IServerConnection serverConnection;
43     private ICVSRepositoryLocation fCVSRoot;
44     private boolean fIsEstablished;
45     private InputStream fResponseStream;
46     private String JavaDoc fServerEncoding;
47     private byte[] readLineBuffer = new byte[256];
48
49     public Connection(ICVSRepositoryLocation cvsroot, IServerConnection serverConnection) {
50         fCVSRoot = cvsroot;
51         this.serverConnection = serverConnection;
52         fServerEncoding = getEncoding(fCVSRoot);
53     }
54     
55     private static byte[] append(byte[] buffer, int index, byte b) {
56         if (index >= buffer.length) {
57             byte[] newBuffer= new byte[index * 2];
58             System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
59             buffer= newBuffer;
60         }
61         buffer[index]= b;
62         return buffer;
63     }
64     /**
65      * Closes the connection.
66      */

67     public void close() {
68         if (!isEstablished())
69             return;
70         try {
71             serverConnection.close();
72         } catch (IOException ex) {
73             // Generally, errors on close are of no interest.
74
// However, log them if debugging is on
75
if (CVSProviderPlugin.getPlugin().isDebugging()) {
76                 CVSProviderPlugin.log(new CVSCommunicationException(CVSMessages.Connection_cannotClose, fCVSRoot, ex));
77             }
78         } finally {
79             fResponseStream = null;
80             fIsEstablished = false;
81         }
82     }
83     /**
84      * Flushes the request stream.
85      */

86     public void flush() throws CVSException {
87         if (!isEstablished())
88             return;
89         try {
90             getOutputStream().flush();
91         } catch(IOException e) {
92             throw new CVSCommunicationException(fCVSRoot,e);
93         }
94     }
95     
96     /**
97      * Returns the <code>OutputStream</code> used to send requests
98      * to the server.
99      */

100     public OutputStream getOutputStream() {
101         if (!isEstablished())
102             return null;
103         return serverConnection.getOutputStream();
104     }
105     /**
106      * Returns the <code>InputStream</code> used to read responses from
107      * the server.
108      */

109     public InputStream getInputStream() {
110         if (!isEstablished())
111             return null;
112         if (fResponseStream == null)
113             fResponseStream = serverConnection.getInputStream();
114         return fResponseStream;
115     }
116
117     /**
118      * Returns <code>true</code> if the connection is established;
119      * otherwise <code>false</code>.
120      */

121     public boolean isEstablished() {
122         return fIsEstablished;
123     }
124
125     /**
126      * Opens the connection.
127      */

128     public void open(IProgressMonitor monitor) throws CVSException {
129         if (isEstablished())
130             return;
131         try {
132             serverConnection.open(monitor);
133         } catch (IOException e) {
134             throw new CVSCommunicationException(NLS.bind(CVSMessages.Connection_0, new String JavaDoc[] { fCVSRoot.getLocation(true), CVSCommunicationException.getMessageFor(e) }), fCVSRoot, e);
135         }
136         fIsEstablished= true;
137     }
138     /**
139      * Reads a line from the response stream.
140      */

141     public String JavaDoc readLine() throws CVSException {
142         if (!isEstablished())
143             throw new CVSCommunicationException(CVSMessages.Connection_readUnestablishedConnection,fCVSRoot,null);
144         try {
145             InputStream in = getInputStream();
146             int index = 0;
147             int r;
148             while ((r = in.read()) != -1) {
149                 if (r == NEWLINE) break;
150                 readLineBuffer = append(readLineBuffer, index++, (byte) r);
151             }
152
153             String JavaDoc result = new String JavaDoc(readLineBuffer, 0, index, fServerEncoding);
154             if (Policy.isDebugProtocol()) Policy.printProtocolLine(result);
155             return result;
156         } catch (IOException e) {
157             throw new CVSCommunicationException(fCVSRoot,e);
158         }
159     }
160     
161     static String JavaDoc readLine(ICVSRepositoryLocation location, InputStream in) throws IOException {
162         byte[] buffer = new byte[256];
163         int index = 0;
164         int r;
165         while ((r = in.read()) != -1) {
166             if (r == NEWLINE)
167                 break;
168             buffer = append(buffer, index++, (byte) r);
169         }
170
171         String JavaDoc result = new String JavaDoc(buffer, 0, index, getEncoding(location));
172         if (Policy.isDebugProtocol())
173             Policy.printProtocolLine(result);
174         return result;
175     }
176
177     //---- Helper to send strings to the server ----------------------------
178

179     /**
180      * Sends the given string to the server.
181      */

182     public void write(String JavaDoc s) throws CVSException {
183         try {
184             write(s.getBytes(fServerEncoding), false);
185         } catch (UnsupportedEncodingException e) {
186             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, e.getMessage(), e, fCVSRoot);
187             throw new CVSException (status);
188         }
189     }
190     
191     /**
192      * Return the encoding for the given repository location
193      * @return the encoding for the given repository location
194      */

195     public static String JavaDoc getEncoding(ICVSRepositoryLocation location) {
196         return location.getEncoding();
197     }
198
199     /**
200      * Sends the given string and a newline to the server.
201      */

202     public void writeLine(String JavaDoc s) throws CVSException {
203         try {
204             write(s.getBytes(fServerEncoding), true);
205         } catch (UnsupportedEncodingException e) {
206             IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, e.getMessage(), e, fCVSRoot);
207             throw new CVSException (status);
208         }
209     }
210
211     void write (byte[] bytes, boolean newLine) throws CVSException {
212         write(bytes, 0, bytes.length, newLine);
213     }
214     
215     /**
216      * Low level method to write a string to the server. All write* methods are
217      * funneled through this method.
218      */

219     void write(byte[] b, int off, int len, boolean newline) throws CVSException {
220         if (!isEstablished())
221             throw new CVSCommunicationException(CVSMessages.Connection_writeUnestablishedConnection,fCVSRoot,null);
222             
223         if (Policy.isDebugProtocol())
224             Policy.printProtocol(new String JavaDoc(b, off, len), newline);
225     
226         try {
227             OutputStream out= getOutputStream();
228             out.write(b, off, len);
229             if (newline)
230                 out.write(NEWLINE);
231             
232         } catch (IOException e) {
233             throw new CVSCommunicationException(fCVSRoot,e);
234         }
235     }
236 }
237
Popular Tags