Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:com.imaginea.mongodb.common.ConfigMongoInstanceProvider.java

License:Apache License

/**
 * Get the initial MongoIP and mongoPort from config file and returns a
 * MongoInstance.//  www .  j  av a  2  s . c  o m
 * 
 * @throws MongoHostUnknownException
 *             : Mongo Host Unknown
 * @throws IOException
 * @throws FileNotFoundException
 * 
 */
public ConfigMongoInstanceProvider() throws Exception {
    try {

        Properties prop = new Properties();
        String fileName = "src/test/resources/mongo.config";
        InputStream is = new FileInputStream(fileName);
        prop.load(is);

        if (prop != null) {
            mongoHost = prop.getProperty("mongoHost");
            mongoPort = Integer.parseInt(prop.getProperty("mongoPort"));
            mongoInstance = new Mongo(mongoHost, mongoPort);
        }
        System.out.println(mongoInstance.getDatabaseNames());

    } catch (MongoInternalException e) {
        throw new MongoHostUnknownException(
                "HOST_UNKNOWN"
                        + "Change your mongo data directory path or mongod path in ConfigMongoInstanceProvider",
                e);
    } catch (UnknownHostException e) {
        throw new MongoHostUnknownException("HOST_UNKNOWN", e);
    } catch (MongoException e) {
        throw new MongoHostUnknownException("HOST_UNKNOWN", e);
    } catch (Exception e) {

        throw new Exception("ANY_OTHER_EXCEPTION", e);

    }
}

From source file:com.imaginea.mongodb.requestdispatchers.UserLogin.java

License:Apache License

/**
 * Authenticates User by verifying Mongo config details against admin
 * database and authenticating user to that Db. A facility for guest login
 * is also allowed when both fields username and password are empty.
 * <p>/*  ww  w  .ja  v  a2  s . com*/
 * Also stores a mongo instance based on database configuration.
 * 
 * @param request
 *            Request made by user for authentication
 * @param user
 *            Name of user as in admin database in mongo
 * @param password
 *            password of user as in admin database in mongo
 * @param host
 *            mongo host to connect to
 * @param mongoPort
 *            mongo Port to connect to
 *     
 * 
 * 
 */

@POST
@Produces(MediaType.APPLICATION_JSON)
public String authenticateUser(@FormParam("username") String user, @FormParam("password") final String password,
        @FormParam("host") String host, @FormParam("port") final String mongoPort,
        @Context final HttpServletRequest request) {

    // Reassign username for guest user in case of empty username and
    // password fields
    if ("".equals(user) && "".equals(password)) {
        user = "guest";
    }
    if ("127.0.0.1".equals(host)) {
        host = "localhost"; // 1 key for both in map
    }
    final String mongoHost = host;
    final String username = user;
    String response = ErrorTemplate.execute(logger, new ResponseCallback() {
        public Object execute() throws Exception {
            if ("".equals(mongoHost) || "".equals(mongoPort)) {
                ApplicationException e = new ApplicationException(ErrorCodes.MISSING_LOGIN_FIELDS,
                        "Missing Login Fields");
                return formErrorResponse(logger, e);
            }
            Mongo m = new Mongo(mongoHost, Integer.parseInt(mongoPort));
            boolean loginStatus = false;
            if ("guest".equals(username) && "".equals(password)) {
                loginStatus = true;
            } else {
                // Authorize User using <admin> Db
                DB db = m.getDB("admin");
                loginStatus = db.authenticate(username, password.toCharArray());
            }
            if (!loginStatus) {
                ApplicationException e = new ApplicationException(ErrorCodes.INVALID_USERNAME,
                        "Invalid UserName or Password");
                return formErrorResponse(logger, e);
            }
            // Add mongo Configuration Key to key dbInfo in session
            String mongoConfigKey = mongoHost + "_" + mongoPort;
            HttpSession session = request.getSession();

            Object dbInfo = session.getAttribute("dbInfo");
            if (dbInfo == null) {
                List<String> mongosInSession = new ArrayList<String>();
                mongosInSession.add(mongoConfigKey);
                session.setAttribute("dbInfo", mongosInSession);
            } else {
                @SuppressWarnings("unchecked")
                List<String> mongosInSession = (List<String>) dbInfo;
                mongosInSession.add(mongoConfigKey);
                session.setAttribute("dbInfo", mongosInSession);
            }

            // Store a MongoInstance
            if (!mongoConfigToInstanceMapping.containsKey(mongoConfigKey)) {
                Mongo mongoInstance = new Mongo(mongoHost, Integer.parseInt(mongoPort));
                mongoConfigToInstanceMapping.put(mongoConfigKey, mongoInstance);
            }
            Object usersPresent = mongoConfigToUsersMapping.get(mongoConfigKey);
            int noOfUsers = 0;
            if (usersPresent == null) {
                mongoConfigToUsersMapping.put(mongoConfigKey, noOfUsers);
            }
            noOfUsers = mongoConfigToUsersMapping.get(mongoConfigKey) + 1;
            mongoConfigToUsersMapping.put(mongoConfigKey, noOfUsers);

            String status = "Login Success";
            return status;
        }
    });
    return response;
}

From source file:com.impetus.kundera.client.MongoDBClient.java

License:Apache License

@Override
public void connect() {
    if (!isConnected) {
        log.info(">>> Connecting to MONGODB at " + contactNode + " on port " + defaultPort);
        try {//from w  w w.  j  a v a  2 s.  co  m
            mongo = new Mongo(contactNode, Integer.parseInt(defaultPort));
            mongoDb = mongo.getDB(dbName);
            isConnected = true;
            log.info("CONNECTED to MONGODB at " + contactNode + " on port " + defaultPort);
        } catch (NumberFormatException e) {
            log.error("Invalid format for MONGODB port, Unale to connect!" + "; Details:" + e.getMessage());
        } catch (UnknownHostException e) {
            log.error("Unable to connect to MONGODB at host " + contactNode + "; Details:" + e.getMessage());
        } catch (MongoException e) {
            log.error("Unable to connect to MONGODB; Details:" + e.getMessage());
        }
    }
}

From source file:com.jhkt.playgroundArena.shared.tasks.MongoDBTaskRunner.java

License:Apache License

public MongoDBTaskRunner(String hostName, int portNumber) throws UnknownHostException {
    super();/* www  . j a  v a  2s.c  om*/

    _hostNameIdentifier = hostName + portNumber;

    if ((_instanceWrapper = _mongoMaps.get(_hostNameIdentifier)) == null) {
        _instanceWrapper = new ServerConnectionWrapper<Mongo>(new Mongo(hostName, portNumber));
        _mongoMaps.put(_hostNameIdentifier, _instanceWrapper);
    }

    _instanceWrapper.incrementCount();
}

From source file:com.learning.csv.CSVStore.java

License:Apache License

private void setDatabaseParameters() {
    try {//from ww  w.j av  a2s. co  m
        this.m = new Mongo("localhost", 27017);
    } catch (UnknownHostException e) {
        System.out.println("Could not connect to localhost");
        System.exit(1);
    }
    this.db = m.getDB("mydb");
    this.coll = db.getCollection("mycoll");
}

From source file:com.liferay.mongodb.util.MongoDBUtil.java

License:Open Source License

private MongoDBUtil() {
    try {//from  ww w. j  a  v  a  2  s  .  c  om
        _mongo = new Mongo(_getServerAddresses(), _getMongoOptions());
    } catch (Exception e) {
        _log.error(e, e);
    }
}

From source file:com.linuxbox.enkive.audit.mongodb.MongoAuditService.java

License:Open Source License

public MongoAuditService(String server, int port, String database, String collection)
        throws UnknownHostException, MongoException {
    this(new Mongo(server, port), database, collection);
    createdMongo = true;/*from  w ww.j  a  va 2s  .c  o m*/
}

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

public MongoGridDocStoreService(String host, int port, String dbName, String bucketName)
        throws UnknownHostException {
    this(new Mongo(host, port), dbName, bucketName);
    createdMongo = true;/*ww  w  .ja va2 s  .  c  om*/
}

From source file:com.linuxbox.util.lockservice.mongodb.MongoLockService.java

License:Open Source License

public MongoLockService(String server, int port, String database, String collection)
        throws UnknownHostException, MongoException {
    this(new Mongo(server, port), database, collection);
    mongoCreated = true;/*  www .ja va 2  s.  c o  m*/
}

From source file:com.linuxbox.util.queueservice.mongodb.MongoQueueService.java

License:Open Source License

public MongoQueueService(String server, int port, String database, String collection)
        throws UnknownHostException, MongoException {
    this(new Mongo(server, port), database, collection);
    createdMongo = true;/*  w  w w  . j  a  v a  2  s .  c  om*/
}