private static void copyDirectory(File src, File dest) {
    for (String current : src.list()) {
      final File srcFile = new File(src, current);
      final File destFile = new File(dest, current);

      if (srcFile.isDirectory()) {
        destFile.mkdir();
        copyDirectory(srcFile, destFile);
      } else {
        try {
          final InputStream in = new BufferedInputStream(new FileInputStream(srcFile));
          final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));

          try {
            int i;
            while ((i = in.read()) != -1) {
              out.write(i);
            }
          } catch (IOException e) {
            throw new RuntimeException(
                "Error copying " + srcFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(),
                e);
          } finally {
            StreamUtils.safeClose(in);
            StreamUtils.safeClose(out);
          }

        } catch (FileNotFoundException e) {
          throw new RuntimeException(e);
        }
      }
    }
  }
  static void setupCleanDirectories(File jbossHomeDir, Properties props) {
    File tempRoot = getTempRoot(props);
    if (tempRoot == null) {
      return;
    }

    File originalConfigDir =
        getFileUnderAsRoot(
            jbossHomeDir, props, ServerEnvironment.SERVER_CONFIG_DIR, "configuration");
    File orginalDataDir =
        getFileUnderAsRoot(jbossHomeDir, props, ServerEnvironment.SERVER_DATA_DIR, "data");

    File configDir = new File(tempRoot, "config");
    configDir.mkdir();
    File dataDir = new File(tempRoot, "data");
    dataDir.mkdir();

    copyDirectory(originalConfigDir, configDir);
    copyDirectory(orginalDataDir, dataDir);

    props.put(ServerEnvironment.SERVER_CONFIG_DIR, configDir.getAbsolutePath());
    props.put(ServerEnvironment.SERVER_DATA_DIR, dataDir.getAbsolutePath());
  }