1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.ui;
27
28 import com.sshtools.common.configuration.SshToolsConnectionProfile;
29
30 import com.sshtools.j2ssh.SftpClient;
31 import com.sshtools.j2ssh.SshClient;
32 import com.sshtools.j2ssh.SshEventAdapter;
33 import com.sshtools.j2ssh.connection.Channel;
34 import com.sshtools.j2ssh.connection.ChannelFactory;
35 import com.sshtools.j2ssh.forwarding.ForwardingClient;
36 import com.sshtools.j2ssh.session.SessionChannelClient;
37
38 import java.awt.BorderLayout;
39
40 import java.io.IOException;
41
42
43 /**
44 * <p>This frame class embeds a SessionProvider and manages the connection
45 * on behalf of the caller. To invoke a session provider from an external
46 * application is a straight forward procedure. Assuming that the connection
47 * has already been established [see SshClient] you can invoke a frame using
48 * the following code:</p>
49 *
50 * <blockquote><pre>
51 * // Create an SshClient connection
52 * SshClient ssh = new SshClient();
53 *
54 * // Connection code goes here - see SshClient for more details
55 *
56 * SessionProviderFrame frame = new SessionProviderFrame(null,
57 * new SshToolsConnectionProfile(),
58 * ssh,
59 * SessionProviderFactory.getInstance().getProvider("sshterm"));
60 * frame.pack();
61 * frame.show();
62 * </pre></blockquote>
63 *
64 * @author Lee David Painter
65 * @version $Id: SessionProviderFrame.java,v 1.9 2003/11/16 19:30:08 rpernavas Exp $
66 */
67 public class SessionProviderFrame extends SshToolsApplicationFrame
68 implements SessionManager {
69 // Private instance variables
70 private SshToolsApplicationSessionPanel panel;
71 private SessionProvider provider;
72 private SshToolsConnectionProfile profile;
73 private SshClient ssh;
74 private boolean disconnectOnClose = false;
75
76 /**
77 * Construct a new Session Provider frame.
78 *
79 * @param app The SshToolsApplication instance, can be null
80 * @param profile The profile of the connection
81 * @param ssh the client connection
82 * @param provider the provider instance
83 * @throws IOException
84 * @throws SshToolsApplicationException
85 */
86 public SessionProviderFrame(SshToolsConnectionProfile profile,
87 SshClient ssh, SessionProvider provider)
88 throws IOException, SshToolsApplicationException {
89 try {
90 this.provider = provider;
91 this.ssh = ssh;
92 this.profile = profile;
93 setIconImage(provider.getSmallIcon().getImage());
94 setTitle(provider.getName() + " - " +
95 ssh.getConnectionProperties().getHost());
96 getContentPane().setLayout(new BorderLayout());
97 getContentPane().add(panel = (SshToolsApplicationSessionPanel) provider.getProviderClass()
98 .newInstance(),
99 BorderLayout.CENTER);
100
101 return;
102 } catch (IllegalAccessException ex) {
103 } catch (InstantiationException ex) {
104 }
105
106 throw new SshToolsApplicationException("Failed to create instance of " +
107 provider.getProviderClass().getName());
108 }
109
110 /**
111 * Initialize the frame and open the remote session
112 * @param app the application object, can be null
113 * @return
114 * @throws IOException
115 * @throws SshToolsApplicationException
116 */
117 public boolean initFrame(SshToolsApplication app)
118 throws IOException, SshToolsApplicationException {
119 panel.setCurrentConnectionProfile(profile);
120 panel.init(app);
121 init(app, panel);
122 pack();
123
124 return panel.openSession(this, profile);
125 }
126
127 /**
128 * Get the attached session provider panel.
129 * @return
130 */
131 public SshToolsApplicationSessionPanel getSessionPanel() {
132 return panel;
133 }
134
135 /**
136 * Returns the guessed EOL setting of the remote computer
137 * @return
138 */
139 public int getRemoteEOL() {
140 return ssh.getRemoteEOL();
141 }
142
143 /**
144 * Called by the application framework when testing exit state
145 * @return
146 */
147 public boolean canExit() {
148 return panel.canClose();
149 }
150
151 /**
152 * Called by the framework when exiting. Can also be called to close the session.
153 */
154 public void exit() {
155 panel.close();
156 dispose();
157 }
158
159 /**
160 * Implementation of the SessionManager method, simply calls the SshClient
161 * openSession method.
162 * @return
163 * @throws IOException
164 */
165 public SessionChannelClient openSession() throws IOException {
166 return ssh.openSessionChannel();
167 }
168
169 /**
170 * Implementation of the SessionManager method, this does nothing. Overide this
171 * method to provide additional functionality to save changes made by the session
172 * to the profile.
173 *
174 * @param profile
175 */
176 public void applyProfileChanges(SshToolsConnectionProfile profile) {
177 }
178
179 /**
180 * When the session closes, should the connection be disconnected?
181 * @param disconnectOnClose
182 */
183 public void setDisconnectOnClose(boolean disconnectOnClose) {
184 this.disconnectOnClose = disconnectOnClose;
185 }
186
187 /**
188 * Implementation of the SessionManager method, this simply calls the SshClient
189 * method openSftpClient.
190 * @return
191 * @throws IOException
192 */
193 public SftpClient openSftpClient() throws IOException {
194 return ssh.openSftpClient();
195 }
196
197 /**
198 * Implementation of the SessionManager method, this simply calls the SshClient
199 * method openChannel.
200 * @param channel
201 * @return
202 * @throws IOException
203 */
204 public boolean openChannel(Channel channel) throws IOException {
205 return ssh.openChannel(channel);
206 }
207
208 /**
209 * Implementation of the SessionManager method, this simply calls the SshClient
210 * method isConnected.
211 * @return
212 */
213 public boolean isConnected() {
214 return ssh.isConnected();
215 }
216
217 /**
218 * Implementation of the SessionManager method, this simply returns false.
219 * Overide to change this behaviour
220 *
221 * @return
222 */
223 public boolean requestDisconnect() {
224 return disconnectOnClose;
225 }
226
227 /**
228 * Implementation of the SessionManager method, simply calls the SshClient
229 * method getForwardingClient.
230 * @return
231 */
232 public ForwardingClient getForwardingClient() {
233 return ssh.getForwardingClient();
234 }
235
236 /**
237 * Implementation of the SessionManager method, simply calls the SshClient
238 * method sendGlobalRequest.
239 * @param requestname
240 * @param wantreply
241 * @param requestdata
242 * @return
243 * @throws IOException
244 */
245 public byte[] sendGlobalRequest(String requestname, boolean wantreply,
246 byte[] requestdata) throws IOException {
247 return ssh.sendGlobalRequest(requestname, wantreply, requestdata);
248 }
249
250 /**
251 * Implementation of the SessionManager method, simply calls the SshClient
252 * method addEventHandler.
253 * @param eventHandler
254 */
255 public void addEventHandler(SshEventAdapter eventHandler) {
256 ssh.addEventHandler(eventHandler);
257 }
258
259 /**
260 * Implemenation of the SessionManager method, simply calls the SshClient
261 * method getServerId.
262 * @return
263 */
264 public String getServerId() {
265 return ssh.getServerId();
266 }
267
268 /**
269 * Implemenation of the SessionManager method, simply calls the SshClient
270 * method allowChannelOpen.
271 * @param channelType
272 * @param cf
273 * @throws IOException
274 */
275 public void allowChannelOpen(String channelType, ChannelFactory cf)
276 throws IOException {
277 ssh.allowChannelOpen(channelType, cf);
278 }
279
280 /**
281 * Gets the profile currently attached to the frame.
282 * @return
283 */
284 public SshToolsConnectionProfile getProfile() {
285 return profile;
286 }
287 }