KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SshLoadProcess.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: SshLoadProcess.java,v 1.6 2003/05/19 12:20:28 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 SshLoadProcess extends LoadProcess implements BackgroundProcess,
27     Runnable JavaDoc, Cancellable
28 {
29     private boolean fileIsDirectory;
30     private String JavaDoc listing;
31
32     public SshLoadProcess(Buffer buffer, SshFile file)
33     {
34         super(buffer, file);
35     }
36
37     public final String JavaDoc getListing()
38     {
39         return listing;
40     }
41
42     public final boolean fileIsDirectory()
43     {
44         return fileIsDirectory;
45     }
46
47     public void run()
48     {
49         buffer.setBackgroundProcess(this);
50         _run();
51         buffer.setBackgroundProcess(null);
52     }
53
54     private void _run()
55     {
56         Debug.assertTrue(file instanceof SshFile);
57         SshSession session = SshSession.getSession((SshFile)file);
58         Debug.assertTrue(session.isLocked());
59         if (!session.isConnected())
60             session.setOutputBuffer(buffer);
61         if (!session.connect()) {
62             session.dispose();
63             if (errorRunnable != null) {
64                 errorRunnable.setMessage("Couldn't connect");
65                 SwingUtilities.invokeLater(errorRunnable);
66             }
67             return;
68         }
69         session.setOutputBuffer(null);
70         if (file.canonicalPath() == null || file.canonicalPath().length() == 0)
71             file.setCanonicalPath(session.getLoginDirectory());
72         String JavaDoc pass = session.getPassphrase();
73         if (pass != null)
74             file.setPassword(pass);
75         else
76             file.setPassword(session.getPassword());
77         if (session.isDirectory(file.canonicalPath())) {
78             fileIsDirectory = true;
79             listing = session.retrieveDirectoryListing(file);
80             session.unlock();
81             if (listing != null) {
82                 DirectoryCache.getDirectoryCache().put(file, listing);
83             } else {
84                 // Report error!
85
if (errorRunnable != null) {
86                     String JavaDoc message =
87                         "Unable to retrieve directory listing for ".concat(
88                             file.netPath());
89                     errorRunnable.setMessage(message);
90                     SwingUtilities.invokeLater(errorRunnable);
91                 }
92                 return;
93             }
94         } else {
95             // Not a directory.
96
session.unlock();
97             cache = Utilities.getTempFile();
98             Ssh ssh = new Ssh();
99             if (!ssh.copy(file, cache)) {
100                 // Report error!
101
if (errorRunnable != null) {
102                     errorRunnable.setMessage(ssh.getErrorText());
103                     SwingUtilities.invokeLater(errorRunnable);
104                 }
105                 return;
106             }
107             if (!cache.isFile()) {
108                 // Report error!
109
if (errorRunnable != null) {
110                     errorRunnable.setMessage("File not found");
111                     SwingUtilities.invokeLater(errorRunnable);
112                 }
113                 return;
114             }
115             // Start a thread to get a fresh directory listing of the parent
116
// directory in order to speed up completions later.
117
final File parent = file.getParentFile();
118             if (parent != null) {
119                 Thread JavaDoc t = new Thread JavaDoc() {
120                     public void run()
121                     {
122                         ((SshFile)parent).getDirectoryListing(true);
123                     }
124                 };
125                 t.start();
126             }
127         }
128         if (successRunnable != null)
129             SwingUtilities.invokeLater(successRunnable);
130     }
131 }
132
Popular Tags