/** @return the ZooKeeper path used for leadership election by Curator */
  private String buildLeaderPath() {

    String ns = StringUtils.hasText(namespace) ? namespace : DEFAULT_NAMESPACE;
    if (!ns.startsWith("/")) {
      ns = "/" + ns;
    }
    if (!ns.endsWith("/")) {
      ns = ns + "/";
    }
    return String.format(ns + "%s", candidate.getRole());
  }
  /** Start the registration of the {@link #candidate} for leader election. */
  @Override
  public synchronized void start() {
    if (!running) {
      if (client.getState() != CuratorFrameworkState.STARTED) {
        // we want to do curator start here because it needs to
        // be started before leader selector and it gets a little
        // complicated to control ordering via beans so that
        // curator is fully started.
        client.start();
      }
      leaderSelector = new LeaderSelector(client, buildLeaderPath(), new LeaderListener());
      leaderSelector.setId(candidate.getId());
      leaderSelector.autoRequeue();
      leaderSelector.start();

      running = true;
    }
  }