1 package wjhk.jupload2.upload;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.concurrent.BlockingQueue;
7
8 import javax.swing.JProgressBar;
9
10 import wjhk.jupload2.exception.JUploadException;
11 import wjhk.jupload2.filedata.DefaultFileData;
12 import wjhk.jupload2.filedata.FileData;
13 import wjhk.jupload2.gui.JUploadPanel;
14 import wjhk.jupload2.gui.filepanel.FilePanel;
15 import wjhk.jupload2.policies.UploadPolicy;
16
17
18
19
20
21
22
23 public class FilePreparationThread extends Thread {
24
25
26
27
28 List<FileData> fileDataArray = null;
29
30
31
32
33
34 int nbPreparedFiles = 0;
35
36
37
38
39
40 int nbFilesToPrepare = -1;
41
42
43
44
45
46 int nbFilesToSend = 0;
47
48
49
50
51
52
53 long nbTotalNumberOfPreparedBytes = 0;
54
55
56
57
58 JProgressBar preparationProgressBar = null;
59
60
61 JUploadPanel uploadPanel = null;
62
63
64 UploadPolicy uploadPolicy = null;
65
66
67 FileUploadManagerThread fileUploadManagerThread = null;
68
69
70 FilePanel filePanel = null;
71
72
73
74
75 BlockingQueue<UploadFileData> preparedFileQueue = null;
76
77
78
79
80
81
82 public FilePreparationThread(BlockingQueue<UploadFileData> preparedFileQueue,
83 FileUploadManagerThread fileUploadManagerThread, UploadPolicy uploadPolicy) {
84
85 super("FilePreparationThread");
86
87 this.preparedFileQueue = preparedFileQueue;
88 this.fileUploadManagerThread = fileUploadManagerThread;
89 this.uploadPolicy = uploadPolicy;
90 this.uploadPanel = uploadPolicy.getContext().getUploadPanel();
91 this.filePanel = this.uploadPanel.getFilePanel();
92 this.preparationProgressBar = this.uploadPanel.getPreparationProgressBar();
93
94
95 this.filePanel.removeFileNotToUpload();
96
97
98
99 this.fileDataArray = new ArrayList<FileData>(this.filePanel.getFiles().size());
100 for (FileData fd : this.filePanel.getFiles()) {
101 if (fd.getUploadFlag()) {
102 this.fileDataArray.add(fd);
103 }
104 }
105 nbFilesToSend = this.fileDataArray.size();
106
107 this.uploadPolicy.displayDebug("Nb files to send: " + nbFilesToSend, 20);
108
109 this.preparationProgressBar.setMaximum(100 * nbFilesToSend);
110 }
111
112
113
114
115
116
117
118 @Override
119 final public void run() {
120
121
122
123
124 File fileRoot = DefaultFileData.getRoot(fileDataArray);
125 String uploadFileRoot = (fileRoot == null) ? "" : fileRoot.getAbsolutePath();
126
127 if (fileDataArray.size() != nbFilesToSend) {
128 this.uploadPolicy.displayWarn(fileDataArray.size() + " files to prepare, but there are " + nbFilesToSend
129 + " files to send!");
130 }
131
132
133
134
135 int numFileInCurrentUpload = 0;
136
137 for (int i = 0; i < fileDataArray.size() && !this.fileUploadManagerThread.isUploadFinished(); i += 1) {
138
139
140 try {
141 this.uploadPolicy.displayDebug("============== Start of file preparation ("
142 + fileDataArray.get(i).getFileName() + ")", 30);
143 UploadFileData uploadFileData = new UploadFileData(fileDataArray.get(i), numFileInCurrentUpload,
144 this.fileUploadManagerThread, this.uploadPolicy);
145
146
147 this.preparationProgressBar.setString(this.uploadPolicy.getLocalizedString("preparingFile",
148 Integer.valueOf(i + 1), Integer.valueOf(fileDataArray.size())));
149
150 this.preparationProgressBar.repaint(0);
151
152
153
154
155
156 this.uploadPolicy.displayDebug(this.getClass().getName()
157 + ".prepareFiles(): before call to beforeUpload()", 100);
158
159 try {
160
161 uploadFileData.beforeUpload(uploadFileRoot);
162
163
164
165
166 numFileInCurrentUpload += 1;
167
168
169
170 this.uploadPolicy.displayDebug(
171 "============== End of file preparation (" + uploadFileData.getFileName() + ")", 30);
172 try {
173 anotherFileIsPrepared(uploadFileData);
174 } catch (InterruptedException e) {
175
176 i -= 1;
177 }
178 } catch (JUploadException e) {
179
180 this.nbFilesToPrepare -= 1;
181 this.uploadPolicy.displayErr(e.getMessage());
182 throw e;
183 } catch (Exception e) {
184
185 this.nbFilesToPrepare -= 1;
186 String msg = "An exception " + e.getClass().getName()
187 + " occured during file preparation for file " + fileDataArray.get(i).getAbsolutePath()
188 + ". This file will not be uploaded.";
189 this.uploadPolicy.displayErr(msg);
190 throw new JUploadException(msg, e);
191 }
192
193
194
195
196 this.preparationProgressBar.setValue(this.nbPreparedFiles * 100);
197 this.preparationProgressBar.repaint();
198 } catch (JUploadException e) {
199 this.fileUploadManagerThread.setUploadException(e);
200 }
201 }
202
203
204
205 try {
206 this.preparedFileQueue.put(new UploadFileDataPoisonned());
207 } catch (InterruptedException e) {
208
209
210
211
212 this.uploadPolicy
213 .displayWarn("Got interrupted, while posting the poisoned UploadFileData on the preparedQueue!");
214 }
215
216
217 this.preparationProgressBar.setString("");
218 this.uploadPolicy.displayDebug(this.nbPreparedFiles + " files has been prepared (for " + nbFilesToSend
219 + " files to send)", 10);
220 }
221
222
223
224
225
226
227
228 private void anotherFileIsPrepared(UploadFileData newlyPreparedFileData) throws JUploadException,
229 InterruptedException {
230 this.nbPreparedFiles += 1;
231 this.nbTotalNumberOfPreparedBytes += newlyPreparedFileData.getUploadLength();
232 this.preparedFileQueue.put(newlyPreparedFileData);
233 }
234
235
236
237
238
239
240
241
242
243
244 public double getTotalFileBytesToSend() {
245 double totalFileBytesToSend;
246
247
248 if (this.nbPreparedFiles == this.nbFilesToSend) {
249
250 totalFileBytesToSend = this.nbTotalNumberOfPreparedBytes;
251 } else if (this.nbPreparedFiles == 0) {
252 totalFileBytesToSend = 0;
253 } else {
254
255
256 totalFileBytesToSend = this.nbTotalNumberOfPreparedBytes
257 +
258
259
260 (this.fileDataArray.size() - this.nbPreparedFiles) * this.nbTotalNumberOfPreparedBytes
261 / this.nbPreparedFiles;
262 }
263
264 return totalFileBytesToSend;
265 }
266
267
268
269
270 public int getNbPreparedFiles() {
271 return nbPreparedFiles;
272 }
273
274
275
276
277 public int getNbFilesToSend() {
278 return nbFilesToSend;
279 }
280
281
282
283
284 public long getNbTotalNumberOfPreparedBytes() {
285 return nbTotalNumberOfPreparedBytes;
286 }
287 }