in bundle/core/src/main/java/org/apache/karaf/bundle/command/LoadTest.java [65:169]
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(() -> {
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 latch1 = new CountDownLatch(1);
wiring.refreshBundles(Collections.singletonList(bundles[b]),
(FrameworkListener) event -> latch1.countDown());
latch1.await();
} finally {
while (true) {
try {
bundles[b].start(Bundle.START_TRANSIENT);
break;
} catch (Exception e) {
Thread.sleep(1);
}
}
}
} else {
try {
bundles[b].stop(Bundle.STOP_TRANSIENT);
} finally {
while (true) {
try {
bundles[b].start(Bundle.START_TRANSIENT);
break;
} catch (Exception e) {
Thread.sleep(1);
}
}
}
}
Thread.sleep(rand.nextInt(delay));
} catch (Exception e) {
boolean ignore = false;
if (e instanceof BundleException && e.getMessage() != null) {
String msg = e.getMessage();
if ("Cannot acquire global lock to update the bundle.".equals(msg) ||
"Unable to acquire global lock for resolve.".equals(msg) ||
msg.matches("Bundle .* cannot be update, since it is either starting or stopping.")) {
ignore = true;
}
}
if (!ignore) {
e.printStackTrace();
}
} finally {
locks[b].set(false);
}
}
break;
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
latch.countDown();
}
}).start();
}
new Thread(() -> {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("Load test finished");
}).start();
return null;
}