Search in sources :

Example 16 with FrameworkWiring

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

the class SubsystemTest method assertRefresh.

protected void assertRefresh(Collection<Bundle> bundles) throws InterruptedException {
    FrameworkWiring wiring = getSystemBundleAsFrameworkWiring();
    final AtomicBoolean refreshed = new AtomicBoolean(false);
    wiring.refreshBundles(bundles, new FrameworkListener[] { new FrameworkListener() {

        @Override
        public void frameworkEvent(FrameworkEvent event) {
            if (FrameworkEvent.PACKAGES_REFRESHED == event.getType()) {
                synchronized (refreshed) {
                    refreshed.set(true);
                    refreshed.notify();
                }
            }
        }
    } });
    synchronized (refreshed) {
        refreshed.wait(5000);
    }
    assertTrue("Bundles not refreshed", refreshed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameworkEvent(org.osgi.framework.FrameworkEvent) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) FrameworkListener(org.osgi.framework.FrameworkListener)

Example 17 with FrameworkWiring

use of org.osgi.framework.wiring.FrameworkWiring 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 18 with FrameworkWiring

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

the class StartAction method resolveBundles.

private static void resolveBundles(BasicSubsystem subsystem) {
    FrameworkWiring frameworkWiring = Activator.getInstance().getBundleContext().getBundle(0).adapt(FrameworkWiring.class);
    // TODO I think this is insufficient. Do we need both
    // pre-install and post-install environments for the Resolver?
    Collection<Bundle> bundles = getBundles(subsystem);
    if (!frameworkWiring.resolveBundles(bundles)) {
        handleFailedResolution(subsystem, bundles, frameworkWiring);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring)

Example 19 with FrameworkWiring

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

the class SharePolicyTest method test1.

/**
 * Bundle tb5
 * Bundle tb6
 * tb5 imports package exported by tb6
 * tb5 and tb6 in same scope
 * tb5 should resolve
 *
 * Share policies have no effect within the same scope.
 *
 * @throws Exception
 */
@Test
public void test1() throws Exception {
    Bundle tb5 = null;
    Bundle tb6 = null;
    try {
        String tb5Location = getBundleLocation("tb-5.jar");
        String tb6Location = getBundleLocation("tb-6.jar");
        InstallInfo tb5Info = new InstallInfo(tb5Location, new URL(tb5Location));
        InstallInfo tb6Info = new InstallInfo(tb6Location, new URL(tb6Location));
        ScopeUpdate scopeUpdate = getScope().newScopeUpdate();
        scopeUpdate.getBundlesToInstall().add(tb5Info);
        scopeUpdate.commit();
        tb5 = findBundleInRootScope("org.apache.aries.subsystem.scope.itests.tb5");
        assertNotNull(tb5);
        FrameworkWiring frameworkWiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
        assertFalse(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5 })));
        scopeUpdate = getScope().newScopeUpdate();
        scopeUpdate.getBundlesToInstall().add(tb6Info);
        scopeUpdate.commit();
        tb6 = findBundleInRootScope("org.apache.aries.subsystem.scope.itests.tb6");
        assertNotNull(tb6);
        assertTrue(frameworkWiring.resolveBundles(Arrays.asList(new Bundle[] { tb5, tb6 })));
    } finally {
        uninstallQuietly(tb6);
        uninstallQuietly(tb5);
    }
}
Also used : InstallInfo(org.apache.aries.subsystem.scope.InstallInfo) Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) ScopeUpdate(org.apache.aries.subsystem.scope.ScopeUpdate) URL(java.net.URL) Test(org.junit.Test)

Example 20 with FrameworkWiring

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

the class LoadTest method execute.

@Override
public Object execute() throws Exception {
    if (!confirm(session)) {
        return null;
    }
    final BundleContext bundleContext = this.bundleContext.getBundle(0).getBundleContext();
    final FrameworkWiring wiring = bundleContext.getBundle().adapt(FrameworkWiring.class);
    final CountDownLatch latch = new CountDownLatch(threads);
    final Bundle[] bundles = bundleContext.getBundles();
    final AtomicBoolean[] locks = new AtomicBoolean[bundles.length];
    for (int b = 0; b < locks.length; b++) {
        locks[b] = new AtomicBoolean(true);
        // Avoid touching excluded bundles
        if (excludes.contains(Long.toString(bundles[b].getBundleId())) || excludes.contains(bundles[b].getSymbolicName())) {
            continue;
        }
        // Only touch active bundles
        if (bundles[b].getState() != Bundle.ACTIVE) {
            continue;
        }
        // Now set the lock to available
        locks[b].set(false);
    }
    for (int i = 0; i < threads; i++) {
        new Thread(() -> {
            try {
                Random rand = new Random();
                for (int j = 0; j < iterations; j++) {
                    for (; ; ) {
                        int b = rand.nextInt(bundles.length);
                        if (locks[b].compareAndSet(false, true)) {
                            try {
                                // Only touch active bundles
                                if (bundles[b].getState() != Bundle.ACTIVE) {
                                    continue;
                                }
                                if (rand.nextInt(100) < refresh) {
                                    try {
                                        bundles[b].update();
                                        final CountDownLatch latch1 = new CountDownLatch(1);
                                        wiring.refreshBundles(Collections.singletonList(bundles[b]), (FrameworkListener) event -> latch1.countDown());
                                        latch1.await();
                                    } finally {
                                        while (true) {
                                            try {
                                                bundles[b].start(Bundle.START_TRANSIENT);
                                                break;
                                            } catch (Exception e) {
                                                Thread.sleep(1);
                                            }
                                        }
                                    }
                                } else {
                                    try {
                                        bundles[b].stop(Bundle.STOP_TRANSIENT);
                                    } finally {
                                        while (true) {
                                            try {
                                                bundles[b].start(Bundle.START_TRANSIENT);
                                                break;
                                            } catch (Exception e) {
                                                Thread.sleep(1);
                                            }
                                        }
                                    }
                                }
                                Thread.sleep(rand.nextInt(delay));
                            } catch (Exception e) {
                                boolean ignore = false;
                                if (e instanceof BundleException && e.getMessage() != null) {
                                    String msg = e.getMessage();
                                    if ("Cannot acquire global lock to update the bundle.".equals(msg) || "Unable to acquire global lock for resolve.".equals(msg) || msg.matches("Bundle .* cannot be update, since it is either starting or stopping.")) {
                                        ignore = true;
                                    }
                                }
                                if (!ignore) {
                                    e.printStackTrace();
                                }
                            } finally {
                                locks[b].set(false);
                            }
                        }
                        break;
                    }
                }
            } catch (Throwable t) {
                t.printStackTrace();
            } finally {
                latch.countDown();
            }
        }).start();
    }
    new Thread(() -> {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println("Load test finished");
    }).start();
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) BundleException(org.osgi.framework.BundleException) FrameworkListener(org.osgi.framework.FrameworkListener) BundleContext(org.osgi.framework.BundleContext)

Aggregations

FrameworkWiring (org.osgi.framework.wiring.FrameworkWiring)39 Bundle (org.osgi.framework.Bundle)29 Test (org.junit.Test)12 URL (java.net.URL)10 FrameworkListener (org.osgi.framework.FrameworkListener)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 BundleContext (org.osgi.framework.BundleContext)7 HashSet (java.util.HashSet)6 FrameworkEvent (org.osgi.framework.FrameworkEvent)6 Version (org.osgi.framework.Version)6 HashMap (java.util.HashMap)5 ComponentContext (org.osgi.service.component.ComponentContext)5 InputStream (java.io.InputStream)4 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)4 PatchManagement (io.fabric8.patch.management.PatchManagement)3 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)3 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)3 FileInputStream (java.io.FileInputStream)3