Search in sources :

Example 1 with SystemBundleLoader

use of org.eclipse.osgi.internal.loader.SystemBundleLoader in project rt.equinox.framework by eclipse.

the class PackageSource method isServiceAssignableTo.

/**
 * Used by ServiceReferenceImpl for isAssignableTo
 * @param registrant Bundle registering service
 * @param client Bundle desiring to use service
 * @param className class name to use
 * @param serviceClass class of original service object
 * @param container the equinox container
 * @return true if assignable given package wiring
 */
public static boolean isServiceAssignableTo(Bundle registrant, Bundle client, String className, Class<?> serviceClass, EquinoxContainer container) {
    // 1) if the registrant == client always return true
    if (registrant == client) {
        return true;
    }
    // 2) get the package name from the specified className
    String pkgName = BundleLoader.getPackageName(className);
    if (// $NON-NLS-1$
    pkgName.startsWith("java."))
        return true;
    BundleLoader producerBL = getBundleLoader(registrant);
    if (producerBL == null)
        return false;
    BundleLoader consumerBL = getBundleLoader(client);
    if (consumerBL == null)
        return false;
    // 3) for the specified bundle, find the wiring for the package.  If no wiring is found return true
    PackageSource consumerSource = consumerBL.getPackageSource(pkgName);
    if (consumerSource == null)
        return true;
    // work around the issue when the package is in the EE and we delegate to boot for that package
    if (container.isBootDelegationPackage(pkgName)) {
        Bundle systemBundle = container.getStorage().getModuleContainer().getModule(0).getBundle();
        SystemBundleLoader systemLoader = (SystemBundleLoader) getBundleLoader(systemBundle);
        if (systemLoader.isExportedPackage(pkgName)) {
            // in this case we have a common source from the EE
            return true;
        }
    }
    // 4) For the registrant bundle, find the wiring for the package.
    PackageSource producerSource = producerBL.getPackageSource(pkgName);
    if (producerSource == null) {
        if (serviceClass != null && ServiceFactory.class.isAssignableFrom(serviceClass)) {
            @SuppressWarnings("deprecation") Bundle bundle = container.getPackageAdmin().getBundle(serviceClass);
            if (bundle != null && bundle != registrant)
                // bug 326918
                return true;
        }
        // 5) If no wiring is found for the registrant bundle then find the wiring for the classloader of the service object.  If no wiring is found return false.
        producerSource = getPackageSource(serviceClass, pkgName, container.getPackageAdmin());
        if (producerSource == null)
            return false;
    }
    // 6) If the two wirings found are equal then return true; otherwise return false.
    return producerSource.hasCommonSource(consumerSource);
}
Also used : ServiceFactory(org.osgi.framework.ServiceFactory) EquinoxBundle(org.eclipse.osgi.internal.framework.EquinoxBundle) Bundle(org.osgi.framework.Bundle) SystemBundleLoader(org.eclipse.osgi.internal.loader.SystemBundleLoader) SystemBundleLoader(org.eclipse.osgi.internal.loader.SystemBundleLoader) BundleLoader(org.eclipse.osgi.internal.loader.BundleLoader)

Aggregations

EquinoxBundle (org.eclipse.osgi.internal.framework.EquinoxBundle)1 BundleLoader (org.eclipse.osgi.internal.loader.BundleLoader)1 SystemBundleLoader (org.eclipse.osgi.internal.loader.SystemBundleLoader)1 Bundle (org.osgi.framework.Bundle)1 ServiceFactory (org.osgi.framework.ServiceFactory)1