Search in sources :

Example 56 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.

the class SubsystemResolver method prepare.

public void prepare(Collection<Feature> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
    // Build subsystems on the fly
    for (Map.Entry<String, Set<String>> entry : requirements.entrySet()) {
        String[] parts = entry.getKey().split("/");
        if (root == null) {
            root = new Subsystem(parts[0]);
        } else if (!root.getName().equals(parts[0])) {
            throw new IllegalArgumentException("Can not use multiple roots: " + root.getName() + ", " + parts[0]);
        }
        Subsystem ss = root;
        for (int i = 1; i < parts.length; i++) {
            ss = getOrCreateChild(ss, parts[i]);
        }
        for (String requirement : entry.getValue()) {
            ss.require(requirement);
        }
    }
    if (root == null) {
        return;
    }
    // Pre-resolve
    root.build(allFeatures);
    // Add system resources
    BundleRevision sysBundleRev = null;
    boolean hasEeCap = false;
    for (Map.Entry<String, Set<BundleRevision>> entry : system.entrySet()) {
        Subsystem ss = null;
        String[] parts = entry.getKey().split("/");
        String path = parts[0];
        if (path.equals(root.getName())) {
            ss = root;
        }
        for (int i = 1; ss != null && i < parts.length; i++) {
            path += "/" + parts[i];
            ss = ss.getChild(path);
        }
        if (ss != null) {
            ResourceImpl dummy = new ResourceImpl("dummy", "dummy", Version.emptyVersion);
            for (BundleRevision res : entry.getValue()) {
                // We need to explicitely provide service capabilities for bundles
                // We use both actual services and services declared from the headers
                // TODO: use actual services
                Map<String, String> headers = new DictionaryAsMap<>(res.getBundle().getHeaders());
                Resource tmp = ResourceBuilder.build(res.getBundle().getLocation(), headers);
                for (Capability cap : tmp.getCapabilities(ServiceNamespace.SERVICE_NAMESPACE)) {
                    dummy.addCapability(new CapabilityImpl(dummy, cap.getNamespace(), cap.getDirectives(), cap.getAttributes()));
                }
                ss.addSystemResource(res);
                for (Capability cap : res.getCapabilities(null)) {
                    hasEeCap |= cap.getNamespace().equals(EXECUTION_ENVIRONMENT_NAMESPACE);
                }
                if (res.getBundle().getBundleId() == 0) {
                    sysBundleRev = res;
                }
            }
            ss.addSystemResource(dummy);
        }
    }
    // Under Equinox, the osgi.ee capabilities are not provided by the system bundle
    if (!hasEeCap && sysBundleRev != null) {
        String provideCaps = sysBundleRev.getBundle().getHeaders().get(PROVIDE_CAPABILITY);
        environmentResource = new ResourceImpl("environment", "karaf.environment", Version.emptyVersion);
        environmentResource.addCapabilities(ResourceBuilder.parseCapability(environmentResource, provideCaps));
        root.addSystemResource(environmentResource);
    }
}
Also used : CapabilitySet(org.apache.karaf.features.internal.resolver.CapabilitySet) Capability(org.osgi.resource.Capability) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) Resource(org.osgi.resource.Resource) ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) CapabilityImpl(org.apache.karaf.features.internal.resolver.CapabilityImpl) BundleRevision(org.osgi.framework.wiring.BundleRevision) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap)

Example 57 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.

the class AssemblyDeployCallback method installBundle.

@Override
public Bundle installBundle(String region, String uri, InputStream is) throws BundleException {
    // Check blacklist
    if (Blacklist.isBundleBlacklisted(builder.getBlacklistedBundles(), uri)) {
        if (builder.getBlacklistPolicy() == Builder.BlacklistPolicy.Fail) {
            throw new RuntimeException("Bundle " + uri + " is blacklisted");
        }
    }
    // Install
    LOGGER.info("      adding maven artifact: " + uri);
    try {
        String regUri;
        String path;
        if (uri.startsWith("mvn:")) {
            regUri = uri;
            path = Parser.pathFromMaven(uri);
        } else {
            uri = uri.replaceAll("[^0-9a-zA-Z.\\-_]+", "_");
            if (uri.length() > 256) {
                //to avoid the File name too long exception
                uri = uri.substring(0, 255);
            }
            path = "generated/" + uri;
            regUri = "file:" + path;
        }
        final Path bundleSystemFile = systemDirectory.resolve(path);
        Files.createDirectories(bundleSystemFile.getParent());
        Files.copy(is, bundleSystemFile, StandardCopyOption.REPLACE_EXISTING);
        Hashtable<String, String> headers = new Hashtable<>();
        try (JarFile jar = new JarFile(bundleSystemFile.toFile())) {
            Attributes attributes = jar.getManifest().getMainAttributes();
            for (Map.Entry<Object, Object> attr : attributes.entrySet()) {
                headers.put(attr.getKey().toString(), attr.getValue().toString());
            }
        }
        BundleRevision revision = new FakeBundleRevision(headers, uri, nextBundleId.incrementAndGet());
        Bundle bundle = revision.getBundle();
        MapUtils.addToMapSet(dstate.bundlesPerRegion, region, bundle.getBundleId());
        dstate.bundles.put(bundle.getBundleId(), bundle);
        bundles.put(regUri, bundle);
        return bundle;
    } catch (IOException e) {
        throw new BundleException("Unable to install bundle", e);
    }
}
Also used : Path(java.nio.file.Path) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleException(org.osgi.framework.BundleException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 58 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.

the class BundleServiceImpl method getUnsatisfiedRequirements.

@Override
public List<BundleRequirement> getUnsatisfiedRequirements(Bundle bundle, String namespace) {
    List<BundleRequirement> result = new ArrayList<>();
    BundleRevision rev = bundle.adapt(BundleRevision.class);
    if (rev != null) {
        List<BundleRequirement> reqs = rev.getDeclaredRequirements(namespace);
        for (BundleRequirement req : reqs) {
            if (!canBeSatisfied(req)) {
                result.add(req);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 59 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.

the class BundleInfoImpl method populateRevisions.

private String populateRevisions(Bundle bundle) {
    String ret = "";
    BundleRevisions revisions = bundle.adapt(BundleRevisions.class);
    if (revisions == null) {
        return ret;
    }
    for (BundleRevision revision : revisions.getRevisions()) {
        ret = ret + "[" + revision + "]" + " ";
    }
    return ret;
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleRevisions(org.osgi.framework.wiring.BundleRevisions)

Example 60 with BundleRevision

use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.

the class PackageServiceImpl method getImports.

@Override
public List<PackageRequirement> getImports() {
    Bundle[] bundles = bundleContext.getBundles();
    SortedMap<String, PackageRequirement> requirements = new TreeMap<>();
    for (Bundle bundle : bundles) {
        BundleRevision rev = bundle.adapt(BundleRevision.class);
        if (rev != null) {
            List<BundleRequirement> reqs = rev.getDeclaredRequirements(BundleRevision.PACKAGE_NAMESPACE);
            for (BundleRequirement req : reqs) {
                PackageRequirement preq = create(req, bundle);
                requirements.put(preq.getPackageName() + "|" + preq.getFilter() + "|" + preq.getBundle().getBundleId(), preq);
            }
        }
    }
    return new ArrayList<>(requirements.values());
}
Also used : Bundle(org.osgi.framework.Bundle) PackageRequirement(org.apache.karaf.packages.core.PackageRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Aggregations

BundleRevision (org.osgi.framework.wiring.BundleRevision)66 Bundle (org.osgi.framework.Bundle)38 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)14 Map (java.util.Map)11 Test (org.junit.Test)11 BundleCapability (org.osgi.framework.wiring.BundleCapability)11 BundleWiring (org.osgi.framework.wiring.BundleWiring)11 Resource (org.osgi.resource.Resource)11 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)9 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)9 HashSet (java.util.HashSet)8 BundleWire (org.osgi.framework.wiring.BundleWire)8 BundleConstituent (org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent)7 Set (java.util.Set)6 TreeMap (java.util.TreeMap)6 TabularData (javax.management.openmbean.TabularData)6 IOException (java.io.IOException)5 Collection (java.util.Collection)5 List (java.util.List)5