1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package wjhk.jupload2.context;
21
22 import java.awt.Cursor;
23 import java.awt.Frame;
24 import java.io.InputStream;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.text.MessageFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Properties;
33 import java.util.Vector;
34
35 import javax.swing.JApplet;
36 import javax.swing.JOptionPane;
37 import javax.swing.RootPaneContainer;
38 import javax.swing.SwingUtilities;
39
40 import wjhk.jupload2.exception.JUploadException;
41 import wjhk.jupload2.gui.JUploadPanel;
42 import wjhk.jupload2.gui.JUploadPanelImpl;
43 import wjhk.jupload2.gui.JUploadTextArea;
44 import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
45 import wjhk.jupload2.policies.UploadPolicy;
46 import wjhk.jupload2.policies.UploadPolicyFactory;
47 import wjhk.jupload2.upload.FileUploadManagerThread;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class DefaultJUploadContext implements JUploadContext {
65
66
67
68
69
70 private final static String SVN_PROPERTIES_FILENAME = "/conf/svn.properties";
71
72
73
74
75
76 private final static String DEFAULT_PROP_UNKNOWN = "Unknown";
77
78
79
80
81 Properties svnProperties = getSvnProperties();
82
83
84
85
86 Frame frame = null;
87
88
89
90
91 JavascriptHandler jsHandler = null;
92
93
94
95
96 Properties mimeTypesProperties = null;
97
98
99
100
101 UploadPolicy uploadPolicy = null;
102
103
104
105
106 JUploadPanel jUploadPanel = null;
107
108
109
110
111 JUploadTextArea logWindow = null;
112
113
114
115
116
117
118
119 static class Callback {
120 private String method;
121
122 private Object object;
123
124 Callback(Object object, String method) {
125 this.object = object;
126 this.method = method;
127 }
128
129 void invoke() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException,
130 SecurityException {
131 Object args[] = {};
132 Method methods[] = this.object.getClass().getMethods();
133 for (int i = 0; i < methods.length; i++) {
134 if (methods[i].getName().equals(this.method)) {
135 methods[i].invoke(this.object, args);
136 }
137 }
138 }
139
140
141
142
143 public String getMethod() {
144 return method;
145 }
146
147
148
149
150 public Object getObject() {
151 return object;
152 }
153
154 }
155
156
157
158
159
160
161 List<Callback> unloadCallbacks = new ArrayList<Callback>(20);
162
163
164
165
166
167
168
169
170
171
172
173 public void init(Frame frame, RootPaneContainer rootPaneContainer) {
174 try {
175 this.frame = frame;
176
177
178
179
180 Thread.currentThread().setName(rootPaneContainer.getClass().getName());
181
182
183
184
185 this.logWindow = new JUploadTextArea(20, 20, this.uploadPolicy);
186
187
188 this.uploadPolicy = UploadPolicyFactory.getUploadPolicy(this);
189 this.uploadPolicy.displayDebug("After UploadPolicyFactory.getUploadPolicy(this)", 80);
190
191
192
193
194 this.uploadPolicy.displayDebug("Before this.logWindow.setUploadPolicy(this.uploadPolicy)", 80);
195 this.logWindow.setUploadPolicy(this.uploadPolicy);
196
197
198 this.uploadPolicy.displayDebug("Before new JUploadPanelImpl(this.logWindow,this.uploadPolicy)", 80);
199 this.jUploadPanel = new JUploadPanelImpl(this.logWindow, this.uploadPolicy);
200
201
202 this.uploadPolicy.displayDebug("Before rootPaneContainer.setContentPane(this.jUploadPanel);", 80);
203 rootPaneContainer.setContentPane(this.jUploadPanel.getJComponent());
204
205
206
207 this.uploadPolicy.displayDebug("Before new JavascriptHandler(this.uploadPolicy, this.jUploadPanel)", 80);
208 this.jsHandler = new JavascriptHandler(this.uploadPolicy, this.jUploadPanel);
209 this.jsHandler.start();
210
211
212 registerUnload(this, "unload");
213 } catch (final Exception e) {
214 System.out.println(e.getMessage());
215 e.printStackTrace();
216
217 JOptionPane.showMessageDialog(null,
218 "Error during applet initialization!\nHave a look in your Java console (" + e.getClass().getName()
219 + ")", "Error", JOptionPane.ERROR_MESSAGE);
220 }
221
222 this.uploadPolicy.displayDebug("Before new Properties();", 80);
223 this.mimeTypesProperties = new Properties();
224 final String mimetypePropertiesFilename = "/conf/mimetypes.properties";
225 try {
226 InputStream isProperties = this.getClass().getResourceAsStream(mimetypePropertiesFilename);
227 this.mimeTypesProperties.load(isProperties);
228 isProperties.close();
229 this.uploadPolicy.displayDebug("Mime types list loaded Ok (" + mimetypePropertiesFilename + ")", 50);
230 } catch (Exception e) {
231 this.uploadPolicy.displayWarn("Unable to load the mime types list (" + mimetypePropertiesFilename + "): "
232 + e.getClass().getName() + " (" + e.getMessage() + ")");
233 }
234
235 this.uploadPolicy.displayDebug("End of DefaultJUploadContext.init()", 80);
236 }
237
238
239
240
241
242 public void unload() {
243 if (this.jsHandler != null && this.jsHandler.isAlive()) {
244 this.jsHandler.interrupt();
245 this.jsHandler = null;
246 }
247 }
248
249
250 public String getDetailedVersionMessage() {
251 String version = getVersion();
252 String svnRevision = getSvnRevision();
253 boolean gotSvnRevision = !svnRevision.equals(DEFAULT_PROP_UNKNOWN);
254 int buildNumber = getBuildNumber();
255 boolean gotBuildNumber = buildNumber > 0;
256 String buildDate = getBuildDate();
257 boolean gotBuildDate = !buildDate.equals(DEFAULT_PROP_UNKNOWN);
258
259 StringBuffer sb = new StringBuffer();
260 sb.append(version);
261
262 if (gotSvnRevision || gotBuildNumber) {
263 sb.append(" [");
264 String space = "";
265 if (gotSvnRevision) {
266 sb.append("SVN-Rev: ");
267 sb.append(svnRevision);
268 space = " ";
269 }
270 if (gotBuildNumber) {
271 sb.append(space);
272 sb.append("build ");
273 sb.append(buildNumber);
274 }
275 sb.append("]");
276 }
277
278 if (gotBuildDate) {
279 sb.append(" - ");
280 sb.append(buildDate);
281 }
282 return sb.toString();
283 }
284
285
286 public String getVersion() {
287 return getProperty("jupload.version", DEFAULT_PROP_UNKNOWN);
288 }
289
290
291 public String getSvnRevision() {
292 String svnRevision = getProperty("jupload.svn.revision", DEFAULT_PROP_UNKNOWN);
293 if (svnRevision.startsWith("{") || svnRevision.startsWith("${")) {
294
295
296
297
298 return DEFAULT_PROP_UNKNOWN;
299 } else {
300 return svnRevision;
301 }
302 }
303
304
305 public String getLastModified() {
306 return getProperty("jupload.lastSrcDirModificationDate", DEFAULT_PROP_UNKNOWN);
307 }
308
309
310 public String getBuildDate() {
311 String timestamp = getProperty("jupload.buildTimestamp", DEFAULT_PROP_UNKNOWN);
312 if (timestamp.equals(DEFAULT_PROP_UNKNOWN)) {
313 return DEFAULT_PROP_UNKNOWN;
314 } else {
315 Locale locale = Locale.getDefault();
316 if (this.uploadPolicy != null) {
317 locale = this.uploadPolicy.getLocale();
318 }
319 MessageFormat msgFormat = new MessageFormat("{0,date,medium}", locale);
320 try {
321 Object[] args = {
322 new Date(Long.parseLong(timestamp))
323 };
324 return msgFormat.format(args);
325 } catch (NumberFormatException e) {
326
327
328 System.out.println("[WARN] The timestamp can not be read (" + timestamp + "). Will return '"
329 + DEFAULT_PROP_UNKNOWN + "'.");
330 return DEFAULT_PROP_UNKNOWN;
331 }
332 }
333 }
334
335
336 public int getBuildNumber() {
337 String valuePropBuildNumber = getProperty("jupload.buildNumber", "-1");
338 try {
339 return Integer.parseInt(valuePropBuildNumber);
340 } catch (java.lang.NumberFormatException e) {
341 System.out.println("[WARN] " + e.getClass().getName() + " when getting the buildNumber, while parsing '"
342 + valuePropBuildNumber + "'). Will return -1");
343 return -1;
344 }
345 }
346
347
348
349
350
351
352
353
354
355 private String getProperty(String propertyName, String defaultValue) {
356 String value = null;
357 try {
358 value = this.svnProperties.getProperty(propertyName);
359 } catch (Exception e) {
360 System.out.println("[WARN] " + e.getClass().getName() + " when getting the " + propertyName + " property ("
361 + e.getMessage() + "). Will return '" + value + "'");
362 }
363 return (value == null) ? defaultValue : value;
364 }
365
366
367 public JUploadTextArea getLogWindow() {
368 return this.logWindow;
369 }
370
371
372 public String getMimeType(String fileExtension) {
373 String mimeType = this.mimeTypesProperties.getProperty(fileExtension.toLowerCase());
374 return (mimeType == null) ? "application/octet-stream" : mimeType;
375 }
376
377
378 public JUploadPanel getUploadPanel() {
379 return this.jUploadPanel;
380 }
381
382
383
384
385
386
387
388
389 public UploadPolicy getUploadPolicy() throws JUploadException {
390 return this.uploadPolicy;
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 public void setProperty(String prop, String value) {
407
408 class PropertySetter implements Runnable {
409 String prop;
410
411 String value;
412
413 PropertySetter(String prop, String value) {
414 this.prop = prop;
415 this.value = value;
416 }
417
418 public void run() {
419 try {
420
421
422 for (int i = 0; i < 20 && uploadPolicy == null; i += 1) {
423 this.wait(100);
424 }
425 if (uploadPolicy == null) {
426 System.out.println("uploadPolicy is null. Impossible to set " + prop + " to " + value);
427 } else {
428
429 uploadPolicy.setProperty(prop, value);
430 }
431 } catch (Exception e) {
432 uploadPolicy.displayErr(e);
433 }
434 }
435 }
436 try {
437 SwingUtilities.invokeLater(new PropertySetter(prop, value));
438 } catch (Exception e) {
439 if (this.uploadPolicy != null) {
440 this.uploadPolicy.displayErr(e);
441 } else {
442 System.out.println(e.getClass().getName() + ": " + e.getMessage());
443 }
444 }
445 }
446
447
448 public String startUpload() {
449 return this.jsHandler.doCommand(JavascriptHandler.COMMAND_START_UPLOAD);
450 }
451
452
453
454
455
456
457 public void displayErr(String err) {
458 this.uploadPolicy.displayErr(err);
459 }
460
461
462
463
464
465
466 public void displayInfo(String info) {
467 this.uploadPolicy.displayInfo(info);
468 }
469
470
471
472
473
474
475 public void displayWarn(String warn) {
476 this.uploadPolicy.displayWarn(warn);
477 }
478
479
480
481
482
483
484
485
486 public void displayDebug(String debug, int minDebugLevel) {
487 this.uploadPolicy.displayDebug(debug, minDebugLevel);
488 }
489
490
491
492
493
494
495
496
497
498
499 public static Properties getSvnProperties() {
500 Properties properties = new Properties();
501 Boolean bPropertiesLoaded = false;
502
503
504
505
506 try {
507 InputStream isProperties = Class.forName("wjhk.jupload2.JUploadApplet").getResourceAsStream(
508 SVN_PROPERTIES_FILENAME);
509 properties.load(isProperties);
510 isProperties.close();
511 bPropertiesLoaded = true;
512 } catch (Exception e) {
513
514
515
516
517
518
519
520
521
522 System.out.println(e.getClass().getName() + " in DefaultJUploadContext.getSvnProperties() ("
523 + e.getMessage() + ")");
524 }
525
526
527
528 if (!bPropertiesLoaded) {
529 properties.setProperty("buildDate", "Unknown build date (please use the build.xml ant script)");
530 properties.setProperty("lastSrcDirModificationDate",
531 "Unknown last modification date (please use the build.xml ant script)");
532 properties.setProperty("revision", "Unknown revision (please use the build.xml ant script)");
533 }
534 return properties;
535 }
536
537
538 public void registerUnload(Object object, String method) {
539
540
541
542
543
544 this.unloadCallbacks.add(0, new Callback(object, method));
545 }
546
547
548 public synchronized void runUnload() {
549
550 FileUploadManagerThread fileUploadManagerThread = this.getUploadPanel().getFileUploadManagerThread();
551 if (fileUploadManagerThread != null) {
552 fileUploadManagerThread.stopUpload();
553 }
554
555
556 for (Callback callback : this.unloadCallbacks) {
557 try {
558 callback.invoke();
559 } catch (Exception e) {
560 System.out.println(e.getClass().getName() + " while calling the callback: "
561 + callback.getObject().getClass().getName() + "." + callback.getMethod());
562 e.printStackTrace();
563 }
564 }
565 this.unloadCallbacks.clear();
566 }
567
568
569
570
571 void displayDebugParameterValue(String key, String value) {
572 if (this.uploadPolicy != null && this.uploadPolicy.getDebugLevel() >= 80) {
573 this.uploadPolicy.displayDebug("Parameter '" + key + "' loaded. Value: " + value, 80);
574 }
575 }
576
577
578 public int parseInt(String value, int def) {
579 int ret = def;
580
581 try {
582 ret = Integer.parseInt(value);
583 } catch (NumberFormatException e) {
584 ret = def;
585 if (this.uploadPolicy != null) {
586 this.uploadPolicy.displayWarn("Invalid int value: " + value + ", using default value: " + def);
587 }
588 }
589
590 return ret;
591 }
592
593
594 public float parseFloat(String value, float def) {
595 float ret = def;
596
597 try {
598 ret = Float.parseFloat(value);
599 } catch (NumberFormatException e) {
600 ret = def;
601 if (this.uploadPolicy != null) {
602 this.uploadPolicy.displayWarn("Invalid float value: " + value + ", using default value: " + def);
603 }
604 }
605
606 return ret;
607 }
608
609
610 public long parseLong(String value, long def) {
611 long ret = def;
612
613 try {
614 ret = Long.parseLong(value);
615 } catch (NumberFormatException e) {
616 ret = def;
617 if (this.uploadPolicy != null) {
618 this.uploadPolicy.displayWarn("Invalid long value: " + value + ", using default value: " + def);
619 }
620 }
621
622 return ret;
623 }
624
625
626 public boolean parseBoolean(String value, boolean def) {
627
628 if (value.toUpperCase().equals("FALSE")) {
629 return false;
630 } else if (value.toUpperCase().equals("TRUE")) {
631 return true;
632 } else {
633 if (this.uploadPolicy != null) {
634 this.uploadPolicy.displayWarn("Invalid boolean value: " + value + ", using default value: " + def);
635 }
636 return def;
637 }
638 }
639
640
641 public FileListViewMode parseFileListViewMode(String value, FileListViewMode def) {
642 FileListViewMode ret = null;
643 try {
644 ret = FileListViewMode.valueOf(value.toUpperCase());
645 } catch (IllegalArgumentException e) {
646 ret = def;
647 String msg = "Unknown FileListViewMode value '" + value + "'. Using default value: '" + def.toString()
648 + "'";
649 if (this.uploadPolicy != null) {
650 this.uploadPolicy.displayWarn(msg);
651 } else {
652 System.out.println(msg);
653 }
654 }
655 return ret;
656 }
657
658
659
660
661
662 public Cursor setWaitCursor() {
663 return setCursor(new Cursor(Cursor.WAIT_CURSOR));
664 }
665
666
667
668
669
670
671
672 public void displayURL(String url, boolean success) {
673 throw new UnsupportedOperationException("DefaultJUploadContext.displayURL()");
674 }
675
676
677
678
679
680
681 public JApplet getApplet() {
682 throw new UnsupportedOperationException("DefaultJUploadContext.getApplet()");
683 }
684
685
686 public Frame getFrame() {
687 return this.frame;
688 }
689
690
691
692
693
694
695 public Cursor getCursor() {
696 throw new UnsupportedOperationException("DefaultJUploadContext.getCursor()");
697 }
698
699
700
701
702
703
704
705
706 public String getParameter(String key, String def) {
707 throw new UnsupportedOperationException("DefaultJUploadContext.getParameter(String, String)");
708 }
709
710
711
712
713
714
715
716
717 public int getParameter(String key, int def) {
718 throw new UnsupportedOperationException("DefaultJUploadContext.getParameter(String, int))");
719 }
720
721
722
723
724
725
726
727
728 public float getParameter(String key, float def) {
729 throw new UnsupportedOperationException("DefaultJUploadContext.getParameter(String, float)");
730 }
731
732
733
734
735
736
737
738
739 public long getParameter(String key, long def) {
740 throw new UnsupportedOperationException("DefaultJUploadContext.getParameter(String, long)");
741 }
742
743
744
745
746
747
748
749
750 public boolean getParameter(String key, boolean def) {
751 throw new UnsupportedOperationException("DefaultJUploadContext.getParameter(String, boolean)");
752 }
753
754
755
756
757
758
759
760
761 public FileListViewMode getParameter(String key, FileListViewMode def) {
762 throw new UnsupportedOperationException("DefaultJUploadContext.getParameter(String, FileListViewMode)");
763 }
764
765
766
767
768
769
770
771
772 public String normalizeURL(String url) throws JUploadException {
773 throw new UnsupportedOperationException("DefaultJUploadContext.normalizeURL()");
774 }
775
776
777
778
779
780
781 public void readCookieFromNavigator(Vector<String> headers) {
782 throw new UnsupportedOperationException("DefaultJUploadContext.readCookieFromNavigator()");
783 }
784
785
786
787
788
789
790 public void readUserAgentFromNavigator(Vector<String> headers) {
791 throw new UnsupportedOperationException("DefaultJUploadContext.readUserAgentFromNavigator()");
792 }
793
794
795
796
797
798
799
800 public Cursor setCursor(Cursor cursor) {
801 throw new UnsupportedOperationException("DefaultJUploadContext.setCursor(Cursor)");
802 }
803
804
805
806
807
808
809 public void showStatus(String status) {
810 throw new UnsupportedOperationException("DefaultJUploadContext.showStatus()");
811 }
812
813 }