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:mx.org.cedn.avisosconagua.mongo.MongoInterface.java

License:Open Source License

/**
 * Constructor. Creates a new instance of a MongoInterface object.
 * @throws UnknownHostException //  ww w  .  ja v a  2  s . co m
 */
private MongoInterface() throws UnknownHostException {
    boolean running = true;
    for (String key : System.getenv().keySet()) {
        if (key.startsWith("JAVA_MAIN")) {
            if (System.getenv(key).startsWith("org.apache.maven")) {
                running = false;
            }
        }
    }
    if (null != System.getenv("MONGOHQ_URL") && running) {
        mongoClientURI = new MongoClientURI(System.getenv("MONGOHQ_URL"));
    } else {
        //mongodb://conagua:C0n4gu4@192.168.204.147/conagua
        //MONGOHQ_URL=mongodb://heroku:DnZ2AYC8nmtWR3p1Dccs4N9WSLUIrQQTrjcvfLrDlLo8V8yD4Pz6yV5mR5HPuTdEDx2b34v2W0qfufBHUBZlQg@oceanic.mongohq.com:10080/app23903821
        //mongoClientURI = new MongoClientURI("mongodb://heroku:DnZ2AYC8nmtWR3p1Dccs4N9WSLUIrQQTrjcvfLrDlLo8V8yD4Pz6yV5mR5HPuTdEDx2b34v2W0qfufBHUBZlQg@oceanic.mongohq.com:10080/app23903821");
        //mongoClientURI = new MongoClientURI("mongodb://conagua:C0n4gu4@192.168.204.147/conagua");
        mongoClientURI = new MongoClientURI(LOCAL_MONGO_URL);
    }
    mongoClient = new MongoClient(mongoClientURI);
    mongoDB = mongoClient.getDB(mongoClientURI.getDatabase());
    if (null != mongoClientURI.getUsername()) {
        mongoDB.authenticate(mongoClientURI.getUsername(), mongoClientURI.getPassword());
    }
}

From source file:mx.org.cedn.avisosconagua.mongo.UpdateIssueDate.java

License:Open Source License

public static void main(String[] arg) throws Exception {
    MongoClientURI mongoClientURI = new MongoClientURI(System.getenv("MONGOHQ_URL"));
    MongoClient mongoClient = new MongoClient(mongoClientURI);
    DB mongoDB = mongoClient.getDB(mongoClientURI.getDatabase());
    String GENERATED_COL = "GeneratedFiles";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    SimpleDateFormat isoformater = new SimpleDateFormat("YYYY-MM-dd HH:mm");
    if (null != mongoClientURI.getUsername()) {
        mongoDB.authenticate(mongoClientURI.getUsername(), mongoClientURI.getPassword());
    }//from   w w w .  j  a  v  a  2s  . c o m
    DBCollection col = mongoDB.getCollection(GENERATED_COL);
    DBCursor cursor = col.find();
    for (DBObject obj : cursor) {
        String date = (String) obj.get("issueDate");
        Date fec = null;
        try {
            fec = sdf.parse(date);
        } catch (ParseException npe) {

        }
        if (null != fec) {
            date = isoformater.format(fec);
            DBObject act = col.findOne(obj);
            obj.put("issueDate", date);
            col.update(act, obj);
        }
    }
}

From source file:net.modelbased.proasense.storage.reader.EventReaderMongoSync.java

License:Apache License

public List<Document> call() {
    // Connect to MongoDB database
    MongoClient mongoClient = new MongoClient(new MongoClientURI(this.mongoURL));
    MongoDatabase database = mongoClient.getDatabase(this.database);

    MongoCollection<Document> collection = database.getCollection(this.collectionId);

    // Create document list for query result
    List<Document> foundDocuments = new ArrayList<Document>();

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);/*from   w  ww . j  a v a  2 s .c  om*/
        }
    }

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                Long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                Double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
                doubleProperty = true;
            }
        }

        if (longProperty) {
            Long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            Double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
            }
        }

        if (longProperty) {
            Long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            Double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                Double value = (Double) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                Long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                Double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
            }
        }

        if (longProperty) {
            Long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            Double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
                doubleProperty = true;
            }
        }

        if (longProperty) {
            long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
            }
        }

        if (longProperty) {
            long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
            }
        }

        if (longProperty) {
            long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                double value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.ANOMALY) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
            }
        }

        if (longProperty) {
            long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
            }
        }

        if (longProperty) {
            long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.FEEDBACK) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    mongoClient.close();

    return foundDocuments;
}

From source file:net.modelbased.proasense.storage.writer.EventWriterMongoSync.java

License:Apache License

public void run() {
    // Connect to MongoDB database
    MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURL));
    MongoDatabase database = mongoClient.getDatabase(EventProperties.STORAGE_DATABASE_NAME);

    // Create hash map of collections
    Map<String, MongoCollection<Document>> collectionMap = new HashMap<String, MongoCollection<Document>>();

    int cnt = 0;/*from   w  w w  .  j av  a 2 s  .c o m*/
    long timer0 = System.currentTimeMillis();
    long timer1 = timer0;
    long timer2 = timer0;
    boolean skipLog = false;

    Map<String, List<Document>> documentMap = new HashMap<String, List<Document>>();
    try {
        if (isLogfile)
            logfileWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("EventWriterMongoSync_benchmark_" + this.threadNumber + ".txt"),
                    "ISO-8859-1"));

        while (true) {
            long timeoutExpired = System.currentTimeMillis() + this.maxWait;
            EventDocument eventDocument = queue.take();

            String collectionId = eventDocument.getCollectionId();

            if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) {
                cnt++;
                skipLog = false;
            } else
                skipLog = true;

            // Add data for bulk write
            if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) {
                Document document = eventDocument.getDocument();

                if (!collectionMap.containsKey(collectionId)) {
                    collectionMap.put(collectionId, database.getCollection(collectionId));
                    List<Document> documentList = new ArrayList<Document>();
                    documentMap.put(collectionId, documentList);
                }

                documentMap.get(collectionId).add(document);
            }
            // Write data if heartbeat timeout is received
            else {
                for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) {
                    String key = entry.getKey();
                    if (!documentMap.get(key).isEmpty()) {
                        collectionMap.get(key).insertMany(documentMap.get(key));
                        documentMap.get(key).clear();
                    }
                }
            }

            // Write data if bulk size or max wait (ms) is reached
            if ((cnt % this.bulkSize == 0) || (System.currentTimeMillis() >= timeoutExpired)) {
                for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) {
                    String key = entry.getKey();
                    if (!documentMap.get(key).isEmpty()) {
                        collectionMap.get(key).insertMany(documentMap.get(key));
                        documentMap.get(key).clear();
                    }
                }
            }

            // Benchmark output
            if ((!skipLog) && (cnt % this.logSize == 0)) {
                timer2 = System.currentTimeMillis();
                long difference = timer2 - timer1;

                if (difference != 0) {
                    long average = (this.logSize * 1000) / (timer2 - timer1);

                    System.out.println("Benchmark: ");
                    System.out.println("  Records written  : " + cnt);
                    System.out.println("  Average records/s: " + average);
                    timer1 = timer2;

                    if (isLogfile) {
                        logfileWriter.write(cnt + "," + average + System.getProperty("line.separator"));
                        logfileWriter.flush();
                    }

                    if (cnt == this.loadTestMaxMessages) {
                        long loadTestStart = timer0 / 1000;
                        long loadTestEnd = timer2 / 1000;
                        long loadTestTime = loadTestEnd - loadTestStart;
                        long loadTestAverage = this.loadTestMaxMessages / loadTestTime;

                        System.out.println("*****************************");
                        System.out.println("Load test results: ");
                        System.out.println("  Records written  : " + cnt);
                        System.out.println("  Average records/s: " + loadTestAverage);

                        if (isLogfile) {
                            logfileWriter.write(
                                    "*****************************" + System.getProperty("line.separator"));
                            logfileWriter.write("Load test results: " + System.getProperty("line.separator"));
                            logfileWriter.write(
                                    "  Records written  : " + cnt + System.getProperty("line.separator"));
                            logfileWriter.write("  Average records/s: " + loadTestAverage
                                    + System.getProperty("line.separator"));
                            logfileWriter.flush();
                        }
                    }

                }
            }
        }
    } catch (InterruptedException e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    } finally {
        if (isLogfile)
            try {
                logfileWriter.close();
            } catch (IOException e) {
                System.out.println(e.getClass().getName() + ": " + e.getMessage());
            }
    }

    mongoClient.close();
}

From source file:nl.kpmg.af.service.data.DatabaseInitialiser.java

License:Apache License

private MongoMockDatabase createMockDatabase(String host, int port) throws IOException {
    // create the security database
    MongodStarter securitydbstarter = MongodStarter.getDefaultInstance();
    MongodExecutable mongodExecutable = securitydbstarter.prepare(new MongodConfigBuilder()
            .version(Version.Main.V3_2).net(new Net(port, Network.localhostIsIPv6())).build());
    MongodProcess mongod = mongodExecutable.start();
    MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://" + host + ":" + port));
    return new MongoMockDatabase(mongodExecutable, mongod, mongoClient);
}

From source file:nl.kpmg.af.service.data.MongoDBUtil.java

License:Apache License

/**
 * Generate a uri string to connect to a MongoDb instance in the format
 * mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
 *
 * @param username the user that access the database (if null the client will not try to connect
 *        to a database)//from w w  w .  j  av a  2 s. c om
 * @param password tha password (only needed if username is specified, and optional for some
 *        authentication mechanism)
 * @param hosts the list of hosts (only host1 is mandatory), the hostname is in the form
 *        host[:port] to specify the port
 * @param database the name of the database
 * @return
 */
public static MongoClientURI generateMongoUri(@NotNull final String username, @NotNull final String password,
        @NotNull final List<String> hosts, final String database, final String options)
        throws UnknownHostException {
    if (hosts == null || hosts.size() == 0 || hosts.get(0) == null || hosts.get(0).equals("")) {
        throw new UnknownHostException("Unspecified host");
    }
    StringBuilder uriBuilder = new StringBuilder();
    // generate a mongo uri in the format
    uriBuilder.append("mongodb://");
    if (username != null && !username.equals("")) {
        uriBuilder.append(username);
        if (password != null) {
            uriBuilder.append(":").append(password);
        }
        uriBuilder.append("@");
    }

    uriBuilder.append(hosts.get(0));

    for (int i = 1; i < hosts.size(); i++) {
        uriBuilder.append(",");
        uriBuilder.append(hosts.get(i));
    }
    if (database != null) {
        uriBuilder.append("/").append(database);
    }

    if (options != null) {
        if (database == null)
            uriBuilder.append("/");
        uriBuilder.append("?").append(options);
    }

    return new MongoClientURI(uriBuilder.toString());
}

From source file:nl.kpmg.af.service.data.MongoDBUtil.java

License:Apache License

/**
 * Generate a uri string to connect to a MongoDb instance in the format
 * mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
 * <p>//  w  ww.j a v  a  2  s  .  com
 * This function takes a single string for the list of hosts.
 *
 * @param username the user that access the database (if null the client will not try to connect
 *        to a database)
 * @param password tha password (only needed if username is specified, and optional for some
 *        authentication mechanism)
 * @param hosts the list of hosts (only host1 is mandatory), the hostname is in the form
 *        host[:port] to specify the port
 * @param database the name of the database
 * @return
 */
public static MongoClientURI generateMongoUri(@NotNull final String username, @NotNull final String password,
        @NotNull final String hosts, final String database, final String options) throws UnknownHostException {
    if (hosts == null || hosts.equals("")) {
        throw new UnknownHostException("Unspecified host");
    }
    StringBuilder uriBuilder = new StringBuilder();
    // generate a mongo uri in the format
    uriBuilder.append("mongodb://");
    if (username != null && !username.equals("")) {
        uriBuilder.append(username);
        if (password != null) {
            uriBuilder.append(":").append(password);
        }
        uriBuilder.append("@");
    }

    uriBuilder.append(hosts);

    if (database != null) {
        uriBuilder.append("/").append(database);
    }

    if (options != null) {
        if (database == null)
            uriBuilder.append("/");
        uriBuilder.append("?").append(options);
    }

    return new MongoClientURI(uriBuilder.toString());
}

From source file:nl.syntouch.oracle.adapter.cloud.mongodb.endpoint.MongoDBConnection.java

License:Apache License

public void connect() {
    logger.log(CloudAdapterLogger.Level.INFO, "Connecting to [" + mongoUri + "]");
    MongoClientURI uri = new MongoClientURI(mongoUri);
    client = new MongoClient(uri);

    logger.log(CloudAdapterLogger.Level.INFO,
            "Retrieving collection [" + mongoCollection + "] from database [" + mongoDb + "]");
    db = client.getDatabase(mongoDb);//from ww w. ja v  a2  s .  c o m
    collection = db.getCollection(mongoCollection);
}

From source file:no.asgari.civilization.server.application.CivilizationApplication.java

License:Apache License

@Override
public void run(CivilizationConfiguration configuration, Environment environment) throws Exception {
    DB db;//from  w  w  w .  jav a  2  s  . c  o m
    MongoClient mongo;
    if (!Strings.isNullOrEmpty(configuration.mongodbUser)
            && !Strings.isNullOrEmpty(configuration.mongodbPassword)) {
        MongoClientURI clientURI = new MongoClientURI("mongodb://" + configuration.mongodbUser + ":"
                + configuration.mongodbPassword + "@" + configuration.mongohost + ":" + configuration.mongoport
                + "/" + configuration.mongodb);

        mongo = new MongoClient(clientURI);
        db = mongo.getDB(clientURI.getDatabase());
    } else {
        mongo = new MongoClient(configuration.mongohost, configuration.mongoport);
        db = mongo.getDB(configuration.mongodb);
    }
    MongoManaged mongoManaged = new MongoManaged(mongo);
    environment.lifecycle().manage(mongoManaged);

    JacksonDBCollection<Player, String> playerCollection = JacksonDBCollection
            .wrap(db.getCollection(Player.COL_NAME), Player.class, String.class);
    JacksonDBCollection<PBF, String> pbfCollection = JacksonDBCollection.wrap(db.getCollection(PBF.COL_NAME),
            PBF.class, String.class);
    JacksonDBCollection<Chat, String> chatCollection = JacksonDBCollection.wrap(db.getCollection(Chat.COL_NAME),
            Chat.class, String.class);
    createUniqueIndexForPlayer(playerCollection);
    createUsernameCache(playerCollection);
    //createUniqueIndexForPBF(pbfCollection);
    createIndexForChat(chatCollection);
    //createItemCache(); //TODO Have to rewrite the code to make it work, right now everyone gets same number and same draws

    //healtcheck
    environment.healthChecks().register("MongoHealthCheck", new MongoHealthCheck(mongo));

    //Resources
    environment.jersey().register(new GameResource(db));
    environment.jersey().register(new AuthResource(db));
    environment.jersey().register(new PlayerResource(db));
    environment.jersey().register(new DrawResource(db));
    environment.jersey().register(new AdminResource(db));

    //Authenticator
    CachingAuthenticator<BasicCredentials, Player> cachingAuthenticator = new CachingAuthenticator<>(
            new MetricRegistry(), new CivAuthenticator(db), CacheBuilderSpec.parse("expireAfterWrite=120m"));

    //Authentication binder
    Binder authBinder = AuthFactory
            .binder(new BasicAuthFactory<>(cachingAuthenticator, "civilization", Player.class));

    //Authentication
    environment.jersey().register(authBinder);

    // Configure CORS parameters
    FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORSFilter", CrossOriginFilter.class);

    filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false,
            environment.getApplicationContext().getContextPath() + "api/*");
    filter.setInitParameter(ALLOWED_METHODS_PARAM, "GET,PUT,POST,OPTIONS,DELETE");
    filter.setInitParameter(ALLOWED_ORIGINS_PARAM, "*");
    filter.setInitParameter(ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin,authorization");
    filter.setInitParameter(ALLOW_CREDENTIALS_PARAM, "true");
    filter.setInitParameter(EXPOSED_HEADERS_PARAM,
            "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin,Location,Accept-Content-Encoding");
}

From source file:ohtu.personservice.App.java

public static void main(String[] args) throws IOException {

    Kryptoniter kryptoniter = new Kryptoniter(System.getenv("OHTU_KRYPTO"));
    TokenConverter tokenconverter = new TokenConverter();
    Gson gson = new Gson();
    //UrlLoader urlLoader = new UrlLoader();
    //UrlCollection urlCollection = urlLoader.load("CONF_API");

    //UrlCollection urlCollection = new UrlLoader(System.getenv()).load("CONF_API");

    String configurationsUrl = System.getenv("CONF_API");

    HttpResponse hrConf = org.apache.http.client.fluent.Request.Get(configurationsUrl).execute()
            .returnResponse();/* ww  w.j  ava 2s . c  o  m*/

    String responseAsJson = IOUtils.toString(hrConf.getEntity().getContent(), Charset.forName("UTF-8"));

    UrlCollection urlCollection = gson.fromJson(responseAsJson, UrlCollection.class);

    // vaihda seuraavaan joku vapaa tietokanta
    String mongoLab = urlCollection.mongourl();
    MongoClientURI uri = new MongoClientURI(mongoLab);
    Morphia morphia = new Morphia();
    MongoClient mongo = new MongoClient(uri);
    // jos kytt lokaalia mongoa, luo client seuraavasti
    //MongoClient mongo = new MongoClient();

    morphia.mapPackage("ohtu.domainlib");
    // vaihda seuraavaan sama kun kannan nimi kuin mongourlissa
    Datastore datastore = morphia.createDatastore(mongo, "kanta11");

    get("/ping", (request, response) -> {
        preFilter(request);
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String dir = System.getProperty("user.dir");

        return "{ \"name\": \"" + name + "\", \"dir\": \"" + dir + "\" }";
    });

    before("/persons", (request, response) -> {
        preFilter(request);

        if (request.requestMethod().equals("GET")) {
            DateTimeToken dtt_now = DateTimeToken.generate(0);
            DateTimeToken dtt_auth = tokenconverter
                    .toDateTime(kryptoniter.decryptedToken(new Token(request.headers("Authorization"))));

            System.out.println("now :" + dtt_now.toString());
            System.out.println("auth:" + dtt_auth.toString());
            System.out.println("auth is after now :" + dtt_auth.isAfter(dtt_now));

            if (dtt_auth == null || !dtt_auth.isAfter(dtt_now)) {
                halt(401, gson.toJson(Error.withCause("missing, invalid or expired token")));
            }
        }
    });

    get("/persons", (request, response) -> {
        preFilter(request);
        return datastore.find(ohtu.domainlib.Person.class).asList();
    }, new JsonTransformer());

    post("/persons", (request, response) -> {
        preFilter(request);
        ohtu.domainlib.Person person = gson.fromJson(request.body(), ohtu.domainlib.Person.class);

        if (person == null || !person.valid()) {
            halt(400, gson.toJson(Error.withCause("all fields must have a value")));
        }

        if (datastore.createQuery(ohtu.domainlib.Person.class).field("username").equal(person.username())
                .get() != null) {
            halt(400, gson.toJson(Error.withCause("username must be unique")));
        }

        datastore.save(person);
        return person;
    }, new JsonTransformer());

    post("/session", (request, response) -> {
        preFilter(request);
        ohtu.domainlib.Person dataInRequest = gson.fromJson(request.body(), ohtu.domainlib.Person.class);

        ohtu.domainlib.Person person = datastore.createQuery(ohtu.domainlib.Person.class).field("username")
                .equal(dataInRequest.username()).get();

        if (person == null || !person.password().equals(dataInRequest.password())) {
            halt(401, gson.toJson(Error.withCause("invalid credentials")));
        }

        return kryptoniter.encryptedToken(Token.generate());
    }, new JsonTransformer());

    after((request, response) -> {
        response.type("application/json");
    });
}