1 package net.sf.jftp.net; 2 3 import java.io.File ; 4 import java.util.Vector ; 5 6 import net.sf.jftp.config.Settings; 7 import net.sf.jftp.system.logging.Log; 8 9 10 14 public class FtpTransfer extends Transfer implements Runnable 15 { 16 private String host; 17 private int port; 18 private String localPath; 19 private String remotePath; 20 private String file; 21 private String user; 22 private String pass; 23 private FtpConnection con = null; 24 private String type; 25 public Thread runner; 26 private int stat = 0; 27 private boolean started = false; 28 private ConnectionHandler handler; 29 private Vector listeners; 30 private String newName = null; 31 private int transferStatus = 0; 32 private String crlf = null; 33 34 public FtpTransfer(String host, int port, String localPath, 35 String remotePath, String file, String user, 36 String pass, String type, ConnectionHandler handler, 37 Vector listeners, String newName, String crlf) 38 { 39 this.host = host; 40 this.port = port; 41 this.localPath = localPath; 42 this.remotePath = remotePath; 43 this.file = file; 44 this.user = user; 45 this.pass = pass; 46 this.type = type; 47 this.handler = handler; 48 this.listeners = listeners; 49 this.newName = newName; 50 this.crlf = crlf; 51 52 if(handler == null) 53 { 54 handler = new ConnectionHandler(); 55 } 56 57 prepare(); 58 } 59 60 public FtpTransfer(String host, int port, String localPath, 61 String remotePath, String file, String user, 62 String pass, String type, ConnectionHandler handler, 63 Vector listeners, String crlf) 64 { 65 this.host = host; 66 this.port = port; 67 this.localPath = localPath; 68 this.remotePath = remotePath; 69 this.file = file; 70 this.user = user; 71 this.pass = pass; 72 this.type = type; 73 this.handler = handler; 74 this.listeners = listeners; 75 this.crlf = crlf; 76 77 if(handler == null) 78 { 79 handler = new ConnectionHandler(); 80 } 81 82 prepare(); 83 } 84 85 public void prepare() 86 { 87 runner = new Thread (this); 88 runner.setPriority(Thread.MIN_PRIORITY); 89 runner.start(); 90 } 91 92 public void run() 93 { 94 if(handler.getConnections().get(file) == null) 96 { 97 handler.addConnection(file, this); 98 } 99 else if(!pause) 100 { 101 Log.debug("Transfer already in progress: " + file); 102 work = false; 103 stat = 2; 104 105 return; 106 } 107 108 boolean hasPaused = false; 109 110 while(pause) 111 { 112 try 113 { 114 runner.sleep(100); 115 116 if(listeners != null) 117 { 118 for(int i = 0; i < listeners.size(); i++) 119 { 120 ((ConnectionListener) listeners.elementAt(i)).updateProgress(file, 121 PAUSED, 122 -1); 123 } 124 } 125 126 if(!work) 127 { 128 if(listeners != null) 129 { 130 for(int i = 0; i < listeners.size(); i++) 131 { 132 ((ConnectionListener) listeners.elementAt(i)).updateProgress(file, 133 REMOVED, 134 -1); 135 } 136 } 137 } 138 } 139 catch(Exception ex) 140 { 141 } 142 143 hasPaused = true; 144 } 145 146 while((handler.getConnectionSize() >= Settings.getMaxConnections()) && 147 (handler.getConnectionSize() > 0) && work) 148 { 149 try 150 { 151 stat = 4; 152 runner.sleep(400); 153 154 if(!hasPaused && (listeners != null)) 155 { 156 for(int i = 0; i < listeners.size(); i++) 157 { 158 ((ConnectionListener) listeners.elementAt(i)).updateProgress(file, 159 QUEUED, 160 -1); 161 } 162 } 163 else 164 { 165 break; 166 } 167 } 168 catch(Exception ex) 169 { 170 ex.printStackTrace(); 171 } 172 } 173 174 if(!work) 175 { 176 if(listeners != null) 177 { 178 for(int i = 0; i < listeners.size(); i++) 179 { 180 ((ConnectionListener) listeners.elementAt(i)).updateProgress(file, 181 REMOVED, 182 -1); 183 } 184 } 185 186 handler.removeConnection(file); 187 stat = 3; 188 189 return; 190 } 191 192 started = true; 193 194 try 195 { 196 runner.sleep(Settings.ftpTransferThreadPause); 197 } 198 catch(Exception ex) 199 { 200 } 201 202 con = new FtpConnection(host, port, remotePath, crlf); 203 204 con.setConnectionHandler(handler); 205 con.setConnectionListeners(listeners); 206 207 int status = con.login(user, pass); 208 209 if(status == FtpConnection.LOGIN_OK) 210 { 211 File f = new File (localPath); 212 con.setLocalPath(f.getAbsolutePath()); 213 214 if(type.equals(UPLOAD)) 215 { 216 if(newName != null) 217 { 218 transferStatus = con.upload(file, newName); 219 } 220 else 221 { 222 transferStatus = con.upload(file); 223 } 224 } 225 else 226 { 227 transferStatus = con.download(file); 228 } 229 } 230 231 if(!pause) 232 { 233 handler.removeConnection(file); 234 } 235 } 236 237 public int getStatus() 238 { 239 return stat; 240 } 241 242 public int getTransferStatus() 243 { 244 return transferStatus; 245 } 246 247 public boolean hasStarted() 248 { 249 return started; 250 } 251 252 public FtpConnection getFtpConnection() 253 { 254 return con; 255 } 256 257 public DataConnection getDataConnection() 258 { 259 return con.getDataConnection(); 260 } 261 } 262 | Popular Tags |