package org.apache.usergrid.persistence.core.cassandra;


import com.google.common.io.Files;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.netflix.astyanax.test.EmbeddedCassandra;
import java.io.File;
import java.io.IOException;
import org.apache.cassandra.io.util.FileUtils;
import org.apache.usergrid.persistence.core.astyanax.CassandraFig;
import org.apache.usergrid.persistence.core.util.AvailablePortFinder;
import org.safehaus.guicyfig.Env;
import org.safehaus.guicyfig.EnvironResource;
import org.safehaus.guicyfig.GuicyFigModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @TODO - I wanted this in the test module but unfortunately that will create a circular dep
 *         due to the inclusion of the MigrationManager
 */
public class CassandraRule extends EnvironResource {
    private static final Logger LOG = LoggerFactory.getLogger( CassandraRule.class );

    private static final Object mutex = new Object();

    private static EmbeddedCassandra cass;

    private static boolean started = false;

    private final CassandraFig cassandraFig;


    public CassandraRule() {
        super( Env.UNIT );
        Injector injector = Guice.createInjector( new GuicyFigModule( CassandraFig.class ) );
        cassandraFig = injector.getInstance( CassandraFig.class );
    }

    @Override
    protected void before() throws Throwable {

        if ( !cassandraFig.isEmbedded()) {
            LOG.info("Using external Cassandra"); 
        }

        if ( started ) {
            return;
        }
//
//        synchronized ( mutex ) {
//
//            //second into mutex
//            if(started){
//                return;
//            }
//
//            File dataDir = Files.createTempDir();
//            dataDir.deleteOnExit();
//
//            //cleanup before we run, shouldn't be necessary, but had the directory exist during JVM kill
//            if( dataDir.exists() ) {
//                FileUtils.deleteRecursive( dataDir );
//            }
//
//            try {
//                LOG.info( "Starting cassandra" );
//
//                cass = new EmbeddedCassandra( dataDir, "Usergrid", 9160,
//                        AvailablePortFinder.getNextAvailable() );
//                cass.start();
//
//                LOG.info( "Cassandra boostrapped" );
//
//                started = true;
//            }
//            catch ( IOException e ) {
//                throw new RuntimeException( "Unable to start cassandra", e );
//            }
//        }
    }


    @Override
    protected void after() {

        //TODO TN.  this should only really happen when we shut down
//        cass.stop();
    }
}

@@ -1549,37 +1549,33 @@
 turn;%0A        %7D%0A
-//%0A//
+%0A
         synchron
@@ -1591,21 +1591,17 @@
 tex ) %7B%0A
-//%0A//
+%0A
         
@@ -1624,18 +1624,16 @@
 o mutex%0A
-//
         
@@ -1649,18 +1649,16 @@
 arted)%7B%0A
-//
         
@@ -1673,18 +1673,16 @@
 return;%0A
-//
         
@@ -1679,37 +1679,33 @@
 ;%0A            %7D%0A
-//%0A//
+%0A
             File
@@ -1738,18 +1738,16 @@
 pDir();%0A
-//
         
@@ -1770,29 +1770,25 @@
 teOnExit();%0A
-//%0A//
+%0A
             
@@ -1880,18 +1880,16 @@
 VM kill%0A
-//
         
@@ -1913,26 +1913,24 @@
 xists() ) %7B%0A
-//
             
@@ -1967,26 +1967,24 @@
  dataDir );%0A
-//
             
@@ -1985,21 +1985,17 @@
       %7D%0A
-//%0A//
+%0A
         
@@ -2004,18 +2004,16 @@
   try %7B%0A
-//
         
@@ -2050,29 +2050,25 @@
 ssandra%22 );%0A
-//%0A//
+%0A
             
@@ -2128,18 +2128,16 @@
 , 9160,%0A
-//
         
@@ -2190,26 +2190,24 @@
 ilable() );%0A
-//
             
@@ -2224,21 +2224,17 @@
 tart();%0A
-//%0A//
+%0A
         
@@ -2282,13 +2282,9 @@
  );%0A
-//%0A//
+%0A
     
@@ -2311,18 +2311,16 @@
 = true;%0A
-//
         
@@ -2317,34 +2317,32 @@
 ;%0A            %7D%0A
-//
             catc
@@ -2363,18 +2363,16 @@
 n e ) %7B%0A
-//
         
@@ -2441,18 +2441,16 @@
 %22, e );%0A
-//
         
@@ -2455,18 +2455,16 @@
       %7D%0A
-//
         

Rollback accidental rule commit