1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This file was part of JavaCVS by Nicholas Allen ( nallen@freenet.co.uk ).
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 $RCSfile: MindBrightSSHConnection.java,v $
21 Created on $Date: 2003/06/28 06:51:42 $
22 */
23
24 package net.sf.bloof.scm.cvsplugin;
25
26 import mindbright.security.Cipher;
27 import mindbright.ssh.SSHAuthenticator;
28 import mindbright.ssh.SSHChannelController;
29 import mindbright.ssh.SSHClient;
30 import mindbright.ssh.SSHClientUser;
31 import mindbright.ssh.SSHClientUserAdaptor;
32 import mindbright.ssh.SSHConsole;
33 import mindbright.ssh.SSHInteractor;
34 import mindbright.ssh.SSHInteractorAdapter;
35 import mindbright.ssh.SSHPasswordAuthenticator;
36 import mindbright.terminal.Terminal;
37
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.OutputStream;
41 import java.io.PipedInputStream;
42 import java.io.PipedOutputStream;
43 import java.io.UnsupportedEncodingException;
44 /***
45 * Defines a server connection via SSH protocol
46 * @author Nicholas Allen
47 * @author Lukasz Pekacki
48 * */
49 public class MindBrightSSHConnection extends CvsConnection {
50
51 private class ClientUser extends SSHClientUserAdaptor {
52 ClientUser(String aHostName) {
53 super(aHostName);
54 }
55
56 public SSHInteractor getInteractor() {
57 return new Interactor();
58 }
59
60 }
61
62 /***
63 * Private class to send on data received from the server so it can be
64 * read though the connection's input stream.
65 * */
66 private class Console implements SSHConsole {
67 public Console() throws IOException {
68 mPipedOutputStream = new PipedOutputStream(mIn);
69 }
70
71 public Terminal getTerminal() {
72 return null;
73 }
74
75 public void print(String aStr) {
76 displayStatus(aStr);
77 }
78 public void println(String aStr) {
79 displayStatus(aStr);
80 }
81 public void serverConnect(SSHChannelController aController, Cipher aSndCipher) {
82 if (aController != null) {
83 }
84 if (aSndCipher != null) {
85 }
86 }
87
88 public void serverDisconnect(String aReason) {
89 displayStatus("Server disconnected: " + aReason);
90 try {
91 mPipedOutputStream.close();
92 } catch (IOException e) {
93 e.printStackTrace();
94 }
95 }
96
97 public void stderrWriteString(byte[] aStr) {
98 try {
99 displayStatus(new String(aStr, "ascii"));
100 } catch (UnsupportedEncodingException e) {
101 throw new Error("ASCII not supoorted " + e.getMessage());
102 }
103 }
104
105 public void stdoutWriteString(byte[] aStr) {
106 try {
107 mPipedOutputStream.write(aStr);
108 } catch (IOException e) {
109 e.printStackTrace();
110 }
111 }
112 private PipedOutputStream mPipedOutputStream;
113
114 }
115
116 /***
117 * Private class which keeps user notified of progress of connection.
118 * */
119 private class Interactor extends SSHInteractorAdapter {
120
121 public void alert(String aMsg) {
122 displayStatus(aMsg);
123 }
124
125 public void connected(SSHClient aClient) {
126 if (aClient != null) {
127 }
128 displayStatus("Connected to " + getRepositoryLocation());
129 }
130 public boolean isVerbose() {
131 return true;
132 }
133
134 public void report(String aMsg) {
135 displayStatus(aMsg);
136 }
137 }
138
139 private class SSHOutputStream extends OutputStream {
140
141 public void write(byte[] aBytes) throws IOException {
142 write(aBytes, 0, aBytes.length);
143 }
144
145 public void write(byte[] aBytes, int aOffset, int aLength) throws IOException {
146 mSshClient.stdinWriteString(aBytes, aOffset, aLength);
147 }
148 public void write(int aB) throws IOException {
149 mSshClient.stdinWriteChar((char) aB);
150 }
151 }
152 /***
153 * Constructs a MindBrightSSHConnection
154 * @param aCvsAccess access to CVS repository
155 * @throws IOException on IO Error
156 * @throws CvsConnectionException if connection fails
157 */
158 public MindBrightSSHConnection(CvsAccess aCvsAccess)
159 throws IOException, CvsConnectionException {
160 this(aCvsAccess.getLocation(), aCvsAccess.getLoginDetails());
161 }
162
163 /***
164 * Constructs a server connection via SSH with the specified parameters
165 * @param aLocation location of the cvs repository
166 * @param aDetails login details for access
167 * @throws CvsConnectionException if any error occurs
168 */
169 public MindBrightSSHConnection(RepositoryLocation aLocation, LoginDetails aDetails)
170 throws CvsConnectionException {
171 super(aLocation);
172 SSHAuthenticator authenticator =
173 new SSHPasswordAuthenticator(aDetails.getUserName(), aDetails.getPassword());
174 SSHClientUser user = new ClientUser(aLocation.getHostName());
175 mSshClient = new SSHClient(authenticator, user);
176
177 try {
178 mSshClient.setConsole(new Console());
179 mSshClient.doSingleCommand("cvs server", true, 0);
180 } catch (IOException e) {
181 throw new CvsConnectionException(e);
182 }
183 }
184 /***
185 * Closes the connection
186 */
187 public void close() {
188 if (mSshClient != null) {
189 mSshClient.forcedDisconnect();
190 mSshClient = null;
191 }
192 }
193
194 /***
195 * Gets the input stream from which the client should read server responses from
196 * @return InputStream
197 */
198 public InputStream getInputStream() {
199 return mIn;
200 }
201 /***
202 * Gets the output stream over which the client should send its requests to the server
203 * @return OutputStream
204 */
205 public OutputStream getOutputStream() {
206 return mOut;
207 }
208 private PipedInputStream mIn = new PipedInputStream();
209 private OutputStream mOut = new SSHOutputStream();
210 private SSHClient mSshClient;
211
212 }
This page was automatically generated by Maven