Example usage for com.mongodb DBCursor toString

List of usage examples for com.mongodb DBCursor toString

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.sfelf.connectors.mongoOplogCursorConnector.java

License:Open Source License

/**
 * <b>OplogCursor</b> - Establishes a tailable cursor to the mongoDB oplog for the specified namespace and operations
 *               and returns the results to the Mulesoft inbound endpoint. The returned payload is a {@link DBObject}.
 * <p/>// w  w  w  .java 2  s. c o m
 *               Throws a {@link MongoOplogCursorException} if any unexpected issues arise while trying to 
 *               establish the tailable cursor or while processing the results from the cursor.
 * <p/>
 * {@sample.xml ../../../doc/mongoOplogCursor-connector.xml.sample mongooplogcursor:oplogCursor}
 * @param namespace       The exact namespace (ex. database.collection) for which the endpoint should return messages when
 *                      entries are logged in the oplog for that namespace.  
 * @param fullDocument       {@link Boolean} to control if messages will include the full oplog document or just the following fields:
 *                      <ul>              
 *                         <li>ts - timestamp
 *                         <li>op - operation (i=insert, u=update, d=delete)
 *                         <li>ns - namespace
 *                         <li>o._id - _id of the record inserted or updated
 *                         <li>o2._id - _id of the record updated
 *                      </ul>              
 * @param monitorInserts    {@link Boolean} to control if messages will be generated for Inserts (Optional: defaults to false)
 * @param monitorUpdates    {@link Boolean} to control if messages will be generated for Updates (Optional: defaults to false)
 * @param monitorDeletes    {@link Boolean} to control if messages will be generated for Deletes (Optional: defaults to false)
 * @param callback          The callback to be called when a message is received
 * <p/>
 * @throws MongoOplogCursorException If {@link #isConnected() isConnected} == false
 */
@Source(primaryNodeOnly = true, exchangePattern = MessageExchangePattern.ONE_WAY)
public void oplogCursor(
        @FriendlyName("Namespace") @Placement(group = "Query Options", order = 1) String namespace,
        @FriendlyName("Return full document") @Optional @Placement(group = "Query Options", order = 2) @Default("false") Boolean fullDocument,
        @FriendlyName("Monitor Insert Operations") @Optional @Placement(group = "Operations", order = 3) @Default("false") Boolean monitorInserts,
        @FriendlyName("Monitor Update Operations") @Optional @Placement(group = "Operations", order = 4) @Default("false") Boolean monitorUpdates,
        @FriendlyName("Monitor Delete Operations") @Optional @Placement(group = "Operations", order = 5) @Default("false") Boolean monitorDeletes,
        SourceCallback callback) throws MongoOplogCursorException {

    if (!isConnected()) {
        throw new MongoOplogCursorException(
                "Unable to initiate cursor because MongoClient is not connected: " + mongoClient);
    }
    DBCollection log = getCollection(logDB, logCollection, true);
    DBCollection oplog = getCollection(oplogDB, OPLOG_COLLECTION_NAME, false);
    BSONTimestamp lastTimestamp = getLastTimestamp(oplog, log, namespace);
    LOGGER.debug("Last Timestamp: " + lastTimestamp);

    while (!Thread.interrupted()) {
        DBObject query = getOplogQuery(lastTimestamp, namespace, monitorInserts, monitorUpdates,
                monitorDeletes);
        DBCursor cursor = createCursor(oplog, query, fullDocument);
        LOGGER.debug("New cursor: " + cursor.toString());
        try {
            while (cursor.hasNext()) {
                final DBObject doc = cursor.next();
                LOGGER.debug("New document: " + doc);
                if (doc != null) {
                    callback.process(doc);
                    lastTimestamp = (BSONTimestamp) doc.get("ts");
                    updateLastTimestampLog(log, namespace, lastTimestamp);
                    LOGGER.debug("Updated Last Timestamp: " + lastTimestamp);
                } else {
                    LOGGER.debug("Sleeping until next document ready");
                    Thread.sleep(NO_DOC_SLEEP_TIME);
                }
            }
        } catch (Exception e) {
            LOGGER.debug(
                    "Caught Exception while reading from cursor: " + e.getClass() + " - " + e.getMessage());
        } finally {
            LOGGER.debug("Closing Cursor and attempting to acquire a new cursor");
            try {
                cursor.close();
            } catch (Exception e) {
                LOGGER.debug("Caught Exception while closing cursor: " + e.getClass() + " - " + e.getMessage());
            }
        }
    }
    LOGGER.debug("Thread Interrupted:" + "\n" + "log = " + log + "\n" + "oplog = " + oplog);
}

From source file:DataAccess.DAO.LoginDAO.java

public String validate(String username, String password) {

    String returnText = LOGIN_FAILURE;

    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("Restaurant");

    try {//  www . j a  v  a 2 s.  co  m

        //boolean auth = db.authenticate(username, password.toCharArray());
        MongoCredential credential2 = MongoCredential.createCredential(username, "Restaurant",
                password.toCharArray());
        MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential2));

        DB db2 = mongoClient.getDB("Restaurant");
        System.out.println("Connect to database successfully");

        DBCollection coll = db2.getCollection("Users");
        System.out.println("Collection users selected successfully");
        BasicDBObject document2 = new BasicDBObject();
        document2.put("UserName", username);
        document2.put("Password", password);
        //coll.insert(document2);
        DBCursor cur2 = coll.find(document2);

        System.out.println(cur2.toString());

        while (cur2.hasNext()) {
            System.out.println(cur2.next());
        }

        System.out.println("Login is successful!");

        if (credential2 != null) {

            DBCollection table = db.getCollection("Users");

            BasicDBObject document = new BasicDBObject();
            document.put("UserName", username);
            table.insert(document);
            DBCursor cur = table.find(document);

            while (cur.hasNext()) {
                System.out.println(cur.next());
            }

            HttpSession session = SessionBean.getSession();
            session.setAttribute("UserName", user);

            System.out.println("Login is successful!");
            returnText = LOGIN_SUCCESS;

            return "admins";
        } else {

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Incorrect Username and Passowrd", "Please enter correct username and Password"));

            System.out.println("Login is failed!");
            return "login";
        }
        //System.out.println("Done");

        //return returnText;

    } catch (MongoException e) {
        e.printStackTrace();
    } finally {
        mongo.close();
    }
    return returnText;
}