Search in sources :

Example 1 with FrameworkWiring

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

the class BundleWatcherImpl method run.

public void run() {
    logger.debug("Bundle watcher thread started");
    int oldCounter = -1;
    Set<Bundle> watchedBundles = new HashSet<>();
    while (running.get() && watchURLs.size() > 0) {
        if (oldCounter != counter.get()) {
            oldCounter = counter.get();
            watchedBundles.clear();
            for (String bundleURL : watchURLs) {
                // Transform into regexp
                bundleURL = bundleURL.replaceAll("\\*", ".*");
                for (Bundle bundle : bundleService.selectBundles(Collections.singletonList(bundleURL), false)) {
                    if (isMavenSnapshotUrl(getLocation(bundle))) {
                        watchedBundles.add(bundle);
                    }
                }
            }
        }
        if (watchedBundles.size() > 0) {
            // Get the wiring before any in case of a refresh of a dependency
            FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
            File localRepository = this.localRepoDetector.getLocalRepository();
            List<Bundle> updated = new ArrayList<>();
            for (Bundle bundle : watchedBundles) {
                try {
                    updateBundleIfNecessary(localRepository, updated, bundle);
                } catch (IOException ex) {
                    logger.error("Error watching bundle.", ex);
                } catch (BundleException ex) {
                    logger.error("Error updating bundle.", ex);
                }
            }
            if (!updated.isEmpty()) {
                try {
                    final CountDownLatch latch = new CountDownLatch(1);
                    wiring.refreshBundles(updated, (FrameworkListener) event -> latch.countDown());
                    latch.await();
                } catch (InterruptedException e) {
                    running.set(false);
                }
                for (Bundle bundle : updated) {
                    try {
                        if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null) {
                            logger.info("[Watch] Bundle {} is a fragment, so it's not started", bundle.getSymbolicName());
                        } else {
                            bundle.start(Bundle.START_TRANSIENT);
                        }
                    } catch (BundleException ex) {
                        logger.warn("[Watch] Error starting bundle", ex);
                    }
                }
            }
        }
        try {
            Thread.sleep(interval);
        } catch (InterruptedException ex) {
            running.set(false);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Bundle watcher thread stopped");
    }
}
Also used : Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) BundleService(org.apache.karaf.bundle.core.BundleService) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) org.osgi.framework(org.osgi.framework) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BundleWatcher(org.apache.karaf.bundle.core.BundleWatcher) Parser(org.apache.karaf.util.maven.Parser) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) BundleUtils(org.apache.karaf.util.bundles.BundleUtils) Collections(java.util.Collections) InputStream(java.io.InputStream) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with FrameworkWiring

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

the class Resolve method doExecute.

protected Object doExecute(List<Bundle> bundles) throws Exception {
    FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
    wiring.resolveBundles(bundles == null || bundles.isEmpty() ? null : bundles);
    return null;
}
Also used : FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring)

Example 3 with FrameworkWiring

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

the class Refresh method doExecute.

protected Object doExecute(List<Bundle> bundles) throws Exception {
    FrameworkWiring wiring = bundleContext.getBundle(0).adapt(FrameworkWiring.class);
    if (bundles == null || bundles.isEmpty()) {
        bundles = null;
    }
    wiring.refreshBundles(bundles);
    return null;
}
Also used : FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring)

Example 4 with FrameworkWiring

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

the class FileInstall method refresh.

/**
 * Convenience to refresh the packages
 */
static void refresh(Bundle systemBundle, Collection<Bundle> bundles) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    FrameworkWiring wiring = systemBundle.adapt(FrameworkWiring.class);
    wiring.refreshBundles(bundles, new FrameworkListener() {

        public void frameworkEvent(FrameworkEvent event) {
            latch.countDown();
        }
    });
    latch.await();
}
Also used : FrameworkEvent(org.osgi.framework.FrameworkEvent) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) CountDownLatch(java.util.concurrent.CountDownLatch) FrameworkListener(org.osgi.framework.FrameworkListener)

Example 5 with FrameworkWiring

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

the class BaseIntegrationTest method resolveBundles.

protected boolean resolveBundles(Bundle... bundles) throws Exception {
    Bundle systemBundle = m_context.getBundle(0L);
    FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
    frameworkWiring.resolveBundles(Arrays.asList(bundles));
    for (Bundle bundle : bundles) {
        if ((bundle.getState() & Bundle.RESOLVED) == 0) {
            return false;
        }
    }
    return true;
}
Also used : Bundle(org.osgi.framework.Bundle) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring)

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