package org.nebulostore.dfuntesting;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedList;
import java.util.NoSuchElementException;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LocalEnvironmentFactory implements EnvironmentFactory {
  private static final Logger LOGGER = LoggerFactory.getLogger(LocalEnvironmentFactory.class);
  private static final String XML_ENV_CNT_FIELD = "environment-count";
  private static final String XML_ENV_DIR_FIELD = "dir-prefix";
  private static final String ENV_CONFIG_ROOT_DIR = "root-dir";
  private static final String DEFAULT_DIR_PREFIX = "dfuntesting";

  private final Configuration mConfig;

  public LocalEnvironmentFactory(Configuration config) {
    mConfig = config;
  }

  @Override
  public Collection<Environment> createEnvironments() throws IOException {
    LOGGER.info("createEnvironments()");
    int count = mConfig.getInteger(XML_ENV_CNT_FIELD, 0);
    if (count <= 0) {
      throw new IllegalArgumentException(
          "Number of environments has not been provided or was invalid.");
    }
    String dirPrefix = mConfig.getString(XML_ENV_DIR_FIELD, DEFAULT_DIR_PREFIX);

    Collection<Environment> environments = new LinkedList<>();
    for (int envIdx = 0; envIdx < count; ++envIdx) {
      Path tempDirPath;
      tempDirPath = Files.createTempDirectory(dirPrefix);
      LocalEnvironment env = new LocalEnvironment(envIdx, tempDirPath);
      env.setProperty(ENV_CONFIG_ROOT_DIR, tempDirPath);
      environments.add(env);
    }
    return environments;
  }

  @Override
  public void destroyEnvironments(Collection<Environment> envs) {
    LOGGER.info("destroyEnvironments()");
    for (Environment env: envs) {
      Path dirPath;
      try {
        dirPath = (Path) env.getProperty(ENV_CONFIG_ROOT_DIR);
      } catch (ClassCastException | NoSuchElementException e) {
        LOGGER.error("Could not destroy environment.", e);
        continue;
      }
      FileUtils.deleteQuietly(dirPath.toFile());
    }
  }
}

@@ -367,16 +367,435 @@
 ctory;%0A%0A
+/**%0A * Factory of %7B@link LocalEnvironment%7D.%0A *%0A * This factory uses %7B@link Configuration%7D to specify environment configuration.%0A * Configuration uses following fields:%0A * %3Cul%3E%0A * %3Cli%3E environment-count - Number of local environments to create. %3C/li%3E%0A * %3Cli%3E dir-prefix - Temporary directory prefix used for creating local%0A *  environment in system's temporary directory. %3C/li%3E%0A * %3C/ul%3E%0A *%0A * @author Grzegorz Milka%0A */%0A
 public c

Add documentation to LocalEnvironmentFactory.