Search in sources :

Example 1 with BundleSpecification

use of org.eclipse.osgi.service.resolver.BundleSpecification in project rt.equinox.framework by eclipse.

the class ResolverBundle method hasUnresolvedConstraint.

private boolean hasUnresolvedConstraint(ResolverConstraint reason, ResolverBundle detachedFragment, ResolverBundle remainingFragment, ResolverImport[] oldImports, BundleConstraint[] oldRequires, List<ResolverImport> additionalImports, List<BundleConstraint> additionalRequires) {
    ImportPackageSpecification[] remainingFragImports = remainingFragment.getBundleDescription().getImportPackages();
    BundleSpecification[] remainingFragRequires = remainingFragment.getBundleDescription().getRequiredBundles();
    VersionConstraint[] constraints;
    if (reason instanceof ResolverImport)
        constraints = remainingFragImports;
    else
        constraints = remainingFragRequires;
    for (int i = 0; i < constraints.length; i++) if (reason.getName().equals(constraints[i].getName())) {
        detachFragment(remainingFragment, reason);
        return true;
    }
    for (int i = 0; i < oldImports.length; i++) {
        if (oldImports[i].getVersionConstraint().getBundle() != detachedFragment.getBundleDescription())
            // the constraint is not from the detached fragment
            continue;
        for (int j = 0; j < remainingFragImports.length; j++) {
            if (oldImports[i].getName().equals(remainingFragImports[j].getName())) {
                // same constraint, must reuse the constraint object but swap out the fragment info
                additionalImports.add(oldImports[i]);
                oldImports[i].setVersionConstraint(remainingFragImports[j]);
                break;
            }
        }
    }
    for (int i = 0; i < oldRequires.length; i++) {
        if (oldRequires[i].getVersionConstraint().getBundle() != detachedFragment.getBundleDescription())
            // the constraint is not from the detached fragment
            continue;
        for (int j = 0; j < remainingFragRequires.length; j++) {
            if (oldRequires[i].getName().equals(remainingFragRequires[j].getName())) {
                // same constraint, must reuse the constraint object but swap out the fragment info
                additionalRequires.add(oldRequires[i]);
                oldRequires[i].setVersionConstraint(remainingFragRequires[j]);
                break;
            }
        }
    }
    return false;
}
Also used : VersionConstraint(org.eclipse.osgi.service.resolver.VersionConstraint) ImportPackageSpecification(org.eclipse.osgi.service.resolver.ImportPackageSpecification) BundleSpecification(org.eclipse.osgi.service.resolver.BundleSpecification) VersionConstraint(org.eclipse.osgi.service.resolver.VersionConstraint)

Example 2 with BundleSpecification

use of org.eclipse.osgi.service.resolver.BundleSpecification in project rt.equinox.framework by eclipse.

the class ResolverBundle method initialize.

void initialize(boolean useSelectedExports) {
    if (getBundleDescription().getHost() != null) {
        host = new BundleConstraint(this, getBundleDescription().getHost());
        exports = new ResolverExport[0];
        imports = new ResolverImport[0];
        requires = new BundleConstraint[0];
        GenericSpecification[] requirements = getBundleDescription().getGenericRequires();
        List<GenericConstraint> constraints = new ArrayList<>();
        for (GenericSpecification requirement : requirements) {
            if (StateImpl.OSGI_EE_NAMESPACE.equals(requirement.getType())) {
                constraints.add(new GenericConstraint(this, requirement, resolver.isDevelopmentMode()));
            }
        }
        genericReqiures = constraints.toArray(new GenericConstraint[constraints.size()]);
        GenericDescription[] capabilities = getBundleDescription().getGenericCapabilities();
        GenericCapability identity = null;
        for (GenericDescription capability : capabilities) {
            if (IdentityNamespace.IDENTITY_NAMESPACE.equals(capability.getType())) {
                identity = new GenericCapability(this, capability, resolver.isDevelopmentMode());
                break;
            }
        }
        genericCapabilities = identity == null ? new GenericCapability[0] : new GenericCapability[] { identity };
        return;
    }
    ImportPackageSpecification[] actualImports = getBundleDescription().getImportPackages();
    // Reorder imports so that optionals are at the end so that we wire statics before optionals
    List<ResolverImport> importList = new ArrayList<>(actualImports.length);
    for (int i = actualImports.length - 1; i >= 0; i--) if (ImportPackageSpecification.RESOLUTION_OPTIONAL.equals(actualImports[i].getDirective(Constants.RESOLUTION_DIRECTIVE)))
        importList.add(new ResolverImport(this, actualImports[i]));
    else
        importList.add(0, new ResolverImport(this, actualImports[i]));
    imports = importList.toArray(new ResolverImport[importList.size()]);
    ExportPackageDescription[] actualExports = useSelectedExports ? getBundleDescription().getSelectedExports() : getBundleDescription().getExportPackages();
    exports = new ResolverExport[actualExports.length];
    for (int i = 0; i < actualExports.length; i++) exports[i] = new ResolverExport(this, actualExports[i]);
    BundleSpecification[] actualRequires = getBundleDescription().getRequiredBundles();
    requires = new BundleConstraint[actualRequires.length];
    for (int i = 0; i < requires.length; i++) requires[i] = new BundleConstraint(this, actualRequires[i]);
    GenericSpecification[] actualGenericRequires = getBundleDescription().getGenericRequires();
    genericReqiures = new GenericConstraint[actualGenericRequires.length];
    for (int i = 0; i < genericReqiures.length; i++) {
        genericReqiures[i] = new GenericConstraint(this, actualGenericRequires[i], resolver.isDevelopmentMode());
    }
    GenericDescription[] actualCapabilities = useSelectedExports ? getBundleDescription().getSelectedGenericCapabilities() : getBundleDescription().getGenericCapabilities();
    genericCapabilities = new GenericCapability[actualCapabilities.length];
    for (int i = 0; i < genericCapabilities.length; i++) {
        genericCapabilities[i] = new GenericCapability(this, actualCapabilities[i], resolver.isDevelopmentMode());
    }
    fragments = null;
    fragmentExports = null;
    fragmentImports = null;
    fragmentRequires = null;
    fragmentGenericCapabilities = null;
    fragmentGenericRequires = null;
}
Also used : ExportPackageDescription(org.eclipse.osgi.service.resolver.ExportPackageDescription) ArrayList(java.util.ArrayList) GenericDescription(org.eclipse.osgi.service.resolver.GenericDescription) BundleSpecification(org.eclipse.osgi.service.resolver.BundleSpecification) VersionConstraint(org.eclipse.osgi.service.resolver.VersionConstraint) ImportPackageSpecification(org.eclipse.osgi.service.resolver.ImportPackageSpecification) GenericSpecification(org.eclipse.osgi.service.resolver.GenericSpecification)

Example 3 with BundleSpecification

use of org.eclipse.osgi.service.resolver.BundleSpecification in project rt.equinox.framework by eclipse.

the class ResolverImpl method stateResolveFragConstraints.

private void stateResolveFragConstraints(ResolverBundle rb) {
    ResolverBundle host = (ResolverBundle) rb.getHost().getSelectedSupplier();
    ImportPackageSpecification[] imports = rb.getBundleDescription().getImportPackages();
    for (int i = 0; i < imports.length; i++) {
        ResolverImport hostImport = host == null ? null : host.getImport(imports[i].getName());
        ResolverExport export = (ResolverExport) (hostImport == null ? null : hostImport.getSelectedSupplier());
        BaseDescription supplier = export == null ? null : export.getExportPackageDescription();
        state.resolveConstraint(imports[i], supplier);
    }
    BundleSpecification[] requires = rb.getBundleDescription().getRequiredBundles();
    for (int i = 0; i < requires.length; i++) {
        BundleConstraint hostRequire = host == null ? null : host.getRequire(requires[i].getName());
        ResolverBundle bundle = (ResolverBundle) (hostRequire == null ? null : hostRequire.getSelectedSupplier());
        BaseDescription supplier = bundle == null ? null : bundle.getBundleDescription();
        state.resolveConstraint(requires[i], supplier);
    }
    GenericConstraint[] genericRequires = rb.getGenericRequires();
    for (int i = 0; i < genericRequires.length; i++) {
        VersionSupplier[] matchingCapabilities = genericRequires[i].getMatchingCapabilities();
        if (matchingCapabilities == null)
            state.resolveConstraint(genericRequires[i].getVersionConstraint(), null);
        else
            for (int j = 0; j < matchingCapabilities.length; j++) state.resolveConstraint(genericRequires[i].getVersionConstraint(), matchingCapabilities[j].getBaseDescription());
    }
}
Also used : BaseDescription(org.eclipse.osgi.service.resolver.BaseDescription) BundleSpecification(org.eclipse.osgi.service.resolver.BundleSpecification) VersionConstraint(org.eclipse.osgi.service.resolver.VersionConstraint) ImportPackageSpecification(org.eclipse.osgi.service.resolver.ImportPackageSpecification)

Example 4 with BundleSpecification

use of org.eclipse.osgi.service.resolver.BundleSpecification in project rt.equinox.framework by eclipse.

the class ResolverBundle method attachFragment.

void attachFragment(ResolverBundle fragment, boolean dynamicAttach) {
    if (isFragment())
        // cannot attach to fragments;
        return;
    if (!getBundleDescription().attachFragments() || (isResolved() && !getBundleDescription().dynamicFragments()))
        // host is restricting attachment
        return;
    if (fragment.getHost().getNumPossibleSuppliers() > 0 && !((HostSpecification) fragment.getHost().getVersionConstraint()).isMultiHost())
        // fragment is restricting attachment
        return;
    ImportPackageSpecification[] newImports = fragment.getBundleDescription().getImportPackages();
    BundleSpecification[] newRequires = fragment.getBundleDescription().getRequiredBundles();
    ExportPackageDescription[] newExports = fragment.getBundleDescription().getExportPackages();
    GenericDescription[] newGenericCapabilities = fragment.getBundleDescription().getGenericCapabilities();
    GenericSpecification[] newGenericRequires = fragment.getBundleDescription().getGenericRequires();
    // if this is not during initialization then check if constraints conflict
    if (dynamicAttach && constraintsConflict(fragment.getBundleDescription(), newImports, newRequires, newGenericRequires))
        // do not allow fragments with conflicting constraints
        return;
    if (isResolved() && newExports.length > 0)
        fragment.setNewFragmentExports(true);
    initFragments();
    // already attached to this host
    for (Iterator<ResolverBundle> iFragments = fragments.iterator(); iFragments.hasNext(); ) {
        ResolverBundle existingFragment = iFragments.next();
        String bsn = existingFragment.getName();
        if (bsn != null && bsn.equals(fragment.getName()))
            return;
    }
    if (fragments.contains(fragment))
        return;
    fragments.add(fragment);
    fragment.getHost().addPossibleSupplier(this);
    if (newImports.length > 0) {
        ArrayList<ResolverImport> hostImports = new ArrayList<>(newImports.length);
        for (int i = 0; i < newImports.length; i++) if (!isImported(newImports[i].getName()))
            hostImports.add(new ResolverImport(this, newImports[i]));
        fragmentImports.put(fragment.bundleID, hostImports);
    }
    if (newRequires.length > 0) {
        ArrayList<BundleConstraint> hostRequires = new ArrayList<>(newRequires.length);
        for (int i = 0; i < newRequires.length; i++) if (!isRequired(newRequires[i].getName()))
            hostRequires.add(new BundleConstraint(this, newRequires[i]));
        fragmentRequires.put(fragment.bundleID, hostRequires);
    }
    if (newGenericRequires.length > 0) {
        ArrayList<GenericConstraint> hostGenericRequires = new ArrayList<>(newGenericRequires.length);
        for (int i = 0; i < newGenericRequires.length; i++) {
            // only add namespaces that are not osgi.ee
            if (!StateImpl.OSGI_EE_NAMESPACE.equals(newGenericRequires[i].getType())) {
                hostGenericRequires.add(new GenericConstraint(this, newGenericRequires[i], resolver.isDevelopmentMode()));
            }
        }
        if (!hostGenericRequires.isEmpty())
            fragmentGenericRequires.put(fragment.bundleID, hostGenericRequires);
    }
    ArrayList<ResolverExport> hostExports = new ArrayList<>(newExports.length);
    if (newExports.length > 0 && dynamicAttach) {
        for (int i = 0; i < newExports.length; i++) {
            ResolverExport[] currentExports = getExports(newExports[i].getName());
            boolean foundEquivalent = false;
            for (int j = 0; j < currentExports.length && !foundEquivalent; j++) {
                if (equivalentExports(currentExports[j], newExports[i]))
                    foundEquivalent = true;
            }
            if (!foundEquivalent) {
                ExportPackageDescription hostExport = new ExportPackageDescriptionImpl(getBundleDescription(), newExports[i]);
                hostExports.add(new ResolverExport(this, hostExport));
            }
        }
        fragmentExports.put(fragment.bundleID, hostExports);
    }
    List<GenericCapability> hostCapabilities = new ArrayList<>(newGenericCapabilities.length);
    if (newGenericCapabilities.length > 0 && dynamicAttach) {
        for (GenericDescription capability : newGenericCapabilities) {
            if (!IdentityNamespace.IDENTITY_NAMESPACE.equals(capability.getType())) {
                GenericDescription hostCapabililty = new GenericDescriptionImpl(getBundleDescription(), capability);
                hostCapabilities.add(new GenericCapability(this, hostCapabililty, resolver.isDevelopmentMode()));
            }
        }
        if (hostCapabilities.size() > 0) {
            fragmentGenericCapabilities.put(fragment.bundleID, hostCapabilities);
            if (isResolved())
                fragment.setNewFragmentCapabilities(true);
        }
    }
    if (dynamicAttach) {
        resolver.getResolverExports().put(hostExports.toArray(new ResolverExport[hostExports.size()]));
        resolver.addGenerics(hostCapabilities.toArray(new GenericCapability[hostCapabilities.size()]));
    }
}
Also used : HostSpecification(org.eclipse.osgi.service.resolver.HostSpecification) ExportPackageDescription(org.eclipse.osgi.service.resolver.ExportPackageDescription) ArrayList(java.util.ArrayList) GenericDescription(org.eclipse.osgi.service.resolver.GenericDescription) GenericDescriptionImpl(org.eclipse.osgi.internal.resolver.GenericDescriptionImpl) ExportPackageDescriptionImpl(org.eclipse.osgi.internal.resolver.ExportPackageDescriptionImpl) GenericSpecification(org.eclipse.osgi.service.resolver.GenericSpecification) BundleSpecification(org.eclipse.osgi.service.resolver.BundleSpecification) VersionConstraint(org.eclipse.osgi.service.resolver.VersionConstraint) ImportPackageSpecification(org.eclipse.osgi.service.resolver.ImportPackageSpecification)

Example 5 with BundleSpecification

use of org.eclipse.osgi.service.resolver.BundleSpecification in project rt.equinox.framework by eclipse.

the class GroupingChecker method createPackageRoots.

private PackageRoots createPackageRoots(ResolverBundle bundle, String packageName, List<ResolverBundle> visited) {
    if (visited.contains(bundle))
        return null;
    // prevent endless cycles
    visited.add(bundle);
    // check imports
    if (bundle.getBundleDescription().isResolved()) {
        // must check resolved imports to get any dynamically resolved imports
        ExportPackageDescription[] imports = bundle.getBundleDescription().getResolvedImports();
        for (ExportPackageDescription importPkg : imports) {
            if (importPkg.getExporter() == bundle.getBundleDescription() || !importPkg.getName().equals(packageName))
                continue;
            List<ResolverExport> exports = bundle.getResolver().getResolverExports().get(packageName);
            for (ResolverExport export : exports) {
                if (export.getExportPackageDescription() == importPkg)
                    return getPackageRoots(export.getExporter(), packageName, visited);
            }
        }
    } else {
        ResolverImport imported = bundle.getImport(packageName);
        if (imported != null && imported.getSelectedSupplier() != null) {
            // make sure we are not resolved to our own import
            ResolverExport selectedExport = (ResolverExport) imported.getSelectedSupplier();
            if (selectedExport.getExporter() != bundle) {
                // this is all the roots if the package is imported
                return getPackageRoots(selectedExport.getExporter(), packageName, visited);
            }
        }
    }
    // check if the bundle exports the package
    ResolverExport[] exports = bundle.getExports(packageName);
    List<PackageRoots> roots = new ArrayList<>(0);
    // check roots from required bundles
    BundleConstraint[] requires = bundle.getRequires();
    for (int i = 0; i < requires.length; i++) {
        ResolverBundle supplier = (ResolverBundle) requires[i].getSelectedSupplier();
        if (supplier == null)
            // no supplier, probably optional
            continue;
        if (supplier.getExport(packageName) != null) {
            // the required bundle exports the package; get the package roots from it
            PackageRoots requiredRoots = getPackageRoots(supplier, packageName, visited);
            if (requiredRoots != nullPackageRoots)
                roots.add(requiredRoots);
        } else {
            // the bundle does not export the package; but it may reexport another bundle that does
            BundleConstraint[] supplierRequires = supplier.getRequires();
            for (int j = 0; j < supplierRequires.length; j++) {
                ResolverBundle reexported = (ResolverBundle) supplierRequires[j].getSelectedSupplier();
                if (reexported == null || !((BundleSpecification) supplierRequires[j].getVersionConstraint()).isExported())
                    continue;
                if (reexported.getExport(packageName) != null) {
                    // the reexported bundle exports the package; get the package roots from it
                    PackageRoots reExportedRoots = getPackageRoots(reexported, packageName, visited);
                    if (reExportedRoots != nullPackageRoots)
                        roots.add(reExportedRoots);
                }
            }
        }
    }
    if (exports.length > 0 || roots.size() > 1) {
        PackageRoots[] requiredRoots = roots.toArray(new PackageRoots[roots.size()]);
        if (exports.length == 0) {
            PackageRoots superSet = requiredRoots[0];
            for (int i = 1; i < requiredRoots.length; i++) {
                if (requiredRoots[i].superSet(superSet)) {
                    superSet = requiredRoots[i];
                } else if (!superSet.superSet(requiredRoots[i])) {
                    superSet = null;
                    break;
                }
            }
            if (superSet != null)
                return superSet;
        }
        // in this case we cannot share the package roots object; must create one specific for this bundle
        PackageRoots result = new PackageRoots(packageName);
        // first merge all the roots from required bundles
        for (int i = 0; i < requiredRoots.length; i++) result.merge(requiredRoots[i]);
        // always add this bundles exports to the end if it exports the package
        for (int i = 0; i < exports.length; i++) result.addRoot(exports[i]);
        return result;
    }
    return roots.size() == 0 ? nullPackageRoots : roots.get(0);
}
Also used : ExportPackageDescription(org.eclipse.osgi.service.resolver.ExportPackageDescription) BundleSpecification(org.eclipse.osgi.service.resolver.BundleSpecification)

Aggregations

BundleSpecification (org.eclipse.osgi.service.resolver.BundleSpecification)5 ImportPackageSpecification (org.eclipse.osgi.service.resolver.ImportPackageSpecification)4 VersionConstraint (org.eclipse.osgi.service.resolver.VersionConstraint)4 ExportPackageDescription (org.eclipse.osgi.service.resolver.ExportPackageDescription)3 ArrayList (java.util.ArrayList)2 GenericDescription (org.eclipse.osgi.service.resolver.GenericDescription)2 GenericSpecification (org.eclipse.osgi.service.resolver.GenericSpecification)2 ExportPackageDescriptionImpl (org.eclipse.osgi.internal.resolver.ExportPackageDescriptionImpl)1 GenericDescriptionImpl (org.eclipse.osgi.internal.resolver.GenericDescriptionImpl)1 BaseDescription (org.eclipse.osgi.service.resolver.BaseDescription)1 HostSpecification (org.eclipse.osgi.service.resolver.HostSpecification)1