Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient() 

Source Link

Document

Creates an instance based on a (single) mongodb node (localhost, default port).

Usage

From source file:mail.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int success = 0;
    String id = request.getParameter("email");
    System.out.println(id);/*from www  .  j  a  va2 s  .c o  m*/
    //String id="anjaliverma25792@gmail.com";
    MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("WebDB");
    DBCollection table = db.getCollection("User");
    Document d = new Document();
    d.put("Email", id);
    DBObject ob = new BasicDBObject(d);
    DBCursor cur = table.find(ob);
    System.out.println(cur.size());
    System.out.println("Above value");
    String user = null;
    if (cur.hasNext()) {
        success = 1;
        cur.next();
        DBObject temp = cur.curr();
        String pass = (String) temp.get("Password");

        HttpSession session = request.getSession();
        user = (String) temp.get("Name");

        final String username = "nature.lover.ritika@gmail.com";
        final String password = "ritika7vision";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session sess = Session.getInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            Message message = new MimeMessage(sess);
            message.setFrom(new InternetAddress("papertree.official@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(id)); //retrive email id from forgot your password form.
            message.setSubject("PaperTree: Password Reset");
            message.setText("Dear, " + user + ". Your password is " + pass
                    + ". \n\n Directly LOGIN And START READING...");

            Transport.send(message);

            // Mongo DB connectivity LEFT 

            //                        response.sendRedirect("success.jsp");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }
    if (success == 1) {
        response.sendRedirect("success.jsp?success=yes");

    } else {
        response.sendRedirect("success.jsp?success=no");

    }
}

From source file:ConfigClassInDefaultPackage.java

License:Apache License

@Override
public Mongo mongo() throws Exception {
    return new MongoClient();
}

From source file:DataSimulate.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes no args//from   w w w. ja v  a 2 s.  c  om
 * @throws IOException 
 */
public static void main(final String[] args) throws IOException {
    // connect to the local database server
    MongoClient mongoClient = new MongoClient();

    /*
    // Authenticate - optional
    MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password);
    MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
    */

    // get handle to "mydb"
    DB db = mongoClient.getDB("awf_db");

    // get a list of the collections in this database and print them out
    Set<String> collectionNames = db.getCollectionNames();
    for (final String s : collectionNames) {
        System.out.println(s);
    }
    // get a collection object to work with
    DBCollection coll = db.getCollection("influencer");

    //Create a boolean for either advertiser or influencer
    boolean advOrInf;

    //Loop 100 times creating new users
    //Should create about 50 influencers and about 50 advertisers
    /*for(int i = 0; i < 100; i++) {
       advOrInf = new Random().nextInt(2)==0;
            
       if(advOrInf == true) {
    //Create an advertiser account
    generateAdvertiser();
       }
       else {
    //Create an influencer account
    generateInfluencer();
       }
    }*/
    for (int i = 0; i < 1000; i++) {
        generateInfluencer();
    }

    System.out.println(coll);
    /*
    BasicDBObject doc = new BasicDBObject("_id", "josh")
    .append("isActive", "true")
    .append("sphere", "Politics")
    .append("allowExplicit", "true")
    .append("totalValue", 0)
    .append("totalTweets", 0)
    .append("payoutOption", 50);
    coll.insert(doc);
    DBObject myDoc = coll.findOne();
    System.out.println(myDoc);
     */
    mongoClient.close();
}

From source file:gMIRC_server.java

public static void cleaner() {
    try {//from www .j  av a  2  s .  c o  m
        boolean hard_clean = false;
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");

        DBCursor cursor = coll.find();
        try {
            Date now = new Date();
            long timestamp_now = now.getTime();
            long treshold = timestamp_now - (1000 * 60 * 5); //5 minutes
            while (cursor.hasNext()) {
                hard_clean = true;
                BasicDBObject temp = (BasicDBObject) cursor.next();
                Date time_temp = (Date) temp.get("timestamp");
                long timestamp_temp = time_temp.getTime();
                if (timestamp_temp < treshold) {
                    String target = temp.getString("username");
                    gMIRC_handler.SoftDelete(target);
                }
            }
            HardClean();
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:gMIRC_server.java

public static void HardClean() {
    try {//from w  w w  .  j  a  v a2 s. c  o  m
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll[] = new DBCollection[4];
        coll[0] = db.getCollection("channelCollection");
        coll[1] = db.getCollection("inbox");
        coll[2] = db.getCollection("activeUser");
        coll[3] = db.getCollection("passiveUser");

        DBCursor cursor = coll[3].find();

        try {
            while (cursor.hasNext()) {
                BasicDBObject temp = (BasicDBObject) cursor.next();
                String username = temp.getString("username");
                BasicDBObject query = new BasicDBObject("username", username);
                System.out.println("cleaning " + username);
                for (int i = 0; i < 4; i++) {
                    DBCursor cursor2 = coll[i].find(query);

                    try {
                        while (cursor2.hasNext()) {
                            DBObject temp2 = cursor2.next();
                            coll[i].remove(temp2);
                        }
                    } finally {
                        cursor2.close();
                    }
                }
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:gMIRC_server.java

/**
 * ONLY IF NO USER IS IN THE ACTIVE OR PASIVE COLLECTION;getting server to
 * clean state//  ww  w  .j a v  a 2s . c o m
 */
private static void UltimateClean() {
    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll[] = new DBCollection[4];
        coll[0] = db.getCollection("activeUser");
        coll[1] = db.getCollection("passiveUser");
        coll[2] = db.getCollection("channelCollection");
        coll[3] = db.getCollection("inbox");

        DBCursor cursor[] = new DBCursor[4];
        cursor[0] = coll[0].find();
        cursor[1] = coll[1].find();
        cursor[2] = coll[2].find();
        cursor[3] = coll[3].find();
        try {
            if (!cursor[0].hasNext() && !cursor[1].hasNext() && cursor[2].hasNext() && cursor[3].hasNext()) {
                System.out.println("SYSTEM RESTARTING with ULTIMATE CLEANING !");
                for (int i = 2; i <= 3; i++) {
                    coll[i].drop();
                }
                System.out.println("RESTART COMPLETE!");
            }
        } finally {
            cursor[0].close();
            cursor[1].close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Beans.SingletonDB.java

@PostConstruct
void loadConfiguration() {

    final Morphia morphia = new Morphia();
    morphia.map(Player.class);

    datastore = morphia.createDatastore(new MongoClient(), "players");
    datastore.ensureIndexes();// w  ww.j  av  a 2s. c  o m

    correctCodes = new HashSet();
}

From source file:br.com.esign.logistics.dao.impl.DatastoreAdapter.java

License:Open Source License

@PostConstruct
public void postConstruct() {
    datastore = morphiaAdapter.createDatastore(new MongoClient(), "logistics");
    datastore.ensureIndexes();//from ww w .jav  a 2s .  c  o m
    logger.log(Level.INFO, "Datastore successfully instantiated.");
}

From source file:br.com.mmsr.api.util.ConectionDB.java

public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(), "tcc");
}

From source file:br.ufg.inf.es.saep.sandbox.persistencia.view.StartSaep.java

License:Creative Commons License

/**
 * Mtodo de inicializao do sistema.//  w  ww  .  j  a va  2 s .c om
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    MongoClient cliente = new MongoClient();
    MongoDatabase mongoDatabase = cliente.getDatabase("Saep");

    InterfaceRadocDAO radocDAO = new BasicRadocDAO(mongoDatabase, "Radoc");
    InterfaceTipoDAO tipoDAO = new BasicTipoDAO(mongoDatabase, "Tipo");
    InterfaceResolucaoDAO resolucaoDAO = new BasicResolucaoDAO(mongoDatabase, "Resolucao");
    InterfaceParecerDAO parecerDAO = new BasicParecerDAO(mongoDatabase, "Parecer");

    mongoDatabase.drop();
    System.out.println("Fim do programa");
}