KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > FtpLoadProcess


1 /*
2  * FtpLoadProcess.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: FtpLoadProcess.java,v 1.2 2003/01/08 13:55:48 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.SwingUtilities JavaDoc;
25
26 public final class FtpLoadProcess extends LoadProcess implements BackgroundProcess,
27     Constants
28 {
29     private FtpSession session;
30     private boolean fileIsDirectory;
31     private String JavaDoc listing;
32
33     public FtpLoadProcess(Buffer buffer, FtpFile file, FtpSession session)
34     {
35         super(buffer, file);
36         this.session = session;
37     }
38
39     public final String JavaDoc getListing()
40     {
41         return listing;
42     }
43
44     public final boolean fileIsDirectory()
45     {
46         return fileIsDirectory;
47     }
48
49     public void run()
50     {
51         Debug.assertTrue(buffer != null);
52         buffer.setBackgroundProcess(this);
53         doLoad();
54         buffer.setBackgroundProcess(null);
55     }
56
57     private void doLoad()
58     {
59         Debug.assertTrue(session != null);
60         Debug.assertTrue(session.isLocked());
61         session.setProgressNotifier(progressNotifier);
62         if (!session.verifyConnected()) {
63             if (!cancelled) {
64                 // Error!
65
if (errorRunnable != null) {
66                     String JavaDoc text = session.getErrorText();
67                     if (text == null || text.length() == 0) {
68                         text = "Unable to connect to " + file.getHostName() + " on port " + file.getPort();
69                     }
70                     errorRunnable.setMessage(text);
71                     SwingUtilities.invokeLater(errorRunnable);
72                 }
73             }
74             return;
75         }
76         if (file.canonicalPath() == null || file.canonicalPath().equals(""))
77             file.setCanonicalPath(session.getLoginDirectory());
78         int result = ERROR;
79         if (session.isDirectory(file.canonicalPath())) {
80             // Directory.
81
fileIsDirectory = true;
82             listing = session.getDirectoryListing((FtpFile)file);
83             if (listing != null)
84                 result = SUCCESS;
85         } else if (session.isFile(file.canonicalPath())) {
86             // Normal file.
87
listing = session.getDirectoryListingForFile(file.canonicalPath());
88             cache = Utilities.getTempFile();
89             if (cache != null)
90                 result = session.get(file, cache, 0);
91         } else {
92             // File not found.
93
if (errorRunnable != null)
94                 errorRunnable.setMessage("File not found");
95         }
96         buffer.setBusy(false);
97         if (result == SUCCESS) {
98             if (successRunnable != null)
99                 SwingUtilities.invokeLater(successRunnable);
100         } else {
101             deleteCache();
102             if (result == CANCELLED) {
103                 if (cancelRunnable != null)
104                     SwingUtilities.invokeLater(cancelRunnable);
105             } else {
106                 // Error!
107
if (errorRunnable != null)
108                     SwingUtilities.invokeLater(errorRunnable);
109             }
110         }
111         // And in any case...
112
session.unlock();
113     }
114
115     private void deleteCache()
116     {
117         if (cache != null) {
118             if (cache.isFile())
119                 cache.delete();
120             cache = null;
121         }
122     }
123 }
124
Popular Tags