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); }