Example usage for com.mongodb ServerAddress ServerAddress

List of usage examples for com.mongodb ServerAddress ServerAddress

Introduction

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

Prototype

public ServerAddress(final InetSocketAddress inetSocketAddress) 

Source Link

Document

Creates a ServerAddress

Usage

From source file:com.teamj.distribuidas.ebanking.persistence.PersistenceManager.java

public PersistenceManager() {
    MongoClientOptions mongoOptions = MongoClientOptions.builder().socketTimeout(60000).connectTimeout(60000)
            .build();/*from   w w  w  .j  ava 2 s  .  c  o  m*/

    try {

        mongoClient = new MongoClient(new ServerAddress("localhost"), mongoOptions);

    } catch (Exception e) {

        throw new RuntimeException("Error", e);
    }

    mongoClient.setWriteConcern(WriteConcern.SAFE);
    morphia = new Morphia();

    morphia.mapPackage(DB_PACKAGE, true);
    mds = morphia.createDatastore(mongoClient, DB_NAME);
    mds.ensureIndexes();
}

From source file:course.PrivateCloudController.java

License:Apache License

public PrivateCloudController(String mongoURIString) throws IOException {
    MongoCredential cred = MongoCredential.createCredential("admin", "cmpe283project1", "admin".toCharArray());
    final MongoClient mongoClient = new MongoClient(new ServerAddress("ds061721.mongolab.com:61721"),
            Arrays.asList(cred));
    final MongoDatabase blogDatabase = mongoClient.getDatabase("cmpe283project1");

    userDAO = new UserDAO(blogDatabase);
    sessionDAO = new SessionDAO(blogDatabase);

    statsDAO = new StatsDAO(mongoClient.getDatabase("cmpe283project1"));

    VMsDBCollection = mongoClient.getDatabase("cmpe283project1").getCollection("virtual_machines");
    if (VMsDBCollection != null) {
        System.out.println("VMs DB Collection found");
    } else//ww  w  .j a v  a 2s.c  o m
        System.out.println("Sorry VMs DB Collection NOT found");

    cfg = createFreemarkerConfiguration();
    this.servInst = null;
    this.Path = null;
    //this.VMsDBCollection = null;

    servInst = new ServiceInstance(new URL(SJSULAB.getVCenterURL()), SJSULAB.getVCenterLogin(),
            SJSULAB.getVCenterPassword(), true);
    if (servInst == null) {
        System.out.println("Connection with " + SJSULAB.getVCenterURL() + " FAILED");
    } else {
        Path = servInst.getRootFolder();
        if (Path == null) {
            System.out.println("Getting Root Folder FAILED");
        }
    }
    setPort(8082);
    initializeRoutes();

    // Start the Statistics Collection Thread
    Thread statsThread = new Thread(new statsCollector());
    statsThread.start();

    //Start the Availability Manager Thread
    AvailabiltyManager av = new AvailabiltyManager();
    av.begin();

}

From source file:de.tum.in.mongodb.MongoDBServiceConfiguration.java

License:Apache License

/**
 * Registers MongoDB Service//  w  w w.j a v a  2s  .co  m
 */
private void doRegister(final ComponentContext componentContext, final Map<String, Object> properties) {
    this.m_properties = properties;
    this.m_host = (String) this.m_properties.get(MONGO_DB_HOST);
    this.m_port = (int) this.m_properties.get(MONGO_DB_PORT);
    this.m_dbname = (String) this.m_properties.get(MONGO_DB_DBNAME);
    this.m_username = (String) this.m_properties.get(MONGO_DB_USERNAME);
    this.m_password = (String) this.m_properties.get(MONGO_DB_PASSWORD);

    if (this.m_username != null) {
        final MongoCredential credential = MongoCredential.createCredential(this.m_username, this.m_dbname,
                this.m_password.toCharArray());
        this.m_mongoClient = new MongoClient(new ServerAddress(this.m_host), Arrays.asList(credential));
        LOGGER.info("Authenticated as '" + this.m_username + "'");
    }

    this.registerMongoDBService(componentContext);
}

From source file:dk.au.cs.karibu.backend.mongo.StandardMongoConfiguration.java

License:Apache License

/**
 * Constructs a <code>StandardMongoConfiguration</code>.
 * /*from  w  w  w  .  j  a v  a 2s . co m*/
 * @param mongoProperties the properties of the mongo
 * @throws UnknownHostException 
 */
public StandardMongoConfiguration(Properties mongoProperties) throws UnknownHostException {
    String databaseName = FailFast.readProperty(mongoProperties, DATABASE_NAME);

    String addressString = FailFast.readProperty(mongoProperties, SERVER_ADDRESS_LIST);
    String[] addressStrings = addressString.split(",");

    ArrayList<ServerAddress> serverAddressList = new ArrayList<ServerAddress>();
    String nodename = null;

    // Do not catch the potential UnknownHostException here. Why?
    // Because this code will be run under initialization and thus
    // will fail-fast during startup - it signals a wrong
    // setup of the mongo db machines to connect to.
    for (String entry : addressStrings) {
        nodename = entry;
        serverAddressList.add(new ServerAddress(nodename));
    }

    // Note: FailFast not used there as the fields may be
    // missing for backward compatability
    String username = mongoProperties.getProperty(USERNAME);
    String password = mongoProperties.getProperty(PASSWORD);

    init(databaseName, serverAddressList, username, password);
}

From source file:ELK.ELKController.java

License:Apache License

public ELKController(String mongoURIString) throws IOException {
    MongoCredential cred = MongoCredential.createCredential("admin", "cmpe283", "admin".toCharArray());
    final MongoClient mongoClient = new MongoClient(new ServerAddress("ds047107.mongolab.com:47107"),
            Arrays.asList(cred));
    final MongoDatabase blogDatabase = mongoClient.getDatabase("cmpe283");

    userDAO = new UserDAO(blogDatabase);
    sessionDAO = new SessionDAO(blogDatabase);

    // statsDAO= new StatsDAO(mongoClient.getDatabase("cmpe283project1"));
    //  AvailabiltyManager av =new AvailabiltyManager();
    //  av.start();

    //  Thread statsThread= new Thread(new statsCollector());
    // statsThread.start();

    cfg = createFreemarkerConfiguration();
    // this.si = null;
    // this.rootFolder = null;

    //  si = new ServiceInstance(new URL(SJSULAB.getVmwareHostURL()), SJSULAB.getVmwareLogin(), SJSULAB.getVmwarePassword(), true);
    //  rootFolder = si.getRootFolder();

    setPort(8082);//from w  w w.  ja v a 2s .  c  o  m
    initializeRoutes();
    VMsDBCollection = mongoClient.getDatabase("cmpe283").getCollection("virtual_machines");
    if (VMsDBCollection != null) {
        System.out.println("VMs DB Collection found");
    } else
        System.out.println("Sorry VMs DB Collection NOT found");

}

From source file:es.dheraspi.sums.model.DAOMongoDB.java

@Override
public void insertSummoner(Summoner summoner, String region) {
    MongoCredential credential = MongoCredential.createCredential(user, dbname, password.toCharArray());
    try (MongoClient mongoClient = new MongoClient(new ServerAddress(host), Arrays.asList(credential))) {
        db = mongoClient.getDatabase(dbname);
        MongoCollection<Document> coll = db.getCollection("summoners");

        int profileIconID = summoner.getProfileIconID();

        Bson doc = new Document("$set", new Document("_id", summoner.getID()))
                .append("name", summoner.getName()).append("level", summoner.getLevel())
                .append("profileIconID", profileIconID < 0 ? 0 : profileIconID);

        Bson filter = Filters.eq("_id", region);
        switch (region) {
        case "EUW":
            break;
        case "EUNE":
            break;
        case "NA":
            break;
        case "LAN":
            break;
        case "LAS":
            break;
        case "BR":
            break;
        case "TR":
            break;
        case "RU":
            break;
        case "OCE":
            break;
        case "KR":
            break;
        case "JP":
            break;
        }/*from  w  w  w. j  a va2 s .  co m*/
        UpdateOptions options = new UpdateOptions().upsert(true);

        coll.updateOne(filter, doc, options);
    } catch (APIException ex) {
        //Some unknown error when trying to get matchList
    }
}

From source file:es.dheraspi.sums.model.DAOMongoDB.java

@Override
public Map<String, String> getLanesSumsAnalysis(Summoner summoner) {
    Map<String, String> lanes_data = new HashMap<String, String>();
    MongoCredential credential = MongoCredential.createCredential(user, dbname, password.toCharArray());
    try (MongoClient mongoClient = new MongoClient(new ServerAddress(host), Arrays.asList(credential))) {
        db = mongoClient.getDatabase(dbname);
        MongoCollection<Document> coll = db.getCollection("analysis");

        Bson query = new Document("_id", Long.toString(summoner.getID()));
        FindIterable<Document> iterable = coll.find(query);

        try {//from   w  w w .j a  v a 2  s .  c om
            Document bot_doc = (Document) iterable.first().get("bot");
            Document mid_doc = (Document) iterable.first().get("mid");
            Document jun_doc = (Document) iterable.first().get("jun");
            Document top_doc = (Document) iterable.first().get("top");

            Iterator<String> kiter = bot_doc.keySet().iterator();
            Iterator<Object> viter = bot_doc.values().iterator();
            String bot = "";
            try {
                bot = bot + "[{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                for (int i = 0; i < bot_doc.size() - 1; i++) {
                    bot = bot + ",{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                }
                bot = bot + "]";
            } catch (NoSuchElementException ex) {
            }

            kiter = mid_doc.keySet().iterator();
            viter = mid_doc.values().iterator();
            String mid = "";
            try {
                mid = mid + "[{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                for (int i = 0; i < mid_doc.size() - 1; i++) {
                    mid = mid + ",{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                }
                mid = mid + "]";
            } catch (NoSuchElementException ex) {
            }

            kiter = jun_doc.keySet().iterator();
            viter = jun_doc.values().iterator();
            String jun = "";
            try {
                jun = jun + "[{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                for (int i = 0; i < jun_doc.size() - 1; i++) {
                    jun = jun + ",{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                }
                jun = jun + "]";
            } catch (NoSuchElementException ex) {
            }

            kiter = top_doc.keySet().iterator();
            viter = top_doc.values().iterator();
            String top = "";
            try {
                top = top + "[{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                for (int i = 0; i < top_doc.size() - 1; i++) {
                    top = top + ",{\"summoners\": \"" + kiter.next() + "\", \"wins\": " + viter.next() + "}";
                }
                top = top + "]";
            } catch (NoSuchElementException ex) {
            }

            if (!bot.equals(""))
                lanes_data.put("bot", bot);
            if (!mid.equals(""))
                lanes_data.put("mid", mid);
            if (!jun.equals(""))
                lanes_data.put("jun", jun);
            if (!top.equals(""))
                lanes_data.put("top", top);
        } catch (NullPointerException ex) {
            // No games found
        }

        return lanes_data;
    }
}

From source file:es.dheraspi.sums.model.DAOMongoDB.java

@Override
public String getLanesChampsSums(Summoner summoner) {
    MongoCredential credential = MongoCredential.createCredential(user, dbname, password.toCharArray());
    try (MongoClient mongoClient = new MongoClient(new ServerAddress(host), Arrays.asList(credential))) {
        db = mongoClient.getDatabase(dbname);
        MongoCollection<Document> coll = db.getCollection("analysis");

        Bson query = new Document("_id", Long.toString(summoner.getID()));
        FindIterable<Document> iterable = coll.find(query);

        Document doc = iterable.iterator().next();
        Document result = (Document) doc.get("lane_champ_sums");

        return result.toJson();
    } catch (NoSuchElementException | NullPointerException ex) {
        return "empty";
    }//from  w w  w .j a v a2s .co  m
}

From source file:example.GSSAPICredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    // Set this property to avoid the default behavior where the program prompts on the command line for username/password
    //        Security.setProperty("auth.login.defaultCallbackHandler", "example.DefaultSecurityCallbackHandler");

    String server = args[0];/*  w w w  .ja v  a  2 s.c  o m*/
    String user = args[1];
    String databaseName = args[2];

    System.out.println("javax.security.auth.useSubjectCredsOnly: "
            + System.getProperty("javax.security.auth.useSubjectCredsOnly"));
    System.out.println("java.security.krb5.realm: " + System.getProperty("java.security.krb5.realm"));
    System.out.println("java.security.krb5.kdc: " + System.getProperty("java.security.krb5.kdc"));
    System.out.println(
            "auth.login.defaultCallbackHandler: " + Security.getProperty("auth.login.defaultCallbackHandler"));
    System.out.println("login.configuration.provider: " + Security.getProperty("login.configuration.provider"));
    System.out.println(
            "java.security.auth.login.config: " + Security.getProperty("java.security.auth.login.config"));
    System.out.println("login.config.url.1: " + Security.getProperty("login.config.url.1"));
    System.out.println("login.config.url.2: " + Security.getProperty("login.config.url.2"));
    System.out.println("login.config.url.3: " + Security.getProperty("login.config.url.3"));

    System.out.println("server: " + server);
    System.out.println("user: " + user);
    System.out.println("database: " + databaseName);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            asList(MongoCredential.createGSSAPICredential(user).withMechanismProperty("SERVICE_NAME",
                    "mongodb")),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
    System.out.println("Count: " + testDB.getCollection("test").count());
}

From source file:example.MongoCredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];//from   ww  w  .  java  2  s .co  m
    String user = args[1];
    String password = args[2];
    String databaseName = args[3];

    System.out.println("server: " + server);
    System.out.println("user: " + user);
    System.out.println("database: " + databaseName);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            asList(MongoCredential.createMongoCRCredential(user, "test", password.toCharArray())),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

    System.out.println("Count: " + testDB.getCollection("test").count());

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));

}