package net.wouto.simplemongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class SimpleConnection {
    
    private String ip;
    private int port;
    
    private MongoClient client;
    private SimpleScheduler scheduler;
    
    private MongoCredential credential;
    
    public SimpleConnection() {
        this("localhost");
    }
    
    public SimpleConnection(String ip) {
        this(ip, 27017);
    }
    
    public SimpleConnection(String ip, int port) {
        this.ip = ip;
        this.port = port;
        this.scheduler = new SimpleScheduler(this);
        this.credential = null;
    }
    
    public void setAuthentication(String username, String password, String database) {
        this.credential = MongoCredential.createCredential(username, database, password.toCharArray());
    }
    
    public void connect() throws UnknownHostException {
        ServerAddress address = new ServerAddress(this.ip, this.port);
        if (this.credential != null) {
            this.client = new MongoClient(address, Arrays.asList(this.credential));
        } else {
            this.client = new MongoClient(address);
        }
    }
    
    public void disconnect() {
        this.client.close();
        this.client = null;
    }
    
    public SimpleScheduler getScheduler() {
        return this.scheduler;
    }
    
    public SimpleCollection getCollection(String database, String collection) {
        if (this.client == null) {
            return null;
        }
        return this.scheduler.getCollection(database, collection);
    }
    
    public String getIp() {
        return this.ip;
    }
    
    public int getPort() {
        return this.port;
    }
    
    public MongoClient getConnection() {
        return this.client;
    }
    
    public boolean isConnected() {
        return (this.client != null);
    }
    
    
}

@@ -82,25 +82,24 @@
 b.MongoC
-redential
+lientURI
 ;%0Aimport
@@ -111,29 +111,31 @@
 mongodb.
-ServerAddress
+MongoCredential
 ;%0Aimport
@@ -169,33 +169,8 @@
 ion;
-%0Aimport java.util.Arrays;
 %0A%0Apu
@@ -346,34 +346,79 @@
 ate 
-MongoCredential credential
+String username;%0A    private String password;%0A    private String authDB
 ;%0A  
@@ -731,26 +731,82 @@
    this.
-credential
+username = null;%0A        this.password = null;%0A        this.authDB
  = null;
@@ -921,264 +921,371 @@
 his.
-credential = MongoCredential.createCredential(username, database, password.toCharArray());%0A    %7D%0A    %0A    public void connect() throws UnknownHostException %7B%0A        ServerAddress address = new ServerAddress(this.ip, this.port);%0A        if (this.credential
+username = username;%0A        this.password = password;%0A        this.authDB = database;%0A    %7D%0A    %0A    public void connect() throws UnknownHostException %7B%0A        if (this.ip.endsWith(%22/%22)) %7B%0A            this.ip = this.ip.substring(0, this.ip.length() - 1);%0A        %7D%0A        MongoClientURI uri;%0A        if (this.username != null && this.password != null && authDB
  != 
@@ -1300,35 +1300,27 @@
             
-this.client
+uri
  = new Mongo
@@ -1329,48 +1329,128 @@
 ient
-(address, Array
+URI(%22mongodb://%22 + this.username + %22:%22 + thi
 s.
+p
 as
-List(this.credential)
+sword + %22@%22 + this.ip + %22:%22 + this.port + %22/?authSource=%22 + this.authDB
 );%0A 
@@ -1473,35 +1473,27 @@
             
-this.client
+uri
  = new Mongo
@@ -1502,28 +1502,114 @@
 ient
-(address);%0A        %7D
+URI(%22mongodb://%22 + this.ip + %22:%22 + this.port + %22/%22);%0A        %7D%0A        this.client = new MongoClient(uri);
 %0A   

Fix authentication to databases not working properly