/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * Licensed 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.
 */

package org.jongo.util;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoURI;

import java.net.UnknownHostException;

class MongoHolder {

    private static final String MONGOHQ_FLAG = "jongo.mongohq.uri";
    private static volatile Mongo MONGO;

    public static Mongo getInstance() throws UnknownHostException {
        if (MONGO == null) {
            setInstance();
        }
        return MONGO;
    }

    private synchronized static void setInstance() throws UnknownHostException {
        if (mustRunTestsAgainstMongoHQ()) {
            MONGO = getMongoHQ();
        } else {
            MONGO = getLocalMongo();
        }
    }

    private static boolean mustRunTestsAgainstMongoHQ() {
        return System.getProperty(MONGOHQ_FLAG) != null;
    }

    private static Mongo getMongoHQ() throws UnknownHostException {
        String uri = System.getProperty(MONGOHQ_FLAG);
        MongoURI mongoURI = new MongoURI(uri);
        DB db = mongoURI.connectDB();
        db.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
        return db.getMongo();
    }

    private static Mongo getLocalMongo() throws UnknownHostException {
        return new Mongo("127.0.0.1");
    }
}

@@ -914,49 +914,8 @@
 ri%22;
-%0A    private static volatile Mongo MONGO;
 %0A%0A  
@@ -984,184 +984,8 @@
 n %7B%0A
-        if (MONGO == null) %7B%0A            setInstance();%0A        %7D%0A        return MONGO;%0A    %7D%0A%0A    private synchronized static void setInstance() throws UnknownHostException %7B%0A
     
@@ -1040,28 +1040,31 @@
     
-MONGO = getMongoHQ()
+return MongoHQ.instance
 ;%0A  
@@ -1074,15 +1074,8 @@
    %7D
- else %7B
 %0A   
@@ -1083,46 +1083,35 @@
     
-    MONGO = getLocalMongo();%0A        %7D
+return LocalMongo.instance;
 %0A   
@@ -1259,57 +1259,178 @@
 tic 
-Mongo getMongoHQ() throws UnknownHostException %7B%0A
+class MongoHQ %7B%0A%0A        private static Mongo instance = getAuthenticatedInstance();%0A%0A        private static Mongo getAuthenticatedInstance() %7B%0A            try %7B%0A        
     
@@ -1480,16 +1480,24 @@
 _FLAG);%0A
+        
         
@@ -1543,16 +1543,24 @@
         
+ 
+       
 DB db = 
@@ -1589,16 +1589,24 @@
         
+       
+ 
 db.authe
@@ -1662,16 +1662,24 @@
 ord());%0A
+        
         
@@ -1708,120 +1708,557 @@
     
-%7D%0A%0A    private static Mongo getLocalMongo() throws UnknownHostException %7B%0A        return new Mongo(%22127.0.0.1%22);
+        %7D catch (UnknownHostException e) %7B%0A                throw new RuntimeException(%22Unable to reach mongo database test instance%22, e);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private static class LocalMongo %7B%0A%0A        private static Mongo instance = getLocalInstance();%0A%0A        private static Mongo getLocalInstance() %7B%0A            try %7B%0A                return new Mongo(%22127.0.0.1%22);%0A            %7D catch (UnknownHostException e) %7B%0A                throw new RuntimeException(%22Unable to reach mongo database test instance%22, e);%0A            %7D%0A        %7D
 %0A   

Initialize MongoHolder on demand