package fr.synchrotron.soleil.ica.ci.lib.mongodb.util;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author Gregory Boissinot
 */
public class BasicMongoDBDataSource implements MongoDBDataSource {

    public static final String DEFAULT_MONGODB_HOST = "localhost";
    public static final int DEFAULT_MONGODB_PORT = 27017;
    public static final String DEFAULT_MONGODB_DBNAME = "artifactRepository";

    private MongoClient mongo;
    private String mongoDBName;

    public BasicMongoDBDataSource() {
        init(DEFAULT_MONGODB_HOST, DEFAULT_MONGODB_PORT, DEFAULT_MONGODB_DBNAME);
    }

    public BasicMongoDBDataSource(String mongoHost, int mongoPort) {
        init(mongoHost, mongoPort, DEFAULT_MONGODB_DBNAME);
    }

    public BasicMongoDBDataSource(String mongoHost, int mongoPort, String mongoDBName) {
        init(mongoHost, mongoPort, mongoDBName);
    }

    public BasicMongoDBDataSource(Properties properties) {
        init(properties.getProperty("mongo.host"),
                Integer.parseInt(properties.getProperty("mongo.port")),
                properties.getProperty("mongo.dbname"));
    }

    public BasicMongoDBDataSource(List mongoDBInstances) {
        initInstances(mongoDBInstances, DEFAULT_MONGODB_DBNAME);
    }

    public BasicMongoDBDataSource(List mongoDBInstances, String mongoDBName) {
        initInstances(mongoDBInstances, mongoDBName);
    }

    private void init(String mongoHost, int mongoPort, String mongoDBName) {
        try {
            mongo = new MongoClient(mongoHost, mongoPort);
        } catch (UnknownHostException ue) {
            throw new MongoDBException(ue);
        }
        this.mongoDBName = mongoDBName;
    }

    private void initInstances(List<MongoDBInstance> mongoDBInstances, String mongoDBName) {
        List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
        for (MongoDBInstance mongoDBInstance : mongoDBInstances) {
            try {
                serverAddresses.add(new ServerAddress(mongoDBInstance.getHost(), mongoDBInstance.getPort()));
            } catch (UnknownHostException ue) {
                throw new MongoDBException(ue);
            }
        }
        mongo = new MongoClient(serverAddresses);
        this.mongoDBName = mongoDBName;
    }

    public DB getMongoDB() {
        return mongo.getDB(mongoDBName);
    }

    public DB getMongoDB(String dbName) {
        return mongo.getDB(dbName);
    }

}
@@ -1625,32 +1625,92 @@
  mongoDBName) %7B%0A
+        checkParameters(mongoHost, mongoPort, mongoDBName);%0A
         try %7B%0A  
@@ -2159,32 +2159,233 @@
           try %7B%0A
+                final String mongoHost = mongoDBInstance.getHost();%0A                final int mongoPort = mongoDBInstance.getPort();%0A                checkParameters(mongoHost, mongoPort, mongoDBName);%0A
                 
@@ -2431,55 +2431,23 @@
 ongo
-DBInstance.get
 Host
-()
 , mongo
-DBInstance.get
 Port
-()
 ));%0A
@@ -2655,32 +2655,559 @@
 oDBName;%0A    %7D%0A%0A
+    private void checkParameters(Object... parameters) %7B%0A%0A        if (parameters.length == 0) %7B%0A            return;%0A        %7D%0A%0A        for (Object parameter : parameters) %7B%0A            if (parameter == null) %7B%0A                throw new NullPointerException(%22All parameters are requited%22);%0A            %7D%0A%0A            if (parameter instanceof Number) %7B%0A                if (parameter.equals(-1)) %7B%0A                    throw new NullPointerException(%22All parameters are requited%22);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A
     public DB ge

Check parameters