1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
|
/* PluginAppletViewer -- Handles embedding of the applet panel
Copyright (C) 2008 Red Hat
This file is part of IcedTea.
IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
IcedTea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
/*
* Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package sun.applet;
import static net.sourceforge.jnlp.runtime.Translator.R;
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.SocketPermission;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.security.AccessController;
import java.security.AllPermission;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.SwingUtilities;
import net.sourceforge.jnlp.LaunchException;
import net.sourceforge.jnlp.NetxPanel;
import net.sourceforge.jnlp.PluginParameters;
import net.sourceforge.jnlp.runtime.JNLPClassLoader;
import net.sourceforge.jnlp.security.appletextendedsecurity.AppletSecurityLevel;
import net.sourceforge.jnlp.security.appletextendedsecurity.AppletStartupSecuritySettings;
import net.sourceforge.jnlp.security.appletextendedsecurity.ExecuteUnsignedApplet;
import net.sourceforge.jnlp.splashscreen.SplashController;
import net.sourceforge.jnlp.splashscreen.SplashPanel;
import net.sourceforge.jnlp.splashscreen.SplashUtils;
import netscape.javascript.JSObject;
import sun.awt.AppContext;
import sun.awt.SunToolkit;
import sun.awt.X11.XEmbeddedFrame;
import sun.misc.Ref;
import com.sun.jndi.toolkit.url.UrlUtil;
import net.sourceforge.jnlp.util.logging.OutputController;
/*
*/
// FIXME: declare JSProxy implementation
@SuppressWarnings("serial")
public class PluginAppletViewer extends XEmbeddedFrame
implements AppletContext, Printable, SplashController {
/**
* Enumerates the current status of an applet
*
* PRE_INIT -> Parsing and initialization phase
* INIT_COMPLETE -> Initialization complete, reframe pending
* REFRAME_COMPLETE -> Reframe complete, applet is initialized and usable by the user
* INACTIVE -> Browser has directed that the applet be destroyed (this state is non-overridable except by DESTROYED)
* DESTROYED -> Applet has been destroyed
*/
private static enum PAV_INIT_STATUS {
PRE_INIT, INIT_COMPLETE, REFRAME_COMPLETE, INACTIVE, DESTROYED
};
/**
* The panel in which the applet is being displayed.
*/
private NetxPanel panel;
static final ReentrantLock panelLock = new ReentrantLock();
// CONDITION PREDICATE: panel.isAlive()
static final Condition panelLive = panelLock.newCondition();
private int identifier;
// Instance identifier -> PluginAppletViewer object.
private static ConcurrentMap<Integer, PluginAppletViewer> applets =
new ConcurrentHashMap<Integer, PluginAppletViewer>();
private static final ReentrantLock appletsLock = new ReentrantLock();
// CONDITION PREDICATE: !applets.containsKey(identifier)
private static final Condition appletAdded = appletsLock.newCondition();
private static PluginStreamHandler streamhandler;
private static PluginCallRequestFactory requestFactory;
private static ConcurrentMap<Integer, PAV_INIT_STATUS> status =
new ConcurrentHashMap<Integer, PAV_INIT_STATUS>();
private static final ReentrantLock statusLock = new ReentrantLock();
// CONDITION PREDICATE: !status.get(identifier).equals(PAV_INIT_STATUS.INIT_COMPLETE)
private static final Condition initComplete = statusLock.newCondition();
private WindowListener windowEventListener = null;
private AppletEventListener appletEventListener = null;
public static final long APPLET_TIMEOUT = 180000000000L; // 180s in ns
private static final Object requestMutex = new Object();
private static long requestIdentityCounter = 0L;
private Image bufFrameImg;
private Graphics bufFrameImgGraphics;
private SplashPanel splashPanel;
private static long REQUEST_TIMEOUT=60000;//60s
private static void waitForRequestCompletion(PluginCallRequest request) {
try {
if (!request.isDone()) {
request.wait(REQUEST_TIMEOUT);
}
if (!request.isDone()) {
// Do not wait indefinitely to avoid the potential of deadlock
throw new RuntimeException("Possible deadlock, releasing");
}
} catch (InterruptedException ex) {
throw new RuntimeException("Interrupted waiting for call request.", ex);
}
}
/**
* Null constructor to allow instantiation via newInstance()
*/
public PluginAppletViewer() {
}
public static PluginAppletViewer framePanel(int identifier, long handle, int width, int height, NetxPanel panel) {
PluginDebug.debug("Framing ", panel);
// SecurityManager MUST be set, and only privileged code may call framePanel()
System.getSecurityManager().checkPermission(new AllPermission());
PluginAppletViewer appletFrame = new PluginAppletViewer(handle, identifier, panel);
appletFrame.setSize(width, height);
appletFrame.appletEventListener = new AppletEventListener(appletFrame, appletFrame);
panel.addAppletListener(appletFrame.appletEventListener);
// Clear references, if any
if (applets.containsKey(identifier)) {
PluginAppletViewer oldFrame = applets.get(identifier);
oldFrame.remove(panel);
panel.removeAppletListener(oldFrame.appletEventListener);
}
appletsLock.lock();
applets.put(identifier, appletFrame);
appletAdded.signalAll();
appletsLock.unlock();
PluginDebug.debug(panel, " framed");
return appletFrame;
}
/**
* Create new plugin appletviewer frame
*/
private PluginAppletViewer(long handle, final int identifier,
NetxPanel appletPanel) {
super(handle, true);
this.identifier = identifier;
this.panel = appletPanel;
synchronized(appletPanels) {
if (!appletPanels.contains(panel))
appletPanels.addElement(panel);
}
windowEventListener = new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
destroyApplet(identifier);
}
public void windowIconified(WindowEvent evt) {
appletStop();
}
public void windowDeiconified(WindowEvent evt) {
appletStart();
}
};
addWindowListener(windowEventListener);
final AppletPanel fPanel = panel;
try {
SwingUtilities.invokeAndWait(new SplashCreator(fPanel));
} catch (Exception e) {
OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); // Not much we can do other than print
}
}
public void replaceSplash(final SplashPanel newSplash) {
if (splashPanel == null) {
return;
}
if (newSplash == null) {
removeSplash();
return;
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
splashPanel.getSplashComponent().setVisible(false);
splashPanel.stopAnimation();
remove(splashPanel.getSplashComponent());
newSplash.setPercentage(splashPanel.getPercentage());
newSplash.setSplashWidth(splashPanel.getSplashWidth());
newSplash.setSplashHeight(splashPanel.getSplashHeight());
newSplash.adjustForSize();
splashPanel = newSplash;
add("Center", splashPanel.getSplashComponent());
pack();
}
});
} catch (Exception e) {
OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); // Not much we can do other than print
}
}
@Override
public void removeSplash() {
if (splashPanel == null) {
return;
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
splashPanel.getSplashComponent().setVisible(false);
splashPanel.stopAnimation();
removeAll();
setLayout(new BorderLayout());
//remove(splashPanel.getSplashComponent());
splashPanel = null;
//remove(panel);
// Re-add the applet to notify container
add(panel);
panel.setVisible(true);
pack();
}
});
} catch (Exception e) {
OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e); // Not much we can do other than print
}
}
@Override
public int getSplashWidth() {
if (splashPanel != null) {
return splashPanel.getSplashWidth();
} else {
return -1;
}
}
@Override
public int getSplashHeigth() {
if (splashPanel != null) {
return splashPanel.getSplashHeight();
} else {
return -1;
}
}
private static class AppletEventListener implements AppletListener {
final Frame frame;
final PluginAppletViewer appletViewer;
public AppletEventListener(Frame frame, PluginAppletViewer appletViewer) {
this.frame = frame;
this.appletViewer = appletViewer;
}
public void appletStateChanged(AppletEvent evt) {
AppletPanel src = (AppletPanel) evt.getSource();
panelLock.lock();
panelLive.signalAll();
panelLock.unlock();
switch (evt.getID()) {
case AppletPanel.APPLET_RESIZE: {
if (src != null) {
appletViewer.setSize(appletViewer.getPreferredSize());
appletViewer.validate();
}
break;
}
case AppletPanel.APPLET_LOADING_COMPLETED: {
Applet a = src.getApplet(); // sun.applet.AppletPanel
// Fixed #4754451: Applet can have methods running on main
// thread event queue.
//
// The cause of this bug is that the frame of the applet
// is created in main thread group. Thus, when certain
// AWT/Swing events are generated, the events will be
// dispatched through the wrong event dispatch thread.
//
// To fix this, we rearrange the AppContext with the frame,
// so the proper event queue will be looked up.
//
// Swing also maintains a Frame list for the AppContext,
// so we will have to rearrange it as well.
//
if (a != null)
AppletPanel.changeFrameAppContext(frame, SunToolkit.targetToAppContext(a));
else
AppletPanel.changeFrameAppContext(frame, AppContext.getAppContext());
updateStatus(appletViewer.identifier, PAV_INIT_STATUS.INIT_COMPLETE);
break;
}
case AppletPanel.APPLET_START: {
if (src.status != AppletPanel.APPLET_INIT && src.status != AppletPanel.APPLET_STOP) {
String s="Applet started, but but reached invalid state";
PluginDebug.debug(s);
SplashPanel sp=SplashUtils.getErrorSplashScreen(appletViewer.panel.getWidth(), appletViewer.panel.getHeight(), new Exception(s));
appletViewer.replaceSplash(sp);
}
break;
}
case AppletPanel.APPLET_ERROR: {
String s="Undefined error causing applet not to staart appeared";
PluginDebug.debug(s);
SplashPanel sp=SplashUtils.getErrorSplashScreen(appletViewer.panel.getWidth(), appletViewer.panel.getHeight(), new Exception(s));
appletViewer.replaceSplash(sp);
break;
}
}
}
}
public static void setStreamhandler(PluginStreamHandler sh) {
streamhandler = sh;
}
public static void setPluginCallRequestFactory(PluginCallRequestFactory rf) {
requestFactory = rf;
}
private static void handleInitializationMessage(int identifier, String message) throws IOException, LaunchException {
/* The user has specified via a global setting that applets should not be run.*/
if (AppletStartupSecuritySettings.getInstance().getSecurityLevel() == AppletSecurityLevel.DENY_ALL) {
throw new LaunchException(null, null, R("LSFatal"), R("LCClient"), R("LUnsignedApplet"), R("LUnsignedAppletPolicyDenied"));
}
// If there is a key for this status, it means it
// was either initialized before, or destroy has been
// processed. Stop moving further.
if (updateStatus(identifier, PAV_INIT_STATUS.PRE_INIT) != null)
return;
// Extract the information from the message
String[] msgParts = new String[4];
for (int i = 0; i < 3; i++) {
int spaceLocation = message.indexOf(' ');
int nextSpaceLocation = message.indexOf(' ', spaceLocation + 1);
msgParts[i] = message.substring(spaceLocation + 1, nextSpaceLocation);
message = message.substring(nextSpaceLocation + 1);
}
long handle = Long.parseLong(msgParts[0]);
String width = msgParts[1];
String height = msgParts[2];
int spaceLocation = message.indexOf(' ', "tag".length() + 1);
String documentBase = message.substring("tag".length() + 1, spaceLocation);
String paramString = message.substring(spaceLocation + 1);
PluginDebug.debug("Handle = ", handle, "\n",
"Width = ", width, "\n",
"Height = ", height, "\n",
"DocumentBase = ", documentBase, "\n",
"Params = ", paramString);
PluginAppletPanelFactory factory = new PluginAppletPanelFactory();
AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
URL url = new URL(documentBase);
URLConnection conn = url.openConnection();
/* The original URL may have been redirected - this
* sets it to whatever URL/codebase we ended up getting
*/
url = conn.getURL();
PluginParameters params = new PluginParameterParser().parse(width, height, paramString);
// Let user know we are starting up
streamhandler.write("instance " + identifier + " status " + amh.getMessage("status.start"));
factory.createPanel(streamhandler, identifier, handle, url, params);
long maxTimeToSleep = APPLET_TIMEOUT;
appletsLock.lock();
try {
while (!applets.containsKey(identifier) &&
maxTimeToSleep > 0) { // Map is populated only by reFrame
maxTimeToSleep -= waitTillTimeout(appletsLock, appletAdded,
maxTimeToSleep);
}
}
finally {
appletsLock.unlock();
}
// If wait exceeded maxWait, we timed out. Throw an exception
if (maxTimeToSleep <= 0) {
// Caught in handleMessage
throw new RuntimeException("Applet initialization timeout");
}
// We should not try to destroy an applet during
// initialization. It may cause an inconsistent state,
// which would bad if it's a trusted applet that
// read/writes to files
waitForAppletInit(applets.get(identifier).panel);
// Should we proceed with reframing?
PluginDebug.debug("Init complete");
if (updateStatus(identifier, PAV_INIT_STATUS.REFRAME_COMPLETE).equals(PAV_INIT_STATUS.INACTIVE)) {
destroyApplet(identifier);
return;
}
}
/**
* Handle an incoming message from the plugin.
*/
public static void handleMessage(int identifier, int reference, String message) {
PluginDebug.debug("PAV handling: ", message);
try {
if (message.startsWith("handle")) {
handleInitializationMessage(identifier, message);
} else if (message.startsWith("destroy")) {
// Set it inactive, and try to do cleanup is applicable
PAV_INIT_STATUS previousStatus = updateStatus(identifier, PAV_INIT_STATUS.INACTIVE);
PluginDebug.debug("Destroy status set for ", identifier);
if (previousStatus != null &&
previousStatus.equals(PAV_INIT_STATUS.REFRAME_COMPLETE)) {
destroyApplet(identifier);
}
} else {
PluginDebug.debug("Handling message: ", message, " instance ", identifier, " ", Thread.currentThread());
// Wait till initialization finishes
while (!applets.containsKey(identifier) &&
(
!status.containsKey(identifier) ||
status.get(identifier).equals(PAV_INIT_STATUS.PRE_INIT)
))
;
// don't bother processing further for inactive applets
if (status.get(identifier).equals(PAV_INIT_STATUS.INACTIVE))
return;
applets.get(identifier).handleMessage(reference, message);
}
} catch (Exception e) {
OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e);
// If an exception happened during pre-init, we need to update status
updateStatus(identifier, PAV_INIT_STATUS.INACTIVE);
throw new RuntimeException("Failed to handle message: " +
message + " for instance " + identifier, e);
}
}
/**
* Sets the status unless an overriding status is set (e.g. if
* status is DESTROYED, it may not be overridden).
*
* @param identifier The identifier for which the status is to be set
* @param status The status to switch to
* @return The previous status
*/
private static synchronized PAV_INIT_STATUS updateStatus(int identifier, PAV_INIT_STATUS newStatus) {
PAV_INIT_STATUS prev = status.get(identifier);
// If the status is set
if (status.containsKey(identifier)) {
// Nothing may override destroyed status
if (status.get(identifier).equals(PAV_INIT_STATUS.DESTROYED)) {
return prev;
}
// If status is inactive, only DESTROYED may override it
if (status.get(identifier).equals(PAV_INIT_STATUS.INACTIVE)) {
if (!newStatus.equals(PAV_INIT_STATUS.DESTROYED)) {
return prev;
}
}
}
// Else set to given status
statusLock.lock();
status.put(identifier, newStatus);
initComplete.signalAll();
statusLock.unlock();
return prev;
}
/**
* Destroys the given applet instance.
*
* This function may be called multiple times without problems.
* It does a synchronized check on the status and will only
* attempt to destroy the applet if not previously destroyed.
*
* @param identifier The instance which is to be destroyed
*/
private static synchronized void destroyApplet(int identifier) {
// We should not try to destroy an applet during
// initialization. It may cause an inconsistent state.
waitForAppletInit( applets.get(identifier).panel );
PluginDebug.debug("DestroyApplet called for ", identifier);
PAV_INIT_STATUS prev = updateStatus(identifier, PAV_INIT_STATUS.DESTROYED);
// If already destroyed, return
if (prev.equals(PAV_INIT_STATUS.DESTROYED)) {
PluginDebug.debug(identifier, " already destroyed. Returning.");
return;
}
PluginDebug.debug("Attempting to destroy frame ", identifier);
// Try to dispose the panel right away
final PluginAppletViewer pav = applets.get(identifier);
if (pav != null) {
pav.dispose();
// If panel is already disposed, return
if (pav.panel.applet == null) {
PluginDebug.debug(identifier, " panel inactive. Returning.");
return;
}
PluginDebug.debug("Attempting to destroy panel ", identifier);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pav.appletClose();
}
});
}
PluginDebug.debug(identifier, " destroyed");
}
/**
* Function to block until applet initialization is complete.
*
* This function will return if the wait is longer than {@link #APPLET_TIMEOUT}
*
* @param panel the instance to wait for.
*/
public static void waitForAppletInit(NetxPanel panel) {
PluginDebug.debug("Waiting for applet init");
// Wait till initialization finishes
long maxTimeToSleep = APPLET_TIMEOUT;
panelLock.lock();
try {
while (!panel.isInitialized() &&
maxTimeToSleep > 0) {
PluginDebug.debug("Waiting for applet panel ", panel, " to initialize...");
maxTimeToSleep -= waitTillTimeout(panelLock, panelLive, maxTimeToSleep);
}
}
finally {
panelLock.unlock();
}
PluginDebug.debug("Applet panel ", panel, " initialized");
}
/* Resizes an applet panel, waiting for the applet to initialze.
* Should be done asynchronously to avoid the chance of deadlock. */
private void resizeAppletPanel(final int width, final int height) {
// Wait for panel to come alive
waitForAppletInit(panel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
panel.updateSizeInAtts(height, width);
setSize(width, height);
// There is a rather odd drawing bug whereby resizing
// the panel makes no difference on initial call
// because the panel thinks that it is already the
// right size. Validation has no effect there either.
// So we work around by setting size to 1, validating,
// and then setting to the right size and validating
// again. This is not very efficient, and there is
// probably a better way -- but resizing happens
// quite infrequently, so for now this is how we do it
panel.setSize(1, 1);
panel.validate();
panel.setSize(width, height);
panel.validate();
panel.applet.resize(width, height);
panel.applet.validate();
}
});
}
public void handleMessage(int reference, String message) {
if (message.startsWith("width")) {
// 0 => width, 1=> width_value, 2 => height, 3=> height_value
String[] dimMsg = message.split(" ");
final int width = Integer.parseInt(dimMsg[1]);
final int height = Integer.parseInt(dimMsg[3]);
/* Resize the applet asynchronously, to avoid the chance of
* deadlock while waiting for the applet to initialize.
*
* In general, worker threads should spawn new threads for any blocking operations. */
Thread resizeAppletThread = new Thread("resizeAppletThread") {
@Override
public void run() {
resizeAppletPanel(width, height);
}
};
/* Let it eventually complete */
resizeAppletThread.start();
} else if (message.startsWith("GetJavaObject")) {
// FIXME: how do we determine what security context this
// object should belong to?
Object o;
// Wait for the panel to initialize
// (happens in a separate thread)
waitForAppletInit(panel);
PluginDebug.debug(panel, " -- ", panel.getApplet(), " -- initialized: ", panel.isInitialized());
// Still null?
if (panel.getApplet() == null) {
streamhandler.write("instance " + identifier + " reference " + -1 + " fatalError: " + "Initialization failed");
streamhandler.write("context 0 reference " + reference + " Error");
return;
}
o = panel.getApplet();
PluginDebug.debug("Looking for object ", o, " panel is ", panel);
AppletSecurityContextManager.getSecurityContext(0).store(o);
PluginDebug.debug("WRITING 1: ", "context 0 reference ", reference, " GetJavaObject "
, AppletSecurityContextManager.getSecurityContext(0).getIdentifier(o));
streamhandler.write("context 0 reference " + reference + " GetJavaObject "
+ AppletSecurityContextManager.getSecurityContext(0).getIdentifier(o));
PluginDebug.debug("WRITING 1 DONE");
}
}
/*
* Methods for java.applet.AppletContext
*/
private static Map<URL, AudioClip> audioClips = new HashMap<URL, AudioClip>();
/**
* Get an audio clip.
*/
public AudioClip getAudioClip(URL url) {
checkConnect(url);
synchronized (audioClips) {
AudioClip clip = audioClips.get(url);
if (clip == null) {
audioClips.put(url, clip = new AppletAudioClip(url));
}
return clip;
}
}
private static Map<URL, AppletImageRef> imageRefs = new HashMap<URL, AppletImageRef>();
/**
* Get an image.
*/
public Image getImage(URL url) {
return getCachedImage(url);
}
private Image getCachedImage(URL url) {
return (Image) getCachedImageRef(url).get();
}
/**
* Get an image ref.
*/
private synchronized Ref getCachedImageRef(URL url) {
PluginDebug.debug("getCachedImageRef() searching for ", url);
try {
String originalURL = url.toString();
String codeBase = panel.getCodeBase().toString();
if (originalURL.startsWith(codeBase)) {
PluginDebug.debug("getCachedImageRef() got URL = ", url);
PluginDebug.debug("getCachedImageRef() plugin codebase = ", codeBase);
String resourceName = originalURL.substring(codeBase.length());
JNLPClassLoader loader = (JNLPClassLoader) panel.getAppletClassLoader();
URL localURL = null;
if (loader.resourceAvailableLocally(resourceName))
url = loader.getResource(resourceName);
url = localURL != null ? localURL : url;
}
PluginDebug.debug("getCachedImageRef() getting img from URL = ", url);
synchronized (imageRefs) {
AppletImageRef ref = imageRefs.get(url);
if (ref == null) {
ref = new AppletImageRef(url);
imageRefs.put(url, ref);
}
return ref;
}
} catch (Exception e) {
OutputController.getLogger().log(OutputController.Level.ERROR_ALL, "Error occurred when trying to fetch image:");
OutputController.getLogger().log(e);
return null;
}
}
/**
* Flush the image cache.
*/
static void flushImageCache() {
imageRefs.clear();
}
private static Vector<NetxPanel> appletPanels = new Vector<NetxPanel>();
/**
* Get an applet by name.
*/
public Applet getApplet(String name) {
name = name.toLowerCase();
SocketPermission panelSp =
new SocketPermission(panel.getCodeBase().getHost(), "connect");
synchronized(appletPanels) {
for (Enumeration<NetxPanel> e = appletPanels.elements(); e.hasMoreElements();) {
AppletPanel p = e.nextElement();
String param = p.getParameter("name");
if (param != null) {
param = param.toLowerCase();
}
if (name.equals(param) &&
p.getDocumentBase().equals(panel.getDocumentBase())) {
SocketPermission sp =
new SocketPermission(p.getCodeBase().getHost(), "connect");
if (panelSp.implies(sp)) {
return p.applet;
}
}
}
}
return null;
}
/**
* Return an enumeration of all the accessible
* applets on this page.
*/
public Enumeration<Applet> getApplets() {
Vector<Applet> v = new Vector<Applet>();
SocketPermission panelSp =
new SocketPermission(panel.getCodeBase().getHost(), "connect");
synchronized(appletPanels) {
for (Enumeration<NetxPanel> e = appletPanels.elements(); e.hasMoreElements();) {
AppletPanel p = e.nextElement();
if (p.getDocumentBase().equals(panel.getDocumentBase())) {
SocketPermission sp =
new SocketPermission(p.getCodeBase().getHost(), "connect");
if (panelSp.implies(sp)) {
v.addElement(p.applet);
}
}
}
}
return v.elements();
}
public void showDocument(URL url) {
PluginDebug.debug("Showing document...");
showDocument(url, "_self");
}
public void showDocument(URL url, String target) {
// If it is a javascript document, eval on current page.
if ("javascript".equals(url.getProtocol())) {
// Snip protocol off string
String evalString = url.toString().substring("javascript:".length());
eval(getWindow(), evalString);
return;
}
try {
Long reference = getRequestIdentifier();
write("reference " + reference + " LoadURL " + UrlUtil.encode(url.toString(), "UTF-8") + " " + target);
} catch (IOException exception) {
// Deliberately ignore IOException. showDocument may be
// called from threads other than the main thread after
// streamhandler.pluginOutputStream has been closed.
}
}
/**
* Show status.
*/
public void showStatus(String status) {
try {
// FIXME: change to postCallRequest
// For statuses, we cannot have a newline
status = status.replace("\n", " ");
write("status " + status);
} catch (IOException exception) {
// Deliberately ignore IOException. showStatus may be
// called from threads other than the main thread after
// streamhandler.pluginOutputStream has been closed.
}
}
/**
* Returns an incremental number (unique identifier) for a message.
* If identifier hits Long.MAX_VALUE it loops back starting at 0.
*
* @return A unique Long identifier for the request
*/
private static long getRequestIdentifier() {
synchronized(requestMutex) {
if (requestIdentityCounter == Long.MAX_VALUE) {
requestIdentityCounter = 0L;
}
return requestIdentityCounter++;
}
}
public long getWindow() {
PluginDebug.debug("STARTING getWindow");
Long reference = getRequestIdentifier();
PluginCallRequest request = requestFactory.getPluginCallRequest("window",
"instance " + identifier + " reference " +
+reference + " " + "GetWindow", reference);
PluginDebug.debug("STARTING postCallRequest");
streamhandler.postCallRequest(request);
PluginDebug.debug("STARTING postCallRequest done");
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait request 1");
synchronized (request) {
PluginDebug.debug("wait request 2");
while ((Long) request.getObject() == 0)
request.wait();
PluginDebug.debug("wait request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug("STARTING getWindow DONE");
return (Long) request.getObject();
}
// FIXME: make private, access via reflection.
public static Object getMember(long internal, String name) {
AppletSecurityContextManager.getSecurityContext(0).store(name);
int nameID = AppletSecurityContextManager.getSecurityContext(0).getIdentifier(name);
Long reference = getRequestIdentifier();
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("member",
"instance " + 0 + " reference " + reference + " GetMember " +
internal + " " + nameID, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait getMEM request 1");
synchronized (request) {
PluginDebug.debug("wait getMEM request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait getMEM request 3 GOT: ", request.getObject().getClass());
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" getMember DONE");
return request.getObject();
}
public static void setMember(long internal, String name, Object value) {
PluginDebug.debug("Setting to class " + value.getClass() + ":" + value.getClass().isPrimitive());
PluginAppletSecurityContext securityContext = AppletSecurityContextManager.getSecurityContext(0);
securityContext.store(name);
int nameID = securityContext.getIdentifier(name);
Long reference = getRequestIdentifier();
// work on a copy of value, as we don't want to be manipulating
// complex objects
String objIDStr = securityContext.toObjectIDString(value,
value.getClass(), true /* unbox primitives */);
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("void",
"instance " + 0 + " reference " + reference + " SetMember " +
internal + " " + nameID + " " + objIDStr, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait setMem request: ", request.getMessage());
PluginDebug.debug("wait setMem request 1");
synchronized (request) {
PluginDebug.debug("wait setMem request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait setMem request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" setMember DONE");
}
// FIXME: handle long index as well.
public static void setSlot(long internal, int index, Object value) {
PluginAppletSecurityContext securityContext = AppletSecurityContextManager.getSecurityContext(0);
securityContext.store(value);
Long reference = getRequestIdentifier();
String objIDStr = securityContext.toObjectIDString(value,
value.getClass(), true /* unbox primitives */);
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("void",
"instance " + 0 + " reference " + reference + " SetSlot " +
internal + " " + index + " " + objIDStr, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait setSlot request 1");
synchronized (request) {
PluginDebug.debug("wait setSlot request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait setSlot request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" setSlot DONE");
}
public static Object getSlot(long internal, int index) {
Long reference = getRequestIdentifier();
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("member",
"instance " + 0 + " reference " + reference + " GetSlot " +
internal + " " + index, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait getSlot request 1");
synchronized (request) {
PluginDebug.debug("wait getSlot request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait getSlot request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" getSlot DONE");
return request.getObject();
}
public static Object eval(long internal, String s) {
AppletSecurityContextManager.getSecurityContext(0).store(s);
int stringID = AppletSecurityContextManager.getSecurityContext(0).getIdentifier(s);
Long reference = getRequestIdentifier();
// Prefix with dummy instance for convenience.
// FIXME: rename GetMemberPluginCallRequest ObjectPluginCallRequest.
PluginCallRequest request = requestFactory.getPluginCallRequest("member",
"instance " + 0 + " reference " + reference + " Eval " +
internal + " " + stringID, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait eval request 1");
synchronized (request) {
PluginDebug.debug("wait eval request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait eval request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" getSlot DONE");
return request.getObject();
}
public static void removeMember(long internal, String name) {
AppletSecurityContextManager.getSecurityContext(0).store(name);
int nameID = AppletSecurityContextManager.getSecurityContext(0).getIdentifier(name);
Long reference = getRequestIdentifier();
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("void",
"instance " + 0 + " reference " + reference + " RemoveMember " +
internal + " " + nameID, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait removeMember request 1");
synchronized (request) {
PluginDebug.debug("wait removeMember request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait removeMember request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" RemoveMember DONE");
}
public static Object call(long internal, String name, Object args[]) {
// FIXME: when is this removed from the object store?
// FIXME: reference should return the ID.
// FIXME: convenience method for this long line.
AppletSecurityContextManager.getSecurityContext(0).store(name);
int nameID = AppletSecurityContextManager.getSecurityContext(0).getIdentifier(name);
Long reference = getRequestIdentifier();
String argIDs = "";
for (Object arg : args) {
AppletSecurityContextManager.getSecurityContext(0).store(arg);
argIDs += AppletSecurityContextManager.getSecurityContext(0).getIdentifier(arg) + " ";
}
argIDs = argIDs.trim();
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("member",
"instance " + 0 + " reference " + reference + " Call " +
internal + " " + nameID + " " + argIDs, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait call request 1");
synchronized (request) {
PluginDebug.debug("wait call request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait call request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" Call DONE");
return request.getObject();
}
public static Object requestPluginCookieInfo(URI uri) {
PluginCallRequest request;
Long reference = getRequestIdentifier();
try {
String encodedURI = UrlUtil.encode(uri.toString(), "UTF-8");
request = requestFactory.getPluginCallRequest("cookieinfo",
"plugin PluginCookieInfo " + "reference " + reference +
" " + encodedURI, reference);
} catch (UnsupportedEncodingException e) {
OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e);
return null;
}
PluginMessageConsumer.registerPriorityWait(reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait cookieinfo request 1");
synchronized (request) {
PluginDebug.debug("wait cookieinfo request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait cookieinfo request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for cookieinfo request.",
e);
}
PluginDebug.debug(" Cookieinfo DONE");
return request.getObject();
}
public static Object requestPluginProxyInfo(URI uri) {
String requestURI = null;
Long reference = getRequestIdentifier();
try {
// there is no easy way to get SOCKS proxy info. So, we tell mozilla that we want proxy for
// an HTTP uri in case of non http/ftp protocols. If we get back a SOCKS proxy, we can
// use that, if we get back an http proxy, we fallback to DIRECT connect
String scheme = uri.getScheme();
String port = uri.getPort() != -1 ? ":" + uri.getPort() : "";
if (!uri.getScheme().startsWith("http") && !uri.getScheme().equals("ftp"))
scheme = "http";
requestURI = UrlUtil.encode(scheme + "://" + uri.getHost() + port + "/" + uri.getPath(), "UTF-8");
} catch (Exception e) {
PluginDebug.debug("Cannot construct URL from ", uri.toString(), " ... falling back to DIRECT proxy");
OutputController.getLogger().log(OutputController.Level.ERROR_ALL,e);
return null;
}
PluginCallRequest request = requestFactory.getPluginCallRequest("proxyinfo",
"plugin PluginProxyInfo reference " + reference + " " +
requestURI, reference);
PluginMessageConsumer.registerPriorityWait(reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait call request 1");
synchronized (request) {
PluginDebug.debug("wait call request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait call request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" Call DONE");
return request.getObject();
}
public static void JavaScriptFinalize(long internal) {
Long reference = getRequestIdentifier();
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("void",
"instance " + 0 + " reference " + reference + " Finalize " +
internal, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
try {
PluginDebug.debug("wait finalize request 1");
synchronized (request) {
PluginDebug.debug("wait finalize request 2");
while (request.isDone() == false)
request.wait();
PluginDebug.debug("wait finalize request 3");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for call request.",
e);
}
PluginDebug.debug(" finalize DONE");
}
public static String javascriptToString(long internal) {
Long reference = getRequestIdentifier();
// Prefix with dummy instance for convenience.
PluginCallRequest request = requestFactory.getPluginCallRequest("member",
"instance " + 0 + " reference " + reference + " ToString " +
internal, reference);
streamhandler.postCallRequest(request);
streamhandler.write(request.getMessage());
PluginDebug.debug("wait ToString request 1");
synchronized (request) {
PluginDebug.debug("wait ToString request 2");
waitForRequestCompletion(request);
PluginDebug.debug("wait ToString request 3");
}
PluginDebug.debug(" ToString DONE");
return (String) request.getObject();
}
// FIXME: make this private and access it from JSObject using
// reflection.
private void write(String message) throws IOException {
PluginDebug.debug("WRITING 2: ", "instance ", identifier, " " + message);
streamhandler.write("instance " + identifier + " " + message);
PluginDebug.debug("WRITING 2 DONE");
}
public void setStream(String key, InputStream stream) throws IOException {
// We do nothing.
}
@Override
public InputStream getStream(String key) {
// We do nothing.
return null;
}
@Override
public Iterator<String> getStreamKeys() {
// We do nothing.
return null;
}
/**
* System parameters.
*/
static Hashtable<String, String> systemParam = new Hashtable<String, String>();
static {
systemParam.put("codebase", "codebase");
systemParam.put("code", "code");
systemParam.put("alt", "alt");
systemParam.put("width", "width");
systemParam.put("height", "height");
systemParam.put("align", "align");
systemParam.put("vspace", "vspace");
systemParam.put("hspace", "hspace");
}
/**
* Make sure the atrributes are uptodate.
*/
public void updateAtts() {
Dimension d = panel.getSize();
Insets in = panel.getInsets();
int width = d.width - (in.left + in.right);
int height = d.height - (in.top + in.bottom);
panel.updateSizeInAtts(height, width);
}
/**
* Restart the applet.
*/
void appletRestart() {
panel.sendEvent(AppletPanel.APPLET_STOP);
panel.sendEvent(AppletPanel.APPLET_DESTROY);
panel.sendEvent(AppletPanel.APPLET_INIT);
panel.sendEvent(AppletPanel.APPLET_START);
}
/**
* Reload the applet.
*/
void appletReload() {
panel.sendEvent(AppletPanel.APPLET_STOP);
panel.sendEvent(AppletPanel.APPLET_DESTROY);
panel.sendEvent(AppletPanel.APPLET_DISPOSE);
/**
* Fixed #4501142: Classlaoder sharing policy doesn't
* take "archive" into account. This will be overridden
* by Java Plug-in. [stanleyh]
*/
AppletPanel.flushClassLoader(panel.getClassLoaderCacheKey());
/*
* Make sure we don't have two threads running through the event queue
* at the same time.
*/
try {
((AppletViewerPanel)panel).joinAppletThread();
((AppletViewerPanel)panel).release();
} catch (InterruptedException e) {
return; // abort the reload
}
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
((AppletViewerPanel)panel).createAppletThread();
return null;
}
});
panel.sendEvent(AppletPanel.APPLET_LOAD);
panel.sendEvent(AppletPanel.APPLET_INIT);
panel.sendEvent(AppletPanel.APPLET_START);
}
public int print(Graphics graphics, PageFormat pf, int pageIndex) {
return Printable.NO_SUCH_PAGE;
}
/**
* Start the applet.
*/
void appletStart() {
panel.sendEvent(AppletPanel.APPLET_START);
}
/**
* Stop the applet.
*/
void appletStop() {
panel.sendEvent(AppletPanel.APPLET_STOP);
}
/**
* Shutdown a viewer.
* Stop, Destroy, Dispose and Quit a viewer
*/
private void appletShutdown(AppletPanel p) {
p.sendEvent(AppletPanel.APPLET_STOP);
p.sendEvent(AppletPanel.APPLET_DESTROY);
p.sendEvent(AppletPanel.APPLET_DISPOSE);
p.sendEvent(AppletPanel.APPLET_QUIT);
}
/**
* Close this viewer.
* Stop, Destroy, Dispose and Quit an AppletView, then
* reclaim resources and exit the program if this is
* the last applet.
*/
void appletClose() {
// The caller thread is event dispatch thread, so
// spawn a new thread to avoid blocking the event queue
// when calling appletShutdown.
//
final AppletPanel p = panel;
new Thread(new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
ClassLoader cl = p.applet.getClass().getClassLoader();
// Since we want to deal with JNLPClassLoader, extract it if this
// is a codebase loader
if (cl instanceof JNLPClassLoader.CodeBaseClassLoader)
cl = ((JNLPClassLoader.CodeBaseClassLoader) cl).getParentJNLPClassLoader();
appletShutdown(p);
appletPanels.removeElement(p);
// Mark classloader unusable
((JNLPClassLoader) cl).decrementLoaderUseCount();
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
dispose();
}
});
} catch (Exception e) { // ignore, we are just disposing it
}
if (countApplets() == 0) {
appletSystemExit();
}
updateStatus(identifier, PAV_INIT_STATUS.DESTROYED);
}
}).start();
}
/**
* Exit the program.
* Exit from the program (if not stand alone) - do no clean-up
*/
private void appletSystemExit() {
// Do nothing. Exit is handled by another
// block of code, called when _all_ applets are gone
}
/**
* How many applets are running?
*/
public static int countApplets() {
return appletPanels.size();
}
private static void checkConnect(URL url) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
java.security.Permission perm =
url.openConnection().getPermission();
if (perm != null)
security.checkPermission(perm);
else
security.checkConnect(url.getHost(), url.getPort());
} catch (java.io.IOException ioe) {
security.checkConnect(url.getHost(), url.getPort());
}
}
}
/**
* {@inheritDoc}
*
* This method calls paint directly, rather than via super.update() since
* the parent class's update() just does a couple of checks (both of
* which are accounted for) and then calls paint anyway.
*/
public void paint(Graphics g) {
// If the image or the graphics don't exist, create new ones
if (bufFrameImg == null || bufFrameImgGraphics == null) {
// although invisible applets do not have right to paint
// we rather paint to 1x1 to be sure all callbacks will be completed
bufFrameImg = createImage(Math.max(1, getWidth()), Math.max(1, getHeight()));
bufFrameImgGraphics = bufFrameImg.getGraphics();
}
// Paint off-screen
for (Component c: this.getComponents()) {
c.paint(bufFrameImgGraphics);
}
// Draw the painted image
g.drawImage(bufFrameImg, 0, 0, this);
}
@Override
public void update(Graphics g) {
paint(g);
}
/**
* Waits on a given condition queue until timeout.
*
* <b>This function assumes that the monitor lock has already been
* acquired by the caller.</b>
*
* If the given lock is null, this function returns immediately.
*
* @param lock the lock that must be held when this method is called.
* @param cond the condition queue on which to wait for notifications.
* @param timeout The maximum time to wait (nanoseconds)
* @return Approximate time spent sleeping (not guaranteed to be perfect)
*/
public static long waitTillTimeout(ReentrantLock lock, Condition cond,
long timeout) {
// Can't wait on null. Return 0 indicating no wait happened.
if (lock == null)
return 0;
assert lock.isHeldByCurrentThread();
// Record when we started sleeping
long sleepStart = 0L;
try {
sleepStart = System.nanoTime();
cond.await(timeout, TimeUnit.NANOSECONDS);
} catch (InterruptedException ie) {} // Discarded, time to return
// Return the difference
return System.nanoTime() - sleepStart;
}
private class SplashCreator implements Runnable {
private final AppletPanel fPanel;
public SplashCreator(AppletPanel fPanel) {
this.fPanel = fPanel;
}
public void run() {
add("Center", fPanel);
fPanel.setVisible(false);
splashPanel = SplashUtils.getSplashScreen(fPanel.getWidth(), fPanel.getHeight());
if (splashPanel != null) {
splashPanel.startAnimation();
PluginDebug.debug("Added splash " + splashPanel);
add("Center", splashPanel.getSplashComponent());
}
pack();
}
}
}
|