1 21 22 package org.armedbear.j; 23 24 import javax.swing.SwingUtilities ; 25 26 public final class SshLoadProcess extends LoadProcess implements BackgroundProcess, 27 Runnable , Cancellable 28 { 29 private boolean fileIsDirectory; 30 private String listing; 31 32 public SshLoadProcess(Buffer buffer, SshFile file) 33 { 34 super(buffer, file); 35 } 36 37 public final String 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 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 if (errorRunnable != null) { 86 String 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 session.unlock(); 97 cache = Utilities.getTempFile(); 98 Ssh ssh = new Ssh(); 99 if (!ssh.copy(file, cache)) { 100 if (errorRunnable != null) { 102 errorRunnable.setMessage(ssh.getErrorText()); 103 SwingUtilities.invokeLater(errorRunnable); 104 } 105 return; 106 } 107 if (!cache.isFile()) { 108 if (errorRunnable != null) { 110 errorRunnable.setMessage("File not found"); 111 SwingUtilities.invokeLater(errorRunnable); 112 } 113 return; 114 } 115 final File parent = file.getParentFile(); 118 if (parent != null) { 119 Thread t = new Thread () { 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 |