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:example.ScramSha1CredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];// ww  w. j  ava2  s .  c  om
    String user = args[1];
    String password = args[2];
    String source = args[3];

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

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createScramSha1Credential(user, source, password.toCharArray())),
            new MongoClientOptions.Builder().build());
    DB testDB = mongoClient.getDB("test");

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

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

From source file:example.X509CredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];/*  w  ww .  j  a  v  a2  s . c om*/
    String user = "CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US";

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

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createMongoX509Credential(user)),
            new MongoClientOptions.Builder().socketFactory(SSLSocketFactory.getDefault()).build());
    DB testDB = mongoClient.getDB("test");

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

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

}

From source file:fr.ippon.ippevent.nosqlha.mongo.configuration.MongoCongurationProperties.java

License:Apache License

private static ServerAddress newServerAddress(String hostname) {
    try {/*w  w w  . j ava2  s.com*/
        return new ServerAddress(hostname);
    } catch (UnknownHostException e) {
        throw Throwables.propagate(e);
    }
}

From source file:GeoHazardServices.Inst.java

License:Apache License

public Services() {

    System.out.println("Constructor");
    Locale.setDefault(new Locale("en", "US"));
    scheduler = new FairScheduler();
    worker = new ArrayList<WorkerThread>();

    URL config;/*from ww  w. ja  va 2s. c o m*/

    try {
        /* Try to load environment from context. */
        javax.naming.Context initCtx = new InitialContext();
        config = new File((String) initCtx.lookup("java:comp/env/config")).toURI().toURL();
    } catch (NamingException e1) {
        /* Otherwise, use the local properties file. */
        config = this.getClass().getClassLoader().getResource("config.properties");
    } catch (MalformedURLException e) {
        System.err.println("Malformed URL in environment context.");
        e.printStackTrace();
        return;
    }

    try {
        Properties prop = new Properties();
        prop.load(config.openStream());
        String dbname = prop.getProperty("db");
        String url = prop.getProperty("url");
        if (dbname == null || url == null)
            throw new IOException("Could not load properties.");

        String nodes[] = url.split(",");
        List<ServerAddress> addresses = new ArrayList<ServerAddress>();
        for (String node : nodes)
            addresses.add(new ServerAddress(node));

        System.out.println(addresses);

        mongoClient = new MongoClient(addresses);
        db = mongoClient.getDB(dbname);

    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        return;
    }

    loadSettings();

    /* Start scheduler thread. */
    new Thread(scheduler).start();

    loadInstitutions();

    /* Required to deliver dates in UTC instead of simply dropping the time zone as per default !!! */
    gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonUTCdateAdapter()).create();

    /* TODO: find right place */
    providers = new HashMap<String, IDataProvider>();
    providers.put("floodsim", new FloodProvider(db));

    Listener.registerService(this);
}

From source file:HAL.libraries.blackboard_client.BlackboardClient.java

License:Open Source License

/**
 * Constructs a BlackboardClient for the server at the specified host.
 *
 * @param host The mongoDB host.//from  w  ww  . j  av a  2  s .  c o m
 * @throws MongoConnectionException Connecting to the database failed.
 * @throws UnknownHostException The IP address of a host could not be determined.
 **/
public BlackboardClient(String host) throws UnknownHostException, GeneralMongoException {
    this.subscriptions = new ArrayList<BlackboardSubscription>();
    this.mongo = MongoDBConnection.getInstanceForHost(new ServerAddress(host)).getMongoClient();
}

From source file:io.debezium.connector.mongodb.MongoUtil.java

License:Apache License

/**
 * Parse the server address string, of the form {@code host:port} or {@code host}.
 * <p>// ww w. java  2s.c om
 * The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.
 * 
 * @param addressStr the string containing the host and port; may be null
 * @return the server address, or {@code null} if the string did not contain a host or host:port pair
 */
public static ServerAddress parseAddress(String addressStr) {
    if (addressStr != null) {
        addressStr = addressStr.trim();
        Matcher matcher = ADDRESS_PATTERN.matcher(addressStr);
        if (!matcher.matches()) {
            matcher = IPV6_ADDRESS_PATTERN.matcher(addressStr);
        }
        if (matcher.matches()) {
            // Both regex have the same groups
            String host = matcher.group(1);
            String port = matcher.group(3);
            if (port == null) {
                return new ServerAddress(host.trim());
            }
            return new ServerAddress(host.trim(), Integer.parseInt(port));
        }
    }
    return null;
}

From source file:io.hipstogram.trident.mongodb.MongoDBClient.java

License:Apache License

/**
 * Get a MongoDB client (MongoDB Java API)
 * @return A MongoDB client//from  www . j  a  v  a  2s.  co  m
 */
private MongoClient getClient() {
    if (client == null) {
        try {
            List<ServerAddress> addresses = new LinkedList<ServerAddress>();
            for (String host : hosts)
                addresses.add(new ServerAddress(host));
            client = new MongoClient(addresses);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return client;
}

From source file:io.opentracing.contrib.mongo.TracingMongoClient.java

License:Apache License

public TracingMongoClient(Tracer tracer, final String host) {
    this(tracer, new ServerAddress(host));
}

From source file:io.opentracing.contrib.mongo.TracingMongoClient.java

License:Apache License

public TracingMongoClient(Tracer tracer, final String host, final MongoClientOptions options) {
    this(tracer, new ServerAddress(host), options);
}

From source file:io.opentracing.contrib.mongo.TracingMongoClient.java

License:Apache License

private static List<ServerAddress> toServerAddressList(List<String> hosts) {
    List<ServerAddress> list = new ArrayList<>();
    for (String host : hosts) {
        list.add(new ServerAddress(host));
    }/*from  w  ww . ja  v  a 2s . c o  m*/
    return list;
}