Search in sources :

Example 21 with BundleRevision

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

the class WovenClassListener method modified.

@Override
public void modified(WovenClass wovenClass) {
    if (wovenClass.getState() != WovenClass.TRANSFORMED) {
        // the defined state is reached.
        return;
    }
    List<String> dynamicImports = wovenClass.getDynamicImports();
    if (dynamicImports.isEmpty()) {
        // Nothing to do if there are no dynamic imports.
        return;
    }
    BundleWiring wiring = wovenClass.getBundleWiring();
    Bundle bundle = wiring.getBundle();
    BundleRevision revision = bundle.adapt(BundleRevision.class);
    BundleConstituent constituent = new BundleConstituent(null, revision);
    Collection<BasicSubsystem> basicSubsystems = subsystems.getSubsystemsByConstituent(constituent);
    BasicSubsystem subsystem = basicSubsystems.iterator().next();
    // Find the scoped subsystem in the region.
    subsystem = scopedSubsystem(subsystem);
    if (subsystem.getSubsystemId() == 0) {
        // The root subsystem needs no sharing policy.
        return;
    }
    if (EnumSet.of(Subsystem.State.INSTALLING, Subsystem.State.INSTALLED).contains(subsystem.getState())) {
        // The scoped subsystem must be resolved before adding dynamic 
        // package imports to the sharing policy in order to minimize 
        // unpredictable wirings. Resolving the scoped subsystem will also
        // resolve all of the unscoped subsystems in the region.
        AccessController.doPrivileged(new StartAction(subsystem, subsystem, subsystem, Restriction.RESOLVE_ONLY));
    }
    Bundle systemBundle = context.getBundle(org.osgi.framework.Constants.SYSTEM_BUNDLE_LOCATION);
    FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
    // The following map tracks all of the necessary updates as each dynamic
    // import is processed. The key is the tail region of the connection 
    // whose filter needs updating.
    Map<Region, RegionUpdaterInfo> updates = new HashMap<Region, RegionUpdaterInfo>();
    for (String dynamicImport : dynamicImports) {
        // For each dynamic import, collect the necessary update information.
        DynamicImportPackageHeader header = new DynamicImportPackageHeader(dynamicImport);
        List<DynamicImportPackageRequirement> requirements = header.toRequirements(revision);
        for (DynamicImportPackageRequirement requirement : requirements) {
            Collection<BundleCapability> providers = frameworkWiring.findProviders(requirement);
            if (providers.isEmpty()) {
                // import, no updates are made.
                continue;
            }
            addSharingPolicyUpdates(requirement, subsystem, providers, updates);
        }
    }
    // Now update each sharing policy only once.
    for (RegionUpdaterInfo update : updates.values()) {
        RegionUpdater updater = new RegionUpdater(update.tail(), update.head());
        try {
            updater.addRequirements(update.requirements());
        } catch (IllegalStateException e) {
        // Something outside of the subsystems implementation has
        // deleted the edge between the parent and child subsystems.
        // Assume the dynamic import sharing policy is being handled
        // elsewhere. See ARIES-1429.
        } catch (Exception e) {
            throw new SubsystemException(e);
        }
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) SubsystemException(org.osgi.service.subsystem.SubsystemException) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) SubsystemException(org.osgi.service.subsystem.SubsystemException) DynamicImportPackageHeader(org.apache.aries.subsystem.core.archive.DynamicImportPackageHeader) BundleConstituent(org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent) DynamicImportPackageRequirement(org.apache.aries.subsystem.core.archive.DynamicImportPackageRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Region(org.eclipse.equinox.region.Region) FilteredRegion(org.eclipse.equinox.region.RegionDigraph.FilteredRegion) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 22 with BundleRevision

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

the class SystemRepository method addingBundle.

@Override
public AtomicReference<BundleRevisionResource> addingBundle(Bundle bundle, BundleEvent event) {
    // The state mask must guarantee this will only be called when the bundle is in the INSTALLED state.
    BundleRevision revision = bundle.adapt(BundleRevision.class);
    BundleRevisionResource resource = new BundleRevisionResource(revision);
    if (ThreadLocalSubsystem.get() == null) {
        // This is an explicitly installed bundle. It must be prevented
        // from resolving as part of adding it to the repository. Searching
        // for service requirements and capabilities will result in a call
        // to findEntries which will cause the framework to attempt a
        // resolution.
        ThreadLocalBundleRevision.set(revision);
        try {
            repository.addResource(resource);
        } finally {
            ThreadLocalBundleRevision.remove();
        }
    } else {
        // If this is a bundle being installed as part of a subsystem
        // installation, it is already protected.
        repository.addResource(resource);
    }
    return new AtomicReference<BundleRevisionResource>(resource);
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 23 with BundleRevision

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

the class SystemRepository method modifiedBundle.

@Override
public void modifiedBundle(Bundle bundle, BundleEvent event, AtomicReference<BundleRevisionResource> object) {
    if (BundleEvent.UPDATED == event.getType()) {
        BundleRevision revision = bundle.adapt(BundleRevision.class);
        BundleRevisionResource resource = new BundleRevisionResource(revision);
        repository.removeResource(object.getAndSet(resource));
        repository.addResource(resource);
    }
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 24 with BundleRevision

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

the class BundleRevisionResourceTest method testNoModellerServiceRequirements.

@Test
public void testNoModellerServiceRequirements() {
    BundleRevision br = EasyMock.createNiceMock(BundleRevision.class);
    expect(br.getRequirements(anyObject(String.class))).andReturn(Collections.<Requirement>emptyList());
    expect(br.getCapabilities(anyObject(String.class))).andReturn(Collections.<Capability>emptyList());
    replay(br);
    BundleRevisionResource brr = new BundleRevisionResource(br);
    assertEquals(0, brr.getRequirements("osgi.service").size());
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision) Test(org.junit.Test)

Example 25 with BundleRevision

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

the class Builder method resolve.

private Map<String, Integer> resolve(DownloadManager manager, Resolver resolver, Collection<Features> repositories, Collection<String> features, Collection<String> bundles, Collection<String> overrides, Collection<String> optionals) throws Exception {
    BundleRevision systemBundle = getSystemBundle();
    AssemblyDeployCallback callback = new AssemblyDeployCallback(manager, this, systemBundle, repositories);
    Deployer deployer = new Deployer(manager, resolver, callback, callback);
    // Install framework
    Deployer.DeploymentRequest request = createDeploymentRequest();
    // Add overrides
    request.overrides.addAll(overrides);
    // Add optional resources
    final List<Resource> resources = new ArrayList<>();
    Downloader downloader = manager.createDownloader();
    for (String optional : optionals) {
        downloader.download(optional, provider -> {
            Resource resource = ResourceBuilder.build(provider.getUrl(), getHeaders(provider));
            synchronized (resources) {
                resources.add(resource);
            }
        });
    }
    downloader.await();
    request.globalRepository = new BaseRepository(resources);
    // Install features
    for (String feature : features) {
        MapUtils.addToMapSet(request.requirements, FeaturesService.ROOT_REGION, feature);
    }
    for (String bundle : bundles) {
        MapUtils.addToMapSet(request.requirements, FeaturesService.ROOT_REGION, "bundle:" + bundle);
    }
    Set<String> prereqs = new HashSet<>();
    while (true) {
        try {
            deployer.deploy(callback.getDeploymentState(), request);
            break;
        } catch (Deployer.PartialDeploymentException e) {
            if (!prereqs.containsAll(e.getMissing())) {
                prereqs.addAll(e.getMissing());
            } else {
                throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
            }
        }
    }
    return callback.getStartupBundles();
}
Also used : ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Downloader(org.apache.karaf.features.internal.download.Downloader) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BundleRevision(org.osgi.framework.wiring.BundleRevision) Deployer(org.apache.karaf.features.internal.service.Deployer) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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