}
public void waitForState(final long bundleId, final int state, final RelativeTimeout timeout) {
Bundle bundle = bundleContext.getBundle(bundleId);
if (bundle == null || (timeout.isNoWait() && (bundle == null || bundle.getState() < state))) {
throw new TimeoutException("There is no waiting timeout set and bundle has state '"
+ bundleStateToString(bundle) + "' not '" + bundleStateToString(state)
+ "' as expected");
}
long startedTrying = System.currentTimeMillis();
do {
bundle = bundleContext.getBundle(bundleId);
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
while ((bundle == null || bundle.getState() < state)
&& (timeout.isNoTimeout() || System.currentTimeMillis() < startedTrying
+ timeout.getValue()));
if (bundle == null || bundle.getState() < state) {
throw new TimeoutException("Timeout passed and bundle has state '"
+ bundleStateToString(bundle.getState()) + "' not '" + bundleStateToString(state)
+ "' as expected");
}
}