protected void waitForServiceState(ServiceName serviceName, State expectedState, long timeout) {
ObjectName objectName = OBJECT_NAME;
MBeanServerConnection mbeanServer = getMBeanServerConnection();
ServiceContainerMXBean proxy = MBeanProxy.get(mbeanServer, objectName, ServiceContainerMXBean.class);
State currentState = State.valueOf(proxy.getServiceStatus(serviceName.getCanonicalName()).getStateName());
while (timeout > 0 && currentState != expectedState) {
try {
// TODO: Change this to use mbean notifications
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
timeout -= 100;
currentState = State.valueOf(proxy.getServiceStatus(serviceName.getCanonicalName()).getStateName());
}
if (currentState != expectedState)
throw new IllegalStateException("Unexpected state for [" + serviceName + "] - " + currentState);
}