KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > connection > AbstractConnection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.cvsclient.connection;
21
22 import java.io.IOException JavaDoc;
23
24 import org.netbeans.lib.cvsclient.util.*;
25 import org.netbeans.lib.cvsclient.request.*;
26
27
28 /**
29  * This class abstracts the common features and functionality that all connection protocols to CVS
30  * share
31  *
32  * @author Sriram Seshan
33  */

34 public abstract class AbstractConnection implements Connection {
35
36
37     /**
38      * The name of the repository this connection is made to
39      */

40     private String JavaDoc repository = null;
41
42     /**
43      * The socket's input stream.
44      */

45     private LoggedDataInputStream inputStream;
46
47     /**
48      * The socket's output stream.
49      */

50     private LoggedDataOutputStream outputStream;
51
52     /** Creates a new instance of AbstractConnection */
53     public AbstractConnection() {
54     }
55     
56     /**
57      * Get an input stream for receiving data from the server.
58      * @return a data input stream
59      */

60     public LoggedDataInputStream getInputStream() {
61         return inputStream;
62     }
63     
64     /**
65      * Set an input stream for receiving data from the server.
66      * The old stream (if any) is closed.
67      * @param inputStream The data input stream
68      */

69     protected final void setInputStream(LoggedDataInputStream inputStream) {
70         if (this.inputStream == inputStream) return ;
71         if (this.inputStream != null) {
72             try {
73                 this.inputStream.close();
74             } catch (IOException JavaDoc ioex) {/*Ignore*/}
75         }
76         this.inputStream = inputStream;
77     }
78
79     /**
80      * Get an output stream for sending data to the server.
81      * @return an output stream
82      */

83     public LoggedDataOutputStream getOutputStream() {
84         return outputStream;
85     }
86  
87     /**
88      * Set an output stream for sending data to the server.
89      * The old stream (if any) is closed.
90      * @param outputStream The data output stream
91      */

92     protected final void setOutputStream(LoggedDataOutputStream outputStream) {
93         if (this.outputStream == outputStream) return ;
94         if (this.outputStream != null) {
95             try {
96                 this.outputStream.close();
97             } catch (IOException JavaDoc ioex) {/*Ignore*/}
98         }
99         this.outputStream = outputStream;
100     }
101
102     /**
103      * Get the repository path.
104      * @return the repository path, e.g. /home/banana/foo/cvs
105      */

106     public String JavaDoc getRepository() {
107         return repository;
108     }
109
110     /**
111      * Set the repository path.
112      * @param repository the repository
113      */

114     public void setRepository(String JavaDoc repository) {
115         this.repository = repository;
116     }
117     
118     /**
119      * Verifies that this open connection is a connetion to a working CVS server.
120      * Clients should close this connection after verifying.
121      */

122     protected void verifyProtocol() throws IOException JavaDoc {
123         try {
124             outputStream.writeBytes(new RootRequest(repository).getRequestString(), "US-ASCII");
125             outputStream.writeBytes(new UseUnchangedRequest().getRequestString(), "US-ASCII");
126             outputStream.writeBytes(new ValidRequestsRequest().getRequestString(), "US-ASCII");
127             outputStream.writeBytes("noop \n", "US-ASCII");
128         } catch (UnconfiguredRequestException e) {
129             throw new RuntimeException JavaDoc("Internal error verifying CVS protocol: " + e.getMessage());
130         }
131         outputStream.flush();
132
133         StringBuffer JavaDoc responseNameBuffer = new StringBuffer JavaDoc();
134         int c;
135         while ((c = inputStream.read()) != -1) {
136             responseNameBuffer.append((char)c);
137             if (c == '\n') break;
138         }
139
140         String JavaDoc response = responseNameBuffer.toString();
141         if (!response.startsWith("Valid-requests")) {
142             throw new IOException JavaDoc("Unexpected server response: " + response);
143         }
144     }
145 }
146
Popular Tags