Java tutorial
/* Copyright 2013 Red Hat, Inc. and/or its affiliates. This file is part of lightblue. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.redhat.lightblue.mongo.config.metadata; import com.mongodb.DB; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.ServerAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.net.ssl.SSLSocketFactory; /** * TODO revisit having spun this out into a common module. May only be needed by metadata. * * @author nmalik */ public class MongoConfiguration { private String name; private final List<ServerAddress> servers = new ArrayList<>(); private String collection; private Integer connectionsPerHost; private Boolean ssl = Boolean.TRUE; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the collection */ public String getCollection() { return collection; } /** * @param collection the collection to set */ public void setCollection(String collection) { this.collection = collection; } public void addServerAddress(String hostname, Integer port) throws UnknownHostException { this.servers.add(new ServerAddress(hostname, port)); } /** * @return the servers */ public Iterator<ServerAddress> getServerAddresses() { return servers.iterator(); } public void clearServerAddresses() { servers.clear(); } /** * @return the connectionsPerHost */ public Integer getConnectionsPerHost() { return connectionsPerHost; } /** * @param connectionsPerHost the connectionsPerHost to set */ public void setConnectionsPerHost(Integer connectionsPerHost) { this.connectionsPerHost = connectionsPerHost; } /** * @return the ssl */ public Boolean getSsl() { return ssl; } /** * @param ssl the ssl to set */ public void setSsl(Boolean ssl) { this.ssl = ssl; } /** * Returns an options object with defaults overriden where there is a valid override. * * @return */ public MongoClientOptions getMongoClientOptions() { MongoClientOptions.Builder builder = MongoClientOptions.builder(); if (connectionsPerHost != null) { builder.connectionsPerHost(connectionsPerHost); } if (ssl != null && ssl.booleanValue()) { // taken from MongoClientURI, written this way so we don't have to construct a URI to connect builder.socketFactory(SSLSocketFactory.getDefault()); } return builder.build(); } public DB getDB() throws UnknownHostException { MongoClient client = new MongoClient(servers, getMongoClientOptions()); return client.getDB(getName()); } }