KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > net > FtpTransfer


1 package net.sf.jftp.net;
2
3 import java.io.File JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import net.sf.jftp.config.Settings;
7 import net.sf.jftp.system.logging.Log;
8
9
10 /**
11 * This class is used internally by FtpConnection.
12 * You probably don't have to use it directly.
13 */

14 public class FtpTransfer extends Transfer implements Runnable JavaDoc
15 {
16     private String JavaDoc host;
17     private int port;
18     private String JavaDoc localPath;
19     private String JavaDoc remotePath;
20     private String JavaDoc file;
21     private String JavaDoc user;
22     private String JavaDoc pass;
23     private FtpConnection con = null;
24     private String JavaDoc type;
25     public Thread JavaDoc runner;
26     private int stat = 0;
27     private boolean started = false;
28     private ConnectionHandler handler;
29     private Vector JavaDoc listeners;
30     private String JavaDoc newName = null;
31     private int transferStatus = 0;
32     private String JavaDoc crlf = null;
33
34     public FtpTransfer(String JavaDoc host, int port, String JavaDoc localPath,
35                        String JavaDoc remotePath, String JavaDoc file, String JavaDoc user,
36                        String JavaDoc pass, String JavaDoc type, ConnectionHandler handler,
37                        Vector JavaDoc listeners, String JavaDoc newName, String JavaDoc 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 JavaDoc host, int port, String JavaDoc localPath,
61                        String JavaDoc remotePath, String JavaDoc file, String JavaDoc user,
62                        String JavaDoc pass, String JavaDoc type, ConnectionHandler handler,
63                        Vector JavaDoc listeners, String JavaDoc 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 JavaDoc(this);
88         runner.setPriority(Thread.MIN_PRIORITY);
89         runner.start();
90     }
91
92     public void run()
93     {
94         //System.out.println(file);
95
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc f = new File JavaDoc(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