/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author valerio schiavoni
 *
 */

package org.apache.gora.infinispan;

import org.apache.gora.GoraTestDriver;
import org.apache.gora.infinispan.store.InfinispanStore;

import java.io.File;

import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.service.CassandraDaemon;

// Logging imports
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper class for third party tests using gora-infinispan backend. 
 * @see GoraTestDriver for test specifics.
 * This driver is the base for all test cases that require an embedded Infinispan
 * server. 
 * It starts (setUp) and stops (tearDown) embedded Infinispan server.
 * 

 */

public class GoraInfinispanTestDriver extends GoraTestDriver {
  private static Logger log = LoggerFactory.getLogger(GoraInfinispanTestDriver.class);
  
  private String baseDirectory = "target/test";

  private CassandraDaemon cassandraDaemon;

  private Thread cassandraThread;

  /**
   * @return temporary base directory of running cassandra instance
   */
  public String getBaseDirectory() {
    return baseDirectory;
  }

  public GoraInfinispanTestDriver() {
    super(InfinispanStore.class);
  }
	
  /**
   * Starts embedded Cassandra server.
   *
   * @throws Exception
   * 	if an error occurs
   */
  @Override
  public void setUpClass() throws Exception {
    super.setUpClass();
    log.info("Starting embedded Cassandra Server...");
    try {
      cleanupDirectoriesFailover();
      FileUtils.createDirectory(baseDirectory);
      System.setProperty("log4j.configuration", "log4j-server.properties");
      System.setProperty("cassandra.config", "cassandra.yaml");
      
      cassandraDaemon = new CassandraDaemon();
      cassandraDaemon.init(null);
      cassandraThread = new Thread(new Runnable() {
	
        public void run() {
          try {
            cassandraDaemon.start();
          } catch (Exception e) {
            log.error("Embedded casandra server run failed!", e);
          }
        }
      });
	
      cassandraThread.setDaemon(true);
      cassandraThread.start();
    } catch (Exception e) {
      log.error("Embedded casandra server start failed!", e);

      // cleanup
      tearDownClass();
    }
  }

  /**
   * Stops embedded Cassandra server.
   *
   * @throws Exception
   * 	if an error occurs
   */
  @Override
  public void tearDownClass() throws Exception {
    super.tearDownClass();
    log.info("Shutting down Embedded Cassandra server...");
    if (cassandraThread != null) {
      cassandraDaemon.stop();
      cassandraDaemon.destroy();
      cassandraThread.interrupt();
      cassandraThread = null;
    }
    cleanupDirectoriesFailover();
  }  

  /**
   * Cleans up cassandra's temporary base directory.
   *
   * In case o failure waits for 250 msecs and then tries it again, 3 times totally.
   */
  public void cleanupDirectoriesFailover() {
    int tries = 3;
    while (tries-- > 0) {
      try {
        cleanupDirectories();
        break;
      } catch (Exception e) {
        // ignore exception
        try {
          Thread.sleep(250);
        } catch (InterruptedException e1) {
          // ignore exception
        }
      }
    }
  }

  /**
   * Cleans up cassandra's temporary base directory.
   *
   * @throws Exception
   * 	if an error occurs
   */
  public void cleanupDirectories() throws Exception {
    File dirFile = new File(baseDirectory);
    if (dirFile.exists()) {
      FileUtils.deleteRecursive(dirFile);
    }
  }
}

@@ -1002,108 +1002,8 @@
 e;%0A%0A
-import org.apache.cassandra.io.util.FileUtils;%0Aimport org.apache.cassandra.service.CassandraDaemon;%0A
 %0A// 
@@ -1523,283 +1523,8 @@
 ;%0A  
-%0A  private String baseDirectory = %22target/test%22;%0A%0A  private CassandraDaemon cassandraDaemon;%0A%0A  private Thread cassandraThread;%0A%0A  /**%0A   * @return temporary base directory of running cassandra instance%0A   */%0A  public String getBaseDirectory() %7B%0A    return baseDirectory;%0A  %7D
 %0A%0A  
@@ -1821,782 +1821,34 @@
 ded 
-Cassandra Server...%22);%0A    try %7B%0A      cleanupDirectoriesFailover();%0A      FileUtils.createDirectory(baseDirectory);%0A      System.setProperty(%22log4j.configuration%22, %22log4j-server.properties%22);%0A      System.setProperty(%22cassandra.config%22, %22cassandra.yaml%22);%0A      %0A      cassandraDaemon = new CassandraDaemon();%0A      cassandraDaemon.init(null);%0A      cassandraThread = new Thread(new Runnable() %7B%0A%09%0A        public void run() %7B%0A          try %7B%0A            cassandraDaemon.start();%0A          %7D catch (Exception e) %7B%0A            log.error(%22Embedded casandra server run failed!%22, e);%0A          %7D%0A        %7D%0A      %7D);%0A%09%0A      cassandraThread.setDaemon(true);%0A      cassandraThread.start(
+Infinispan Server...%22
 );
-%0A
    
- %7D catch (Exception e) %7B%0A      log.error(%22Embedded casandra server start failed!%22, e);%0A
 %0A   
@@ -1884,25 +1884,24 @@
 lass();%0A    
-%7D
 %0A  %7D%0A%0A  /**%0A
@@ -2093,1074 +2093,17 @@
     
-log.info(%22Shutting down Embedded Cassandra server...%22);%0A    if (cassandraThread != null) %7B%0A      cassandraDaemon.stop();%0A      cassandraDaemon.destroy();%0A      cassandraThread.interrupt();%0A      cassandraThread = null;%0A    %7D%0A    cleanupDirectoriesFailover();%0A  %7D  %0A%0A  /**%0A   * Cleans up cassandra's temporary base directory.%0A   *%0A   * In case o failure waits for 250 msecs and then tries it again, 3 times totally.%0A   */%0A  public void cleanupDirectoriesFailover() %7B%0A    int tries = 3;%0A    while (tries-- %3E 0) %7B%0A      try %7B%0A        cleanupDirectories();%0A        break;%0A      %7D catch (Exception e) %7B%0A        // ignore exception%0A        try %7B%0A          Thread.sleep(250);%0A        %7D catch (InterruptedException e1) %7B%0A          // ignore exception%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  /**%0A   * Cleans up cassandra's temporary base directory.%0A   *%0A   * @throws Exception%0A   * %09if an error occurs%0A   */%0A  public void cleanupDirectories() throws Exception %7B%0A    File dirFile = new File(baseDirectory);%0A    if (dirFile.exists()) %7B%0A      FileUtils.deleteRecursive(dirFile);%0A    %7D%0A  %7D
+%0A  %7D  %0A%0A%0A 
 %0A%7D%0A

remove cassandra-related code