// Path: src/main/java/com/lowereast/guiceymongo/GuiceyMongoException.java
// public class GuiceyMongoException extends RuntimeException {
//     public GuiceyMongoException() {
//     }
//     public GuiceyMongoException(String message) {
//         super(message);
//     }
//     public GuiceyMongoException(String message, Throwable throwable) {
//         super(message, throwable);
//     }
//     public GuiceyMongoException(Throwable throwable) {
//         super(throwable);
//     }
// }


import com.google.inject.*;
import com.lowereast.guiceymongo.GuiceyMongoException;
import com.lowereast.guiceymongo.guice.annotation.GuiceyMongoDatabase;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import java.net.UnknownHostException;
import java.util.UUID;
	}

	private void cacheDB() throws MongoException, UnknownHostException {
		String databaseKey = ((GuiceyMongoDatabase)key.getAnnotation()).value();
		
		String clonedConfiguration = getInstance(_injector, Key.get(String.class, AnnotationUtil.clonedConfiguration(_configuration)));
		if (clonedConfiguration == null) {
			String database = _injector.getInstance(Key.get(String.class, AnnotationUtil.configuredDatabase(_configuration, databaseKey)));
			Mongo connection = getConnection(_configuration, databaseKey);
			_cachedDB = connection.getDB(database);
            // test connection
            _cachedDB.getCollectionNames();
		} else {
			Mongo connection = getConnection(clonedConfiguration, databaseKey);
			_cachedDB = connection.getDB(UUID.randomUUID().toString());
			String clonedDatabase = _injector.getInstance(Key.get(String.class, AnnotationUtil.configuredDatabase(clonedConfiguration, databaseKey)));
			_cachedDB.eval("var c = connect('" + clonedDatabase + "'); c.system.js.find().forEach(function(x){db.system.js.save(x)}); db.system.js.ensureIndex({_id: 1});");
			
			Runtime.getRuntime().addShutdownHook(new RemoveTemporaryDB(_cachedDB));
		}
	}
 
	private DB _cachedDB;
	
	public DB get() {
		try {
			if (_cachedDB == null)
				cacheDB();
			return _cachedDB;
		} catch (MongoException e) {
// Path: src/main/java/com/lowereast/guiceymongo/GuiceyMongoException.java
// public class GuiceyMongoException extends RuntimeException {
//     public GuiceyMongoException() {
//     }
//     public GuiceyMongoException(String message) {
//         super(message);
//     }
//     public GuiceyMongoException(String message, Throwable throwable) {
//         super(message, throwable);
//     }
//     public GuiceyMongoException(Throwable throwable) {
//         super(throwable);
//     }
// }


// Path: src/main/java/com/lowereast/guiceymongo/guice/spi/DBProviderModule.java
import com.google.inject.*;
import com.lowereast.guiceymongo.GuiceyMongoException;
import com.lowereast.guiceymongo.guice.annotation.GuiceyMongoDatabase;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import java.net.UnknownHostException;
import java.util.UUID;

	}

	private void cacheDB() throws MongoException, UnknownHostException {
		String databaseKey = ((GuiceyMongoDatabase)key.getAnnotation()).value();
		
		String clonedConfiguration = getInstance(_injector, Key.get(String.class, AnnotationUtil.clonedConfiguration(_configuration)));
		if (clonedConfiguration == null) {
			String database = _injector.getInstance(Key.get(String.class, AnnotationUtil.configuredDatabase(_configuration, databaseKey)));
			Mongo connection = getConnection(_configuration, databaseKey);
			_cachedDB = connection.getDB(database);
            // test connection
            _cachedDB.getCollectionNames();
		} else {
			Mongo connection = getConnection(clonedConfiguration, databaseKey);
			_cachedDB = connection.getDB(UUID.randomUUID().toString());
			String clonedDatabase = _injector.getInstance(Key.get(String.class, AnnotationUtil.configuredDatabase(clonedConfiguration, databaseKey)));
			_cachedDB.eval("var c = connect('" + clonedDatabase + "'); c.system.js.find().forEach(function(x){db.system.js.save(x)}); db.system.js.ensureIndex({_id: 1});");
			
			Runtime.getRuntime().addShutdownHook(new RemoveTemporaryDB(_cachedDB));
		}
	}
 
	private DB _cachedDB;
	
	public DB get() {
		try {
			if (_cachedDB == null)
				cacheDB();
			return _cachedDB;
		} catch (MongoException e) {
			throw new GuiceyMongoException("Could not connect to an instance of MongoDB", e);