Example usage for com.mongodb MongoClient getConnectPoint

List of usage examples for com.mongodb MongoClient getConnectPoint

Introduction

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

Prototype

@Deprecated
@Nullable
public String getConnectPoint() 

Source Link

Document

Gets a String representation of current connection point, i.e.

Usage

From source file:com.ca.apm.mongo.Topology.java

License:Open Source License

protected MongoClient setupDbClient(final String dbHost, final int dbPort) {
    final boolean useSSL = Collector.getBooleanProp(Collector.USE_SSL_PROP, props);
    final String clientTrustStore = Collector.getOptionalStringProp(Collector.SSL_CLIENT_TRUST_STORE_FILE_PROP,
            props);/*ww  w .j a v  a2s . c  om*/
    final String clientPasswd = Collector.getOptionalStringProp(Collector.SSL_CLIENT_TRUST_STORE_PASSWD_PROP,
            props);

    try {
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();

        if (useSSL) {
            System.setProperty(Collector.SSL_CLIENT_TRUST_STORE_FILE_PROP, clientTrustStore);
            System.setProperty(Collector.SSL_CLIENT_TRUST_STORE_PASSWD_PROP, clientPasswd);
            builder = builder.socketFactory(SSLSocketFactory.getDefault());
        }

        final MongoClientOptions options = builder.build();
        MongoClient dbClient = new MongoClient(new ServerAddress(dbHost, dbPort), mongoCreds, options);
        logger.log(Level.FINE, "Connected to mongo at {0}", dbClient.getConnectPoint());
        logger.log(Level.FINE, "Client options: " + dbClient.getMongoClientOptions());
        return dbClient;
    } catch (Exception ex) {
        throw new RuntimeException("Can't initialize mongo client", ex);
    }
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void fixCollection(ButtonBase button) {
    final MongoClient m = getCollectionNode().getDbNode().getMongoNode().getMongoClient();
    ArrayList<MongoNode> mongoNodes = UMongo.instance.getMongos();
    String[] mongonames = new String[mongoNodes.size() - 1];
    MongoClient[] mongos = new MongoClient[mongonames.length];
    int i = 0;/*from   w w w .jav a2 s  .  co m*/
    for (MongoNode node : mongoNodes) {
        MongoClient m2 = node.getMongoClient();
        if (m == m2) {
            continue;
        }
        mongonames[i] = m2.toString();
        mongos[i] = m2;
        ++i;
    }
    ComboBox src = (ComboBox) getBoundUnit(Item.fcSrcMongo);
    src.items = mongonames;
    src.structureComponent();
    FormDialog dialog = (FormDialog) getBoundUnit(Item.fcDialog);
    if (!dialog.show()) {
        return;
    }

    final DBCollection dstCol = getCollectionNode().getCollection();
    final MongoClient srcMongo = mongos[src.getIntValue()];
    final boolean upsert = getBooleanFieldValue(Item.fcUpsert);

    final String dbname = dstCol.getDB().getName();
    final String colname = dstCol.getName();
    final DBCollection srcCol = srcMongo.getDB(dbname).getCollection(colname);
    String txt = "About to copy from ";
    txt += srcMongo.getConnectPoint() + "(" + srcCol.count() + ")";
    txt += " to ";
    txt += m.getConnectPoint() + "(" + dstCol.count() + ")";
    if (!new ConfirmDialog(null, "Confirm Fix Collection", null, txt).show()) {
        return;
    }

    new DbJob() {
        @Override
        public Object doRun() {
            DBCursor cur = srcCol.find();
            int count = 0;
            int dup = 0;
            while (cur.hasNext()) {
                DBObject obj = cur.next();
                if (upsert) {
                    BasicDBObject id = new BasicDBObject("_id", obj.get("_id"));
                    dstCol.update(id, obj, true, false);
                } else {
                    try {
                        dstCol.insert(obj);
                    } catch (DuplicateKey e) {
                        // dup keys are expected here
                        ++dup;
                    }
                }
                ++count;
            }
            DBObject res = new BasicDBObject("count", count);
            res.put("dups", dup);
            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Fix Collection";
        }
    }.addJob();
}

From source file:com.edgytech.umongo.ReplSetPanel.java

License:Apache License

public void compareReplicas(ButtonBase button) {
    final String stat = getStringFieldValue(Item.crStat);
    new DbJob() {

        @Override//from  ww  w.jav  a  2  s  .c  o  m
        public Object doRun() {
            ReplSetNode node = getReplSetNode();
            if (!node.hasChildren())
                return null;

            ArrayList<MongoClient> svrs = new ArrayList<MongoClient>();
            for (XmlUnit unit : node.getChildren()) {
                ServerNode svr = (ServerNode) unit;
                MongoClient svrm = svr.getServerMongoClient();
                try {
                    svrm.getDatabaseNames();
                } catch (Exception e) {
                    continue;
                }
                svrs.add(svrm);
            }

            BasicDBObject res = new BasicDBObject();
            MongoClient m = getReplSetNode().getMongoClient();
            for (String dbname : m.getDatabaseNames()) {
                DB db = m.getDB(dbname);
                BasicDBObject dbres = new BasicDBObject();
                for (String colname : db.getCollectionNames()) {
                    DBCollection col = db.getCollection(colname);
                    BasicDBObject colres = new BasicDBObject();
                    BasicDBObject values = new BasicDBObject();
                    boolean same = true;
                    long ref = -1;
                    for (MongoClient svrm : svrs) {
                        DBCollection svrcol = svrm.getDB(dbname).getCollection(colname);
                        long value = 0;
                        if (stat.startsWith("Count")) {
                            value = svrcol.count();
                        } else if (stat.startsWith("Data Size")) {
                            CommandResult stats = svrcol.getStats();
                            value = stats.getLong("size");
                        }
                        values.append(svrm.getConnectPoint(), value);
                        if (ref < 0)
                            ref = value;
                        else if (ref != value)
                            same = false;
                    }
                    if (!same) {
                        colres.append("values", values);
                        dbres.append(colname, colres);
                    }
                }
                if (!dbres.isEmpty()) {
                    res.append(dbname, dbres);
                }
            }

            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Compare Replicas";
        }
    }.addJob();
}

From source file:it.wami.map.mongodeploy.OsmToMongoDB.java

License:Apache License

/**
 * //from   ww  w .  j av  a 2s  . c  o m
 * @param host
 * @param port
 * @param mco 
 * @return
 */
private static MongoClient createMongo(String host, int port, MongoClientOptions mco) {
    MongoClient mongo = null;
    try {
        mongo = new MongoClient(new ServerAddress(host, port), mco);
        System.out.println(mongo.getConnectPoint());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mongo;
}

From source file:org.apache.rya.shell.RyaConnectionCommands.java

License:Apache License

@CliCommand(value = CONNECT_MONGO_CMD, help = "Connect the shell to an instance of MongoDB.")
public String connectToMongo(@CliOption(key = {
        "username" }, mandatory = false, help = "The username that will be used to connect to MongoDB when performing administrative tasks.") final String username,
        @CliOption(key = {/* w  w  w . j  a v  a 2s.  c  om*/
                "hostname" }, mandatory = true, help = "The hostname of the MongoDB that will be connected to.") final String hostname,
        @CliOption(key = {
                "port" }, mandatory = true, help = "The port of the MongoDB that will be connected to.") final String port) {

    try {
        // If a username was provided, then prompt for a password.
        char[] password = null;
        if (username != null) {
            password = passwordPrompt.getPassword();
        }

        // Create the Mongo Connection Details that describe the Mongo DB Server we are interacting with.
        final MongoConnectionDetails connectionDetails = new MongoConnectionDetails(hostname,
                Integer.parseInt(port), Optional.ofNullable(username), Optional.ofNullable(password));

        // Connect to a MongoDB server. TODO Figure out how to provide auth info?
        final MongoClient adminClient = new MongoClient(hostname, Integer.parseInt(port));
        // Make sure the client is closed at shutdown.
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                adminClient.close();
            }
        });

        try {
            //attempt to get the connection point, essentially pinging mongo server.
            adminClient.getConnectPoint();
        } catch (final MongoException e) {
            //had to rethrow to get scope on adminClient.
            adminClient.close();
            throw e;
        }

        // Initialize the connected to Mongo shared state.
        final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, adminClient);
        sharedState.connectedToMongo(connectionDetails, ryaClient);

    } catch (final IOException | MongoException e) {
        throw new RuntimeException("Could not connection to MongoDB. Reason: " + e.getMessage(), e);
    }

    return "Connected. You must select a Rya instance to interact with next.";
}