001: /*
002: * FtpLoadProcess.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: FtpLoadProcess.java,v 1.2 2003/01/08 13:55:48 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 FtpLoadProcess extends LoadProcess implements
027: BackgroundProcess, Constants {
028: private FtpSession session;
029: private boolean fileIsDirectory;
030: private String listing;
031:
032: public FtpLoadProcess(Buffer buffer, FtpFile file,
033: FtpSession session) {
034: super (buffer, file);
035: this .session = session;
036: }
037:
038: public final String getListing() {
039: return listing;
040: }
041:
042: public final boolean fileIsDirectory() {
043: return fileIsDirectory;
044: }
045:
046: public void run() {
047: Debug.assertTrue(buffer != null);
048: buffer.setBackgroundProcess(this );
049: doLoad();
050: buffer.setBackgroundProcess(null);
051: }
052:
053: private void doLoad() {
054: Debug.assertTrue(session != null);
055: Debug.assertTrue(session.isLocked());
056: session.setProgressNotifier(progressNotifier);
057: if (!session.verifyConnected()) {
058: if (!cancelled) {
059: // Error!
060: if (errorRunnable != null) {
061: String text = session.getErrorText();
062: if (text == null || text.length() == 0) {
063: text = "Unable to connect to "
064: + file.getHostName() + " on port "
065: + file.getPort();
066: }
067: errorRunnable.setMessage(text);
068: SwingUtilities.invokeLater(errorRunnable);
069: }
070: }
071: return;
072: }
073: if (file.canonicalPath() == null
074: || file.canonicalPath().equals(""))
075: file.setCanonicalPath(session.getLoginDirectory());
076: int result = ERROR;
077: if (session.isDirectory(file.canonicalPath())) {
078: // Directory.
079: fileIsDirectory = true;
080: listing = session.getDirectoryListing((FtpFile) file);
081: if (listing != null)
082: result = SUCCESS;
083: } else if (session.isFile(file.canonicalPath())) {
084: // Normal file.
085: listing = session.getDirectoryListingForFile(file
086: .canonicalPath());
087: cache = Utilities.getTempFile();
088: if (cache != null)
089: result = session.get(file, cache, 0);
090: } else {
091: // File not found.
092: if (errorRunnable != null)
093: errorRunnable.setMessage("File not found");
094: }
095: buffer.setBusy(false);
096: if (result == SUCCESS) {
097: if (successRunnable != null)
098: SwingUtilities.invokeLater(successRunnable);
099: } else {
100: deleteCache();
101: if (result == CANCELLED) {
102: if (cancelRunnable != null)
103: SwingUtilities.invokeLater(cancelRunnable);
104: } else {
105: // Error!
106: if (errorRunnable != null)
107: SwingUtilities.invokeLater(errorRunnable);
108: }
109: }
110: // And in any case...
111: session.unlock();
112: }
113:
114: private void deleteCache() {
115: if (cache != null) {
116: if (cache.isFile())
117: cache.delete();
118: cache = null;
119: }
120: }
121: }
|