1 package wjhk.jupload2.upload;
2
3 import java.util.concurrent.ArrayBlockingQueue;
4 import java.util.concurrent.BlockingQueue;
5
6 import wjhk.jupload2.exception.JUploadEOFException;
7 import wjhk.jupload2.exception.JUploadException;
8 import wjhk.jupload2.filedata.FileData;
9 import wjhk.jupload2.gui.JUploadPanel;
10 import wjhk.jupload2.gui.filepanel.FilePanel;
11 import wjhk.jupload2.policies.UploadPolicy;
12 import wjhk.jupload2.upload.helper.ProgressBarManager;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class FileUploadManagerThreadImpl extends Thread implements FileUploadManagerThread {
26
27
28
29
30
31
32 final static int FILE_PREPARATION_QUEUE_SIZE = 50;
33
34
35
36
37
38
39 FilePanel filePanel = null;
40
41
42
43
44
45 FilePreparationThread filePreparationThread = null;
46
47
48
49
50
51 PacketConstructionThread packetConstructionThread = null;
52
53
54
55
56 FileUploadThread fileUploadThread = null;
57
58
59
60
61 ProgressBarManager progressBarManager;
62
63
64
65
66
67 int nbSuccessfullyUploadedFiles = 0;
68
69
70
71
72
73
74
75 boolean uploadFinished = false;
76
77
78
79
80
81
82 boolean stop = false;
83
84
85 JUploadException uploadException = null;
86
87
88 JUploadPanel uploadPanel = null;
89
90
91 UploadPolicy uploadPolicy = null;
92
93
94
95
96
97
98
99
100
101
102
103 public FileUploadManagerThreadImpl(UploadPolicy uploadPolicy) throws JUploadException {
104 super("FileUploadManagerThreadImpl thread");
105 constructor(uploadPolicy, null);
106 }
107
108
109
110
111
112
113
114
115
116
117 FileUploadManagerThreadImpl(UploadPolicy uploadPolicy, FileUploadThread fileUploadThreadParam)
118 throws JUploadException {
119 super("FileUploadManagerThreadImpl test thread");
120 constructor(uploadPolicy, fileUploadThreadParam);
121 }
122
123
124
125
126
127
128
129
130 private synchronized void constructor(UploadPolicy uploadPolicy, FileUploadThread fileUploadThreadParam)
131 throws JUploadException {
132
133
134 this.uploadPolicy = uploadPolicy;
135 this.uploadPanel = uploadPolicy.getContext().getUploadPanel();
136 this.filePanel = this.uploadPanel.getFilePanel();
137
138 BlockingQueue<UploadFileData> preparedFileQueue = new ArrayBlockingQueue<UploadFileData>(
139 this.filePanel.getFilesLength());
140
141
142
143 BlockingQueue<UploadFilePacket> packetQueue;
144 if (fileUploadThreadParam == null) {
145 packetQueue = new ArrayBlockingQueue<UploadFilePacket>(this.filePanel.getFilesLength());
146 } else {
147 packetQueue = fileUploadThreadParam.getPacketQueue();
148 }
149
150 this.filePreparationThread = new FilePreparationThread(preparedFileQueue, this, this.uploadPolicy);
151
152
153 this.packetConstructionThread = new PacketConstructionThread(preparedFileQueue, packetQueue, this,
154 this.uploadPolicy);
155
156
157 createUploadThread(packetQueue, fileUploadThreadParam);
158
159 this.progressBarManager = new ProgressBarManager(this.uploadPolicy, this.filePreparationThread);
160 }
161
162
163
164
165 @Override
166 final public void run() {
167 try {
168 this.uploadPolicy.displayDebug("Start of the FileUploadManagerThreadImpl", 5);
169
170
171
172 progressBarManager.uploadIsStarted();
173
174
175 this.filePreparationThread.start();
176 this.packetConstructionThread.start();
177 this.fileUploadThread.start();
178
179
180 this.uploadPolicy.beforeUpload();
181
182
183 this.uploadPanel.updateButtonState();
184
185
186
187
188
189 while (this.fileUploadThread.isAlive() && !isUploadFinished()) {
190 try {
191 this.uploadPolicy.displayDebug("Waiting for fileUploadThread to die", 10);
192 this.fileUploadThread.join();
193 } catch (InterruptedException e) {
194
195
196 this.uploadPolicy
197 .displayWarn("An InterruptedException occured in FileUploadManagerThreadImpl.run()");
198 }
199 }
200
201
202
203 for (FileData fd : this.uploadPanel.getFilePanel().getFiles()) {
204 if (fd.getUploadFlag() && fd.isPreparedForUpload()) {
205 fd.afterUpload();
206 }
207 }
208
209
210 this.uploadPanel.updateButtonState();
211 this.uploadPanel.getFilePanel().reload();
212 this.uploadPolicy.getContext().showStatus("");
213 this.uploadPolicy.getContext().getUploadPanel().getStatusLabel().setText("");
214
215
216
217 if (getUploadException() != null) {
218 this.uploadPolicy.sendDebugInformation("Error in Upload", getUploadException());
219 } else if (isUploadStopped()) {
220 this.uploadPolicy.displayInfo("Upload stopped by the user. "
221 + this.nbSuccessfullyUploadedFiles
222 + " file(s) uploaded in "
223 + (int) ((System.currentTimeMillis() - this.progressBarManager.getGlobalStartTime()) / 1000)
224 + " seconds. Average upload speed: "
225 + ((this.progressBarManager.getUploadDuration() > 0) ? ((int) (this.progressBarManager
226 .getNbUploadedBytes() / this.progressBarManager.getUploadDuration())) : 0)
227 + " (kbytes/s)");
228 } else {
229 this.uploadPolicy.displayInfo("Upload finished normally. "
230 + this.nbSuccessfullyUploadedFiles
231 + " file(s) uploaded in "
232 + (int) ((System.currentTimeMillis() - this.progressBarManager.getGlobalStartTime()) / 1000)
233 + " seconds. Average upload speed: "
234 + ((this.progressBarManager.getUploadDuration() > 0) ? ((int) (this.progressBarManager
235 .getNbUploadedBytes() / this.progressBarManager.getUploadDuration())) : 0)
236 + " (kbytes/s)");
237
238 try {
239 this.uploadPolicy.afterUpload(this.getUploadException(), this.fileUploadThread.getResponseMsg());
240 } catch (JUploadException e1) {
241 this.uploadPolicy.displayErr("error in uploadPolicy.afterUpload (JUploadPanel)", e1);
242 }
243 }
244
245
246
247 this.progressBarManager.uploadIsFinished();
248
249
250 try {
251 sleep(5000);
252 } catch (InterruptedException e) {
253
254 }
255
256
257
258
259
260 if (this == this.uploadPanel.getFileUploadManagerThread()) {
261 this.progressBarManager.clearBarContent();
262 this.uploadPolicy.getContext().getUploadPanel().getStatusLabel().setText("");
263 }
264
265 this.uploadPolicy.displayDebug("End of the FileUploadManagerThreadImpl", 5);
266 } catch (Exception e) {
267
268 JUploadException jue = (e instanceof JUploadException) ? (JUploadException) e : new JUploadException(e);
269 setUploadException(jue);
270
271
272 stopUpload();
273 } finally {
274
275 this.uploadPanel.updateButtonState();
276 }
277
278
279 }
280
281
282
283
284 public synchronized void setUploadException(JUploadException uploadExceptionParam) {
285
286
287 if (isUploadStopped() && uploadExceptionParam instanceof JUploadEOFException) {
288
289
290
291 } else {
292
293 if (this.uploadException != null) {
294 this.uploadPolicy
295 .displayWarn("An exception has already been set in FileUploadManagerThreadImpl. The next one is just logged.");
296 } else {
297 this.uploadException = uploadExceptionParam;
298 }
299
300 String exceptionMsg = (uploadExceptionParam.getCause() == null) ? uploadExceptionParam.getMessage()
301 : uploadExceptionParam.getCause().getMessage();
302 String errMsg = this.uploadPolicy.getLocalizedString("errDuringUpload") + "\n\n" + exceptionMsg;
303 this.uploadPolicy.displayErr(errMsg, uploadException);
304 }
305 }
306
307
308
309
310 public JUploadException getUploadException() {
311 return this.uploadException;
312 }
313
314
315
316
317 public boolean isUploadFinished() {
318
319
320
321 return this.uploadFinished || this.stop || this.uploadException != null;
322 }
323
324
325
326
327 public boolean isUploadStopped() {
328 return this.stop;
329 }
330
331
332
333
334 public synchronized void nbBytesUploaded(long nbBytes, UploadFileData uploadFileData) throws JUploadException {
335 this.progressBarManager.nbBytesUploaded(nbBytes, uploadFileData);
336 }
337
338
339
340
341
342 public synchronized void setUploadStatus(UploadFilePacket uploadFilePacket, UploadFileData uploadFileData,
343 int uploadStatus) throws JUploadException {
344 this.progressBarManager.setUploadStatus(uploadFilePacket, uploadFileData, uploadStatus);
345 }
346
347
348
349
350 public synchronized void stopUpload() {
351 this.stop = true;
352
353
354 this.uploadFinished = true;
355
356
357 if (this.filePreparationThread != null && this.filePreparationThread.isAlive()) {
358 this.filePreparationThread.interrupt();
359 }
360 if (this.packetConstructionThread != null && this.packetConstructionThread.isAlive()) {
361 this.packetConstructionThread.interrupt();
362 }
363 if (this.fileUploadThread != null && this.fileUploadThread.isAlive()) {
364 this.fileUploadThread.interrupt();
365 }
366
367
368
369
370 this.interrupt();
371 }
372
373
374
375
376
377
378
379
380
381 public synchronized void anotherFileHasBeenSent(UploadFilePacket uploadFilePacket,
382 UploadFileData newlyUploadedFileData) throws JUploadException {
383 this.progressBarManager.anotherFileHasBeenSent(uploadFilePacket, newlyUploadedFileData);
384 }
385
386
387
388
389 public synchronized void currentRequestIsFinished(UploadFilePacket uploadFilePacket) throws JUploadException {
390
391 this.progressBarManager.setUploadStatus(uploadFilePacket, uploadFilePacket.get(uploadFilePacket.size() - 1),
392 FileUploadManagerThread.UPLOAD_STATUS_UPLOADED);
393
394
395
396 for (FileData fileData : uploadFilePacket) {
397 this.filePanel.remove(fileData);
398 this.nbSuccessfullyUploadedFiles += 1;
399 }
400
401 this.filePanel.cleanHierarchy();
402
403
404 if (!this.uploadFinished) {
405 this.uploadFinished = (this.nbSuccessfullyUploadedFiles == this.filePreparationThread.getNbFilesToSend());
406 }
407 }
408
409
410
411
412
413
414
415
416
417
418 private synchronized void createUploadThread(BlockingQueue<UploadFilePacket> packetQueue,
419 FileUploadThread fileUploadThreadParam) throws JUploadException {
420 if (fileUploadThreadParam != null) {
421
422
423 this.fileUploadThread = fileUploadThreadParam;
424 fileUploadThreadParam.setFileUploadThreadManager(this);
425 } else {
426 try {
427 if (this.uploadPolicy.getPostURL().substring(0, 4).equals("ftp:")) {
428 this.fileUploadThread = new FileUploadThreadFTP(this.uploadPolicy, packetQueue, this);
429 } else {
430 this.fileUploadThread = new FileUploadThreadHTTP(this.uploadPolicy, packetQueue, this);
431 }
432 } catch (JUploadException e1) {
433
434 this.uploadPolicy.displayErr(e1);
435 }
436 }
437 }
438 }