NavigableMap<URI, Bundle> stopBundles(Set<URI> bundles) { NavigableMap<URI, Bundle> expect, update; do { expect = get(); update = ImmutableSortedMap.<URI, Bundle>naturalOrder() .putAll(Maps.filterKeys(expect, not(in(bundles)))) .build(); } while (!compareAndSet(expect, update)); List<URI> couldNotStop = new ArrayList<>(); NavigableMap<URI, Bundle> stopped = Maps.filterKeys(expect, in(bundles)); for (Map.Entry<URI, Bundle> e : stopped.entrySet()) { URI bundleURI = e.getKey(); Bundle bundle = e.getValue(); try { bundle.stop(); } catch (BundleException exc) { LOG.error("Failed to stop bundle " + bundleURI, exc); couldNotStop.add(bundleURI); } } if (!couldNotStop.isEmpty()) { throw new ModularException("Failed to stop bundles %s", couldNotStop); } return stopped; }
private Bundle startBundle(URI bundleURI) { NavigableMap<URI, Bundle> expect, update; Bundle bundle = null; do { expect = get(); if (expect.containsKey(bundleURI)) break; BundleContext ctx = m_framework.getBundleContext(); bundle = ctx.getBundle(bundleURI.toASCIIString()); if (bundle != null) { try { bundle.update(); } catch (BundleException e) { String msg = e.getMessage(); throw loggedModularException(e, "Unable to update bundle %s. %s", bundleURI, msg); } catch (Throwable t) { throw loggedModularException(t, "Unable to update bundle %s", bundleURI); } } else { try { bundle = ctx.installBundle(bundleURI.toASCIIString()); } catch (BundleException e) { String msg = e.getMessage(); throw loggedModularException(e, "Unable to install bundle %s. %s", bundleURI, msg); } catch (Throwable t) { throw loggedModularException(t, "Unable to instal bundle %s", bundleURI); } } try { bundle.start(); } catch (BundleException e) { String msg = e.getMessage(); throw loggedModularException(e, "Unable to start bundle %s. %s", bundleURI, msg); } catch (Throwable t) { throw loggedModularException(t, "Unable to start bundle %s", bundleURI); } update = ImmutableSortedMap.<URI, Bundle>naturalOrder() .putAll(expect) .put(bundleURI, bundle) .build(); } while (!compareAndSet(expect, update)); return get().get(bundleURI); }
Optional<Bundle> stopBundle(URI bundleURI) { NavigableMap<URI, Bundle> expect, update; do { expect = get(); update = ImmutableSortedMap.<URI, Bundle>naturalOrder() .putAll(Maps.filterKeys(expect, not(equalTo(bundleURI)))) .build(); } while (expect.containsKey(bundleURI) && !compareAndSet(expect, update)); Bundle bundle = expect.get(bundleURI); if (bundle != null) { try { bundle.stop(); } catch (BundleException e) { throw loggedModularException(e, "Failed to stop bundle %s", bundleURI); } } return Optional.ofNullable(bundle); }
static class BundleRef extends AtomicReference<NavigableMap<URI, Bundle>> { private static final long serialVersionUID = -3691039780541403034L; static NavigableMap<URI, Bundle> EMPTY_MAP = ImmutableSortedMap.of(); final Framework m_framework; public BundleRef(Framework framework, NavigableMap<URI, Bundle> initialRef) { super(initialRef); m_framework = framework; } public BundleRef(Framework framework) { this(framework, EMPTY_MAP); } private Bundle startBundle(URI bundleURI) { NavigableMap<URI, Bundle> expect, update; Bundle bundle = null; do { expect = get(); if (expect.containsKey(bundleURI)) break; BundleContext ctx = m_framework.getBundleContext(); bundle = ctx.getBundle(bundleURI.toASCIIString()); if (bundle != null) { try { bundle.update(); } catch (BundleException e) { String msg = e.getMessage(); throw loggedModularException(e, "Unable to update bundle %s. %s", bundleURI, msg); } catch (Throwable t) { throw loggedModularException(t, "Unable to update bundle %s", bundleURI); } } else { try { bundle = ctx.installBundle(bundleURI.toASCIIString()); } catch (BundleException e) { String msg = e.getMessage(); throw loggedModularException(e, "Unable to install bundle %s. %s", bundleURI, msg); } catch (Throwable t) { throw loggedModularException(t, "Unable to instal bundle %s", bundleURI); } } try { bundle.start(); } catch (BundleException e) { String msg = e.getMessage(); throw loggedModularException(e, "Unable to start bundle %s. %s", bundleURI, msg); } catch (Throwable t) { throw loggedModularException(t, "Unable to start bundle %s", bundleURI); } update = ImmutableSortedMap.<URI, Bundle>naturalOrder() .putAll(expect) .put(bundleURI, bundle) .build(); } while (!compareAndSet(expect, update)); return get().get(bundleURI); } <T> T getService(URI bundleURI, Class<T> svcClazz) { Bundle bundle = get().get(bundleURI); if (bundle == null) { synchronized (this) { bundle = startBundle(bundleURI); } } BundleContext ctx = bundle.getBundleContext(); for (ServiceReference<?> ref : bundle.getRegisteredServices()) { if (ref.isAssignableTo(bundle, svcClazz.getName())) { return svcClazz.cast(ctx.getService(ref)); } } return null; } Optional<Bundle> stopBundle(URI bundleURI) { NavigableMap<URI, Bundle> expect, update; do { expect = get(); update = ImmutableSortedMap.<URI, Bundle>naturalOrder() .putAll(Maps.filterKeys(expect, not(equalTo(bundleURI)))) .build(); } while (expect.containsKey(bundleURI) && !compareAndSet(expect, update)); Bundle bundle = expect.get(bundleURI); if (bundle != null) { try { bundle.stop(); } catch (BundleException e) { throw loggedModularException(e, "Failed to stop bundle %s", bundleURI); } } return Optional.ofNullable(bundle); } void uninstallBundle(URI bundleURI) { stopBundle(bundleURI) .ifPresent( (Bundle b) -> { try { b.uninstall(); } catch (Throwable t) { throw loggedModularException(t, "Failed to uninstall %s", b.getLocation()); } }); } NavigableMap<URI, Bundle> stopBundles(Set<URI> bundles) { NavigableMap<URI, Bundle> expect, update; do { expect = get(); update = ImmutableSortedMap.<URI, Bundle>naturalOrder() .putAll(Maps.filterKeys(expect, not(in(bundles)))) .build(); } while (!compareAndSet(expect, update)); List<URI> couldNotStop = new ArrayList<>(); NavigableMap<URI, Bundle> stopped = Maps.filterKeys(expect, in(bundles)); for (Map.Entry<URI, Bundle> e : stopped.entrySet()) { URI bundleURI = e.getKey(); Bundle bundle = e.getValue(); try { bundle.stop(); } catch (BundleException exc) { LOG.error("Failed to stop bundle " + bundleURI, exc); couldNotStop.add(bundleURI); } } if (!couldNotStop.isEmpty()) { throw new ModularException("Failed to stop bundles %s", couldNotStop); } return stopped; } void uninstallBundles(Set<URI> bundles) { List<URI> couldNotUninstall = new ArrayList<>(); for (Map.Entry<URI, Bundle> e : stopBundles(bundles).entrySet()) { URI bundleURI = e.getKey(); Bundle bundle = e.getValue(); try { bundle.uninstall(); } catch (BundleException exc) { LOG.error("Failed to uninstall bundle " + bundleURI, exc); couldNotUninstall.add(bundleURI); } if (!couldNotUninstall.isEmpty()) { throw new ModularException("Failed to uninstall bundles %s", couldNotUninstall); } } } }