package net.explorviz.security.persistence.mongo;

import com.mongodb.MongoClient;
import java.net.UnknownHostException;
import javax.ws.rs.InternalServerErrorException;
import net.explorviz.shared.annotations.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles the access to the {@link MongoClient} instance. Only one instance of {@link MongoClient}
 * should exist in the application for performance reasons (pooling).
 *
 */
public final class MongoClientHelper {

  private final Logger LOGGER = LoggerFactory.getLogger(this.getClass().getSimpleName());


  private static final String DEFAUL_ADDRESS = "192.168.99.100";
  private static final String DEFAULT_PORT = "27017";



  @Config("mongo.ip")
  private String host;

  @Config("mongo.port")
  private String port;



  private MongoClient client = null;

  /**
   * Retrieves the connection to the database (i.e. the {@link MongoClient}). If the client is
   * closed, all connections are lost and the mongoDB instance cannot longer be used. Use
   * {@link MongoClientHelper#reset()} to obtain a new client.
   *
   * @return the client
   */
  public MongoClient getMongoClient() {
    if (this.client == null) {
      this.LOGGER.info("Connecting to " + this.host + ":" + this.port);

      try {
        this.client = new MongoClient(this.host + ":" + this.port);
        this.LOGGER.info("Connected to " + this.host + ":" + this.port);
      } catch (final UnknownHostException e) {
        this.LOGGER.error("Target not reachable: " + this.host + ":" + this.port);
        throw new InternalServerErrorException();
      }
    }

    return this.client;
  }

}

@@ -76,16 +76,58 @@
 Client;%0A
+import com.mongodb.MongoTimeoutException;%0A
 import j
@@ -1244,81 +1244,8 @@
 ) %7B%0A
-      this.LOGGER.info(%22Connecting to %22 + this.host + %22:%22 + this.port);%0A%0A
     
@@ -1252,16 +1252,16 @@
   try %7B%0A
+
         
@@ -1312,32 +1312,72 @@
 %22 + this.port);%0A
+        this.client.getDatabaseNames();%0A
         this.LOG
@@ -1429,24 +1429,24 @@
 this.port);%0A
-
       %7D catc
@@ -1475,16 +1475,40 @@
 ception 
+%7C MongoTimeoutException 
 e) %7B%0A   

Return 500 if the database is not reachable