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 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
|
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
package net.sourceforge.jnlp.runtime;
import static net.sourceforge.jnlp.runtime.Translator.R;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketPermission;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlContext;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import net.sourceforge.jnlp.util.JarFile;
import java.util.jar.Manifest;
import net.sourceforge.jnlp.security.appletextendedsecurity.UnsignedAppletTrustConfirmation;
import net.sourceforge.jnlp.AppletDesc;
import net.sourceforge.jnlp.ApplicationDesc;
import net.sourceforge.jnlp.DownloadOptions;
import net.sourceforge.jnlp.ExtensionDesc;
import net.sourceforge.jnlp.JARDesc;
import net.sourceforge.jnlp.JNLPFile;
import net.sourceforge.jnlp.JNLPMatcher;
import net.sourceforge.jnlp.JNLPMatcherException;
import net.sourceforge.jnlp.LaunchDesc;
import net.sourceforge.jnlp.LaunchException;
import net.sourceforge.jnlp.NullJnlpFileException;
import net.sourceforge.jnlp.ParseException;
import net.sourceforge.jnlp.PluginBridge;
import net.sourceforge.jnlp.ResourcesDesc;
import net.sourceforge.jnlp.SecurityDesc;
import net.sourceforge.jnlp.Version;
import net.sourceforge.jnlp.cache.CacheUtil;
import net.sourceforge.jnlp.cache.IllegalResourceDescriptorException;
import net.sourceforge.jnlp.cache.ResourceTracker;
import net.sourceforge.jnlp.cache.UpdatePolicy;
import net.sourceforge.jnlp.security.AppVerifier;
import net.sourceforge.jnlp.security.JNLPAppVerifier;
import net.sourceforge.jnlp.security.PluginAppVerifier;
import net.sourceforge.jnlp.security.SecurityDialogs;
import net.sourceforge.jnlp.tools.JarCertVerifier;
import net.sourceforge.jnlp.util.FileUtils;
import net.sourceforge.jnlp.util.StreamUtils;
import sun.misc.JarIndex;
/**
* Classloader that takes it's resources from a JNLP file. If the
* JNLP file defines extensions, separate classloaders for these
* will be created automatically. Classes are loaded with the
* security context when the classloader was created.
*
* @author <a href="mailto:jmaxwell@users.sourceforge.net">Jon A. Maxwell (JAM)</a> - initial author
* @version $Revision: 1.20 $
*/
public class JNLPClassLoader extends URLClassLoader {
// todo: initializePermissions should get the permissions from
// extension classes too so that main file classes can load
// resources in an extension.
/** Signed JNLP File and Template */
final public static String TEMPLATE = "JNLP-INF/APPLICATION_TEMPLATE.JNLP";
final public static String APPLICATION = "JNLP-INF/APPLICATION.JNLP";
/** Actions to specify how cache is to be managed **/
public static enum DownloadAction {
DOWNLOAD_TO_CACHE, REMOVE_FROM_CACHE, CHECK_CACHE
}
/** True if the application has a signed JNLP File */
private boolean isSignedJNLP = false;
/** map from JNLPFile unique key to shared classloader */
private static Map<String, JNLPClassLoader> uniqueKeyToLoader = new ConcurrentHashMap<String, JNLPClassLoader>();
/** map from JNLPFile unique key to lock, the lock is needed to enforce correct
* initialization of applets that share a unique key*/
private static Map<String, ReentrantLock> uniqueKeyToLock = new HashMap<String, ReentrantLock>();
/** the directory for native code */
private File nativeDir = null; // if set, some native code exists
/** a list of directories that contain native libraries */
private List<File> nativeDirectories = Collections.synchronizedList(new LinkedList<File>());
/** security context */
private AccessControlContext acc = AccessController.getContext();
/** the permissions for the cached jar files */
private List<Permission> resourcePermissions;
/** the app */
private ApplicationInstance app = null; // here for faster lookup in security manager
/** list of this, local and global loaders this loader uses */
private JNLPClassLoader loaders[] = null; // ..[0]==this
/** whether to strictly adhere to the spec or not */
private boolean strict = true;
/** loads the resources */
private ResourceTracker tracker = new ResourceTracker(true); // prefetch
/** the update policy for resources */
private UpdatePolicy updatePolicy;
/** the JNLP file */
private JNLPFile file;
/** the resources section */
private ResourcesDesc resources;
/** the security section */
private SecurityDesc security;
/** Permissions granted by the user during runtime. */
private ArrayList<Permission> runtimePermissions = new ArrayList<Permission>();
/** all jars not yet part of classloader or active */
private List<JARDesc> available = new ArrayList<JARDesc>();
/** the jar cert verifier tool to verify our jars */
private final JarCertVerifier jcv;
private boolean signing = false;
/** ArrayList containing jar indexes for various jars available to this classloader */
private ArrayList<JarIndex> jarIndexes = new ArrayList<JarIndex>();
/** Set of classpath strings declared in the manifest.mf files */
private Set<String> classpaths = new HashSet<String>();
/** File entries in the jar files available to this classloader */
private TreeSet<String> jarEntries = new TreeSet<String>();
/** Map of specific original (remote) CodeSource Urls to securitydesc */
private HashMap<URL, SecurityDesc> jarLocationSecurityMap =
new HashMap<URL, SecurityDesc>();
/*Set to prevent once tried-to-get resources to be tried again*/
private Set<URL> alreadyTried = Collections.synchronizedSet(new HashSet<URL>());
/** Loader for codebase (which is a path, rather than a file) */
private CodeBaseClassLoader codeBaseLoader;
/** True if the jar with the main class has been found
* */
private boolean foundMainJar= false;
/** Name of the application's main class */
private String mainClass = null;
/**
* Variable to track how many times this loader is in use
*/
private int useCount = 0;
/**
* Create a new JNLPClassLoader from the specified file.
*
* @param file the JNLP file
*/
protected JNLPClassLoader(JNLPFile file, UpdatePolicy policy) throws LaunchException {
this(file,policy,null);
}
/**
* Create a new JNLPClassLoader from the specified file.
*
* @param file the JNLP file
* @param policy the UpdatePolicy for this class loader
* @param mainName name of the application's main class
*/
protected JNLPClassLoader(JNLPFile file, UpdatePolicy policy, String mainName) throws LaunchException {
super(new URL[0], JNLPClassLoader.class.getClassLoader());
if (JNLPRuntime.isDebug())
System.out.println("New classloader: " + file.getFileLocation());
this.file = file;
this.updatePolicy = policy;
this.resources = file.getResources();
this.mainClass = mainName;
AppVerifier verifier;
if (file instanceof PluginBridge && !((PluginBridge)file).useJNLPHref()) {
verifier = new PluginAppVerifier();
} else {
verifier = new JNLPAppVerifier();
}
jcv = new JarCertVerifier(verifier);
// initialize extensions
initializeExtensions();
initializeResources();
// initialize permissions
initializePermissions();
setSecurity();
installShutdownHooks();
}
/**
* Install JVM shutdown hooks to clean up resources allocated by this
* ClassLoader.
*/
private void installShutdownHooks() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
/*
* Delete only the native dir created by this classloader (if
* there is one). Other classloaders (parent, peers) will all
* cleanup things they created
*/
if (nativeDir != null) {
if (JNLPRuntime.isDebug()) {
System.out.println("Cleaning up native directory" + nativeDir.getAbsolutePath());
}
try {
FileUtils.recursiveDelete(nativeDir,
new File(System.getProperty("java.io.tmpdir")));
} catch (IOException e) {
/*
* failed to delete a file in tmpdir, no big deal (not
* to mention that the VM is shutting down at this
* point so no much we can do)
*/
}
}
}
});
}
private void setSecurity() throws LaunchException {
URL codebase = null;
if (file.getCodeBase() != null) {
codebase = file.getCodeBase();
} else {
//Fixme: codebase should be the codebase of the Main Jar not
//the location. Although, it still works in the current state.
codebase = file.getResources().getMainJAR().getLocation();
}
/**
* When we're trying to load an applet, file.getSecurity() will return
* null since there is no jnlp file to specify permissions. We
* determine security settings here, after trying to verify jars.
*/
if (file instanceof PluginBridge) {
if (signing == true) {
this.security = new SecurityDesc(file,
SecurityDesc.ALL_PERMISSIONS,
codebase.getHost());
} else {
this.security = new SecurityDesc(file,
SecurityDesc.SANDBOX_PERMISSIONS,
codebase.getHost());
}
} else { //regular jnlp file
/*
* Various combinations of the jars being signed and <security> tags being
* present are possible. They are treated as follows
*
* Jars JNLP File Result
*
* Signed <security> Appropriate Permissions
* Signed no <security> Sandbox
* Unsigned <security> Error
* Unsigned no <security> Sandbox
*
*/
if (!file.getSecurity().getSecurityType().equals(SecurityDesc.SANDBOX_PERMISSIONS) && !signing) {
if (jcv.allJarsSigned()) {
throw new LaunchException(file, null, R("LSFatal"), R("LCClient"), R("LSignedJNLPAppDifferentCerts"), R("LSignedJNLPAppDifferentCertsInfo"));
} else {
throw new LaunchException(file, null, R("LSFatal"), R("LCClient"), R("LUnsignedJarWithSecurity"), R("LUnsignedJarWithSecurityInfo"));
}
} else if (signing == true) {
this.security = file.getSecurity();
} else {
this.security = new SecurityDesc(file,
SecurityDesc.SANDBOX_PERMISSIONS,
codebase.getHost());
}
}
}
/**
* Gets the lock for a given unique key, creating one if it does not yet exist.
* This operation is atomic & thread-safe.
*
* @param file the file whose unique key should be used
* @return the lock
*/
private static ReentrantLock getUniqueKeyLock(String uniqueKey) {
synchronized (uniqueKeyToLock) {
ReentrantLock storedLock = uniqueKeyToLock.get(uniqueKey);
if (storedLock == null) {
storedLock = new ReentrantLock();
uniqueKeyToLock.put(uniqueKey, storedLock);
}
return storedLock;
}
}
/**
* Creates a fully initialized JNLP classloader for the specified JNLPFile,
* to be used as an applet/application's classloader.
* In contrast, JNLP classloaders can also be constructed simply to merge
* its resources into another classloader.
*
* @param file the file to load classes for
* @param policy the update policy to use when downloading resources
* @param mainName Overrides the main class name of the application
*/
private static JNLPClassLoader createInstance(JNLPFile file, UpdatePolicy policy, String mainName) throws LaunchException {
String uniqueKey = file.getUniqueKey();
JNLPClassLoader baseLoader = uniqueKeyToLoader.get(uniqueKey);
JNLPClassLoader loader = new JNLPClassLoader(file, policy, mainName);
// If security level is 'high' or greater, we must check if the user allows unsigned applets
// when the JNLPClassLoader is created. We do so here, because doing so in the constructor
// causes unwanted side-effects for some applets
if (!loader.getSigning() && file instanceof PluginBridge) {
UnsignedAppletTrustConfirmation.checkUnsignedWithUserIfRequired((PluginBridge)file);
}
// New loader init may have caused extentions to create a
// loader for this unique key. Check.
JNLPClassLoader extLoader = uniqueKeyToLoader.get(uniqueKey);
if (extLoader != null && extLoader != loader) {
if (loader.signing && !extLoader.signing)
if (!SecurityDialogs.showNotAllSignedWarningDialog(file))
throw new LaunchException(file, null, R("LSFatal"), R("LCClient"), R("LSignedAppJarUsingUnsignedJar"), R("LSignedAppJarUsingUnsignedJarInfo"));
loader.merge(extLoader);
extLoader.decrementLoaderUseCount(); // loader urls have been merged, ext loader is no longer used
}
// loader is now current + ext. But we also need to think of
// the baseLoader
if (baseLoader != null && baseLoader != loader) {
loader.merge(baseLoader);
}
return loader;
}
/**
* Returns a JNLP classloader for the specified JNLP file.
*
* @param file the file to load classes for
* @param policy the update policy to use when downloading resources
*/
public static JNLPClassLoader getInstance(JNLPFile file, UpdatePolicy policy) throws LaunchException {
return getInstance(file, policy, null);
}
/**
* Returns a JNLP classloader for the specified JNLP file.
*
* @param file the file to load classes for
* @param policy the update policy to use when downloading resources
* @param mainName Overrides the main class name of the application
*/
public static JNLPClassLoader getInstance(JNLPFile file, UpdatePolicy policy, String mainName) throws LaunchException {
JNLPClassLoader baseLoader = null;
JNLPClassLoader loader = null;
String uniqueKey = file.getUniqueKey();
synchronized ( getUniqueKeyLock(uniqueKey) ) {
baseLoader = uniqueKeyToLoader.get(uniqueKey);
// A null baseloader implies that no loader has been created
// for this codebase/jnlp yet. Create one.
if (baseLoader == null ||
(file.isApplication() &&
!baseLoader.getJNLPFile().getFileLocation().equals(file.getFileLocation()))) {
loader = createInstance(file, policy, mainName);
} else {
// if key is same and locations match, this is the loader we want
if (!file.isApplication()) {
// If this is an applet, we do need to consider its loader
loader = new JNLPClassLoader(file, policy, mainName);
if (baseLoader != null)
baseLoader.merge(loader);
}
loader = baseLoader;
}
// loaders are mapped to a unique key. Only extensions and parent
// share a key, so it is safe to always share based on it
loader.incrementLoaderUseCount();
uniqueKeyToLoader.put(uniqueKey, loader);
}
return loader;
}
/**
* Returns a JNLP classloader for the JNLP file at the specified
* location.
*
* @param location the file's location
* @param version the file's version
* @param policy the update policy to use when downloading resources
* @param mainName Overrides the main class name of the application
*/
public static JNLPClassLoader getInstance(URL location, String uniqueKey, Version version, UpdatePolicy policy, String mainName)
throws IOException, ParseException, LaunchException {
JNLPClassLoader loader;
synchronized ( getUniqueKeyLock(uniqueKey) ) {
loader = uniqueKeyToLoader.get(uniqueKey);
if (loader == null || !location.equals(loader.getJNLPFile().getFileLocation())) {
JNLPFile jnlpFile = new JNLPFile(location, uniqueKey, version, false, policy);
loader = getInstance(jnlpFile, policy, mainName);
}
}
return loader;
}
/**
* Load the extensions specified in the JNLP file.
*/
void initializeExtensions() {
ExtensionDesc[] ext = resources.getExtensions();
List<JNLPClassLoader> loaderList = new ArrayList<JNLPClassLoader>();
loaderList.add(this);
if (mainClass == null) {
Object obj = file.getLaunchInfo();
if (obj instanceof ApplicationDesc) {
ApplicationDesc ad = (ApplicationDesc) file.getLaunchInfo();
mainClass = ad.getMainClass();
} else if (obj instanceof AppletDesc) {
AppletDesc ad = (AppletDesc) file.getLaunchInfo();
mainClass = ad.getMainClass();
}
}
//if (ext != null) {
for (int i = 0; i < ext.length; i++) {
try {
String uniqueKey = this.getJNLPFile().getUniqueKey();
JNLPClassLoader loader = getInstance(ext[i].getLocation(), uniqueKey, ext[i].getVersion(), updatePolicy, mainClass);
loaderList.add(loader);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//}
loaders = loaderList.toArray(new JNLPClassLoader[loaderList.size()]);
}
/**
* Make permission objects for the classpath.
*/
void initializePermissions() {
resourcePermissions = new ArrayList<Permission>();
JARDesc jars[] = resources.getJARs();
for (int i = 0; i < jars.length; i++) {
Permission p = CacheUtil.getReadPermission(jars[i].getLocation(),
jars[i].getVersion());
if (JNLPRuntime.isDebug()) {
if (p == null)
System.out.println("Unable to add permission for " + jars[i].getLocation());
else
System.out.println("Permission added: " + p.toString());
}
if (p != null)
resourcePermissions.add(p);
}
}
/**
* Check if a described jar file is invalid
* @param jar the jar to check
* @return true if file exists AND is an invalid jar, false otherwise
*/
boolean isInvalidJar(JARDesc jar){
File cacheFile = tracker.getCacheFile(jar.getLocation());
if (cacheFile == null)
return false;//File cannot be retrieved, do not claim it is an invalid jar
boolean isInvalid = false;
try {
JarFile jarFile = new JarFile(cacheFile.getAbsolutePath());
jarFile.close();
} catch (IOException ioe){
//Catch a ZipException or any other read failure
isInvalid = true;
}
return isInvalid;
}
/**
* Determine how invalid jars should be handled
* @return whether to filter invalid jars, or error later on
*/
private boolean shouldFilterInvalidJars(){
if (file instanceof PluginBridge){
PluginBridge pluginBridge = (PluginBridge)file;
/*Ignore on applet, ie !useJNLPHref*/
return !pluginBridge.useJNLPHref();
}
return false;//Error is default behaviour
}
/**
* Load all of the JARs used in this JNLP file into the
* ResourceTracker for downloading.
*/
void initializeResources() throws LaunchException {
if (file instanceof PluginBridge){
PluginBridge bridge = (PluginBridge)file;
for (String codeBaseFolder : bridge.getCodeBaseFolders()){
try {
addToCodeBaseLoader(new URL(file.getCodeBase(), codeBaseFolder));
} catch (MalformedURLException mfe) {
System.err.println("Problem trying to add folder to code base:");
System.err.println(mfe.getMessage());
}
}
}
JARDesc jars[] = resources.getJARs();
if (jars.length == 0) {
boolean allSigned = (loaders.length > 1) /* has extensions */;
for (int i = 1; i < loaders.length; i++) {
if (!loaders[i].getSigning()) {
allSigned = false;
break;
}
}
if(allSigned)
signing = true;
//Check if main jar is found within extensions
foundMainJar = foundMainJar || hasMainInExtensions();
return;
}
/*
if (jars == null || jars.length == 0) {
throw new LaunchException(null, null, R("LSFatal"),
R("LCInit"), R("LFatalVerification"), "No jars!");
}
*/
List<JARDesc> initialJars = new ArrayList<JARDesc>();
for (int i = 0; i < jars.length; i++) {
available.add(jars[i]);
if (jars[i].isEager())
initialJars.add(jars[i]); // regardless of part
tracker.addResource(jars[i].getLocation(),
jars[i].getVersion(),
getDownloadOptionsForJar(jars[i]),
jars[i].isCacheable() ? JNLPRuntime.getDefaultUpdatePolicy() : UpdatePolicy.FORCE
);
}
//If there are no eager jars, initialize the first jar
if(initialJars.size() == 0)
initialJars.add(jars[0]);
if (strict)
fillInPartJars(initialJars); // add in each initial part's lazy jars
waitForJars(initialJars); //download the jars first.
//A ZipException will propagate later on if the jar is invalid and not checked here
if (shouldFilterInvalidJars()){
//We filter any invalid jars
Iterator<JARDesc> iterator = initialJars.iterator();
while (iterator.hasNext()){
JARDesc jar = iterator.next();
if (isInvalidJar(jar)) {
//Remove this jar as an available jar
iterator.remove();
tracker.removeResource(jar.getLocation());
available.remove(jar);
}
}
}
if (JNLPRuntime.isVerifying()) {
try {
jcv.add(initialJars, tracker);
} catch (Exception e) {
//we caught an Exception from the JarCertVerifier class.
//Note: one of these exceptions could be from not being able
//to read the cacerts or trusted.certs files.
e.printStackTrace();
throw new LaunchException(null, null, R("LSFatal"),
R("LCInit"), R("LFatalVerification"), R("LFatalVerificationInfo") + ": " +e.getMessage());
}
//Case when at least one jar has some signing
if (jcv.isFullySigned()) {
signing = true;
if (!jcv.allJarsSigned() &&
!SecurityDialogs.showNotAllSignedWarningDialog(file))
throw new LaunchException(file, null, R("LSFatal"), R("LCClient"), R("LSignedAppJarUsingUnsignedJar"), R("LSignedAppJarUsingUnsignedJarInfo"));
// Check for main class in the downloaded jars, and check/verify signed JNLP fill
checkForMain(initialJars);
// If jar with main class was not found, check available resources
while (!foundMainJar && available != null && available.size() != 0)
addNextResource();
// If the jar with main class was not found, check extension
// jnlp's resources
foundMainJar = foundMainJar || hasMainInExtensions();
// If jar with main class was not found and there are no more
// available jars, throw a LaunchException
if (file.getLaunchInfo() != null) {
if (!foundMainJar
&& (available == null || available.size() == 0))
throw new LaunchException(file, null, R("LSFatal"),
R("LCClient"), R("LCantDetermineMainClass"),
R("LCantDetermineMainClassInfo"));
}
// If main jar was found, but a signed JNLP file was not located
if (!isSignedJNLP && foundMainJar)
file.setSignedJNLPAsMissing();
//user does not trust this publisher
if (!jcv.isTriviallySigned()) {
checkTrustWithUser();
} else {
/**
* If the user trusts this publisher (i.e. the publisher's certificate
* is in the user's trusted.certs file), we do not show any dialogs.
*/
}
} else {
// Otherwise this jar is simply unsigned -- make sure to ask
// for permission on certain actions
signing = false;
}
}
for (JARDesc jarDesc : file.getResources().getJARs()) {
try {
File cachedFile;
try {
cachedFile = tracker.getCacheFile(jarDesc.getLocation());
} catch (IllegalResourceDescriptorException irde){
//Caused by ignored resource being removed due to not being valid
System.err.println("JAR " + jarDesc.getLocation() + " is not a valid jar file. Continuing.");
continue;
}
if (cachedFile == null) {
System.err.println("JAR " + jarDesc.getLocation() + " not found. Continuing.");
continue; // JAR not found. Keep going.
}
// TODO: Should be toURI().toURL()
URL location = cachedFile.toURL();
SecurityDesc jarSecurity = file.getSecurity();
if (file instanceof PluginBridge) {
URL codebase = null;
if (file.getCodeBase() != null) {
codebase = file.getCodeBase();
} else {
//Fixme: codebase should be the codebase of the Main Jar not
//the location. Although, it still works in the current state.
codebase = file.getResources().getMainJAR().getLocation();
}
if (signing) {
jarSecurity = new SecurityDesc(file,
SecurityDesc.ALL_PERMISSIONS,
codebase.getHost());
} else {
jarSecurity = new SecurityDesc(file,
SecurityDesc.SANDBOX_PERMISSIONS,
codebase.getHost());
}
}
jarLocationSecurityMap.put(jarDesc.getLocation(), jarSecurity);
} catch (MalformedURLException mfe) {
System.err.println(mfe.getMessage());
}
}
activateJars(initialJars);
}
/***
* Checks for the jar that contains the main class. If the main class was
* found, it checks to see if the jar is signed and whether it contains a
* signed JNLP file
*
* @param jars Jars that are checked to see if they contain the main class
* @throws LaunchException Thrown if the signed JNLP file, within the main jar, fails to be verified or does not match
*/
void checkForMain(List<JARDesc> jars) throws LaunchException {
// Check launch info
if (mainClass == null) {
LaunchDesc launchDesc = file.getLaunchInfo();
if (launchDesc == null) {
return;
}
mainClass = launchDesc.getMainClass();
}
// The main class may be specified in the manifest
// Check main jar
if (mainClass == null) {
JARDesc mainJarDesc = file.getResources().getMainJAR();
mainClass = getMainClassName(mainJarDesc.getLocation());
}
// Check first jar
if (mainClass == null) {
JARDesc firstJarDesc = jars.get(0);
mainClass = getMainClassName(firstJarDesc.getLocation());
}
// Still not found? Iterate and set if only 1 was found
if (mainClass == null) {
for (JARDesc jarDesc: jars) {
String mainClassInThisJar = getMainClassName(jarDesc.getLocation());
if (mainClassInThisJar != null) {
if (mainClass == null) { // first main class
mainClass = mainClassInThisJar;
} else { // There is more than one main class. Set to null and break.
mainClass = null;
break;
}
}
}
}
String desiredJarEntryName = mainClass + ".class";
for (int i = 0; i < jars.size(); i++) {
try {
File localFile = tracker
.getCacheFile(jars.get(i).getLocation());
if (localFile == null) {
System.err.println("JAR " + jars.get(i).getLocation() + " not found. Continuing.");
continue; // JAR not found. Keep going.
}
JarFile jarFile = new JarFile(localFile);
Enumeration<JarEntry> entries = jarFile.entries();
JarEntry je;
while (entries.hasMoreElements()) {
je = entries.nextElement();
String jeName = je.getName().replaceAll("/", ".");
if (jeName.equals(desiredJarEntryName)) {
foundMainJar = true;
verifySignedJNLP(jars.get(i), jarFile);
break;
}
}
jarFile.close();
} catch (IOException e) {
/*
* After this exception is caught, it is escaped. This will skip
* the jarFile that may have thrown this exception and move on
* to the next jarFile (if there are any)
*/
}
}
}
/**
* Gets the name of the main method if specified in the manifest
*
* @param location The JAR location
* @return the main class name, null if there isn't one of if there was an error
*/
String getMainClassName(URL location) {
String mainClass = null;
File f = tracker.getCacheFile(location);
if( f != null) {
JarFile mainJar = null;
try {
mainJar = new JarFile(f);
mainClass = mainJar.getManifest().
getMainAttributes().getValue("Main-Class");
} catch (IOException ioe) {
mainClass = null;
} finally {
StreamUtils.closeSilently(mainJar);
}
}
return mainClass;
}
/**
* Returns true if this loader has the main jar
*/
public boolean hasMainJar() {
return this.foundMainJar;
}
/**
* Returns true if extension loaders have the main jar
*/
private boolean hasMainInExtensions() {
boolean foundMain = false;
for (int i = 1; i < loaders.length && !foundMain; i++) {
foundMain = loaders[i].hasMainJar();
}
return foundMain;
}
/**
* Is called by checkForMain() to check if the jar file is signed and if it
* contains a signed JNLP file.
*
* @param jarDesc JARDesc of jar
* @param jarFile the jar file
* @throws LaunchException thrown if the signed JNLP file, within the main jar, fails to be verified or does not match
*/
private void verifySignedJNLP(JARDesc jarDesc, JarFile jarFile)
throws LaunchException {
List<JARDesc> desc = new ArrayList<JARDesc>();
desc.add(jarDesc);
// Initialize streams
InputStream inStream = null;
InputStreamReader inputReader = null;
FileReader fr = null;
InputStreamReader jnlpReader = null;
try {
// NOTE: verification should have happened by now. In other words,
// calling jcv.verifyJars(desc, tracker) here should have no affect.
if (jcv.isFullySigned()) {
Enumeration<JarEntry> entries = jarFile.entries();
JarEntry je;
while (entries.hasMoreElements()) {
je = entries.nextElement();
String jeName = je.getName().toUpperCase();
if (jeName.equals(TEMPLATE) || jeName.equals(APPLICATION)) {
if (JNLPRuntime.isDebug())
System.err.println("Creating Jar InputStream from JarEntry");
inStream = jarFile.getInputStream(je);
inputReader = new InputStreamReader(inStream);
if (JNLPRuntime.isDebug())
System.err.println("Creating File InputStream from lauching JNLP file");
JNLPFile jnlp = this.getJNLPFile();
URL url = jnlp.getFileLocation();
File jn = null;
// If the file is on the local file system, use original path, otherwise find cached file
if (url.getProtocol().toLowerCase().equals("file"))
jn = new File(url.getPath());
else
jn = CacheUtil.getCacheFile(url, null);
fr = new FileReader(jn);
jnlpReader = fr;
// Initialize JNLPMatcher class
JNLPMatcher matcher;
if (jeName.equals(APPLICATION)) { // If signed application was found
if (JNLPRuntime.isDebug())
System.err.println("APPLICATION.JNLP has been located within signed JAR. Starting verfication...");
matcher = new JNLPMatcher(inputReader, jnlpReader, false);
} else { // Otherwise template was found
if (JNLPRuntime.isDebug())
System.err.println("APPLICATION_TEMPLATE.JNLP has been located within signed JAR. Starting verfication...");
matcher = new JNLPMatcher(inputReader, jnlpReader,
true);
}
// If signed JNLP file does not matches launching JNLP file, throw JNLPMatcherException
if (!matcher.isMatch())
throw new JNLPMatcherException("Signed Application did not match launching JNLP File");
this.isSignedJNLP = true;
if (JNLPRuntime.isDebug())
System.err.println("Signed Application Verification Successful");
break;
}
}
}
} catch (JNLPMatcherException e) {
/*
* Throws LaunchException if signed JNLP file fails to be verified
* or fails to match the launching JNLP file
*/
throw new LaunchException(file, null, R("LSFatal"), R("LCClient"),
R("LSignedJNLPFileDidNotMatch"), R(e.getMessage()));
/*
* Throwing this exception will fail to initialize the application
* resulting in the termination of the application
*/
} catch (Exception e) {
if (JNLPRuntime.isDebug())
e.printStackTrace(System.err);
/*
* After this exception is caught, it is escaped. If an exception is
* thrown while handling the jar file, (mainly for
* JarCertVerifier.add) it assumes the jar file is unsigned and
* skip the check for a signed JNLP file
*/
} finally {
//Close all streams
StreamUtils.closeSilently(inStream);
StreamUtils.closeSilently(inputReader);
StreamUtils.closeSilently(fr);
StreamUtils.closeSilently(jnlpReader);
}
if (JNLPRuntime.isDebug())
System.err.println("Ending check for signed JNLP file...");
}
/**
* Prompt the user for trust on all the signers that require approval.
* @throws LaunchException if the user does not approve every dialog prompt.
*/
private void checkTrustWithUser() throws LaunchException {
if (JNLPRuntime.isTrustAll()){
return;
}
if (jcv.isFullySigned() && !jcv.getAlreadyTrustPublisher()) {
jcv.checkTrustWithUser(file);
}
}
/**
* Add applet's codebase URL. This allows compatibility with
* applets that load resources from their codebase instead of
* through JARs, but can slow down resource loading. Resources
* loaded from the codebase are not cached.
*/
public void enableCodeBase() {
addToCodeBaseLoader(file.getCodeBase());
}
/**
* Sets the JNLP app this group is for; can only be called once.
*/
public void setApplication(ApplicationInstance app) {
if (this.app != null) {
if (JNLPRuntime.isDebug()) {
Exception ex = new IllegalStateException("Application can only be set once");
ex.printStackTrace();
}
return;
}
this.app = app;
}
/**
* Returns the JNLP app for this classloader
*/
public ApplicationInstance getApplication() {
return app;
}
/**
* Returns the JNLP file the classloader was created from.
*/
public JNLPFile getJNLPFile() {
return file;
}
/**
* Returns the permissions for the CodeSource.
*/
protected PermissionCollection getPermissions(CodeSource cs) {
try {
Permissions result = new Permissions();
// should check for extensions or boot, automatically give all
// access w/o security dialog once we actually check certificates.
// copy security permissions from SecurityDesc element
if (security != null) {
// Security desc. is used only to track security settings for the
// application. However, an application may comprise of multiple
// jars, and as such, security must be evaluated on a per jar basis.
// set default perms
PermissionCollection permissions = security.getSandBoxPermissions();
// If more than default is needed:
// 1. Code must be signed
// 2. ALL or J2EE permissions must be requested (note: plugin requests ALL automatically)
if (cs == null) {
throw new NullPointerException("Code source was null");
}
if (cs.getCodeSigners() != null) {
if (cs.getLocation() == null) {
throw new NullPointerException("Code source location was null");
}
if (getCodeSourceSecurity(cs.getLocation()) == null) {
throw new NullPointerException("Code source security was null");
}
if (getCodeSourceSecurity(cs.getLocation()).getSecurityType() == null) {
if (JNLPRuntime.isDebug()){
new NullPointerException("Warning! Code source security type was null").printStackTrace();
}
}
Object securityType = getCodeSourceSecurity(cs.getLocation()).getSecurityType();
if (SecurityDesc.ALL_PERMISSIONS.equals(securityType)
|| SecurityDesc.J2EE_PERMISSIONS.equals(securityType)) {
permissions = getCodeSourceSecurity(cs.getLocation()).getPermissions(cs);
}
}
Enumeration<Permission> e = permissions.elements();
while (e.hasMoreElements()) {
result.add(e.nextElement());
}
}
// add in permission to read the cached JAR files
for (int i = 0; i < resourcePermissions.size(); i++) {
result.add(resourcePermissions.get(i));
}
// add in the permissions that the user granted.
for (int i = 0; i < runtimePermissions.size(); i++) {
result.add(runtimePermissions.get(i));
}
// Class from host X should be allowed to connect to host X
if (cs.getLocation() != null && cs.getLocation().getHost().length() > 0)
result.add(new SocketPermission(cs.getLocation().getHost(),
"connect, accept"));
return result;
} catch (RuntimeException ex) {
if (JNLPRuntime.isDebug()) {
ex.printStackTrace();
}
throw ex;
}
}
protected void addPermission(Permission p) {
runtimePermissions.add(p);
}
/**
* Adds to the specified list of JARS any other JARs that need
* to be loaded at the same time as the JARs specified (ie, are
* in the same part).
*/
protected void fillInPartJars(List<JARDesc> jars) {
for (int i = 0; i < jars.size(); i++) {
String part = jars.get(i).getPart();
for (int a = 0; a < available.size(); a++) {
JARDesc jar = available.get(a);
if (part != null && part.equals(jar.getPart()))
if (!jars.contains(jar))
jars.add(jar);
}
}
}
/**
* Ensures that the list of jars have all been transferred, and
* makes them available to the classloader. If a jar contains
* native code, the libraries will be extracted and placed in
* the path.
*
* @param jars the list of jars to load
*/
protected void activateJars(final List<JARDesc> jars) {
PrivilegedAction<Void> activate = new PrivilegedAction<Void>() {
@SuppressWarnings("deprecation")
public Void run() {
// transfer the Jars
waitForJars(jars);
for (int i = 0; i < jars.size(); i++) {
JARDesc jar = jars.get(i);
available.remove(jar);
// add jar
File localFile = tracker.getCacheFile(jar.getLocation());
try {
URL location = jar.getLocation(); // non-cacheable, use source location
if (localFile != null) {
// TODO: Should be toURI().toURL()
location = localFile.toURL(); // cached file
// This is really not the best way.. but we need some way for
// PluginAppletViewer::getCachedImageRef() to check if the image
// is available locally, and it cannot use getResources() because
// that prefetches the resource, which confuses MediaTracker.waitForAll()
// which does a wait(), waiting for notification (presumably
// thrown after a resource is fetched). This bug manifests itself
// particularly when using The FileManager applet from Webmin.
JarFile jarFile = new JarFile(localFile);
Enumeration<JarEntry> e = jarFile.entries();
while (e.hasMoreElements()) {
JarEntry je = e.nextElement();
// another jar in my jar? it is more likely than you think
if (je.getName().endsWith(".jar")) {
// We need to extract that jar so that it can be loaded
// (inline loading with "jar:..!/..." path will not work
// with standard classloader methods)
String extractedJarLocation = localFile.getParent() + "/" + je.getName();
File parentDir = new File(extractedJarLocation).getParentFile();
if (!parentDir.isDirectory() && !parentDir.mkdirs()) {
throw new RuntimeException(R("RNestedJarExtration"));
}
FileOutputStream extractedJar = new FileOutputStream(extractedJarLocation);
InputStream is = jarFile.getInputStream(je);
byte[] bytes = new byte[1024];
int read = is.read(bytes);
int fileSize = read;
while (read > 0) {
extractedJar.write(bytes, 0, read);
read = is.read(bytes);
fileSize += read;
}
is.close();
extractedJar.close();
// 0 byte file? skip
if (fileSize <= 0) {
continue;
}
tracker.addResource(new File(extractedJarLocation).toURL(), null, null, null);
URL codebase = file.getCodeBase();
if (codebase == null) {
//FIXME: codebase should be the codebase of the Main Jar not
//the location. Although, it still works in the current state.
codebase = file.getResources().getMainJAR().getLocation();
}
SecurityDesc jarSecurity = null;
if (jcv.isFullySigned()) {
// Already trust application, nested jar should be given
jarSecurity = new SecurityDesc(file,
SecurityDesc.ALL_PERMISSIONS,
codebase.getHost());
} else {
jarSecurity = new SecurityDesc(file,
SecurityDesc.SANDBOX_PERMISSIONS,
codebase.getHost());
}
try {
URL fileURL = new URL("file://" + extractedJarLocation);
// there is no remote URL for this, so lets fake one
URL fakeRemote = new URL(jar.getLocation().toString() + "!" + je.getName());
CachedJarFileCallback.getInstance().addMapping(fakeRemote, fileURL);
addURL(fakeRemote);
jarLocationSecurityMap.put(fakeRemote, jarSecurity);
} catch (MalformedURLException mfue) {
if (JNLPRuntime.isDebug())
System.err.println("Unable to add extracted nested jar to classpath");
mfue.printStackTrace();
}
}
jarEntries.add(je.getName());
}
jarFile.close();
}
addURL(jar.getLocation());
// there is currently no mechanism to cache files per
// instance.. so only index cached files
if (localFile != null) {
CachedJarFileCallback.getInstance().addMapping(jar.getLocation(), localFile.toURL());
JarFile jarFile = new JarFile(localFile.getAbsolutePath());
Manifest mf = jarFile.getManifest();
// Only check classpath if this is the plugin and there is no jnlp_href usage.
// Note that this is different from proprietary plugin behaviour.
// If jnlp_href is used, the app should be treated similarly to when
// it is run from javaws as a webstart.
if (file instanceof PluginBridge && !((PluginBridge) file).useJNLPHref()) {
classpaths.addAll(getClassPathsFromManifest(mf, jar.getLocation().getPath()));
}
JarIndex index = JarIndex.getJarIndex(jarFile, null);
if (index != null)
jarIndexes.add(index);
jarFile.close();
} else {
CachedJarFileCallback.getInstance().addMapping(jar.getLocation(), jar.getLocation());
}
if (JNLPRuntime.isDebug())
System.err.println("Activate jar: " + location);
}
catch (Exception ex) {
if (JNLPRuntime.isDebug())
ex.printStackTrace();
}
// some programs place a native library in any jar
activateNative(jar);
}
return null;
}
};
AccessController.doPrivileged(activate, acc);
}
/**
* Search for and enable any native code contained in a JAR by copying the
* native files into the filesystem. Called in the security context of the
* classloader.
*/
protected void activateNative(JARDesc jar) {
if (JNLPRuntime.isDebug())
System.out.println("Activate native: " + jar.getLocation());
File localFile = tracker.getCacheFile(jar.getLocation());
if (localFile == null)
return;
String[] librarySuffixes = { ".so", ".dylib", ".jnilib", ".framework", ".dll" };
try {
JarFile jarFile = new JarFile(localFile, false);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
if (e.isDirectory()) {
continue;
}
String name = new File(e.getName()).getName();
boolean isLibrary = false;
for (String suffix : librarySuffixes) {
if (name.endsWith(suffix)) {
isLibrary = true;
break;
}
}
if (!isLibrary) {
continue;
}
if (nativeDir == null)
nativeDir = getNativeDir();
File outFile = new File(nativeDir, name);
if (!outFile.isFile()) {
FileUtils.createRestrictedFile(outFile, true);
}
CacheUtil.streamCopy(jarFile.getInputStream(e),
new FileOutputStream(outFile));
}
jarFile.close();
} catch (IOException ex) {
if (JNLPRuntime.isDebug())
ex.printStackTrace();
}
}
/**
* Return the base directory to store native code files in.
* This method does not need to return the same directory across
* calls.
*/
protected File getNativeDir() {
final int rand = (int)((Math.random()*2 - 1) * Integer.MAX_VALUE);
nativeDir = new File(System.getProperty("java.io.tmpdir")
+ File.separator + "netx-native-"
+ (rand & 0xFFFF));
File parent = nativeDir.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
return null;
}
try {
FileUtils.createRestrictedDirectory(nativeDir);
// add this new native directory to the search path
addNativeDirectory(nativeDir);
return nativeDir;
} catch (IOException e) {
return null;
}
}
/**
* Adds the {@link File} to the search path of this {@link JNLPClassLoader}
* when trying to find a native library
*/
protected void addNativeDirectory(File nativeDirectory) {
nativeDirectories.add(nativeDirectory);
}
/**
* Returns a list of all directories in the search path of the current classloader
* when it tires to find a native library.
* @return a list of directories in the search path for native libraries
*/
protected List<File> getNativeDirectories() {
return nativeDirectories;
}
/**
* Return the absolute path to the native library.
*/
protected String findLibrary(String lib) {
String syslib = System.mapLibraryName(lib);
for (File dir : getNativeDirectories()) {
File target = new File(dir, syslib);
if (target.exists())
return target.toString();
}
String result = super.findLibrary(lib);
if (result != null)
return result;
return findLibraryExt(lib);
}
/**
* Try to find the library path from another peer classloader.
*/
protected String findLibraryExt(String lib) {
for (int i = 0; i < loaders.length; i++) {
String result = null;
if (loaders[i] != this)
result = loaders[i].findLibrary(lib);
if (result != null)
return result;
}
return null;
}
/**
* Wait for a group of JARs, and send download events if there
* is a download listener or display a progress window otherwise.
*
* @param jars the jars
*/
private void waitForJars(List jars) {
URL urls[] = new URL[jars.size()];
for (int i = 0; i < jars.size(); i++) {
JARDesc jar = (JARDesc) jars.get(i);
urls[i] = jar.getLocation();
}
CacheUtil.waitForResources(app, tracker, urls, file.getTitle());
}
/**
* Find the loaded class in this loader or any of its extension loaders.
*/
protected Class findLoadedClassAll(String name) {
for (int i = 0; i < loaders.length; i++) {
Class result = null;
if (loaders[i] == this) {
result = JNLPClassLoader.super.findLoadedClass(name);
} else {
result = loaders[i].findLoadedClassAll(name);
}
if (result != null)
return result;
}
// Result is still null. Return what the codebaseloader
// has (which returns null if it is not loaded there either)
if (codeBaseLoader != null)
return codeBaseLoader.findLoadedClassFromParent(name);
else
return null;
}
/**
* Find a JAR in the shared 'extension' classloaders, this
* classloader, or one of the classloaders for the JNLP file's
* extensions.
*/
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> result = findLoadedClassAll(name);
// try parent classloader
if (result == null) {
try {
ClassLoader parent = getParent();
if (parent == null)
parent = ClassLoader.getSystemClassLoader();
return parent.loadClass(name);
} catch (ClassNotFoundException ex) {
}
}
// filter out 'bad' package names like java, javax
// validPackage(name);
// search this and the extension loaders
if (result == null) {
try {
result = loadClassExt(name);
} catch (ClassNotFoundException cnfe) {
// Not found in external loader either
// Look in 'Class-Path' as specified in the manifest file
try {
for (String classpath: classpaths) {
JARDesc desc;
try {
URL jarUrl = new URL(file.getCodeBase(), classpath);
desc = new JARDesc(jarUrl, null, null, false, true, false, true);
} catch (MalformedURLException mfe) {
throw new ClassNotFoundException(name, mfe);
}
addNewJar(desc);
}
result = loadClassExt(name);
return result;
} catch (ClassNotFoundException cnfe1) {
if (JNLPRuntime.isDebug()) {
cnfe1.printStackTrace();
}
}
// As a last resort, look in any available indexes
// Currently this loads jars directly from the site. We cannot cache it because this
// call is initiated from within the applet, which does not have disk read/write permissions
for (JarIndex index : jarIndexes) {
// Non-generic code in sun.misc.JarIndex
@SuppressWarnings("unchecked")
LinkedList<String> jarList = index.get(name.replace('.', '/'));
if (jarList != null) {
for (String jarName : jarList) {
JARDesc desc;
try {
desc = new JARDesc(new URL(file.getCodeBase(), jarName),
null, null, false, true, false, true);
} catch (MalformedURLException mfe) {
throw new ClassNotFoundException(name);
}
try {
addNewJar(desc);
} catch (Exception e) {
if (JNLPRuntime.isDebug()) {
e.printStackTrace();
}
}
}
// If it still fails, let it error out
result = loadClassExt(name);
}
}
}
}
if (result == null) {
throw new ClassNotFoundException(name);
}
return result;
}
/**
* Adds a new JARDesc into this classloader.
* <p>
* This will add the JARDesc into the resourceTracker and block until it
* is downloaded.
* @param desc the JARDesc for the new jar
*/
private void addNewJar(final JARDesc desc) {
this.addNewJar(desc, JNLPRuntime.getDefaultUpdatePolicy());
}
/**
* Adds a new JARDesc into this classloader.
* @param desc the JARDesc for the new jar
* @param updatePolicy the UpdatePolicy for the resource
*/
private void addNewJar(final JARDesc desc, UpdatePolicy updatePolicy) {
available.add(desc);
tracker.addResource(desc.getLocation(),
desc.getVersion(),
null,
updatePolicy
);
// Give read permissions to the cached jar file
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
Permission p = CacheUtil.getReadPermission(desc.getLocation(),
desc.getVersion());
resourcePermissions.add(p);
return null;
}
});
final URL remoteURL = desc.getLocation();
final URL cachedUrl = tracker.getCacheURL(remoteURL); // blocks till download
available.remove(desc); // Resource downloaded. Remove from available list.
try {
// Verify if needed
final List<JARDesc> jars = new ArrayList<JARDesc>();
jars.add(desc);
// Decide what level of security this jar should have
// The verification and security setting functions rely on
// having AllPermissions as those actions normally happen
// during initialization. We therefore need to do those
// actions as privileged.
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
public Void run() throws Exception {
jcv.add(jars, tracker);
checkTrustWithUser();
final SecurityDesc security;
if (jcv.isFullySigned()) {
security = new SecurityDesc(file,
SecurityDesc.ALL_PERMISSIONS,
file.getCodeBase().getHost());
} else {
security = new SecurityDesc(file,
SecurityDesc.SANDBOX_PERMISSIONS,
file.getCodeBase().getHost());
}
jarLocationSecurityMap.put(remoteURL, security);
return null;
}
});
addURL(remoteURL);
CachedJarFileCallback.getInstance().addMapping(remoteURL, cachedUrl);
} catch (Exception e) {
// Do nothing. This code is called by loadClass which cannot
// throw additional exceptions. So instead, just ignore it.
// Exception => jar will not get added to classpath, which will
// result in CNFE from loadClass.
e.printStackTrace();
}
}
/**
* Find the class in this loader or any of its extension loaders.
*/
@Override
protected Class findClass(String name) throws ClassNotFoundException {
for (int i = 0; i < loaders.length; i++) {
try {
if (loaders[i] == this) {
final String fName = name;
return AccessController.doPrivileged(
new PrivilegedExceptionAction<Class<?>>() {
public Class<?> run() throws ClassNotFoundException {
return JNLPClassLoader.super.findClass(fName);
}
}, getAccessControlContextForClassLoading());
} else {
return loaders[i].findClass(name);
}
} catch (ClassNotFoundException ex) {
} catch (ClassFormatError cfe) {
cfe.printStackTrace();
} catch (PrivilegedActionException pae) {
} catch (NullJnlpFileException ex) {
throw new ClassNotFoundException(this.mainClass + " in main classloader ", ex);
}
}
// Try codebase loader
if (codeBaseLoader != null)
return codeBaseLoader.findClassNonRecursive(name);
// All else failed. Throw CNFE
throw new ClassNotFoundException(name);
}
/**
* Search for the class by incrementally adding resources to the
* classloader and its extension classloaders until the resource
* is found.
*/
private Class loadClassExt(String name) throws ClassNotFoundException {
// make recursive
addAvailable();
// find it
try {
return findClass(name);
} catch (ClassNotFoundException ex) {
}
// add resources until found
while (true) {
JNLPClassLoader addedTo = null;
try {
addedTo = addNextResource();
} catch (LaunchException e) {
/*
* This method will never handle any search for the main class
* [It is handled in initializeResources()]. Therefore, this
* exception will never be thrown here and is escaped
*/
throw new IllegalStateException(e);
}
if (addedTo == null)
throw new ClassNotFoundException(name);
try {
return addedTo.findClass(name);
} catch (ClassNotFoundException ex) {
}
}
}
/**
* Finds the resource in this, the parent, or the extension
* class loaders.
*
* @return a <code>URL</code> for the resource, or <code>null</code>
* if the resource could not be found.
*/
@Override
public URL findResource(String name) {
URL result = null;
try {
Enumeration<URL> e = findResources(name);
if (e.hasMoreElements()) {
result = e.nextElement();
}
} catch (IOException e) {
if (JNLPRuntime.isDebug()) {
e.printStackTrace();
}
}
// If result is still null, look in the codebase loader
if (result == null && codeBaseLoader != null)
result = codeBaseLoader.findResource(name);
return result;
}
/**
* Find the resources in this, the parent, or the extension
* class loaders. Load lazy resources if not found in current resources.
*/
@Override
public Enumeration<URL> findResources(String name) throws IOException {
Enumeration<URL> resources = findResourcesBySearching(name);
try {
// if not found, load all lazy resources; repeat search
while (!resources.hasMoreElements() && addNextResource() != null) {
resources = findResourcesBySearching(name);
}
} catch (LaunchException le) {
le.printStackTrace();
}
return resources;
}
/**
* Find the resources in this, the parent, or the extension
* class loaders.
*/
private Enumeration<URL> findResourcesBySearching(String name) throws IOException {
List<URL> resources = new ArrayList<URL>();
Enumeration<URL> e = null;
for (int i = 0; i < loaders.length; i++) {
// TODO check if this will blow up or not
// if loaders[1].getResource() is called, wont it call getResource() on
// the original caller? infinite recursion?
if (loaders[i] == this) {
final String fName = name;
try {
e = AccessController.doPrivileged(
new PrivilegedExceptionAction<Enumeration<URL>>() {
public Enumeration<URL> run() throws IOException {
return JNLPClassLoader.super.findResources(fName);
}
}, getAccessControlContextForClassLoading());
} catch (PrivilegedActionException pae) {
}
} else {
e = loaders[i].findResources(name);
}
final Enumeration<URL> fURLEnum = e;
try {
resources.addAll(AccessController.doPrivileged(
new PrivilegedExceptionAction<Collection<URL>>() {
public Collection<URL> run() {
List<URL> resources = new ArrayList<URL>();
while (fURLEnum != null && fURLEnum.hasMoreElements()) {
resources.add(fURLEnum.nextElement());
}
return resources;
}
}, getAccessControlContextForClassLoading()));
} catch (PrivilegedActionException pae) {
}
}
// Add resources from codebase (only if nothing was found above,
// otherwise the server will get hammered)
if (resources.isEmpty() && codeBaseLoader != null) {
e = codeBaseLoader.findResources(name);
while (e.hasMoreElements())
resources.add(e.nextElement());
}
return Collections.enumeration(resources);
}
/**
* Returns if the specified resource is available locally from a cached jar
*
* @param s The name of the resource
* @return Whether or not the resource is available locally
*/
public boolean resourceAvailableLocally(String s) {
return jarEntries.contains(s);
}
/**
* Adds whatever resources have already been downloaded in the
* background.
*/
protected void addAvailable() {
// go through available, check tracker for it and all of its
// part brothers being available immediately, add them.
for (int i = 1; i < loaders.length; i++) {
loaders[i].addAvailable();
}
}
/**
* Adds the next unused resource to the classloader. That
* resource and all those in the same part will be downloaded
* and added to the classloader before returning. If there are
* no more resources to add, the method returns immediately.
*
* @return the classloader that resources were added to, or null
* @throws LaunchException Thrown if the signed JNLP file, within the main jar, fails to be verified or does not match
*/
protected JNLPClassLoader addNextResource() throws LaunchException {
if (available.size() == 0) {
for (int i = 1; i < loaders.length; i++) {
JNLPClassLoader result = loaders[i].addNextResource();
if (result != null)
return result;
}
return null;
}
// add jar
List<JARDesc> jars = new ArrayList<JARDesc>();
jars.add(available.get(0));
fillInPartJars(jars);
checkForMain(jars);
activateJars(jars);
return this;
}
// this part compatibility with previous classloader
/**
* @deprecated
*/
@Deprecated
public String getExtensionName() {
String result = file.getInformation().getTitle();
if (result == null)
result = file.getInformation().getDescription();
if (result == null && file.getFileLocation() != null)
result = file.getFileLocation().toString();
if (result == null && file.getCodeBase() != null)
result = file.getCodeBase().toString();
return result;
}
/**
* @deprecated
*/
@Deprecated
public String getExtensionHREF() {
return file.getFileLocation().toString();
}
public boolean getSigning() {
return signing;
}
protected SecurityDesc getSecurity() {
return security;
}
/**
* Returns the security descriptor for given code source URL
*
* @param source the origin (remote) url of the code
* @return The SecurityDescriptor for that source
*/
protected SecurityDesc getCodeSourceSecurity(URL source) {
SecurityDesc sec=jarLocationSecurityMap.get(source);
if (sec == null && !alreadyTried.contains(source)) {
alreadyTried.add(source);
//try to load the jar which is requesting the permissions, but was NOT downloaded by standard way
if (JNLPRuntime.isDebug()) {
System.out.println("Application is trying to get permissions for " + source.toString() + ", which was not added by standard way. Trying to download and verify!");
}
try {
JARDesc des = new JARDesc(source, null, null, false, false, false, false);
addNewJar(des);
sec = jarLocationSecurityMap.get(source);
} catch (Throwable t) {
if (JNLPRuntime.isDebug()) {
t.printStackTrace();
}
sec = null;
}
}
if (sec == null){
System.out.println(Translator.R("LNoSecInstance",source.toString()));
}
return sec;
}
/**
* Merges the code source/security descriptor mapping from another loader
*
* @param extLoader The loader form which to merge
* @throws SecurityException if the code is called from an untrusted source
*/
private void merge(JNLPClassLoader extLoader) {
try {
System.getSecurityManager().checkPermission(new AllPermission());
} catch (SecurityException se) {
throw new SecurityException("JNLPClassLoader() may only be called from trusted sources!");
}
// jars
for (URL u : extLoader.getURLs())
addURL(u);
// Codebase
addToCodeBaseLoader(extLoader.file.getCodeBase());
// native search paths
for (File nativeDirectory : extLoader.getNativeDirectories())
addNativeDirectory(nativeDirectory);
// security descriptors
for (URL key : extLoader.jarLocationSecurityMap.keySet()) {
jarLocationSecurityMap.put(key, extLoader.jarLocationSecurityMap.get(key));
}
}
/**
* Adds the given path to the path loader
*
* @param URL the path to add
* @throws IllegalArgumentException If the given url is not a path
*/
private void addToCodeBaseLoader(URL u) {
if (u == null) {
return;
}
// Only paths may be added
if (!u.getFile().endsWith("/")) {
throw new IllegalArgumentException("addToPathLoader only accepts path based URLs");
}
// If there is no loader yet, create one, else add it to the
// existing one (happens when called from merge())
if (codeBaseLoader == null) {
codeBaseLoader = new CodeBaseClassLoader(new URL[] { u }, this);
} else {
codeBaseLoader.addURL(u);
}
}
private DownloadOptions getDownloadOptionsForJar(JARDesc jar) {
return file.getDownloadOptionsForJar(jar);
}
/**
* Returns a set of paths that indicate the Class-Path entries in the
* manifest file. The paths are rooted in the same directory as the
* originalJarPath.
* @param mf the manifest
* @param originalJarPath the remote/original path of the jar containing
* the manifest
* @return a Set of String where each string is a path to the jar on
* the original jar's classpath.
*/
private Set<String> getClassPathsFromManifest(Manifest mf, String originalJarPath) {
Set<String> result = new HashSet<String>();
if (mf != null) {
// extract the Class-Path entries from the manifest and split them
String classpath = mf.getMainAttributes().getValue("Class-Path");
if (classpath == null || classpath.trim().length() == 0) {
return result;
}
String[] paths = classpath.split(" +");
for (String path : paths) {
if (path.trim().length() == 0) {
continue;
}
// we want to search for jars in the same subdir on the server
// as the original jar that contains the manifest file, so find
// out its subdirectory and use that as the dir
String dir = "";
int lastSlash = originalJarPath.lastIndexOf("/");
if (lastSlash != -1) {
dir = originalJarPath.substring(0, lastSlash + 1);
}
String fullPath = dir + path;
result.add(fullPath);
}
}
return result;
}
/**
* Increments loader use count by 1
*
* @throws SecurityException if caller is not trusted
*/
private void incrementLoaderUseCount() {
// For use by trusted code only
if (System.getSecurityManager() != null)
System.getSecurityManager().checkPermission(new AllPermission());
// NB: There will only ever be one class-loader per unique-key
synchronized ( getUniqueKeyLock(file.getUniqueKey()) ){
useCount++;
}
}
/**
* Returns all loaders that this loader uses, including itself
*/
JNLPClassLoader[] getLoaders() {
return loaders;
}
/**
* Remove jars from the file system.
*
* @param jars Jars marked for removal.
*/
void removeJars(JARDesc[] jars) {
for (JARDesc eachJar : jars) {
try {
tracker.removeResource(eachJar.getLocation());
} catch (Exception e) {
if (JNLPRuntime.isDebug()) {
System.err.println(e.getMessage());
System.err.println("Failed to remove resource from tracker, continuing..");
}
}
File cachedFile = CacheUtil.getCacheFile(eachJar.getLocation(), null);
String directoryUrl = CacheUtil.getCacheParentDirectory(cachedFile.getAbsolutePath());
File directory = new File(directoryUrl);
if (JNLPRuntime.isDebug())
System.out.println("Deleting cached file: " + cachedFile.getAbsolutePath());
cachedFile.delete();
if (JNLPRuntime.isDebug())
System.out.println("Deleting cached directory: " + directory.getAbsolutePath());
directory.delete();
}
}
/**
* Downloads and initializes jars into this loader.
*
* @param ref Path of the launch or extension JNLP File containing the
* resource. If null, main JNLP's file location will be used instead.
* @param part The name of the path.
* @throws LaunchException
*/
void initializeNewJarDownload(URL ref, String part, Version version) {
JARDesc[] jars = ManageJnlpResources.findJars(this, ref, part, version);
for (JARDesc eachJar : jars) {
if (JNLPRuntime.isDebug())
System.out.println("Downloading and initializing jar: " + eachJar.getLocation().toString());
this.addNewJar(eachJar, UpdatePolicy.FORCE);
}
}
/**
* Manages DownloadService jars which are not mentioned in the JNLP file
* @param ref Path to the resource.
* @param version The version of resource. If null, no version is specified.
* @param action The action to perform with the resource. Either DOWNLOADTOCACHE, REMOVEFROMCACHE, or CHECKCACHE.
* @return true if CHECKCACHE and the resource is cached.
*/
boolean manageExternalJars(URL ref, String version, DownloadAction action) {
boolean approved = false;
JNLPClassLoader foundLoader = LocateJnlpClassLoader.getLoaderByResourceUrl(this, ref, version);
Version resourceVersion = (version == null) ? null : new Version(version);
if (foundLoader != null)
approved = true;
else if (ref.toString().startsWith(file.getCodeBase().toString()))
approved = true;
else if (SecurityDesc.ALL_PERMISSIONS.equals(security.getSecurityType()))
approved = true;
if (approved) {
if (foundLoader == null)
foundLoader = this;
if (action == DownloadAction.DOWNLOAD_TO_CACHE) {
JARDesc jarToCache = new JARDesc(ref, resourceVersion, null, false, true, false, true);
if (JNLPRuntime.isDebug())
System.out.println("Downloading and initializing jar: " + ref.toString());
foundLoader.addNewJar(jarToCache, UpdatePolicy.FORCE);
} else if (action == DownloadAction.REMOVE_FROM_CACHE) {
JARDesc[] jarToRemove = { new JARDesc(ref, resourceVersion, null, false, true, false, true) };
foundLoader.removeJars(jarToRemove);
} else if (action == DownloadAction.CHECK_CACHE) {
return CacheUtil.isCached(ref, resourceVersion);
}
}
return false;
}
/**
* Decrements loader use count by 1
*
* If count reaches 0, loader is removed from list of available loaders
*
* @throws SecurityException if caller is not trusted
*/
public void decrementLoaderUseCount() {
// For use by trusted code only
if (System.getSecurityManager() != null)
System.getSecurityManager().checkPermission(new AllPermission());
String uniqueKey = file.getUniqueKey();
// NB: There will only ever be one class-loader per unique-key
synchronized ( getUniqueKeyLock(uniqueKey) ) {
useCount--;
if (useCount <= 0) {
uniqueKeyToLoader.remove(uniqueKey);
}
}
}
/**
* Returns an appropriate AccessControlContext for loading classes in
* the running instance.
*
* The default context during class-loading only allows connection to
* codebase. However applets are allowed to load jars from arbitrary
* locations and the codebase only access falls short if a class from
* one location needs a class from another.
*
* Given protected access since CodeBaseClassloader uses this function too.
*
* @return The appropriate AccessControlContext for loading classes for this instance
*/
public AccessControlContext getAccessControlContextForClassLoading() {
AccessControlContext context = AccessController.getContext();
try {
context.checkPermission(new AllPermission());
return context; // If context already has all permissions, don't bother
} catch (AccessControlException ace) {
// continue below
}
// Since this is for class-loading, technically any class from one jar
// should be able to access a class from another, therefore making the
// original context code source irrelevant
PermissionCollection permissions = this.security.getSandBoxPermissions();
// Local cache access permissions
for (Permission resourcePermission : resourcePermissions) {
permissions.add(resourcePermission);
}
// Permissions for all remote hosting urls
for (URL u: jarLocationSecurityMap.keySet()) {
permissions.add(new SocketPermission(u.getHost(),
"connect, accept"));
}
// Permissions for codebase urls (if there is a loader)
if (codeBaseLoader != null) {
for (URL u : codeBaseLoader.getURLs()) {
permissions.add(new SocketPermission(u.getHost(),
"connect, accept"));
}
}
ProtectionDomain pd = new ProtectionDomain(null, permissions);
return new AccessControlContext(new ProtectionDomain[] { pd });
}
/*
* Helper class to expose protected URLClassLoader methods.
*/
public static class CodeBaseClassLoader extends URLClassLoader {
JNLPClassLoader parentJNLPClassLoader;
/**
* Classes that are not found, so that findClass can skip them next time
*/
ConcurrentHashMap<String, URL[]> notFoundResources = new ConcurrentHashMap<String, URL[]>();
public CodeBaseClassLoader(URL[] urls, JNLPClassLoader cl) {
super(urls, cl);
parentJNLPClassLoader = cl;
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
Class<?> findClassNonRecursive(String name) throws ClassNotFoundException {
// If we have searched this path before, don't try again
if (Arrays.equals(super.getURLs(), notFoundResources.get(name)))
throw new ClassNotFoundException(name);
try {
final String fName = name;
return AccessController.doPrivileged(
new PrivilegedExceptionAction<Class<?>>() {
public Class<?> run() throws ClassNotFoundException {
return CodeBaseClassLoader.super.findClass(fName);
}
}, parentJNLPClassLoader.getAccessControlContextForClassLoading());
} catch (PrivilegedActionException pae) {
notFoundResources.put(name, super.getURLs());
throw new ClassNotFoundException("Could not find class " + name, pae);
} catch (NullJnlpFileException njf) {
notFoundResources.put(name, super.getURLs());
throw new ClassNotFoundException("Could not find class " + name, njf);
}
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
// Calls JNLPClassLoader#findClass which may call into this.findClassNonRecursive
return getParentJNLPClassLoader().findClass(name);
}
/**
* Returns the output of super.findLoadedClass().
*
* The method is renamed because ClassLoader.findLoadedClass() is final
*
* @param name The name of the class to find
* @return Output of ClassLoader.findLoadedClass() which is the class if found, null otherwise
* @see java.lang.ClassLoader#findLoadedClass(String)
*/
public Class<?> findLoadedClassFromParent(String name) {
return findLoadedClass(name);
}
/**
* Returns JNLPClassLoader that encompasses this loader
*
* @return parent JNLPClassLoader
*/
public JNLPClassLoader getParentJNLPClassLoader() {
return parentJNLPClassLoader;
}
@Override
public Enumeration<URL> findResources(String name) throws IOException {
// If we have searched this path before, don't try again
if (Arrays.equals(super.getURLs(), notFoundResources.get(name)))
return (new Vector<URL>(0)).elements();
if (!name.startsWith("META-INF")) {
Enumeration<URL> urls = super.findResources(name);
if (!urls.hasMoreElements()) {
notFoundResources.put(name, super.getURLs());
}
return urls;
}
return (new Vector<URL>(0)).elements();
}
@Override
public URL findResource(String name) {
// If we have searched this path before, don't try again
if (Arrays.equals(super.getURLs(), notFoundResources.get(name)))
return null;
URL url = null;
if (!name.startsWith("META-INF")) {
try {
final String fName = name;
url = AccessController.doPrivileged(
new PrivilegedExceptionAction<URL>() {
public URL run() {
return CodeBaseClassLoader.super.findResource(fName);
}
}, parentJNLPClassLoader.getAccessControlContextForClassLoading());
} catch (PrivilegedActionException pae) {
}
if (url == null) {
notFoundResources.put(name, super.getURLs());
}
return url;
}
return null;
}
}
}
|