@@ -0,0 +1,2638 @@
+package me.prettyprint.cassandra.testutils;%0A%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A%0Aimport org.apache.cassandra.contrib.utils.service.CassandraServiceDataCleaner;%0Aimport org.apache.cassandra.io.util.FileUtils;%0Aimport org.apache.cassandra.service.EmbeddedCassandraService;%0Aimport org.apache.thrift.transport.TTransportException;%0A%0A/**%0A*%0A* @author Ran Tavory (rantav@gmail.com)%0A*%0A*/%0Apublic class EmbeddedServerHelper %7B%0A%0A  private static final String TMP = %22tmp%22;%0A%0A  private EmbeddedCassandraService cassandra;%0A%0A  /**%0A   * Set embedded cassandra up and spawn it in a new thread.%0A   *%0A   * @throws TTransportException%0A   * @throws IOException%0A   * @throws InterruptedException%0A   */%0A  public void setup() throws TTransportException, IOException, InterruptedException %7B%0A    // delete tmp dir first%0A    rmdir(TMP);%0A    // make a tmp dir and copy storag-conf.xml and log4j.properties to it%0A    copy(%22/storage-conf.xml%22, TMP);%0A    copy(%22/log4j.properties%22, TMP);%0A    System.setProperty(%22storage-config%22, TMP);%0A%0A    CassandraServiceDataCleaner cleaner = new CassandraServiceDataCleaner();%0A    cleaner.prepare();%0A    cassandra = new EmbeddedCassandraService();%0A    cassandra.init();%0A    Thread t = new Thread(cassandra);%0A    t.setDaemon(true);%0A    t.start();%0A  %7D%0A%0A  public void teardown() %7B%0A    CassandraServiceDataCleaner cleaner = new CassandraServiceDataCleaner();%0A%0A    try %7B%0A      cleaner.cleanupDataDirectories();%0A      rmdir(TMP);%0A    %7D catch (IOException e) %7B%0A      // IGNORE%0A    %7D%0A  %7D%0A%0A  private static void rmdir(String dir) throws IOException %7B%0A    File dirFile = new File(dir);%0A    if (dirFile.exists()) %7B%0A      FileUtils.deleteDir(new File(dir));%0A    %7D%0A  %7D%0A  /**%0A   * Copies a resource from within the jar to a directory.%0A   *%0A   * @param resource%0A   * @param directory%0A   * @throws IOException%0A   */%0A  private static void copy(String resource, String directory) throws IOException %7B%0A    mkdir(directory);%0A    InputStream is = EmbeddedServerHelper.class.getResourceAsStream(resource);%0A    String fileName = resource.substring(resource.lastIndexOf(%22/%22) + 1);%0A    File file = new File(directory + System.getProperty(%22file.separator%22) + fileName);%0A    OutputStream out = new FileOutputStream(file);%0A    byte buf%5B%5D = new byte%5B1024%5D;%0A    int len;%0A    while ((len = is.read(buf)) %3E 0) %7B%0A      out.write(buf, 0, len);%0A    %7D%0A    out.close();%0A    is.close();%0A  %7D%0A%0A  /**%0A   * Creates a directory%0A   * @param dir%0A   * @throws IOException%0A   */%0A  private static void mkdir(String dir) throws IOException %7B%0A    FileUtils.createDirectory(dir);%0A  %7D%0A%7D%0A

Move EmbeddedServerHelper from test to mail so it's easily reused by external applications. take two...