private void stop(int numOfServicesStarted, boolean stopOnlyStartedServices) {
    // stop in reverse order of start
    Exception firstException = null;
    List<Service> services = getServices();
    for (int i = numOfServicesStarted - 1; i >= 0; i--) {
      Service service = services.get(i);
      if (LOG.isDebugEnabled()) {
        LOG.debug("Stopping service #" + i + ": " + service);
      }
      STATE state = service.getServiceState();
      //depending on the stop police
      if (state == STATE.STARTED 
         || (!stopOnlyStartedServices && state == STATE.INITED)) {
        Exception ex = ServiceOperations.stopQuietly(LOG, service);
        if (ex != null && firstException == null) {
          firstException = ex;
        }
      }
    }
    //after stopping all services, rethrow the first exception raised
    if (firstException != null) {
      throw ServiceStateException.convert(firstException);
    }
  }
@Test(timeout = 10000)
  public void testAddInitedChildInStop() throws Throwable {
    CompositeService parent = new CompositeService("parent");
    BreakableService child = new BreakableService();
    child.init(new Configuration());
    parent.init(new Configuration());
    parent.start();
    parent.stop();
    AddSiblingService.addChildToService(parent, child);
    assertInState(STATE.INITED, child);
  }