private static URI[] getServiceAddresses(ServiceId serviceProvider, SecurityServerId serverId)
      throws Exception {
    log.trace("getServiceAddresses({})", serviceProvider);

    Collection<String> hostNames = GlobalConf.getProviderAddress(serviceProvider.getClientId());
    if (hostNames == null || hostNames.isEmpty()) {
      throw new CodedException(
          X_UNKNOWN_MEMBER,
          "Could not find addresses for service provider \"%s\"",
          serviceProvider);
    }

    if (serverId != null) {
      final String securityServerAddress = GlobalConf.getSecurityServerAddress(serverId);
      if (securityServerAddress == null) {
        throw new CodedException(
            X_INVALID_SECURITY_SERVER, "Could not find security server \"%s\"", serverId);
      }

      if (!hostNames.contains(securityServerAddress)) {
        throw new CodedException(
            X_INVALID_SECURITY_SERVER, "Invalid security server \"%s\"", serviceProvider);
      }

      hostNames = Collections.singleton(securityServerAddress);
    }

    String protocol = SystemProperties.isSslEnabled() ? "https" : "http";
    int port = SystemProperties.getServerProxyPort();

    List<URI> addresses = new ArrayList<>(hostNames.size());
    for (String host : hostNames) {
      addresses.add(new URI(protocol, null, host, port, "/", null, null));
    }

    return addresses.toArray(new URI[] {});
  }