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 java.io.IOException;
029: import java.awt.BorderLayout;
030: import com.sshtools.common.configuration.SshToolsConnectionProfile;
031: import com.sshtools.j2ssh.SftpClient;
032: import com.sshtools.j2ssh.SshClient;
033: import com.sshtools.j2ssh.SshEventAdapter;
034: import com.sshtools.j2ssh.connection.Channel;
035: import com.sshtools.j2ssh.connection.ChannelFactory;
036: import com.sshtools.j2ssh.forwarding.ForwardingClient;
037: import com.sshtools.j2ssh.session.SessionChannelClient;
038:
039: /**
040: * <p>This frame class embeds a SessionProvider and manages the connection
041: * on behalf of the caller. To invoke a session provider from an external
042: * application is a straight forward procedure. Assuming that the connection
043: * has already been established [see SshClient] you can invoke a frame using
044: * the following code:</p>
045: *
046: * <blockquote><pre>
047: * // Create an SshClient connection
048: * SshClient ssh = new SshClient();
049: *
050: * // Connection code goes here - see SshClient for more details
051: *
052: * SessionProviderFrame frame = new SessionProviderFrame(null,
053: * new SshToolsConnectionProfile(),
054: * ssh,
055: * SessionProviderFactory.getInstance().getProvider("sshterm"));
056: * frame.pack();
057: * frame.show();
058: * </pre></blockquote>
059: *
060: * @author Lee David Painter
061: * @version $Id: SessionProviderInternalFrame.java,v 1.3 2003/09/24 11:26:32 martianx Exp $
062: */
063: public class SessionProviderInternalFrame extends
064: SshToolsApplicationInternalFrame implements SessionManager {
065: // Private instance variables
066: private SshToolsApplicationSessionPanel panel;
067: private SessionProvider provider;
068: private SshToolsConnectionProfile profile;
069: private SshClient ssh;
070: private boolean disconnectOnClose = false;
071:
072: /**
073: * Construct a new Session Provider frame.
074: *
075: * @param app The SshToolsApplication instance, can be null
076: * @param profile The profile of the connection
077: * @param ssh the client connection
078: * @param provider the provider instance
079: * @throws IOException
080: * @throws SshToolsApplicationException
081: */
082: public SessionProviderInternalFrame(
083: SshToolsConnectionProfile profile, SshClient ssh,
084: SessionProvider provider) throws IOException,
085: SshToolsApplicationException {
086: try {
087: this .provider = provider;
088: this .ssh = ssh;
089: this .profile = profile;
090: //setIconImage(provider.getSmallIcon().getImage());
091: setTitle(provider.getName() + " - "
092: + ssh.getConnectionProperties().getHost());
093: getContentPane().setLayout(new BorderLayout());
094: getContentPane().add(
095: panel = (SshToolsApplicationSessionPanel) provider
096: .getProviderClass().newInstance(),
097: BorderLayout.CENTER);
098: return;
099: } catch (IllegalAccessException ex) {
100: } catch (InstantiationException ex) {
101: }
102: throw new SshToolsApplicationException(
103: "Failed to create instance of "
104: + provider.getProviderClass().getName());
105: }
106:
107: /**
108: * Initialize the frame and open the remote session
109: * @param app the application object, can be null
110: * @return
111: * @throws IOException
112: * @throws SshToolsApplicationException
113: */
114: public boolean initFrame(SshToolsApplication app)
115: throws IOException, SshToolsApplicationException {
116: panel.init(app);
117: init(app, panel);
118: pack();
119: return panel.openSession(this , profile);
120: }
121:
122: /**
123: * Get the attached session provider panel.
124: * @return
125: */
126: public SshToolsApplicationSessionPanel getSessionPanel() {
127: return panel;
128: }
129:
130: /**
131: * Called by the application framework when testing exit state
132: * @return
133: */
134: public boolean canExit() {
135: return panel.canClose();
136: }
137:
138: /**
139: * Called by the framework when exiting. Can also be called to close the session.
140: */
141: public void exit() {
142: panel.close();
143: dispose();
144: }
145:
146: /**
147: * Implementation of the SessionManager method, simply calls the SshClient
148: * openSession method.
149: * @return
150: * @throws IOException
151: */
152: public SessionChannelClient openSession() throws IOException {
153: return ssh.openSessionChannel();
154: }
155:
156: /**
157: * Returns the guessed EOL setting of the remote computer
158: * @return
159: */
160: public int getRemoteEOL() {
161: return ssh.getRemoteEOL();
162: }
163:
164: /**
165: * Implementation of the SessionManager method, this does nothing. Overide this
166: * method to provide additional functionality to save changes made by the session
167: * to the profile.
168: *
169: * @param profile
170: */
171: public void applyProfileChanges(SshToolsConnectionProfile profile) {
172: }
173:
174: /**
175: * Implementation of the SessionManager method, this simply calls the SshClient
176: * method openSftpClient.
177: * @return
178: * @throws IOException
179: */
180: public SftpClient openSftpClient() throws IOException {
181: return ssh.openSftpClient();
182: }
183:
184: /**
185: * Implementation of the SessionManager method, this simply calls the SshClient
186: * method openChannel.
187: * @param channel
188: * @return
189: * @throws IOException
190: */
191: public boolean openChannel(Channel channel) throws IOException {
192: return ssh.openChannel(channel);
193: }
194:
195: /**
196: * Implementation of the SessionManager method, this simply calls the SshClient
197: * method isConnected.
198: * @return
199: */
200: public boolean isConnected() {
201: return ssh.isConnected();
202: }
203:
204: /**
205: * When the session closes, should the connection be disconnected?
206: * @param disconnectOnClose
207: */
208: public void setDisconnectOnClose(boolean disconnectOnClose) {
209: this .disconnectOnClose = disconnectOnClose;
210: }
211:
212: /**
213: * Implementation of the SessionManager method, this simply returns false.
214: * Overide to change this behaviour
215: *
216: * @return
217: */
218: public boolean requestDisconnect() {
219: return disconnectOnClose;
220: }
221:
222: /**
223: * Implementation of the SessionManager method, simply calls the SshClient
224: * method getForwardingClient.
225: * @return
226: */
227: public ForwardingClient getForwardingClient() {
228: return ssh.getForwardingClient();
229: }
230:
231: /**
232: * Implementation of the SessionManager method, simply calls the SshClient
233: * method sendGlobalRequest.
234: * @param requestname
235: * @param wantreply
236: * @param requestdata
237: * @return
238: * @throws IOException
239: */
240: public byte[] sendGlobalRequest(String requestname,
241: boolean wantreply, byte[] requestdata) throws IOException {
242: return ssh.sendGlobalRequest(requestname, wantreply,
243: 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)
272: throws 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: }
|