001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import com.sshtools.common.configuration.SshToolsConnectionProfile;
029:
030: import com.sshtools.j2ssh.SftpClient;
031: import com.sshtools.j2ssh.SshClient;
032: import com.sshtools.j2ssh.SshEventAdapter;
033: import com.sshtools.j2ssh.connection.Channel;
034: import com.sshtools.j2ssh.connection.ChannelFactory;
035: import com.sshtools.j2ssh.forwarding.ForwardingClient;
036: import com.sshtools.j2ssh.session.SessionChannelClient;
037:
038: import java.awt.BorderLayout;
039:
040: import java.io.IOException;
041:
042: /**
043: * <p>This frame class embeds a SessionProvider and manages the connection
044: * on behalf of the caller. To invoke a session provider from an external
045: * application is a straight forward procedure. Assuming that the connection
046: * has already been established [see SshClient] you can invoke a frame using
047: * the following code:</p>
048: *
049: * <blockquote><pre>
050: * // Create an SshClient connection
051: * SshClient ssh = new SshClient();
052: *
053: * // Connection code goes here - see SshClient for more details
054: *
055: * SessionProviderFrame frame = new SessionProviderFrame(null,
056: * new SshToolsConnectionProfile(),
057: * ssh,
058: * SessionProviderFactory.getInstance().getProvider("sshterm"));
059: * frame.pack();
060: * frame.show();
061: * </pre></blockquote>
062: *
063: * @author Lee David Painter
064: * @version $Id: SessionProviderFrame.java,v 1.9 2003/11/16 19:30:08 rpernavas Exp $
065: */
066: public class SessionProviderFrame extends SshToolsApplicationFrame
067: implements SessionManager {
068: // Private instance variables
069: private SshToolsApplicationSessionPanel panel;
070: private SessionProvider provider;
071: private SshToolsConnectionProfile profile;
072: private SshClient ssh;
073: private boolean disconnectOnClose = false;
074:
075: /**
076: * Construct a new Session Provider frame.
077: *
078: * @param app The SshToolsApplication instance, can be null
079: * @param profile The profile of the connection
080: * @param ssh the client connection
081: * @param provider the provider instance
082: * @throws IOException
083: * @throws SshToolsApplicationException
084: */
085: public SessionProviderFrame(SshToolsConnectionProfile profile,
086: SshClient ssh, SessionProvider provider)
087: throws IOException, SshToolsApplicationException {
088: try {
089: this .provider = provider;
090: this .ssh = ssh;
091: this .profile = profile;
092: setIconImage(provider.getSmallIcon().getImage());
093: setTitle(provider.getName() + " - "
094: + ssh.getConnectionProperties().getHost());
095: getContentPane().setLayout(new BorderLayout());
096: getContentPane().add(
097: panel = (SshToolsApplicationSessionPanel) provider
098: .getProviderClass().newInstance(),
099: BorderLayout.CENTER);
100:
101: return;
102: } catch (IllegalAccessException ex) {
103: } catch (InstantiationException ex) {
104: }
105:
106: throw new SshToolsApplicationException(
107: "Failed to create instance of "
108: + provider.getProviderClass().getName());
109: }
110:
111: /**
112: * Initialize the frame and open the remote session
113: * @param app the application object, can be null
114: * @return
115: * @throws IOException
116: * @throws SshToolsApplicationException
117: */
118: public boolean initFrame(SshToolsApplication app)
119: throws IOException, SshToolsApplicationException {
120: panel.setCurrentConnectionProfile(profile);
121: panel.init(app);
122: init(app, panel);
123: pack();
124:
125: return panel.openSession(this , profile);
126: }
127:
128: /**
129: * Get the attached session provider panel.
130: * @return
131: */
132: public SshToolsApplicationSessionPanel getSessionPanel() {
133: return panel;
134: }
135:
136: /**
137: * Returns the guessed EOL setting of the remote computer
138: * @return
139: */
140: public int getRemoteEOL() {
141: return ssh.getRemoteEOL();
142: }
143:
144: /**
145: * Called by the application framework when testing exit state
146: * @return
147: */
148: public boolean canExit() {
149: return panel.canClose();
150: }
151:
152: /**
153: * Called by the framework when exiting. Can also be called to close the session.
154: */
155: public void exit() {
156: panel.close();
157: dispose();
158: }
159:
160: /**
161: * Implementation of the SessionManager method, simply calls the SshClient
162: * openSession method.
163: * @return
164: * @throws IOException
165: */
166: public SessionChannelClient openSession() throws IOException {
167: return ssh.openSessionChannel();
168: }
169:
170: /**
171: * Implementation of the SessionManager method, this does nothing. Overide this
172: * method to provide additional functionality to save changes made by the session
173: * to the profile.
174: *
175: * @param profile
176: */
177: public void applyProfileChanges(SshToolsConnectionProfile profile) {
178: }
179:
180: /**
181: * When the session closes, should the connection be disconnected?
182: * @param disconnectOnClose
183: */
184: public void setDisconnectOnClose(boolean disconnectOnClose) {
185: this .disconnectOnClose = disconnectOnClose;
186: }
187:
188: /**
189: * Implementation of the SessionManager method, this simply calls the SshClient
190: * method openSftpClient.
191: * @return
192: * @throws IOException
193: */
194: public SftpClient openSftpClient() throws IOException {
195: return ssh.openSftpClient();
196: }
197:
198: /**
199: * Implementation of the SessionManager method, this simply calls the SshClient
200: * method openChannel.
201: * @param channel
202: * @return
203: * @throws IOException
204: */
205: public boolean openChannel(Channel channel) throws IOException {
206: return ssh.openChannel(channel);
207: }
208:
209: /**
210: * Implementation of the SessionManager method, this simply calls the SshClient
211: * method isConnected.
212: * @return
213: */
214: public boolean isConnected() {
215: return ssh.isConnected();
216: }
217:
218: /**
219: * Implementation of the SessionManager method, this simply returns false.
220: * Overide to change this behaviour
221: *
222: * @return
223: */
224: public boolean requestDisconnect() {
225: return disconnectOnClose;
226: }
227:
228: /**
229: * Implementation of the SessionManager method, simply calls the SshClient
230: * method getForwardingClient.
231: * @return
232: */
233: public ForwardingClient getForwardingClient() {
234: return ssh.getForwardingClient();
235: }
236:
237: /**
238: * Implementation of the SessionManager method, simply calls the SshClient
239: * method sendGlobalRequest.
240: * @param requestname
241: * @param wantreply
242: * @param requestdata
243: * @return
244: * @throws IOException
245: */
246: public byte[] sendGlobalRequest(String requestname,
247: boolean wantreply, byte[] requestdata) throws IOException {
248: return ssh.sendGlobalRequest(requestname, wantreply,
249: requestdata);
250: }
251:
252: /**
253: * Implementation of the SessionManager method, simply calls the SshClient
254: * method addEventHandler.
255: * @param eventHandler
256: */
257: public void addEventHandler(SshEventAdapter eventHandler) {
258: ssh.addEventHandler(eventHandler);
259: }
260:
261: /**
262: * Implemenation of the SessionManager method, simply calls the SshClient
263: * method getServerId.
264: * @return
265: */
266: public String getServerId() {
267: return ssh.getServerId();
268: }
269:
270: /**
271: * Implemenation of the SessionManager method, simply calls the SshClient
272: * method allowChannelOpen.
273: * @param channelType
274: * @param cf
275: * @throws IOException
276: */
277: public void allowChannelOpen(String channelType, ChannelFactory cf)
278: throws IOException {
279: ssh.allowChannelOpen(channelType, cf);
280: }
281:
282: /**
283: * Gets the profile currently attached to the frame.
284: * @return
285: */
286: public SshToolsConnectionProfile getProfile() {
287: return profile;
288: }
289: }
|