KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > SSHConnection


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.modules.versioning.system.cvss;
21
22 import org.netbeans.lib.cvsclient.connection.AbstractConnection;
23 import org.netbeans.lib.cvsclient.connection.ConnectionModifier;
24 import org.netbeans.lib.cvsclient.connection.AuthenticationException;
25 import org.netbeans.lib.cvsclient.command.CommandAbortedException;
26 import org.netbeans.lib.cvsclient.util.LoggedDataInputStream;
27 import org.netbeans.lib.cvsclient.util.LoggedDataOutputStream;
28
29 import javax.net.SocketFactory;
30 import java.io.*;
31 import java.util.*;
32 import java.net.Socket JavaDoc;
33 import java.net.UnknownHostException JavaDoc;
34
35 import com.jcraft.jsch.*;
36 import org.openide.util.NbBundle;
37
38 /**
39  * Provides SSH tunnel for :ext: connection method.
40  *
41  * @author Maros Sandor
42  */

43 public class SSHConnection extends AbstractConnection {
44
45     private static final String JavaDoc CVS_SERVER_COMMAND = System.getenv("CVS_SERVER") != null?
46         System.getenv("CVS_SERVER") + " server": "cvs server"; // NOI18N
47

48     private final SocketFactory socketFactory;
49     private final String JavaDoc host;
50     private final int port;
51     private final String JavaDoc username;
52     private final String JavaDoc password;
53     
54     private Session session;
55     private ChannelExec channel;
56
57     /**
58      * Creates new SSH connection object.
59      *
60      * @param socketFactory socket factory to use when connecting to SSH server
61      * @param host host names of the SSH server
62      * @param port port number of SSH server
63      * @param username SSH username
64      * @param password SSH password
65      */

66     public SSHConnection(SocketFactory socketFactory, String JavaDoc host, int port, String JavaDoc username, String JavaDoc password) {
67         this.socketFactory = socketFactory;
68         this.host = host;
69         this.port = port;
70         this.username = username != null ? username : System.getProperty("user.name"); // NOI18N
71
this.password = password;
72     }
73
74     public void open() throws AuthenticationException, CommandAbortedException {
75
76         Properties props = new Properties();
77         props.put("StrictHostKeyChecking", "no"); // NOI18N
78

79         JSch jsch = new JSch();
80         try {
81             session = jsch.getSession(username, host, port);
82             session.setSocketFactory(new SocketFactoryBridge());
83             session.setConfig(props);
84             session.setUserInfo(new SSHUserInfo());
85             session.connect();
86         } catch (JSchException e) {
87             throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "BK3001"));
88         }
89         
90         try {
91             channel = (ChannelExec) session.openChannel("exec"); // NOI18N
92
channel.setCommand(CVS_SERVER_COMMAND);
93             setInputStream(new LoggedDataInputStream(new SshChannelInputStream(channel)));
94             setOutputStream(new LoggedDataOutputStream(channel.getOutputStream()));
95             channel.connect();
96         } catch (JSchException e) {
97             IOException ioe = new IOException(NbBundle.getMessage(SSHConnection.class, "BK3002"));
98             ioe.initCause(e);
99             throw new AuthenticationException(ioe, NbBundle.getMessage(SSHConnection.class, "BK3003"));
100         } catch (IOException e) {
101             throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "BK3003"));
102         }
103     }
104
105     /**
106      * Verifies that we can successfuly connect to the SSH server and run 'cvs server' command on it.
107      *
108      * @throws AuthenticationException if connection to the SSH server cannot be established (network problem)
109      */

110     public void verify() throws AuthenticationException {
111         try {
112             open();
113             try {
114                 Thread.sleep(1000);
115             } catch (InterruptedException JavaDoc e) {
116                 // ignore
117
}
118             if (channel.getExitStatus() != -1) {
119                 throw new AuthenticationException(CVS_SERVER_COMMAND, NbBundle.getMessage(SSHConnection.class, "BK3004", CVS_SERVER_COMMAND));
120             }
121             close();
122         } catch (CommandAbortedException e) {
123             throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "BK3005"));
124         } catch (IOException e) {
125             throw new AuthenticationException(e, NbBundle.getMessage(SSHConnection.class, "Bk3006"));
126         } finally {
127             reset();
128         }
129     }
130
131     private void reset() {
132         session = null;
133         channel = null;
134         setInputStream(null);
135         setOutputStream(null);
136     }
137     
138     public void close() throws IOException {
139         if (channel != null) channel.disconnect();
140         if (session != null) session.disconnect();
141         reset();
142     }
143
144     public boolean isOpen() {
145         return channel != null && channel.isConnected();
146     }
147
148     public int getPort() {
149         return port;
150     }
151
152     public void modifyInputStream(ConnectionModifier modifier) throws IOException {
153         modifier.modifyInputStream(getInputStream());
154     }
155
156     public void modifyOutputStream(ConnectionModifier modifier) throws IOException {
157         modifier.modifyOutputStream(getOutputStream());
158     }
159
160     /**
161      * Provides JSch with SSH password.
162      */

163     private class SSHUserInfo implements UserInfo, UIKeyboardInteractive {
164         public String JavaDoc getPassphrase() {
165             return null;
166         }
167
168         public String JavaDoc getPassword() {
169             return password;
170         }
171
172         public boolean promptPassword(String JavaDoc message) {
173             return true;
174         }
175
176         public boolean promptPassphrase(String JavaDoc message) {
177             return true;
178         }
179
180         public boolean promptYesNo(String JavaDoc message) {
181             return false;
182         }
183
184         public void showMessage(String JavaDoc message) {
185         }
186
187         public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
188                                                   String JavaDoc name,
189                                                   String JavaDoc instruction,
190                                                   String JavaDoc[] prompt,
191                                                   boolean[] echo){
192           String JavaDoc[] response=new String JavaDoc[prompt.length];
193           if(prompt.length==1){
194             response[0]=password;
195           }
196           return response;
197         }
198     }
199     
200     /**
201      * Bridges com.jcraft.jsch.SocketFactory and javax.net.SocketFactory.
202      */

203     private class SocketFactoryBridge implements com.jcraft.jsch.SocketFactory {
204
205         public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException, UnknownHostException JavaDoc {
206             return socketFactory.createSocket(host, port);
207         }
208
209         public InputStream getInputStream(Socket JavaDoc socket) throws IOException {
210             return socket.getInputStream();
211         }
212
213         public OutputStream getOutputStream(Socket JavaDoc socket) throws IOException {
214             return socket.getOutputStream();
215         }
216     }
217     
218     private static class SshChannelInputStream extends FilterInputStream {
219         
220         private final Channel channel;
221
222         public SshChannelInputStream(Channel channel) throws IOException {
223             super(channel.getInputStream());
224             this.channel = channel;
225         }
226
227         public int available() throws IOException {
228             checkChannelState();
229             return super.available();
230         }
231
232         private void checkChannelState() throws IOException {
233             int exitStatus = channel.getExitStatus();
234             if (exitStatus > 0 || exitStatus < -1) throw new IOException(NbBundle.getMessage(SSHConnection.class, "BK3004", CVS_SERVER_COMMAND));
235             if (exitStatus == 0 || channel.isEOF()) throw new EOFException(NbBundle.getMessage(SSHConnection.class, "BK3007"));
236         }
237     }
238 }
239
Popular Tags