private static void transition(final ServiceController<?> targetController, State targetState) throws StartException {
// Short-circuit if the service is already at the target state
if (targetController.getState() == targetState) return;
final StabilityMonitor monitor = new StabilityMonitor();
try {
if (targetController.getSubstate().isRestState()) {
// Force service to transition to desired state
Mode targetMode = modeToggle.get(targetState).get(targetController.getMode());
if (targetMode != null) {
targetController.setMode(targetMode);
}
}
monitor.addController(targetController);
monitor.awaitStability();
if (targetState == State.UP) {
StartException exception = targetController.getStartException();
if (exception != null) {
throw exception;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
monitor.removeController(targetController);
}
}