Example usage for com.mongodb MongoClient getMongoOptions

List of usage examples for com.mongodb MongoClient getMongoOptions

Introduction

In this page you can find the example usage for com.mongodb MongoClient getMongoOptions.

Prototype

@SuppressWarnings("deprecation")
@Deprecated
public MongoOptions getMongoOptions() 

Source Link

Document

Returns the mongo options.

Changes to MongoOptions that are done after connection are not reflected.

Usage

From source file:com.owly.srv.config.ApplicationListener.java

License:Apache License

/**
 * This methond checks status of the stadistics server database
 * /*from  w  w w . ja v  a2  s .  c om*/
 * @param StadisticsServer
 * @param listInitRemoteServer
 * @return a bolean true in case parameters of database are OK, and database
 *         is up.
 */
public boolean checkDatabaseStatus(StatsServer StadisticsServer, ArrayList<RemoteServer> listInitRemoteServer) {

    final String CollectionDB = "StatsSrvCfg";

    // logger.debug("Configuration of the Stats Server : " +
    // StadisticsServer.toString());

    if (!StadisticsServer.isEnabled()) {
        logger.error("Statistics server is disabled, check configuration file");
        RuntimeException ex = new RuntimeException("Error starting the Application ");
        throw ex;

    } else {
        if (!StadisticsServer.getStatsDatabaseType().equals("MONGODB")) {
            logger.error(
                    "Statistics server database should be MongoDB (others not implemented), check configuration file");
            RuntimeException ex = new RuntimeException("Error starting the Application ");
            throw ex;
        } else {
            if (!StadisticsServer.getStatsIpAddress().equals("127.0.0.1")) {
                logger.info("Statistics server IP is not 127.0.0.1 --> Database is a remote server");
            } else {
                logger.info("Statistics server IP is  127.0.0.1 --> Database is local server");
            }

            logger.debug("Checking if MongoDB is up and running for IP : "
                    + StadisticsServer.getStatsIpAddress() + " and port : " + StadisticsServer.getStatsPort());
            try {

                /**** Connect to MongoDB ****/
                // Since 2.10.0, uses MongoClient
                MongoClient mongoClient = new MongoClient(StadisticsServer.getStatsIpAddress(),
                        StadisticsServer.getStatsPort());

                try {
                    Socket socket = mongoClient.getMongoOptions().socketFactory.createSocket();
                    socket.connect(mongoClient.getAddress().getSocketAddress());
                    socket.close();
                } catch (IOException e) {
                    logger.error("mongoDB Error : " + e.getMessage());
                    logger.error("Exception ::", e);
                    RuntimeException ex = new RuntimeException("Error starting the Application ");
                    throw ex;

                }

                /**** Get database ****/
                // if database doesn't exists, MongoDB will create it
                // for you
                DB statsDB = mongoClient.getDB(StadisticsServer.getStatsDatabase());

                StatsServerMongoDAOImpl statServerCfg = new StatsServerMongoDAOImpl(statsDB);

                RemoteServerMongoDAOImpl remoteServerCfg = new RemoteServerMongoDAOImpl(statsDB);

                // Drop all previous configuration in the database

                statServerCfg.deleteAllStatsServer();
                //remoteServerCfg.deleteAllRemoteServer();

                // Create new configuration in database
                int numberStats = statServerCfg.numberStatsServer();

                if (numberStats == 0) {
                    logger.debug("Actual Server Stats configuration NOT in database");
                    logger.debug("Insert configuration in Database");

                    // Insert Stats Server in Database
                    statServerCfg.insertStatsServer(StadisticsServer);

                    // Insert Remote Servers in the Database
                    for (int srv = 0; srv < listInitRemoteServer.size(); srv++) {
                        logger.debug("Remote Server to Insert in database (" + srv + ") : "
                                + listInitRemoteServer.get(srv).toString());
                        RemoteServer remoteServer = listInitRemoteServer.get(srv);
                        String nameSrv = remoteServer.getName();
                        String ipSrv = remoteServer.getNodeIPAddress();

                        // Drop the server from database is already exists
                        remoteServerCfg.deleteRemoteServer(nameSrv, ipSrv);

                        // Insert the remote server. 
                        remoteServerCfg.insertRemoteServer(listInitRemoteServer.get(srv));
                        logger.debug("Remote Serverinserted");

                    }

                    // Add a unique index based on IP and name
                    remoteServerCfg.addUniqueIndex("NodeIPAddress", "Name");

                } else {
                    logger.error("Configuration in database " + CollectionDB
                            + " is not OK, manual action is required");
                    // Close the conection to database
                    logger.info("Closing MongoClient");
                    mongoClient.close();
                    RuntimeException ex = new RuntimeException("Error starting the Application ");
                    throw ex;
                }

                // Close the conection to database
                logger.info("Closing MongoClient");
                mongoClient.close();

            } catch (UnknownHostException e) {
                logger.error("mongoDB Error : " + e.getMessage());
                logger.error("Exception ::", e);
                RuntimeException ex = new RuntimeException("Error starting the Application ");
                throw ex;
            }

        }
    }

    return true;

}