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