Search in sources :

Example 46 with BundleRevision

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

the class Deployer method getBundlesToStart.

protected List<Bundle> getBundlesToStart(Collection<Bundle> bundles, Bundle serviceBundle) {
    // Restart the features service last, regardless of any other consideration
    // so that we don't end up with the service trying to do stuff before we're done
    boolean restart = false;
    SortedMap<Integer, Set<Bundle>> bundlesPerStartLevel = new TreeMap<>();
    for (Bundle bundle : bundles) {
        if (bundle == serviceBundle) {
            restart = true;
        } else {
            int sl = bundle.adapt(BundleStartLevel.class).getStartLevel();
            addToMapSet(bundlesPerStartLevel, sl, bundle);
        }
    }
    if (bundlesPerStartLevel.isEmpty()) {
        bundles = Collections.emptyList();
    } else {
        bundles = bundlesPerStartLevel.remove(bundlesPerStartLevel.firstKey());
    }
    // We hit FELIX-2949 if we don't use the correct order as Felix resolver isn't greedy.
    // In order to minimize that, we make sure we resolve the bundles in the order they
    // are given back by the resolution, meaning that all root bundles (i.e. those that were
    // not flagged as dependencies in features) are started before the others.   This should
    // make sure those important bundles are started first and minimize the problem.
    List<BundleRevision> revs = new ArrayList<>();
    for (Bundle bundle : bundles) {
        revs.add(bundle.adapt(BundleRevision.class));
    }
    List<Bundle> sorted = new ArrayList<>();
    for (BundleRevision rev : RequirementSort.sort(revs)) {
        sorted.add(rev.getBundle());
    }
    if (sorted.isEmpty() && restart) {
        sorted.add(serviceBundle);
    }
    return sorted;
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) MapUtils.removeFromMapSet(org.apache.karaf.features.internal.util.MapUtils.removeFromMapSet) EnumSet(java.util.EnumSet) Set(java.util.Set) MapUtils.addToMapSet(org.apache.karaf.features.internal.util.MapUtils.addToMapSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision) TreeMap(java.util.TreeMap)

Example 47 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 48 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 49 with BundleRevision

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

the class BundleWiringStateMBeanTest method testGetRevisionsDeclaredCapabilities.

@Test
public void testGetRevisionsDeclaredCapabilities() throws Exception {
    BundleRevisions revisions = (BundleRevisions) bundleA.adapt(BundleRevisions.class);
    Assert.assertEquals("Precondition", 1, revisions.getRevisions().size());
    TabularData jmxCapabilitiesTable = brsMBean.getRevisionsDeclaredCapabilities(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
    Assert.assertEquals(1, jmxCapabilitiesTable.size());
    List<BundleCapability> capabilities = ((BundleRevision) revisions.getRevisions().iterator().next()).getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
    CompositeData jmxRevCapabilities = (CompositeData) jmxCapabilitiesTable.values().iterator().next();
    CompositeData[] jmxCapabilities = (CompositeData[]) jmxRevCapabilities.get(BundleWiringStateMBean.CAPABILITIES);
    Map<Map<String, Object>, Map<String, String>> expectedCapabilities = capabilitiesToMap(capabilities);
    Map<Map<String, Object>, Map<String, String>> actualCapabilities = jmxCapReqToMap(jmxCapabilities);
    Assert.assertEquals(expectedCapabilities, actualCapabilities);
}
Also used : CompositeData(javax.management.openmbean.CompositeData) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability) HashMap(java.util.HashMap) Map(java.util.Map) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) TabularData(javax.management.openmbean.TabularData) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 50 with BundleRevision

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

the class BundleWiringStateMBeanTest method testGetCurrentRevisionDeclaredCapabilities.

@Test
public void testGetCurrentRevisionDeclaredCapabilities() throws Exception {
    BundleRevision br = (BundleRevision) bundleA.adapt(BundleRevision.class);
    List<BundleCapability> capabilities = br.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
    CompositeData[] jmxCapabilities = brsMBean.getCurrentRevisionDeclaredCapabilities(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
    Assert.assertEquals(capabilities.size(), jmxCapabilities.length);
    Map<Map<String, Object>, Map<String, String>> expectedCapabilities = capabilitiesToMap(capabilities);
    Map<Map<String, Object>, Map<String, String>> actualCapabilities = jmxCapReqToMap(jmxCapabilities);
    Assert.assertEquals(expectedCapabilities, actualCapabilities);
}
Also used : CompositeData(javax.management.openmbean.CompositeData) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Aggregations

BundleRevision (org.osgi.framework.wiring.BundleRevision)79 Bundle (org.osgi.framework.Bundle)42 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)18 BundleWiring (org.osgi.framework.wiring.BundleWiring)14 BundleCapability (org.osgi.framework.wiring.BundleCapability)13 Test (org.junit.Test)12 Map (java.util.Map)11 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)11 Resource (org.osgi.resource.Resource)11 HashSet (java.util.HashSet)9 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)9 BundleContext (org.osgi.framework.BundleContext)8 BundleException (org.osgi.framework.BundleException)8 List (java.util.List)7 BundleConstituent (org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent)7 BundleWire (org.osgi.framework.wiring.BundleWire)7 Requirement (org.osgi.resource.Requirement)7 File (java.io.File)6 Collection (java.util.Collection)6