Example #1
0
  public FWProps(Map initProps, FrameworkContext fwCtx) {
    // Add explicitly given properties.
    props.putAll(initProps);

    // Setup Debug as early as possible.
    fwCtx.debug = new Debug(this);

    // Setup default (launch) OSGi properties, see OSGi R4 v4.2 sec 4.2.2
    initProperties(fwCtx);

    // Setup default KF framework properties
    initKFProperties();

    // Set up some instance variables that depends on the properties
    SETCONTEXTCLASSLOADER = getBooleanProperty(SETCONTEXTCLASSLOADER_PROP);
    REGISTERSERVICEURLHANDLER = getBooleanProperty(REGISTERSERVICEURLHANDLER_PROP);
    STRICTBOOTCLASSLOADING = getBooleanProperty(STRICTBOOTCLASSLOADING_PROP);
    isDoubleCheckedLockingSafe = getBooleanProperty(IS_DOUBLECHECKED_LOCKING_SAFE_PROP);
  }
 public boolean isAssignableTo(Bundle bundle, String className) {
   final BundleImpl sBundle = registration.bundle;
   if (sBundle == null) {
     throw new IllegalStateException("Service is unregistered");
   }
   final FrameworkContext fwCtx = sBundle.fwCtx;
   if (((BundleImpl) bundle).fwCtx != fwCtx) {
     throw new IllegalArgumentException("Bundle is not from same framework as service");
   }
   // Check if bootdelegated
   if (fwCtx.isBootDelegated(className)) {
     return true;
   }
   final int pos = className.lastIndexOf('.');
   if (pos != -1) {
     final String name = className.substring(0, pos);
     final Pkg p = fwCtx.resolver.getPkg(name);
     // Is package exported by a bundle
     if (p != null) {
       final BundlePackages rbp = sBundle.current().bpkgs;
       final BundlePackages pkgExporter = rbp.getProviderBundlePackages(name);
       List<BundleGeneration> pkgProvider;
       if (pkgExporter == null) {
         // Package not imported by provide, is it required
         pkgProvider = rbp.getRequiredBundleGenerations(name);
       } else {
         pkgProvider = new ArrayList<BundleGeneration>(1);
         pkgProvider.add(pkgExporter.bg);
       }
       final BundlePackages bb = ((BundleImpl) bundle).current().bpkgs;
       final BundlePackages bbp = bb.getProviderBundlePackages(name);
       List<BundleGeneration> pkgConsumer;
       if (bbp == null) {
         // Package not imported by bundle, is it required
         pkgConsumer = bb.getRequiredBundleGenerations(name);
       } else {
         pkgConsumer = new ArrayList<BundleGeneration>(1);
         pkgConsumer.add(bbp.bg);
       }
       if (pkgConsumer == null) {
         // NYI! Check dynamic import?
         if (bb.isExported(name)) {
           // If bundle only exports package, then return true if
           // bundle is provider.
           return pkgProvider != null ? pkgProvider.contains(bb.bg) : true;
         } else {
           // If bundle doesn't import or export package, then return true and
           // assume that the bundle only uses reflection to access service.
           return true;
         }
       } else if (pkgProvider == null) {
         // Package not imported by registrar. E.g. proxy registration.
         final Object sService = registration.service;
         if (sService == null) {
           throw new IllegalStateException("Service is unregistered");
         }
         if (p.providers.size() == 1) {
           // Only one version available, allow.
           return true;
         } else if (sService instanceof ServiceFactory) {
           // Factory, allow.
           return true;
         } else {
           // Use the classloader of bundle to load the class, then check
           // if the service's class is assignable.
           final ClassLoader bCL = bb.getClassLoader();
           if (bCL != null) {
             try {
               final Class<?> bCls = bCL.loadClass(className);
               // NYI, Handle Service Factories.
               return bCls.isAssignableFrom(sService.getClass());
             } catch (final Exception e) {
               // If we can not load, assume that we are just a proxy.
               return true;
             }
           }
         }
         // Fallback: Always OK when singleton provider of the package
       } else { // Package imported by both parties
         // Return true if we have same provider as service.
         for (final Object element : pkgProvider) {
           if (pkgConsumer.contains(element)) {
             return true;
           }
         }
       }
     } else {
       // Not a package under package control. System package?
       if (name.startsWith("java.") || sBundle == bundle) {
         return true;
       } else {
         // NYI! We have a private service, check if bundle can use it.
         // Now, allow it to handle reflection of service.
         return true;
       }
     }
   }
   return false;
 }