Skip to content

Instantly share code, notes, and snippets.

@gravelld
Created July 25, 2012 17:21
Show Gist options
  • Save gravelld/3177368 to your computer and use it in GitHub Desktop.
Save gravelld/3177368 to your computer and use it in GitHub Desktop.
How I update resources in bliss... (at the moment)
final Resource[] resourcesToUpdate = FelixObrUtils.getLatestBundleResourcesToInstall(repoAdminSupplier);
final Resource[] resourcesToUpdateExceptUpdater = Iterables.toArray(
Iterables.filter(
ImmutableList.copyOf(resourcesToUpdate),
Predicates.not(FelixObrUtils.isResourceSymbolicName("com.elsten.bliss.updater"))),
Resource.class);
final Resolver resolver = repoAdminSupplier.get().resolver();
for (Resource resource : resourcesToUpdateExceptUpdater) {
resolver.add(resource);
}
if (resolver.resolve()) {
listener.installBundleStart();
final SynchronousBundleListener bundleListener = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent arg0) {
LOG.debug("(" + arg0.getType() + ") " + arg0.getBundle().getSymbolicName());
if(arg0.getType() == BundleEvent.STOPPING && arg0.getBundle().getSymbolicName().equals("com.elsten.bliss.ui")) {
sendUiStoppingMessageAndBlock();
}
}
// when UI stopping, send message to redirect to update screen and block until acked
private void sendUiStoppingMessageAndBlock() {
Semaphore semaphore = new Semaphore(0);
listener.uiStopping(semaphore);
try {
semaphore.acquire();
} catch (InterruptedException e) {
LOG.error("Interrupted while waiting for semaphore", e);
}
}
};
context.addBundleListener(bundleListener);
final Iterable<Bundle> startedBundles = getStartedBundles(resourcesToUpdateExceptUpdater);
Executors.newSingleThreadExecutor().execute(new CatchThrowableRunnable() {
@Override
public void doRun() {
/*
* To avoid uses constraints on updated code, we should not start the bundles
* straight after update. Instead, record the states of the bundles, deploy,
* refresh, then restart
*/
Bundle sysBundle = context.getBundle(0);
for (Bundle bundle : startedBundles) {
try {
context.getBundle(bundle.getBundleId()).stop();
} catch (BundleException e) {
LOG.error("Failed to stop " + bundle.getSymbolicName(), e);
}
}
resolver.deploy(0);
refreshBundles(null, context);
for (Bundle bundle : startedBundles) {
try {
sysBundle.getBundleContext().getBundle(bundle.getBundleId()).start();
} catch (BundleException e) {
LOG.error("Failed to restart " + bundle.getSymbolicName(), e);
}
}
//context.removeBundleListener(bundleListener);
}
});
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment