1 11 package org.eclipse.osgi.internal.module; 12 13 import java.security.Permission ; 14 import org.eclipse.osgi.service.resolver.*; 15 import org.osgi.framework.*; 16 17 public class PermissionChecker { 18 private BundleContext context; 19 private boolean checkPermissions = false; 20 private ResolverImpl resolver; 21 22 public PermissionChecker(BundleContext context, boolean checkPermissions, ResolverImpl resolver) { 23 this.context = context; 24 this.checkPermissions = checkPermissions && context != null; this.resolver = resolver; 26 } 27 28 32 public boolean checkPermission(VersionConstraint vc, BaseDescription bd) { 33 if (!checkPermissions) 34 return true; 35 boolean success = false; 36 Permission producerPermission = null, consumerPermission = null; 37 Bundle producer = null, consumer = null; 38 int errorType = 0; 39 if (vc instanceof ImportPackageSpecification) { 40 errorType = ResolverError.IMPORT_PACKAGE_PERMISSION; 41 producerPermission = new PackagePermission(bd.getName(), PackagePermission.EXPORT); 42 consumerPermission = new PackagePermission(vc.getName(), PackagePermission.IMPORT); 43 producer = context.getBundle(((ExportPackageDescription) bd).getExporter().getBundleId()); 44 } else { 45 boolean requireBundle = vc instanceof BundleSpecification; 46 errorType = requireBundle ? ResolverError.REQUIRE_BUNDLE_PERMISSION : ResolverError.FRAGMENT_BUNDLE_PERMISSION; 47 producerPermission = new BundlePermission(bd.getName(), requireBundle ? BundlePermission.PROVIDE : BundlePermission.HOST); 48 consumerPermission = new BundlePermission(vc.getName(), requireBundle ? BundlePermission.REQUIRE : BundlePermission.FRAGMENT); 49 producer = context.getBundle(((BundleDescription) bd).getBundleId()); 50 } 51 consumer = context.getBundle(vc.getBundle().getBundleId()); 52 if (producer != null && (producer.getState() & Bundle.UNINSTALLED) == 0) { 53 success = producer.hasPermission(producerPermission); 54 if (!success) { 55 switch (errorType) { 56 case ResolverError.IMPORT_PACKAGE_PERMISSION: 57 errorType = ResolverError.EXPORT_PACKAGE_PERMISSION; 58 break; 59 case ResolverError.REQUIRE_BUNDLE_PERMISSION: 60 case ResolverError.FRAGMENT_BUNDLE_PERMISSION: 61 errorType = errorType == ResolverError.REQUIRE_BUNDLE_PERMISSION ? ResolverError.PROVIDE_BUNDLE_PERMISSION : ResolverError.HOST_BUNDLE_PERMISSION; 62 break; 63 } 64 resolver.getState().addResolverError(vc.getBundle(), errorType, producerPermission.toString(), vc); 65 } 66 } 67 if (success && consumer != null && (consumer.getState() & Bundle.UNINSTALLED) == 0) { 68 success = consumer.hasPermission(consumerPermission); 69 if (!success) 70 resolver.getState().addResolverError(vc.getBundle(), errorType, consumerPermission.toString(), vc); 71 } 72 73 return success; 74 } 75 } 76 | Popular Tags |