Example usage for com.mongodb MongoClientURI MongoClientURI

List of usage examples for com.mongodb MongoClientURI MongoClientURI

Introduction

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

Prototype

public MongoClientURI(final String uri) 

Source Link

Document

Creates a MongoURI from the given string.

Usage

From source file:org.trade.core.TraDENode.java

License:Apache License

public static void testClusterAndPersistence(TraDEProperties properties) {
    // Apply the properties to the XML config
    XmlConfigBuilder builder = new XmlConfigBuilder();
    builder.setProperties(properties);/*from   ww  w  .  jav  a  2s . c o  m*/
    Config config = builder.build();

    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);

    Morphia morphia = new Morphia();

    // Map model classes to db collections
    morphia.mapPackage("org.trade.core.model.data");

    // create the datastore connecting to mongo
    Datastore store = morphia.createDatastore(new MongoClient(new MongoClientURI(properties.getCacheDbUrl())),
            properties.getCacheDbName());
    store.ensureIndexes();

    List<String> keys = new ArrayList<String>();

    IMap<String, DataObject> dataObjects = instance.getMap("dataObjects");
    System.out.println(dataObjects.getName() + " - " + dataObjects.size());

    if (!dataObjects.containsKey(keys.get(0))) {
        DataObject obj = new DataObject("chorModel1", "lattice");
        dataObjects.set(obj.getIdentifier(), obj);
        keys.add(obj.getIdentifier());
    }

    if (!dataObjects.containsKey(keys.get(1))) {
        DataObject obj = new DataObject("userA", "plot");
        dataObjects.set(obj.getIdentifier(), obj);
        keys.add(obj.getIdentifier());
    }

    if (!dataObjects.containsKey(keys.get(2))) {
        DataObject obj = new DataObject("random", "input");
        dataObjects.set(obj.getIdentifier(), obj);
        keys.add(obj.getIdentifier());
    }

    //            System.out.println(dataObjects.getName() + " - " + dataObjects.size());
    //
    //            dataObjects.evictAll();
    //
    //            System.out.println(dataObjects.getName() + " - " + dataObjects.size());
    //
    //            dataObjects.loadAll(true);
    //
    //            System.out.println(dataObjects.getName() + " - " + dataObjects.size());

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    for (String key : keys) {
        DataObject obj = (DataObject) instance.getMap("dataObjects").get(key);
        System.out.println("### DataObject ###");
        System.out.println("Name: " + obj.getName());
        System.out.println("Identifier: " + obj.getIdentifier());
        System.out.println("Entity: " + obj.getEntity());
        System.out.println("State: " + obj.getState());
        System.out.println("##################");
    }

    IMap<String, DataValue> dataValues = instance.getMap("dataValues");
    System.out.println(dataValues.getName() + " - " + dataValues.size());

    List<String> dvKeys = new ArrayList<String>();
    for (DataValue value : store.createQuery(DataValue.class).retrievedFields(true, "identifier").asList()) {
        dvKeys.add(value.getIdentifier());
    }

    if (dvKeys.isEmpty()) {
        DataValue value = new DataValue("hahnml", "script");

        try {
            // byte[] data = Files.readAllBytes(Paths.get("C:\\test\\OpalMC\\scripts\\data\\000abcd0001.dat"));
            byte[] data = Files.readAllBytes(Paths.get("C:\\test\\script.sh"));

            value.setData(data, data.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        dvKeys.add(value.getIdentifier());

        dataValues.set(value.getIdentifier(), value);

        value = new DataValue("hahnml", "opalVideo");

        try {
            // byte[] data = Files.readAllBytes(Paths.get("C:\\test\\OpalMC\\scripts\\data\\000abcd0001.dat"));
            byte[] data = Files.readAllBytes(Paths.get("C:\\test\\opalClusterSnapshots.mp4"));
            value.setContentType("video/mp4");
            value.setData(data, data.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        dvKeys.add(value.getIdentifier());

        dataValues.set(value.getIdentifier(), value);
        System.out.println(dataValues.getName() + " - " + dataValues.size());
    }

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    for (String key : dvKeys) {
        DataValue value = (DataValue) instance.getMap("dataValues").get(key);
        System.out.println("### DataValue ###");
        System.out.println("Owner: " + value.getOwner());
        System.out.println("Identifier: " + value.getIdentifier());
        System.out.println("Timestamp: " + value.getCreationTimestamp());
        System.out.println("State: " + value.getState());

        try {
            if (value.getData() != null) {
                System.out.println("Data: " + value.getData().toString() + ", size: " + value.getData().length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("#################");
    }

    instance.shutdown();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.exit(0);
}

From source file:org.trade.server.IntegrationTestEnvironment.java

License:Apache License

public void destroyEnvironment() {
    // Cleanup the database
    MongoClient dataStoreClient = new MongoClient(new MongoClientURI(properties.getDataPersistenceDbUrl()));
    MongoDatabase dataStore = dataStoreClient.getDatabase(properties.getDataPersistenceDbName());
    dataStore.getCollection(ModelConstants.DATA_MODEL__DATA_COLLECTION).drop();
    dataStore.getCollection(ModelConstants.DATA_DEPENDENCY_GRAPH__DATA_COLLECTION).drop();
    dataStore.getCollection(ModelConstants.DATA_VALUE__DATA_COLLECTION).drop();

    dataStore.getCollection("dataElements").drop();
    dataStore.getCollection("dataObjects").drop();
    dataStore.getCollection("dataModels").drop();
    dataStore.getCollection("dataDependencyGraphs").drop();
    dataStore.getCollection("notifications").drop();
    dataStore.getCollection("dataValues").drop();
    dataStore.getCollection("dataElementInstances").drop();
    dataStore.getCollection("dataObjectInstances").drop();

    dataStoreClient.close();// w ww .ja v a2s.  c  om

    // Stop the server
    try {
        server.stopHTTPServer();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.extension.siddhi.store.mongodb.MongoTableTestUtils.java

License:Open Source License

public static void dropCollection(String uri, String collectionName) {
    try (MongoClient mongoClient = new MongoClient(new MongoClientURI(uri))) {
        mongoClient.getDatabase(databaseName).getCollection(collectionName).drop();
    } catch (MongoException e) {
        log.debug("Clearing DB collection failed due to " + e.getMessage(), e);
        throw e;//from  ww  w . j  a v a2  s . c o m
    }
}

From source file:org.wso2.extension.siddhi.store.mongodb.MongoTableTestUtils.java

License:Open Source License

public static long getDocumentsCount(String uri, String collectionName) {
    try (MongoClient mongoClient = new MongoClient(new MongoClientURI(uri))) {
        return mongoClient.getDatabase(databaseName).getCollection(collectionName).count();
    } catch (MongoException e) {
        log.debug("Getting rows in DB table failed due to " + e.getMessage(), e);
        throw e;//from   ww w .jav  a 2s  .c om
    }
}

From source file:org.wso2.extension.siddhi.store.mongodb.MongoTableTestUtils.java

License:Open Source License

public static boolean doesCollectionExists(String uri, String customCollectionName) {
    try (MongoClient mongoClient = new MongoClient(new MongoClientURI(uri))) {
        for (String collectionName : mongoClient.getDatabase(databaseName).listCollectionNames()) {
            if (customCollectionName.equals(collectionName)) {
                return true;
            }//from  w  w w.  j av  a2 s  .c o  m
        }
        return false;
    } catch (MongoException e) {
        log.debug("Checking whether collection was created failed due to" + e.getMessage(), e);
        throw e;
    }
}

From source file:org.wso2.extension.siddhi.store.mongodb.MongoTableTestUtils.java

License:Open Source License

private static List<Document> getIndexList(String uri, String collectionName) {
    try (MongoClient mongoClient = new MongoClient(new MongoClientURI(uri))) {
        ListIndexesIterable<Document> existingIndexesIterable = mongoClient.getDatabase(databaseName)
                .getCollection(collectionName).listIndexes();
        List<Document> existingIndexDocuments = new ArrayList<>();
        existingIndexesIterable.forEach((Consumer<? super Document>) existingIndex -> {
            existingIndex.remove("ns");
            existingIndexDocuments.add(existingIndex);
        });//from w  ww  .  j  av a  2  s .  c o  m
        return existingIndexDocuments;
    } catch (MongoException e) {
        log.debug("Getting indexes in DB table failed due to " + e.getMessage(), e);
        throw e;
    }
}

From source file:org.wso2.extension.siddhi.store.mongodb.MongoTableTestUtils.java

License:Open Source License

public static Document getDocument(String uri, String collectionName, String findFilter) {
    try (MongoClient mongoClient = new MongoClient(new MongoClientURI(uri))) {
        Document findFilterDocument = Document.parse(findFilter);
        Document firstFind = mongoClient.getDatabase(databaseName).getCollection(collectionName)
                .find(findFilterDocument).first();
        firstFind.remove("_id");
        return firstFind;
    } catch (MongoException e) {
        log.debug("Getting indexes in DB table failed due to " + e.getMessage(), e);
        throw e;/*  w  w  w .  j  a  v a2  s . c o  m*/
    }
}

From source file:org.wso2.extension.siddhi.store.mongodb.MongoTableTestUtils.java

License:Open Source License

public static void createCollection(String uri, String collectionName) {
    dropCollection(uri, collectionName);
    try (MongoClient mongoClient = new MongoClient(new MongoClientURI(uri))) {
        mongoClient.getDatabase(databaseName).createCollection(collectionName);
    } catch (MongoException e) {
        log.debug("Getting indexes in DB table failed due to " + e.getMessage(), e);
        throw e;//from w ww.j  a  v a2s.  c  o m
    }
}

From source file:pt.tiago.mongodbteste.MongoDB.java

private void createConnecntion() throws UnknownHostException {
    StringBuilder str = new StringBuilder();
    str.append("mongodb://");
    str.append(user);//from w w  w.j av a2  s  . co  m
    str.append(":");
    str.append(pass);
    str.append("@ds055690.mongolab.com:55690/");
    str.append(dbName);
    uri = str.toString();
    clientURI = new MongoClientURI(uri);
    client = new MongoClient(clientURI);
    db = client.getDB(clientURI.getDatabase());
    collection.add(db.getCollection("Category"));
    collection.add(db.getCollection("Person"));
    collection.add(db.getCollection("Purchase"));
}

From source file:rapture.mongodb.MongoDBFactory.java

License:Open Source License

private Mongo getMongoFromLocalConfig(String instanceName) {
    String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-" + instanceName);
    log.info("Host is " + mongoHost);
    if (StringUtils.isBlank(mongoHost)) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST,
                mongoMsgCatalog.getMessage("NoHost"));
    }//from w w w . j av a  2 s .c o m
    MongoClientURI uri = new MongoClientURI(mongoHost);
    log.info("Username is " + uri.getUsername());
    log.info("Host is " + uri.getHosts().toString());
    log.info("DBName is " + uri.getDatabase());
    log.info("Collection is " + uri.getCollection());
    try {
        MongoClient mongo = new MongoClient(uri);
        mongoDBs.put(instanceName, mongo.getDB(uri.getDatabase()));
        mongoDatabases.put(instanceName, mongo.getDatabase(uri.getDatabase()));
        mongoInstances.put(instanceName, mongo);
        return mongo;
    } catch (MongoException e) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, new ExceptionToString(e));
    }
}