List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:org.socialhistoryservices.dao.MongoDBSingleton.java
License:Open Source License
private synchronized Mongo setMongo(String[] serverAddresses, MongoClientOptions options) { if (mongo == null) { final List<ServerAddress> replSet = new ArrayList<ServerAddress>(serverAddresses.length); for (String url : serverAddresses) { String[] split = url.split(":", 2); try { ServerAddress host = (split.length == 2) ? new ServerAddress(split[0], Integer.parseInt(split[1])) : new ServerAddress(url); replSet.add(host);/*from w ww. j a v a 2 s .c o m*/ } catch (UnknownHostException e) { System.err.println(e); e.printStackTrace(System.err); } } mongo = new MongoClient(replSet, options); } return mongo; }
From source file:org.sourcepit.akka.poc.mongodb.MongoClientActor.java
License:Apache License
private static ServerAddress toServerAddresse(BSONObject server) throws UnknownHostException { final Object oHost = server.get("host"); final String host = oHost == null ? ServerAddress.defaultHost() : (String) oHost; final Object oPort = server.get("port"); final int port = oPort == null ? ServerAddress.defaultPort() : ((Integer) oPort).intValue(); return new ServerAddress(host, port); }
From source file:org.springframework.boot.autoconfigure.mongo.MongoClientFactory.java
License:Apache License
private MongoClient createEmbeddedMongoClient(MongoClientOptions options, int port) { if (options == null) { options = MongoClientOptions.builder().build(); }/* www .ja v a 2 s . c o m*/ String host = (this.properties.getHost() != null) ? this.properties.getHost() : "localhost"; return new MongoClient(Collections.singletonList(new ServerAddress(host, port)), options); }
From source file:org.springframework.boot.autoconfigure.mongo.MongoClientFactory.java
License:Apache License
private MongoClient createNetworkMongoClient(MongoClientOptions options) { MongoProperties properties = this.properties; if (properties.getUri() != null) { return createMongoClient(properties.getUri(), options); }/*from w w w .ja v a 2 s.c om*/ if (hasCustomAddress() || hasCustomCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } MongoCredential credentials = getCredentials(properties); String host = getValue(properties.getHost(), "localhost"); int port = getValue(properties.getPort(), MongoProperties.DEFAULT_PORT); List<ServerAddress> seeds = Collections.singletonList(new ServerAddress(host, port)); return (credentials != null) ? new MongoClient(seeds, credentials, options) : new MongoClient(seeds, options); } return createMongoClient(MongoProperties.DEFAULT_URI, options); }
From source file:org.springframework.boot.autoconfigure.mongo.MongoProperties.java
License:Apache License
/** * Creates a {@link MongoClient} using the given {@code options} and * {@code environment}. If the configured port is zero, the value of the * {@code local.mongo.port} property retrieved from the {@code environment} is used to * configure the client.//from w w w .ja v a 2 s . co m * * @param options the options * @param environment the environment * @return the Mongo client * @throws UnknownHostException if the configured host is unknown */ public MongoClient createMongoClient(MongoClientOptions options, Environment environment) throws UnknownHostException { try { if (hasCustomAddress() || hasCustomCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } List<MongoCredential> credentials = null; if (hasCustomCredentials()) { String database = this.authenticationDatabase == null ? getMongoClientDatabase() : this.authenticationDatabase; credentials = Arrays .asList(MongoCredential.createCredential(this.username, database, this.password)); } String host = this.host == null ? "localhost" : this.host; int port = determinePort(environment); return new MongoClient(Arrays.asList(new ServerAddress(host, port)), credentials, options); } // The options and credentials are in the URI return new MongoClient(new MongoClientURI(this.uri, builder(options))); } finally { clearPassword(); } }
From source file:org.springframework.boot.autoconfigure.mongo.ReactiveMongoClientFactory.java
License:Apache License
private MongoClient createEmbeddedMongoClient(MongoClientSettings settings, int port) { Builder builder = builder(settings); String host = (this.properties.getHost() != null) ? this.properties.getHost() : "localhost"; builder.applyToClusterSettings(/* w w w . ja va2s. c o m*/ (cluster) -> cluster.hosts(Collections.singletonList(new ServerAddress(host, port)))); return createMongoClient(builder); }
From source file:org.springframework.boot.autoconfigure.mongo.ReactiveMongoClientFactory.java
License:Apache License
private MongoClient createCredentialNetworkMongoClient(MongoClientSettings settings) { Assert.state(this.properties.getUri() == null, "Invalid mongo configuration, " + "either uri or host/port/credentials must be specified"); Builder builder = builder(settings); if (hasCustomCredentials()) { applyCredentials(builder);/*w ww. j a v a2 s .c om*/ } String host = getOrDefault(this.properties.getHost(), "localhost"); int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT); ServerAddress serverAddress = new ServerAddress(host, port); builder.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress))); return createMongoClient(builder); }
From source file:org.swissbib.docproc.flink.plugins.DSV11ContentEnrichment.java
License:Open Source License
private void initializeMongoConnection(HashMap<String, String> configuration) throws Exception { try {//w w w. j a v a 2 s. co m String[] mongoClient = configuration.get("MONGO.CLIENT").split("###"); String[] mongoAuthentication = null; if (configuration.containsKey("MONGO.AUTHENTICATION")) { mongoAuthentication = configuration.get("MONGO.AUTHENTICATION").split("###"); } ServerAddress server = new ServerAddress(mongoClient[0], Integer.valueOf(mongoClient[1])); String[] mongoDB = configuration.get("MONGO.DB.DSV11").split("###"); DB db = null; if (mongoAuthentication != null) { MongoCredential credential = MongoCredential.createMongoCRCredential(mongoAuthentication[1], mongoAuthentication[0], mongoAuthentication[2].toCharArray()); mClient = new MongoClient(server, Arrays.asList(credential)); db = mClient.getDB(mongoDB[0]); } else { mClient = new MongoClient(server); db = mClient.getDB(mongoDB[0]); } //simple test if authentication was successfull CommandResult cR = db.getStats(); if (cR != null && !cR.ok()) { throw new Exception( "authentication against database wasn't possible - no DSV11 Processing will take place when type DSV11ContentEnrichment is called from XSLT templates"); } nativeSource = mClient.getDB(mongoDB[0]); searchCollection = nativeSource.getCollection(mongoDB[1]); } catch (UnknownHostException uHE) { dsv11ProcessingError.error("MongoError", "Mongo Connection couldn't be established"); dsv11ProcessingError.error("MongoError", uHE); uHE.printStackTrace(); throw uHE; } catch (Exception ex) { dsv11ProcessingError.error("MongoError", "General Exception while trying to connect to Mongo"); dsv11ProcessingError.error("MongoError", ex); ex.printStackTrace(); throw ex; } }
From source file:org.swissbib.docproc.flink.plugins.GNDContentEnrichment.java
License:Open Source License
private void initializeMongoConnection(HashMap<String, String> configuration) throws Exception { try {// w ww.ja v a2s . com String[] mongoClient = configuration.get("MONGO.CLIENT").split("###"); String[] mongoAuthentication = null; if (configuration.containsKey("MONGO.AUTHENTICATION")) { mongoAuthentication = configuration.get("MONGO.AUTHENTICATION").split("###"); } ServerAddress server = new ServerAddress(mongoClient[0], Integer.valueOf(mongoClient[1])); String[] mongoDB = configuration.get("MONGO.DB").split("###"); DB db = null; if (mongoAuthentication != null) { MongoCredential credential = MongoCredential.createMongoCRCredential(mongoAuthentication[1], mongoAuthentication[0], mongoAuthentication[2].toCharArray()); mClient = new MongoClient(server, Arrays.asList(credential)); db = mClient.getDB(mongoAuthentication[0]); } else { mClient = new MongoClient(server); db = mClient.getDB(mongoDB[0]); } //simple test if authentication was successfull CommandResult cR = db.getStats(); if (cR != null && !cR.ok()) { throw new Exception( "authentication against database wasn't possible - no GND Processing will take place when type is called from XSLT templates"); } nativeSource = mClient.getDB(mongoDB[0]); searchCollection = nativeSource.getCollection(mongoDB[1]); searchField = mongoDB[2]; responseField = mongoDB[3]; if (mongoDB.length > 4) { responseFieldMACS = mongoDB[4]; } } catch (UnknownHostException uHE) { gndProcessingError.error("MongoError", "Mongo Connection couldn't be established"); gndProcessingError.error("MongoError", uHE); uHE.printStackTrace(); throw uHE; } catch (Exception ex) { gndProcessingError.error("MongoError", "General Exception while trying to connect to Mongo"); gndProcessingError.error("MongoError", ex); ex.printStackTrace(); throw ex; } }
From source file:org.swissbib.srw.SRWUpdateServiceLifecyle.java
License:Open Source License
@Override public void startUp(ConfigurationContext configurationContext, AxisService axisService) { System.out.println("in startup"); final String UPD_DIR = "updateDir"; final String DEL_DIR = "deleteDir"; final String DEL_PATTERN = "deletePattern"; final String TRANSFORM_TEMPLATE = "transformTemplate"; final String FILE_PREFIX = "filePrefix"; final String FILE_SUFFIX = "fileSuffix"; final String RECORD_NS = "recordWithNamespaces"; final String NORMALIZE_CHARS = "normalizeChars"; final String RECORD_IN_RESPONSE = "includeRecordInResponse"; final String LOG_MESSAGES = "logMessages"; final String MONGO_CLIENT = "MONGO.CLIENT"; final String MONGO_AUTHENTICATION = "MONGO.AUTHENTICATION"; final String MONGO_DB = "MONGO.DB"; final String ACTIVE_MONGO_CLIENT = "activeMongoClient"; final String ACTIVE_MONGO_COLLECTION = "activeMongoCollection"; final String CHECK_LEADER_FOR_DELETE = "checkLeaderForDelete"; try {// www . j a v a2 s .c o m Parameter updDir = axisService.getParameter(UPD_DIR); axisService.addParameter(updDir); Parameter delDir = axisService.getParameter(DEL_DIR); axisService.addParameter(delDir); Parameter filePrefix = axisService.getParameter(FILE_PREFIX); axisService.addParameter(filePrefix); Parameter fileSuffix = axisService.getParameter(FILE_SUFFIX); axisService.addParameter(fileSuffix); Parameter recordNS = axisService.getParameter(RECORD_NS); axisService.addParameter(recordNS); Parameter checkLeaderForDelete = axisService.getParameter(CHECK_LEADER_FOR_DELETE); boolean checkLeader = Boolean.valueOf(checkLeaderForDelete.getValue().toString()); axisService.addParameter(CHECK_LEADER_FOR_DELETE, checkLeader); Parameter normalizeChars = axisService.getParameter(NORMALIZE_CHARS); axisService.addParameter(normalizeChars); Parameter recordInResponse = axisService.getParameter(RECORD_IN_RESPONSE); axisService.addParameter(recordInResponse); String transformTemplate = axisService.getParameter(TRANSFORM_TEMPLATE).getValue().toString(); InputStream stream = getClass().getClassLoader().getResourceAsStream(transformTemplate); StreamSource source = new StreamSource(stream); TransformerFactory transformerFactory = TransformerFactory.newInstance(); //Transformer transform = transformerFactory.newTransformer(source); Templates recordTransformer = transformerFactory.newTemplates(source); axisService.addParameter(new Parameter(TRANSFORM_TEMPLATE, recordTransformer)); //logging of messages? Parameter logging = axisService.getParameter(LOG_MESSAGES); boolean logActive = Boolean.valueOf(logging.getValue().toString()); if (logActive) { try { //it is expected: // <parameter name="MONGO.CLIENT">[host]###[port]</parameter> String[] mongoClient = ((String) axisService.getParameter(MONGO_CLIENT).getValue()) .split("###"); //Todo: right now I expect the Mongo storage is running in secure mode //if not the procedure to connect is a little bit different //take a look at GND and DSV11 Plugin in content2SearchDoc repository. Configuration for messageCatcher should be adapted //it is expected that mongoAuthentication contains the values for: //<parameter name="MONGO.AUTHENTICATION">[auth-db]###[user]###[password]</parameter> String[] mongoAuthentication = ((String) axisService.getParameter(MONGO_AUTHENTICATION) .getValue()).split("###"); //it is expected: //<parameter name="MONGO.DB">[logging DB]###[collection]</parameter> String[] mongoDB = ((String) axisService.getParameter(MONGO_DB).getValue()).split("###"); System.out.println("mongoHost: " + mongoClient[0]); System.out.println("mongoPort: " + mongoClient[1]); ServerAddress server = new ServerAddress(mongoClient[0], Integer.valueOf(mongoClient[1])); MongoCredential credential = MongoCredential.createMongoCRCredential(mongoAuthentication[1], mongoAuthentication[0], mongoAuthentication[2].toCharArray()); MongoClient mClient = new MongoClient(server, Arrays.asList(credential)); DB db = mClient.getDB(mongoAuthentication[0]); //simple test if authentication was successfull CommandResult cR = db.getStats(); if (cR != null && cR.ok()) { System.out.println("authenticated"); //mongoDB contains the application DB and collection within this DB DB messageDB = mClient.getDB(mongoDB[0]); DBCollection messageCollection = messageDB.getCollection(mongoDB[1]); axisService.addParameter(new Parameter(ACTIVE_MONGO_COLLECTION, messageCollection)); axisService.addParameter(new Parameter(LOG_MESSAGES, "true")); } else { System.out.println("not authenticated"); throw new Exception( "authentication against Mongo database wasn't possible - message logging isn't possible"); } } catch (UnknownHostException uhExc) { axisService.addParameter(new Parameter(LOG_MESSAGES, "false")); uhExc.printStackTrace(); } catch (Exception exc) { axisService.addParameter(new Parameter(LOG_MESSAGES, "false")); exc.printStackTrace(); } } else { axisService.addParameter(new Parameter(LOG_MESSAGES, "false")); } } catch (AxisFault aF) { System.out.println("Fehler bei Speichern von UPD_DIR und DEL_DIR"); aF.printStackTrace(); } catch (TransformerConfigurationException configExcept) { configExcept.printStackTrace(); } }