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() {
public void run() {
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 latch = new CountDownLatch(1);
wiring.refreshBundles(Collections.singletonList(bundles[b]), new FrameworkListener() {
public void frameworkEvent(FrameworkEvent event) {
latch.countDown();
}
});
latch.await();