001: /*
002: * SshLoadProcess.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: SshLoadProcess.java,v 1.6 2003/05/19 12:20:28 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import javax.swing.SwingUtilities;
025:
026: public final class SshLoadProcess extends LoadProcess implements
027: BackgroundProcess, Runnable, Cancellable {
028: private boolean fileIsDirectory;
029: private String listing;
030:
031: public SshLoadProcess(Buffer buffer, SshFile file) {
032: super (buffer, file);
033: }
034:
035: public final String getListing() {
036: return listing;
037: }
038:
039: public final boolean fileIsDirectory() {
040: return fileIsDirectory;
041: }
042:
043: public void run() {
044: buffer.setBackgroundProcess(this );
045: _run();
046: buffer.setBackgroundProcess(null);
047: }
048:
049: private void _run() {
050: Debug.assertTrue(file instanceof SshFile);
051: SshSession session = SshSession.getSession((SshFile) file);
052: Debug.assertTrue(session.isLocked());
053: if (!session.isConnected())
054: session.setOutputBuffer(buffer);
055: if (!session.connect()) {
056: session.dispose();
057: if (errorRunnable != null) {
058: errorRunnable.setMessage("Couldn't connect");
059: SwingUtilities.invokeLater(errorRunnable);
060: }
061: return;
062: }
063: session.setOutputBuffer(null);
064: if (file.canonicalPath() == null
065: || file.canonicalPath().length() == 0)
066: file.setCanonicalPath(session.getLoginDirectory());
067: String pass = session.getPassphrase();
068: if (pass != null)
069: file.setPassword(pass);
070: else
071: file.setPassword(session.getPassword());
072: if (session.isDirectory(file.canonicalPath())) {
073: fileIsDirectory = true;
074: listing = session.retrieveDirectoryListing(file);
075: session.unlock();
076: if (listing != null) {
077: DirectoryCache.getDirectoryCache().put(file, listing);
078: } else {
079: // Report error!
080: if (errorRunnable != null) {
081: String message = "Unable to retrieve directory listing for "
082: .concat(file.netPath());
083: errorRunnable.setMessage(message);
084: SwingUtilities.invokeLater(errorRunnable);
085: }
086: return;
087: }
088: } else {
089: // Not a directory.
090: session.unlock();
091: cache = Utilities.getTempFile();
092: Ssh ssh = new Ssh();
093: if (!ssh.copy(file, cache)) {
094: // Report error!
095: if (errorRunnable != null) {
096: errorRunnable.setMessage(ssh.getErrorText());
097: SwingUtilities.invokeLater(errorRunnable);
098: }
099: return;
100: }
101: if (!cache.isFile()) {
102: // Report error!
103: if (errorRunnable != null) {
104: errorRunnable.setMessage("File not found");
105: SwingUtilities.invokeLater(errorRunnable);
106: }
107: return;
108: }
109: // Start a thread to get a fresh directory listing of the parent
110: // directory in order to speed up completions later.
111: final File parent = file.getParentFile();
112: if (parent != null) {
113: Thread t = new Thread() {
114: public void run() {
115: ((SshFile) parent).getDirectoryListing(true);
116: }
117: };
118: t.start();
119: }
120: }
121: if (successRunnable != null)
122: SwingUtilities.invokeLater(successRunnable);
123: }
124: }
|