// Path: cfg4j-core/src/main/java/org/cfg4j/source/ConfigurationSource.java
// public interface ConfigurationSource {
// 
//   /**
//    * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}.
//    * Provided {@link Environment} will be used to determine which environment to use.
//    *
//    * @param environment environment to use
//    * @return configuration set for {@code environment}
//    * @throws MissingEnvironmentException when requested environment couldn't be found
//    * @throws IllegalStateException       when unable to fetch configuration
//    */
//   Properties getConfiguration(Environment environment);
// 
//   /**
//    * Initialize this source. This method has to be called before any other method of this instance.
//    *
//    * @throws IllegalStateException        when source was improperly configured
//    * @throws SourceCommunicationException when unable to communicate with source
//    */
//   void init();
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/SourceCommunicationException.java
// public class SourceCommunicationException extends RuntimeException {
// 
//   /**
//    * Indicates problem when communicating with {@link ConfigurationSource}.
//    *
//    * @param msg the detail message
//    * @param e   the cause
//    */
//   public SourceCommunicationException(String msg, Exception e) {
//     super("Unable to communicate with configuration source: " + msg, e);
//   }
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/context/environment/Environment.java
// public interface Environment {
// 
//   /**
//    * Name of the environment. Should not be null.
//    *
//    * @return environment name. Not null.
//    */
//   String getName();
// 
// }


import java.util.Properties;
import static java.util.Objects.requireNonNull;
import com.google.common.net.HostAndPort;
import com.orbitz.consul.Consul;
import com.orbitz.consul.KeyValueClient;
import com.orbitz.consul.model.kv.Value;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.SourceCommunicationException;
import org.cfg4j.source.context.environment.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

    if (path.startsWith("/")) {
      path = path.substring(1);
    }

    if (path.length() > 0 && !path.endsWith("/")) {
      path = path + "/";
    }

    for (Map.Entry<String, String> entry : consulValues.entrySet()) {
      if (entry.getKey().startsWith(path)) {
        properties.put(entry.getKey().substring(path.length()).replace("/", "."), entry.getValue());
      }
    }

    return properties;
  }

  /**
   * @throws SourceCommunicationException when unable to connect to Consul client
   */
  @Override
  public void init() {
    try {
      LOG.info("Connecting to Consul client at " + host + ":" + port);

      Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromParts(host, port)).build();

      kvClient = consul.keyValueClient();
    } catch (Exception e) {
// Path: cfg4j-core/src/main/java/org/cfg4j/source/ConfigurationSource.java
// public interface ConfigurationSource {
// 
//   /**
//    * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}.
//    * Provided {@link Environment} will be used to determine which environment to use.
//    *
//    * @param environment environment to use
//    * @return configuration set for {@code environment}
//    * @throws MissingEnvironmentException when requested environment couldn't be found
//    * @throws IllegalStateException       when unable to fetch configuration
//    */
//   Properties getConfiguration(Environment environment);
// 
//   /**
//    * Initialize this source. This method has to be called before any other method of this instance.
//    *
//    * @throws IllegalStateException        when source was improperly configured
//    * @throws SourceCommunicationException when unable to communicate with source
//    */
//   void init();
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/SourceCommunicationException.java
// public class SourceCommunicationException extends RuntimeException {
// 
//   /**
//    * Indicates problem when communicating with {@link ConfigurationSource}.
//    *
//    * @param msg the detail message
//    * @param e   the cause
//    */
//   public SourceCommunicationException(String msg, Exception e) {
//     super("Unable to communicate with configuration source: " + msg, e);
//   }
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/context/environment/Environment.java
// public interface Environment {
// 
//   /**
//    * Name of the environment. Should not be null.
//    *
//    * @return environment name. Not null.
//    */
//   String getName();
// 
// }


// Path: cfg4j-consul/src/main/java/org/cfg4j/source/consul/ConsulConfigurationSource.java
import java.util.Properties;
import static java.util.Objects.requireNonNull;
import com.google.common.net.HostAndPort;
import com.orbitz.consul.Consul;
import com.orbitz.consul.KeyValueClient;
import com.orbitz.consul.model.kv.Value;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.SourceCommunicationException;
import org.cfg4j.source.context.environment.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


    if (path.startsWith("/")) {
      path = path.substring(1);
    }

    if (path.length() > 0 && !path.endsWith("/")) {
      path = path + "/";
    }

    for (Map.Entry<String, String> entry : consulValues.entrySet()) {
      if (entry.getKey().startsWith(path)) {
        properties.put(entry.getKey().substring(path.length()).replace("/", "."), entry.getValue());
      }
    }

    return properties;
  }

  /**
   * @throws SourceCommunicationException when unable to connect to Consul client
   */
  @Override
  public void init() {
    try {
      LOG.info("Connecting to Consul client at " + host + ":" + port);

      Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromParts(host, port)).build();

      kvClient = consul.keyValueClient();
    } catch (Exception e) {
      throw new SourceCommunicationException("Can't connect to host " + host + ":" + port, e);