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
|
/*******************************************************************************
* Copyright (c) 2004, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.loader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import org.eclipse.osgi.framework.adaptor.*;
import org.eclipse.osgi.framework.debug.Debug;
import org.eclipse.osgi.framework.internal.core.*;
import org.eclipse.osgi.framework.internal.core.Constants;
import org.eclipse.osgi.framework.util.KeyedElement;
import org.eclipse.osgi.framework.util.KeyedHashSet;
import org.eclipse.osgi.internal.loader.buddy.PolicyHandler;
import org.eclipse.osgi.internal.resolver.StateBuilder;
import org.eclipse.osgi.service.resolver.*;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.*;
import org.osgi.framework.wiring.BundleWiring;
/**
* This object is responsible for all classloader delegation for a bundle.
* It represents the loaded state of the bundle. BundleLoader objects
* are created lazily; care should be taken not to force the creation
* of a BundleLoader unless it is necessary.
* @see org.eclipse.osgi.internal.loader.BundleLoaderProxy
*/
public class BundleLoader implements ClassLoaderDelegate {
public final static String DEFAULT_PACKAGE = "."; //$NON-NLS-1$
public final static String JAVA_PACKAGE = "java."; //$NON-NLS-1$
public final static byte FLAG_IMPORTSINIT = 0x01;
public final static byte FLAG_HASDYNAMICIMPORTS = 0x02;
public final static byte FLAG_HASDYNAMICEIMPORTALL = 0x04;
public final static byte FLAG_CLOSED = 0x08;
public final static byte FLAG_LAZYTRIGGER = 0x10;
public final static ClassContext CLASS_CONTEXT = AccessController.doPrivileged(new PrivilegedAction<ClassContext>() {
public ClassContext run() {
return new ClassContext();
}
});
public final static ClassLoader FW_CLASSLOADER = getClassLoader(Framework.class);
private static final int PRE_CLASS = 1;
private static final int POST_CLASS = 2;
private static final int PRE_RESOURCE = 3;
private static final int POST_RESOURCE = 4;
private static final int PRE_RESOURCES = 5;
private static final int POST_RESOURCES = 6;
private static final int PRE_LIBRARY = 7;
private static final int POST_LIBRARY = 8;
/* the proxy */
final private BundleLoaderProxy proxy;
/* Bundle object */
final BundleHost bundle;
final private PolicyHandler policy;
/* List of package names that are exported by this BundleLoader */
final private Collection<String> exportedPackages;
final private Collection<String> substitutedPackages;
/* List of required bundle BundleLoaderProxy objects */
final BundleLoaderProxy[] requiredBundles;
/* List of indexes into the requiredBundles list of reexported bundles */
final int[] reexportTable;
/* cache of required package sources. Key is packagename, value is PackageSource */
final private KeyedHashSet requiredSources;
// note that the following non-final must be access using synchronization
/* cache of imported packages. Key is packagename, Value is PackageSource */
private KeyedHashSet importedSources;
/* If not null, list of package stems to import dynamically. */
private String[] dynamicImportPackageStems;
/* If not null, list of package names to import dynamically. */
private String[] dynamicImportPackages;
/* loader flags */
private byte loaderFlags = 0;
/* The is the BundleClassLoader for the bundle */
private BundleClassLoader classloader;
private ClassLoader parent;
/**
* Returns the package name from the specified class name.
* The returned package is dot seperated.
*
* @param name Name of a class.
* @return Dot separated package name or null if the class
* has no package name.
*/
public final static String getPackageName(String name) {
if (name != null) {
int index = name.lastIndexOf('.'); /* find last period in class name */
if (index > 0)
return name.substring(0, index);
}
return DEFAULT_PACKAGE;
}
/**
* Returns the package name from the specified resource name.
* The returned package is dot seperated.
*
* @param name Name of a resource.
* @return Dot separated package name or null if the resource
* has no package name.
*/
public final static String getResourcePackageName(String name) {
if (name != null) {
/* check for leading slash*/
int begin = ((name.length() > 1) && (name.charAt(0) == '/')) ? 1 : 0;
int end = name.lastIndexOf('/'); /* index of last slash */
if (end > begin)
return name.substring(begin, end).replace('/', '.');
}
return DEFAULT_PACKAGE;
}
/**
* BundleLoader runtime constructor. This object is created lazily
* when the first request for a resource is made to this bundle.
*
* @param bundle Bundle object for this loader.
* @param proxy the BundleLoaderProxy for this loader.
* @exception org.osgi.framework.BundleException
*/
protected BundleLoader(BundleHost bundle, BundleLoaderProxy proxy) throws BundleException {
this.bundle = bundle;
this.proxy = proxy;
try {
bundle.getBundleData().open(); /* make sure the BundleData is open */
} catch (IOException e) {
throw new BundleException(Msg.BUNDLE_READ_EXCEPTION, e);
}
BundleDescription description = proxy.getBundleDescription();
// init the require bundles list.
BundleDescription[] required = description.getResolvedRequires();
if (required.length > 0) {
// get a list of re-exported symbolic names
Set<String> reExportSet = new HashSet<String>(required.length);
BundleSpecification[] requiredSpecs = description.getRequiredBundles();
if (requiredSpecs != null && requiredSpecs.length > 0)
for (int i = 0; i < requiredSpecs.length; i++)
if (requiredSpecs[i].isExported())
reExportSet.add(requiredSpecs[i].getName());
requiredBundles = new BundleLoaderProxy[required.length];
int[] reexported = new int[required.length];
int reexportIndex = 0;
for (int i = 0; i < required.length; i++) {
requiredBundles[i] = getLoaderProxy(required[i]);
if (reExportSet.contains(required[i].getSymbolicName()))
reexported[reexportIndex++] = i;
}
if (reexportIndex > 0) {
reexportTable = new int[reexportIndex];
System.arraycopy(reexported, 0, reexportTable, 0, reexportIndex);
} else {
reexportTable = null;
}
requiredSources = new KeyedHashSet(10, false);
} else {
requiredBundles = null;
reexportTable = null;
requiredSources = null;
}
// init the provided packages set
ExportPackageDescription[] exports = description.getSelectedExports();
if (exports != null && exports.length > 0) {
exportedPackages = Collections.synchronizedCollection(exports.length > 10 ? new HashSet<String>(exports.length) : new ArrayList<String>(exports.length));
initializeExports(exports, exportedPackages);
} else {
exportedPackages = Collections.synchronizedCollection(new ArrayList<String>(0));
}
ExportPackageDescription substituted[] = description.getSubstitutedExports();
if (substituted.length > 0) {
substitutedPackages = substituted.length > 10 ? new HashSet<String>(substituted.length) : new ArrayList<String>(substituted.length);
for (int i = 0; i < substituted.length; i++)
substitutedPackages.add(substituted[i].getName());
} else {
substitutedPackages = null;
}
//This is the fastest way to access to the description for fragments since the hostdescription.getFragments() is slow
BundleFragment[] fragmentObjects = bundle.getFragments();
BundleDescription[] fragments = new BundleDescription[fragmentObjects == null ? 0 : fragmentObjects.length];
for (int i = 0; i < fragments.length; i++)
fragments[i] = fragmentObjects[i].getBundleDescription();
// init the dynamic imports tables
if (description.hasDynamicImports())
addDynamicImportPackage(description.getImportPackages());
// ...and its fragments
for (int i = 0; i < fragments.length; i++)
if (fragments[i].isResolved() && fragments[i].hasDynamicImports())
addDynamicImportPackage(fragments[i].getImportPackages());
//Initialize the policy handler
String buddyList = null;
try {
buddyList = bundle.getBundleData().getManifest().get(Constants.BUDDY_LOADER);
} catch (BundleException e) {
// do nothing; buddyList == null
}
policy = buddyList != null ? new PolicyHandler(this, buddyList, bundle.getFramework().getPackageAdmin()) : null;
if (policy != null)
policy.open(bundle.getFramework().getSystemBundleContext());
}
private void initializeExports(ExportPackageDescription[] exports, Collection<String> exportNames) {
for (int i = 0; i < exports.length; i++) {
if (proxy.forceSourceCreation(exports[i])) {
if (!exportNames.contains(exports[i].getName())) {
// must force filtered and reexport sources to be created early
// to prevent lazy normal package source creation.
// We only do this for the first export of a package name.
proxy.createPackageSource(exports[i], true);
}
}
exportNames.add(exports[i].getName());
}
}
public synchronized KeyedHashSet getImportedSources(KeyedHashSet visited) {
if ((loaderFlags & FLAG_IMPORTSINIT) != 0)
return importedSources;
BundleDescription bundleDesc = proxy.getBundleDescription();
ExportPackageDescription[] packages = bundleDesc.getResolvedImports();
if (packages != null && packages.length > 0) {
if (importedSources == null)
importedSources = new KeyedHashSet(packages.length, false);
for (int i = 0; i < packages.length; i++) {
if (packages[i].getExporter() == bundleDesc)
continue; // ignore imports resolved to this bundle
PackageSource source = createExportPackageSource(packages[i], visited);
if (source != null)
importedSources.add(source);
}
}
loaderFlags |= FLAG_IMPORTSINIT;
return importedSources;
}
public synchronized boolean isLazyTriggerSet() {
return (loaderFlags & FLAG_LAZYTRIGGER) != 0;
}
public void setLazyTrigger() throws BundleException {
synchronized (this) {
loaderFlags |= FLAG_LAZYTRIGGER;
}
BundleLoaderProxy.secureAction.start(bundle, Bundle.START_TRANSIENT | BundleHost.LAZY_TRIGGER);
}
final PackageSource createExportPackageSource(ExportPackageDescription export, KeyedHashSet visited) {
BundleLoaderProxy exportProxy = getLoaderProxy(export.getExporter());
if (exportProxy == null)
// TODO log error!!
return null;
PackageSource requiredSource = exportProxy.getBundleLoader().findRequiredSource(export.getName(), visited);
PackageSource exportSource = exportProxy.createPackageSource(export, false);
if (requiredSource == null)
return exportSource;
return createMultiSource(export.getName(), new PackageSource[] {requiredSource, exportSource});
}
private static PackageSource createMultiSource(String packageName, PackageSource[] sources) {
if (sources.length == 1)
return sources[0];
List<SingleSourcePackage> sourceList = new ArrayList<SingleSourcePackage>(sources.length);
for (int i = 0; i < sources.length; i++) {
SingleSourcePackage[] innerSources = sources[i].getSuppliers();
for (int j = 0; j < innerSources.length; j++)
if (!sourceList.contains(innerSources[j]))
sourceList.add(innerSources[j]);
}
return new MultiSourcePackage(packageName, sourceList.toArray(new SingleSourcePackage[sourceList.size()]));
}
/*
* get the loader proxy for a bundle description
*/
public final BundleLoaderProxy getLoaderProxy(BundleDescription source) {
Object userObject = source.getUserObject();
if (!(userObject instanceof BundleLoaderProxy)) {
// may need to force the proxy to be created
long exportingID = source.getBundleId();
BundleHost exportingBundle = (BundleHost) bundle.getFramework().getBundle(exportingID);
if (exportingBundle == null)
return null;
userObject = exportingBundle.getLoaderProxy();
}
return (BundleLoaderProxy) userObject;
}
public BundleLoaderProxy getLoaderProxy() {
return proxy;
}
/*
* Close the the BundleLoader.
*
*/
synchronized void close() {
if ((loaderFlags & FLAG_CLOSED) != 0)
return;
if (classloader != null)
classloader.close();
if (policy != null)
policy.close(bundle.getFramework().getSystemBundleContext());
loaderFlags |= FLAG_CLOSED; /* This indicates the BundleLoader is destroyed */
}
/**
* This method loads a class from the bundle. The class is searched for in the
* same manner as it would if it was being loaded from a bundle (i.e. all
* hosts, fragments, import, required bundles and local resources are searched.
*
* @param name the name of the desired Class.
* @return the resulting Class
* @exception java.lang.ClassNotFoundException if the class definition was not found.
*/
final public Class<?> loadClass(String name) throws ClassNotFoundException {
BundleClassLoader bcl = createClassLoader();
// The instanceof check here is just to be safe. The javadoc contract stated in BundleClassLoader
// mandate that BundleClassLoaders be an instance of ClassLoader.
if (name.length() > 0 && name.charAt(0) == '[' && bcl instanceof ClassLoader)
return Class.forName(name, false, (ClassLoader) bcl);
return bcl.loadClass(name);
}
/**
* This method gets a resource from the bundle. The resource is searched
* for in the same manner as it would if it was being loaded from a bundle
* (i.e. all hosts, fragments, import, required bundles and
* local resources are searched).
*
* @param name the name of the desired resource.
* @return the resulting resource URL or null if it does not exist.
*/
final URL getResource(String name) {
return createClassLoader().getResource(name);
}
public final synchronized ClassLoader getParentClassLoader() {
if (parent != null)
return parent;
createClassLoader();
return parent;
}
final public synchronized BundleClassLoader createClassLoader() {
if (classloader != null)
return classloader;
String[] classpath;
try {
classpath = bundle.getBundleData().getClassPath();
} catch (BundleException e) {
// no classpath
classpath = new String[0];
bundle.getFramework().publishFrameworkEvent(FrameworkEvent.ERROR, bundle, e);
}
if (classpath == null) {
// no classpath
classpath = new String[0];
bundle.getFramework().publishFrameworkEvent(FrameworkEvent.ERROR, bundle, new BundleException(Msg.BUNDLE_NO_CLASSPATH_MATCH, BundleException.MANIFEST_ERROR));
}
BundleClassLoader bcl = createBCLPrevileged(bundle.getProtectionDomain(), classpath);
parent = getParentPrivileged(bcl);
classloader = bcl;
return classloader;
}
/**
* Finds a class local to this bundle. Only the classloader for this bundle is searched.
* @param name The name of the class to find.
* @return The loaded Class or null if the class is not found.
* @throws ClassNotFoundException
*/
Class<?> findLocalClass(String name) throws ClassNotFoundException {
if (Debug.DEBUG_LOADER)
Debug.println("BundleLoader[" + this + "].findLocalClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
try {
Class<?> clazz = createClassLoader().findLocalClass(name);
if (Debug.DEBUG_LOADER && clazz != null)
Debug.println("BundleLoader[" + this + "] found local class " + name); //$NON-NLS-1$ //$NON-NLS-2$
return clazz;
} catch (ClassNotFoundException e) {
if (e instanceof StatusException) {
if ((((StatusException) e).getStatusCode() & StatusException.CODE_ERROR) != 0)
throw e;
}
return null;
}
}
/**
* Finds the class for a bundle. This method is used for delegation by the bundle's classloader.
*/
public Class<?> findClass(String name) throws ClassNotFoundException {
return findClass(name, true);
}
Class<?> findClass(String name, boolean checkParent) throws ClassNotFoundException {
ClassLoader parentCL = getParentClassLoader();
if (checkParent && parentCL != null && name.startsWith(JAVA_PACKAGE))
// 1) if startsWith "java." delegate to parent and terminate search
// we want to throw ClassNotFoundExceptions if a java.* class cannot be loaded from the parent.
return parentCL.loadClass(name);
return findClassInternal(name, checkParent, parentCL);
}
private Class<?> findClassInternal(String name, boolean checkParent, ClassLoader parentCL) throws ClassNotFoundException {
if (Debug.DEBUG_LOADER)
Debug.println("BundleLoader[" + this + "].loadBundleClass(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String pkgName = getPackageName(name);
boolean bootDelegation = false;
// follow the OSGi delegation model
if (checkParent && parentCL != null && bundle.getFramework().isBootDelegationPackage(pkgName))
// 2) if part of the bootdelegation list then delegate to parent and continue of failure
try {
return parentCL.loadClass(name);
} catch (ClassNotFoundException cnfe) {
// we want to continue
bootDelegation = true;
}
Class<?> result = null;
try {
result = (Class<?>) searchHooks(name, PRE_CLASS);
} catch (ClassNotFoundException e) {
throw e;
} catch (FileNotFoundException e) {
// will not happen
}
if (result != null)
return result;
// 3) search the imported packages
PackageSource source = findImportedSource(pkgName, null);
if (source != null) {
// 3) found import source terminate search at the source
result = source.loadClass(name);
if (result != null)
return result;
throw new ClassNotFoundException(name + " cannot be found by " + this); //$NON-NLS-1$
}
// 4) search the required bundles
source = findRequiredSource(pkgName, null);
if (source != null)
// 4) attempt to load from source but continue on failure
result = source.loadClass(name);
// 5) search the local bundle
if (result == null)
result = findLocalClass(name);
if (result != null)
return result;
// 6) attempt to find a dynamic import source; only do this if a required source was not found
if (source == null) {
source = findDynamicSource(pkgName);
if (source != null) {
result = source.loadClass(name);
if (result != null)
return result;
// must throw CNFE if dynamic import source does not have the class
throw new ClassNotFoundException(name + " cannot be found by " + this); //$NON-NLS-1$
}
}
if (result == null)
try {
result = (Class<?>) searchHooks(name, POST_CLASS);
} catch (ClassNotFoundException e) {
throw e;
} catch (FileNotFoundException e) {
// will not happen
}
// do buddy policy loading
if (result == null && policy != null)
result = policy.doBuddyClassLoading(name);
if (result != null)
return result;
// hack to support backwards compatibiility for bootdelegation
// or last resort; do class context trick to work around VM bugs
if (parentCL != null && !bootDelegation && ((checkParent && bundle.getFramework().compatibiltyBootDelegation) || isRequestFromVM()))
// we don't need to continue if a CNFE is thrown here.
try {
return parentCL.loadClass(name);
} catch (ClassNotFoundException e) {
// we want to generate our own exception below
}
throw new ClassNotFoundException(name + " cannot be found by " + this); //$NON-NLS-1$
}
@SuppressWarnings("unchecked")
private <E> E searchHooks(String name, int type) throws ClassNotFoundException, FileNotFoundException {
ClassLoaderDelegateHook[] delegateHooks = bundle.getFramework().getDelegateHooks();
if (delegateHooks == null)
return null;
E result = null;
for (int i = 0; i < delegateHooks.length && result == null; i++) {
switch (type) {
case PRE_CLASS :
result = (E) delegateHooks[i].preFindClass(name, createClassLoader(), bundle.getBundleData());
break;
case POST_CLASS :
result = (E) delegateHooks[i].postFindClass(name, createClassLoader(), bundle.getBundleData());
break;
case PRE_RESOURCE :
result = (E) delegateHooks[i].preFindResource(name, createClassLoader(), bundle.getBundleData());
break;
case POST_RESOURCE :
result = (E) delegateHooks[i].postFindResource(name, createClassLoader(), bundle.getBundleData());
break;
case PRE_RESOURCES :
result = (E) delegateHooks[i].preFindResources(name, createClassLoader(), bundle.getBundleData());
break;
case POST_RESOURCES :
result = (E) delegateHooks[i].postFindResources(name, createClassLoader(), bundle.getBundleData());
break;
case PRE_LIBRARY :
result = (E) delegateHooks[i].preFindLibrary(name, createClassLoader(), bundle.getBundleData());
break;
case POST_LIBRARY :
result = (E) delegateHooks[i].postFindLibrary(name, createClassLoader(), bundle.getBundleData());
break;
}
}
return result;
}
private boolean isRequestFromVM() {
if (bundle.getFramework().isBootDelegationPackage("*") || !bundle.getFramework().contextBootDelegation) //$NON-NLS-1$
return false;
// works around VM bugs that require all classloaders to have access to parent packages
Class<?>[] context = CLASS_CONTEXT.getClassContext();
if (context == null || context.length < 2)
return false;
// skip the first class; it is the ClassContext class
for (int i = 1; i < context.length; i++)
// find the first class in the context which is not BundleLoader or instanceof ClassLoader
if (context[i] != BundleLoader.class && !ClassLoader.class.isAssignableFrom(context[i]) && !context[i].getName().equals("java.lang.J9VMInternals")) { //$NON-NLS-1$
// only find in parent if the class is not "Class" (Class#forName case) or if the class is not loaded with a BundleClassLoader
ClassLoader cl = getClassLoader(context[i]);
if (cl != FW_CLASSLOADER) { // extra check incase an adaptor adds another class into the stack besides an instance of ClassLoader
if (Class.class != context[i] && !(cl instanceof BundleClassLoader))
return true;
break;
}
}
return false;
}
private static ClassLoader getClassLoader(final Class<?> clazz) {
if (System.getSecurityManager() == null)
return clazz.getClassLoader();
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return clazz.getClassLoader();
}
});
}
/**
* Finds the resource for a bundle. This method is used for delegation by the bundle's classloader.
*/
public URL findResource(String name) {
return findResource(name, true);
}
URL findResource(String name, boolean checkParent) {
if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */
name = name.substring(1); /* remove leading slash before search */
String pkgName = getResourcePackageName(name);
boolean bootDelegation = false;
ClassLoader parentCL = getParentClassLoader();
// follow the OSGi delegation model
// First check the parent classloader for system resources, if it is a java resource.
if (checkParent && parentCL != null) {
if (pkgName.startsWith(JAVA_PACKAGE))
// 1) if startsWith "java." delegate to parent and terminate search
// we never delegate java resource requests past the parent
return parentCL.getResource(name);
else if (bundle.getFramework().isBootDelegationPackage(pkgName)) {
// 2) if part of the bootdelegation list then delegate to parent and continue of failure
URL result = parentCL.getResource(name);
if (result != null)
return result;
bootDelegation = true;
}
}
URL result = null;
try {
result = (URL) searchHooks(name, PRE_RESOURCE);
} catch (FileNotFoundException e) {
return null;
} catch (ClassNotFoundException e) {
// will not happen
}
if (result != null)
return result;
// 3) search the imported packages
PackageSource source = findImportedSource(pkgName, null);
if (source != null)
// 3) found import source terminate search at the source
return source.getResource(name);
// 4) search the required bundles
source = findRequiredSource(pkgName, null);
if (source != null)
// 4) attempt to load from source but continue on failure
result = source.getResource(name);
// 5) search the local bundle
if (result == null)
result = findLocalResource(name);
if (result != null)
return result;
// 6) attempt to find a dynamic import source; only do this if a required source was not found
if (source == null) {
source = findDynamicSource(pkgName);
if (source != null)
// must return the result of the dynamic import and do not continue
return source.getResource(name);
}
if (result == null)
try {
result = (URL) searchHooks(name, POST_RESOURCE);
} catch (FileNotFoundException e) {
return null;
} catch (ClassNotFoundException e) {
// will not happen
}
// do buddy policy loading
if (result == null && policy != null)
result = policy.doBuddyResourceLoading(name);
if (result != null)
return result;
// hack to support backwards compatibiility for bootdelegation
// or last resort; do class context trick to work around VM bugs
if (parentCL != null && !bootDelegation && ((checkParent && bundle.getFramework().compatibiltyBootDelegation) || isRequestFromVM()))
// we don't need to continue if the resource is not found here
return parentCL.getResource(name);
return result;
}
/**
* Finds the resources for a bundle. This method is used for delegation by the bundle's classloader.
*/
public Enumeration<URL> findResources(String name) throws IOException {
// do not delegate to parent because ClassLoader#getResources already did and it is final!!
if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */
name = name.substring(1); /* remove leading slash before search */
String pkgName = getResourcePackageName(name);
Enumeration<URL> result = null;
try {
result = searchHooks(name, PRE_RESOURCES);
} catch (ClassNotFoundException e) {
// will not happen
} catch (FileNotFoundException e) {
return null;
}
if (result != null)
return result;
// start at step 3 because of the comment above about ClassLoader#getResources
// 3) search the imported packages
PackageSource source = findImportedSource(pkgName, null);
if (source != null)
// 3) found import source terminate search at the source
return source.getResources(name);
// 4) search the required bundles
source = findRequiredSource(pkgName, null);
if (source != null)
// 4) attempt to load from source but continue on failure
result = source.getResources(name);
// 5) search the local bundle
// compound the required source results with the local ones
Enumeration<URL> localResults = findLocalResources(name);
result = compoundEnumerations(result, localResults);
// 6) attempt to find a dynamic import source; only do this if a required source was not found
if (result == null && source == null) {
source = findDynamicSource(pkgName);
if (source != null)
return source.getResources(name);
}
if (result == null)
try {
result = searchHooks(name, POST_RESOURCES);
} catch (ClassNotFoundException e) {
// will not happen
} catch (FileNotFoundException e) {
return null;
}
if (policy != null) {
Enumeration<URL> buddyResult = policy.doBuddyResourcesLoading(name);
result = compoundEnumerations(result, buddyResult);
}
return result;
}
private boolean isSubPackage(String parentPackage, String subPackage) {
String prefix = (parentPackage.length() == 0 || parentPackage.equals(DEFAULT_PACKAGE)) ? "" : parentPackage + '.'; //$NON-NLS-1$
return subPackage.startsWith(prefix);
}
public Collection<String> listResources(String path, String filePattern, int options) {
String pkgName = getResourcePackageName(path.endsWith("/") ? path : path + '/'); //$NON-NLS-1$
if ((path.length() > 1) && (path.charAt(0) == '/')) /* if name has a leading slash */
path = path.substring(1); /* remove leading slash before search */
boolean subPackages = (options & BundleWiring.LISTRESOURCES_RECURSE) != 0;
List<String> packages = new ArrayList<String>();
// search imported package names
KeyedHashSet importSources = getImportedSources(null);
if (importSources != null) {
KeyedElement[] imports = importSources.elements();
for (KeyedElement keyedElement : imports) {
String id = ((PackageSource) keyedElement).getId();
if (id.equals(pkgName) || (subPackages && isSubPackage(pkgName, id)))
packages.add(id);
}
}
// now add package names from required bundles
if (requiredBundles != null) {
KeyedHashSet visited = new KeyedHashSet(false);
visited.add(bundle); // always add ourselves so we do not recurse back to ourselves
for (BundleLoaderProxy requiredProxy : requiredBundles) {
BundleLoader requiredLoader = requiredProxy.getBundleLoader();
requiredLoader.addProvidedPackageNames(requiredProxy.getSymbolicName(), pkgName, packages, subPackages, visited);
}
}
boolean localSearch = (options & BundleWiring.LISTRESOURCES_LOCAL) != 0;
// Use LinkedHashSet for optimized performance of contains() plus
// ordering guarantees.
LinkedHashSet<String> result = new LinkedHashSet<String>();
Set<String> importedPackages = new HashSet<String>(0);
for (String name : packages) {
// look for import source
PackageSource externalSource = findImportedSource(name, null);
if (externalSource != null) {
// record this package is imported
importedPackages.add(name);
} else {
// look for require bundle source
externalSource = findRequiredSource(name, null);
}
// only add the content of the external source if this is not a localSearch
if (externalSource != null && !localSearch) {
String packagePath = name.replace('.', '/');
Collection<String> externalResources = externalSource.listResources(packagePath, filePattern);
for (String resource : externalResources) {
// Duplicates avoided by using a set.
result.add(resource);
}
}
}
// now search locally
Collection<String> localResources = createClassLoader().listLocalResources(path, filePattern, options);
for (String resource : localResources) {
String resourcePkg = getResourcePackageName(resource);
if (!importedPackages.contains(resourcePkg)) // Duplicates avoided by using a set.
result.add(resource);
}
return result;
}
/*
* This method is used by Bundle.getResources to do proper parent delegation.
*/
public Enumeration<URL> getResources(String name) throws IOException {
if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */
name = name.substring(1); /* remove leading slash before search */
String pkgName = getResourcePackageName(name);
// follow the OSGi delegation model
// First check the parent classloader for system resources, if it is a java resource.
Enumeration<URL> result = null;
if (pkgName.startsWith(JAVA_PACKAGE) || bundle.getFramework().isBootDelegationPackage(pkgName)) {
// 1) if startsWith "java." delegate to parent and terminate search
// 2) if part of the bootdelegation list then delegate to parent and continue of failure
ClassLoader parentCL = getParentClassLoader();
result = parentCL == null ? null : parentCL.getResources(name);
if (pkgName.startsWith(JAVA_PACKAGE))
return result;
}
return compoundEnumerations(result, findResources(name));
}
public static <E> Enumeration<E> compoundEnumerations(Enumeration<E> list1, Enumeration<E> list2) {
if (list2 == null || !list2.hasMoreElements())
return list1;
if (list1 == null || !list1.hasMoreElements())
return list2;
List<E> compoundResults = new ArrayList<E>();
while (list1.hasMoreElements())
compoundResults.add(list1.nextElement());
while (list2.hasMoreElements()) {
E item = list2.nextElement();
if (!compoundResults.contains(item)) //don't add duplicates
compoundResults.add(item);
}
return Collections.enumeration(compoundResults);
}
/**
* Finds a resource local to this bundle. Only the classloader for this bundle is searched.
* @param name The name of the resource to find.
* @return The URL to the resource or null if the resource is not found.
*/
URL findLocalResource(final String name) {
return createClassLoader().findLocalResource(name);
}
/**
* Returns an Enumeration of URLs representing all the resources with
* the given name. Only the classloader for this bundle is searched.
*
* @param name the resource name
* @return an Enumeration of URLs for the resources
*/
Enumeration<URL> findLocalResources(String name) {
return createClassLoader().findLocalResources(name);
}
/**
* Returns the absolute path name of a native library.
*
* @param name the library name
* @return the absolute path of the native library or null if not found
*/
public String findLibrary(final String name) {
if (System.getSecurityManager() == null)
return findLocalLibrary(name);
return AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return findLocalLibrary(name);
}
});
}
final String findLocalLibrary(final String name) {
String result = null;
try {
result = (String) searchHooks(name, PRE_LIBRARY);
} catch (FileNotFoundException e) {
return null;
} catch (ClassNotFoundException e) {
// will not happen
}
if (result != null)
return result;
result = bundle.getBundleData().findLibrary(name);
if (result != null)
return result;
// look in fragments imports ...
BundleFragment[] fragments = bundle.getFragments();
if (fragments != null)
for (int i = 0; i < fragments.length; i++) {
result = fragments[i].getBundleData().findLibrary(name);
if (result != null)
return result;
}
try {
return (String) searchHooks(name, POST_LIBRARY);
} catch (FileNotFoundException e) {
return null; // this is not necessary; but being consistent in case another step is added below
} catch (ClassNotFoundException e) {
// will not happen
}
return null;
}
/*
* Return the bundle we are associated with.
*/
public final AbstractBundle getBundle() {
return bundle;
}
private BundleClassLoader createBCLPrevileged(final BundleProtectionDomain pd, final String[] cp) {
// Create the classloader as previleged code if security manager is present.
if (System.getSecurityManager() == null)
return createBCL(pd, cp);
return AccessController.doPrivileged(new PrivilegedAction<BundleClassLoader>() {
public BundleClassLoader run() {
return createBCL(pd, cp);
}
});
}
BundleClassLoader createBCL(final BundleProtectionDomain pd, final String[] cp) {
BundleClassLoader bcl = bundle.getBundleData().createClassLoader(BundleLoader.this, pd, cp);
// attach existing fragments to classloader
BundleFragment[] fragments = bundle.getFragments();
if (fragments != null)
for (int i = 0; i < fragments.length; i++) {
try {
bcl.attachFragment(fragments[i].getBundleData(), fragments[i].getProtectionDomain(), fragments[i].getBundleData().getClassPath());
} catch (BundleException be) {
bundle.getFramework().publishFrameworkEvent(FrameworkEvent.ERROR, bundle, be);
}
}
// finish the initialization of the classloader.
bcl.initialize();
return bcl;
}
/**
* Return a string representation of this loader.
* @return String
*/
public final String toString() {
BundleData result = bundle.getBundleData();
return result == null ? "BundleLoader.bundledata == null!" : result.toString(); //$NON-NLS-1$
}
/**
* Return true if the target package name matches
* a name in the DynamicImport-Package manifest header.
*
* @param pkgname The name of the requested class' package.
* @return true if the package should be imported.
*/
private final synchronized boolean isDynamicallyImported(String pkgname) {
if (this instanceof SystemBundleLoader)
return false; // system bundle cannot dynamically import
// must check for startsWith("java.") to satisfy R3 section 4.7.2
if (pkgname.startsWith("java.")) //$NON-NLS-1$
return true;
/* quick shortcut check */
if ((loaderFlags & FLAG_HASDYNAMICIMPORTS) == 0)
return false;
/* "*" shortcut */
if ((loaderFlags & FLAG_HASDYNAMICEIMPORTALL) != 0)
return true;
/* match against specific names */
if (dynamicImportPackages != null)
for (int i = 0; i < dynamicImportPackages.length; i++)
if (pkgname.equals(dynamicImportPackages[i]))
return true;
/* match against names with trailing wildcards */
if (dynamicImportPackageStems != null)
for (int i = 0; i < dynamicImportPackageStems.length; i++)
if (pkgname.startsWith(dynamicImportPackageStems[i]))
return true;
return false;
}
final void addExportedProvidersFor(String symbolicName, String packageName, List<PackageSource> result, KeyedHashSet visited) {
if (!visited.add(bundle))
return;
// See if we locally provide the package.
PackageSource local = null;
if (isExportedPackage(packageName))
local = proxy.getPackageSource(packageName);
else if (isSubstitutedExport(packageName)) {
result.add(findImportedSource(packageName, visited));
return; // should not continue to required bundles in this case
}
// Must search required bundles that are exported first.
if (requiredBundles != null) {
int size = reexportTable == null ? 0 : reexportTable.length;
int reexportIndex = 0;
for (int i = 0; i < requiredBundles.length; i++) {
if (local != null) {
// always add required bundles first if we locally provide the package
// This allows a bundle to provide a package from a required bundle without
// re-exporting the whole required bundle.
requiredBundles[i].getBundleLoader().addExportedProvidersFor(symbolicName, packageName, result, visited);
} else if (reexportIndex < size && reexportTable[reexportIndex] == i) {
reexportIndex++;
requiredBundles[i].getBundleLoader().addExportedProvidersFor(symbolicName, packageName, result, visited);
}
}
}
// now add the locally provided package.
if (local != null && local.isFriend(symbolicName))
result.add(local);
}
final void addProvidedPackageNames(String symbolicName, String packageName, List<String> result, boolean subPackages, KeyedHashSet visitied) {
if (!visitied.add(bundle))
return;
for (String exported : exportedPackages) {
if (exported.equals(packageName) || (subPackages && isSubPackage(packageName, exported))) {
if (!result.contains(exported))
result.add(exported);
}
}
if (substitutedPackages != null)
for (String substituted : substitutedPackages) {
if (substituted.equals(packageName) || (subPackages && isSubPackage(packageName, substituted))) {
if (!result.contains(substituted))
result.add(substituted);
}
}
if (requiredBundles != null) {
int size = reexportTable == null ? 0 : reexportTable.length;
int reexportIndex = 0;
for (int i = 0; i < requiredBundles.length; i++) {
if (reexportIndex < size && reexportTable[reexportIndex] == i) {
reexportIndex++;
requiredBundles[i].getBundleLoader().addProvidedPackageNames(symbolicName, packageName, result, subPackages, visitied);
}
}
}
}
final boolean isExportedPackage(String name) {
return exportedPackages.contains(name);
}
final boolean isSubstitutedExport(String name) {
return substitutedPackages == null ? false : substitutedPackages.contains(name);
}
private void addDynamicImportPackage(ImportPackageSpecification[] packages) {
if (packages == null)
return;
List<String> dynamicImports = new ArrayList<String>(packages.length);
for (int i = 0; i < packages.length; i++)
if (ImportPackageSpecification.RESOLUTION_DYNAMIC.equals(packages[i].getDirective(Constants.RESOLUTION_DIRECTIVE)))
dynamicImports.add(packages[i].getName());
if (dynamicImports.size() > 0)
addDynamicImportPackage(dynamicImports.toArray(new String[dynamicImports.size()]));
}
/**
* Adds a list of DynamicImport-Package manifest elements to the dynamic
* import tables of this BundleLoader. Duplicate packages are checked and
* not added again. This method is not thread safe. Callers should ensure
* synchronization when calling this method.
* @param packages the DynamicImport-Package elements to add.
*/
private void addDynamicImportPackage(String[] packages) {
if (packages == null)
return;
loaderFlags |= FLAG_HASDYNAMICIMPORTS;
int size = packages.length;
List<String> stems;
if (dynamicImportPackageStems == null) {
stems = new ArrayList<String>(size);
} else {
stems = new ArrayList<String>(size + dynamicImportPackageStems.length);
for (int i = 0; i < dynamicImportPackageStems.length; i++) {
stems.add(dynamicImportPackageStems[i]);
}
}
List<String> names;
if (dynamicImportPackages == null) {
names = new ArrayList<String>(size);
} else {
names = new ArrayList<String>(size + dynamicImportPackages.length);
for (int i = 0; i < dynamicImportPackages.length; i++) {
names.add(dynamicImportPackages[i]);
}
}
for (int i = 0; i < size; i++) {
String name = packages[i];
if (isDynamicallyImported(name))
continue;
if (name.equals("*")) { /* shortcut *///$NON-NLS-1$
loaderFlags |= FLAG_HASDYNAMICEIMPORTALL;
return;
}
if (name.endsWith(".*")) //$NON-NLS-1$
stems.add(name.substring(0, name.length() - 1));
else
names.add(name);
}
size = stems.size();
if (size > 0)
dynamicImportPackageStems = stems.toArray(new String[size]);
size = names.size();
if (size > 0)
dynamicImportPackages = names.toArray(new String[size]);
}
/**
* Adds a list of DynamicImport-Package manifest elements to the dynamic
* import tables of this BundleLoader. Duplicate packages are checked and
* not added again.
* @param packages the DynamicImport-Package elements to add.
*/
public final synchronized void addDynamicImportPackage(ManifestElement[] packages) {
if (packages == null)
return;
List<String> dynamicImports = new ArrayList<String>(packages.length);
List<ImportPackageSpecification> dynamicImportSpecs = new ArrayList<ImportPackageSpecification>(packages.length);
for (ManifestElement dynamicImportElement : packages) {
String[] names = dynamicImportElement.getValueComponents();
for (String name : names)
dynamicImports.add(name);
StateBuilder.addImportPackages(dynamicImportElement, dynamicImportSpecs, 2, true);
}
if (dynamicImports.size() > 0) {
addDynamicImportPackage(dynamicImports.toArray(new String[dynamicImports.size()]));
BundleDescription revision = getLoaderProxy().getBundleDescription();
State state = revision.getContainingState();
state.addDynamicImportPackages(revision, dynamicImportSpecs.toArray(new ImportPackageSpecification[dynamicImportSpecs.size()]));
}
}
synchronized public void attachFragment(BundleFragment fragment) throws BundleException {
ExportPackageDescription[] exports = proxy.getBundleDescription().getSelectedExports();
if (classloader == null) {
initializeExports(exports, exportedPackages);
return;
}
String[] classpath = fragment.getBundleData().getClassPath();
if (classpath != null)
classloader.attachFragment(fragment.getBundleData(), fragment.getProtectionDomain(), classpath);
initializeExports(exports, exportedPackages);
}
/*
* Finds a packagesource that is either imported or required from another bundle.
* This will not include an local package source
*/
private PackageSource findSource(String pkgName) {
if (pkgName == null)
return null;
PackageSource result = findImportedSource(pkgName, null);
if (result != null)
return result;
// Note that dynamic imports are not checked to avoid aggressive wiring (bug 105779)
return findRequiredSource(pkgName, null);
}
private PackageSource findImportedSource(String pkgName, KeyedHashSet visited) {
KeyedHashSet imports = getImportedSources(visited);
if (imports == null)
return null;
synchronized (imports) {
return (PackageSource) imports.getByKey(pkgName);
}
}
private PackageSource findDynamicSource(String pkgName) {
if (isDynamicallyImported(pkgName)) {
ExportPackageDescription exportPackage = bundle.getFramework().getAdaptor().getState().linkDynamicImport(proxy.getBundleDescription(), pkgName);
if (exportPackage != null) {
PackageSource source = createExportPackageSource(exportPackage, null);
synchronized (this) {
if (importedSources == null)
importedSources = new KeyedHashSet(false);
}
synchronized (importedSources) {
importedSources.add(source);
}
return source;
}
}
return null;
}
private PackageSource findRequiredSource(String pkgName, KeyedHashSet visited) {
if (requiredBundles == null)
return null;
synchronized (requiredSources) {
PackageSource result = (PackageSource) requiredSources.getByKey(pkgName);
if (result != null)
return result.isNullSource() ? null : result;
}
if (visited == null)
visited = new KeyedHashSet(false);
visited.add(bundle); // always add ourselves so we do not recurse back to ourselves
List<PackageSource> result = new ArrayList<PackageSource>(3);
for (int i = 0; i < requiredBundles.length; i++) {
BundleLoader requiredLoader = requiredBundles[i].getBundleLoader();
requiredLoader.addExportedProvidersFor(proxy.getSymbolicName(), pkgName, result, visited);
}
// found some so cache the result for next time and return
PackageSource source;
if (result.size() == 0) {
// did not find it in our required bundles lets record the failure
// so we do not have to do the search again for this package.
source = NullPackageSource.getNullPackageSource(pkgName);
} else if (result.size() == 1) {
// if there is just one source, remember just the single source
source = result.get(0);
} else {
// if there was more than one source, build a multisource and cache that.
PackageSource[] srcs = result.toArray(new PackageSource[result.size()]);
source = createMultiSource(pkgName, srcs);
}
synchronized (requiredSources) {
requiredSources.add(source);
}
return source.isNullSource() ? null : source;
}
/*
* Gets the package source for the pkgName. This will include the local package source
* if the bundle exports the package. This is used to compare the PackageSource of a
* package from two different bundles.
*/
public final PackageSource getPackageSource(String pkgName) {
PackageSource result = findSource(pkgName);
if (!isExportedPackage(pkgName))
return result;
// if the package is exported then we need to get the local source
PackageSource localSource = proxy.getPackageSource(pkgName);
if (result == null)
return localSource;
if (localSource == null)
return result;
return createMultiSource(pkgName, new PackageSource[] {result, localSource});
}
private ClassLoader getParentPrivileged(final BundleClassLoader bcl) {
if (System.getSecurityManager() == null)
return bcl.getParent();
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return bcl.getParent();
}
});
}
static final class ClassContext extends SecurityManager {
// need to make this method public
public Class<?>[] getClassContext() {
return super.getClassContext();
}
}
static public void closeBundleLoader(BundleLoaderProxy proxy) {
if (proxy == null)
return;
// First close the BundleLoader
BundleLoader loader = proxy.getBasicBundleLoader();
if (loader != null)
loader.close();
proxy.setStale();
// if proxy is not null then make sure to unset user object
// associated with the proxy in the state
BundleDescription description = proxy.getBundleDescription();
// must set it back to the bundle object; not null
// need to make sure the user object is a BundleReference
description.setUserObject(proxy.getBundleData());
}
}
|