KickJava   Java API By Example, From Geeks To Geeks.

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


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 the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.connection;
23
24 import java.io.*;
25
26 import org.netbeans.lib.cvsclient.util.*;
27 // import org.openide.util.RequestProcessor;
28

29 /**
30  * Implements a connection to a local server. See the cvs documents for more
31  * information about different connection methods. Local is popular where
32  * the CVS repository exists on the machine where the client library is
33  * running.<p>
34  * Because this library implements just the client part, it can not operate
35  * directly on the repository. It needs a server to talk to. Therefore
36  * it needs to execute the server process on the local machine.
37  *
38  * @author Robert Greig
39  */

40 public class LocalConnection extends AbstractConnection {
41
42     private static final String JavaDoc CVS_EXE_COMMAND = System.getenv("CVS_EXE") != null?
43         System.getenv("CVS_EXE") + " server": "cvs server"; // NOI18N
44

45     /**
46      * The CVS process that is being run.
47      */

48     protected Process JavaDoc process;
49
50     /**
51      * Creates a instance of ServerConnection.
52      */

53     public LocalConnection() {
54         reset();
55     }
56
57     /**
58      * Authenticate a connection with the server.
59      *
60      * @throws AuthenticationException if an error occurred
61      */

62     private void openConnection()
63             throws AuthenticationException {
64         try {
65             process = Runtime.getRuntime().exec(CVS_EXE_COMMAND);
66             setOutputStream(new LoggedDataOutputStream(process.
67                                                       getOutputStream()));
68             setInputStream(new LoggedDataInputStream(process.getInputStream()));
69         }
70         catch (IOException t) {
71             reset();
72             String JavaDoc locMessage = AuthenticationException.getBundleString(
73                     "AuthenticationException.ServerConnection"); //NOI18N
74
throw new AuthenticationException("Connection error", t, locMessage); //NOI18N
75
}
76     }
77
78     private void reset() {
79         process = null;
80         setInputStream(null);
81         setOutputStream(null);
82     }
83
84     /**
85      * Authenticate with the server. Closes the connection immediately.
86      * Clients can use this method to ensure that they are capable of
87      * authenticating with the server. If no exception is thrown, you can
88      * assume that authentication was successful
89      *
90      * @throws AuthenticationException if the connection with the server
91      * cannot be established
92      */

93     public void verify() throws AuthenticationException {
94         try {
95             openConnection();
96             verifyProtocol();
97             process.destroy();
98         }
99         catch (Exception JavaDoc e) {
100             String JavaDoc locMessage = AuthenticationException.getBundleString(
101                     "AuthenticationException.ServerVerification"); //NOI18N
102
throw new AuthenticationException("Verification error", e, locMessage); //NOI18N
103
}
104         finally {
105             reset();
106         }
107     }
108
109     /**
110      * Authenticate with the server and open a channel of communication
111      * with the server. This Client will
112      * call this method before interacting with the server. It is up to
113      * implementing classes to ensure that they are configured to
114      * talk to the server (e.g. port number etc.)
115      * @throws AuthenticationException if the connection with the server
116      * cannot be established
117      */

118     public void open() throws AuthenticationException {
119         openConnection();
120     }
121
122     /**
123      * Returns true to indicate that the connection was successfully established.
124      */

125     public boolean isOpen() {
126         return process != null;
127     }
128
129     /**
130      * Close the connection with the server.
131      */

132     public void close() throws IOException {
133         try {
134             if (process != null) {
135                 process.destroy();
136             }
137         }
138         finally {
139             reset();
140         }
141     }
142     
143     /**
144      * @return 0, no port is used by the local connection.
145      */

146     public int getPort() {
147         return 0; // No port
148
}
149     
150     /**
151      * Modify the underlying inputstream.
152      * @param modifier the connection modifier that performs the modifications
153      * @throws IOException if an error occurs modifying the streams
154      */

155     public void modifyInputStream(ConnectionModifier modifier)
156             throws IOException {
157         modifier.modifyInputStream(getInputStream());
158     }
159
160     /**
161      * Modify the underlying outputstream.
162      * @param modifier the connection modifier that performs the modifications
163      * @throws IOException if an error occurs modifying the streams
164      */

165     public void modifyOutputStream(ConnectionModifier modifier)
166             throws IOException {
167         modifier.modifyOutputStream(getOutputStream());
168     }
169     
170 }
171
Popular Tags