List of usage examples for com.mongodb MongoClientURI MongoClientURI
public MongoClientURI(final String uri)
From source file:org.icgc.dcc.release.job.imports.util.MongoClientURIBuilder.java
License:Open Source License
private MongoClientURI build(boolean includeusernameInfo) { checkNotNull(host);/*ww w .j a va 2s . co m*/ val builder = new StringBuilder(PREFIX); if (!isNullOrEmpty(username) && !isNullOrEmpty(password) && includeusernameInfo) { builder.append(username).append(':').append(password).append('@'); } builder.append(host); if (port != null) { builder.append(':').append(port); } if (database != null) { builder.append('/').append(database); } if (collection != null) { builder.append('.').append(collection); } return new MongoClientURI(builder.toString()); }
From source file:org.imirp.imirp.db._DbModule.java
License:Apache License
@Provides @Singleton/*from w w w . j a v a 2s . c o m*/ MongoClientURI provideMongoURI(Config config) throws UnknownHostException { MongoClientURI uri = new MongoClientURI(config.getString("imirp.db.uri")); return uri; }
From source file:org.immutables.mongo.fixture.MongoContext.java
License:Apache License
/** * Allows to switch between Fongo and MongoDB based on system parameter {@code mongo}. *//*w w w . j a v a2s .co m*/ private static MongoClient createClient() { String uri = System.getProperty("mongo"); return uri != null ? new MongoClient(new MongoClientURI(uri)) : new Fongo("FakeMongo").getMongo(); }
From source file:org.immutables.mongo.repository.RepositorySetup.java
License:Apache License
/** * Create setup using MongoDB client uri. * <ul>/*from ww w. j av a 2 s .c om*/ * <li>URI should contain database path segment</li> * <li>New internal {@link MongoClient} will be created</li> * <li>New internal executor will be created (with shutdown on jvm exit)</li> * <li>New {@link Gson} instance will be created configured with type adapter factory providers</li> * </ul> * <p> * Setup created by this factory methods should be reused to configure collection repositories for * the same MongoDB database. * <p> * This constructor designed for ease of use in sample scenarious. For more flexibility consider * using {@link #builder()} with custom constructed {@link ListeningExecutorService * executor} and {@link DB database} handle. * @param uri string that will be parsed as {@link MongoClientURI}. * @see MongoClientURI * @return repository setup instance. */ public static RepositorySetup forUri(String uri) { MongoClientURI clientUri = new MongoClientURI(uri); @Nullable String databaseName = clientUri.getDatabase(); checkArgument(databaseName != null, "URI should contain database path segment"); return builder().database(newMongoClient(clientUri).getDB(databaseName)).executor(newExecutor()) .gson(createGson()).build(); }
From source file:org.jberet.repository.MongoRepository.java
License:Open Source License
public MongoRepository(final Properties configProperties) { dataSourceName = configProperties.getProperty(JdbcRepository.DATASOURCE_JNDI_KEY); dbUrl = configProperties.getProperty(JdbcRepository.DB_URL_KEY); //if dataSourceName is configured, use dataSourceName; //else if dbUrl is specified, use dbUrl; if (dataSourceName != null) { dataSourceName = dataSourceName.trim(); }//from w w w. ja v a2 s . c o m if (dataSourceName != null && !dataSourceName.isEmpty()) { try { mongoClient = InitialContext.doLookup(dataSourceName); for (final DB d : mongoClient.getUsedDatabases()) { db = d; break; } } catch (final NamingException e) { throw BatchMessages.MESSAGES.failToLookupDataSource(e, dataSourceName); } } else { if (dbUrl != null) { dbUrl = dbUrl.trim(); try { final MongoClientURI uri = new MongoClientURI(dbUrl); mongoClient = (MongoClient) Mongo.Holder.singleton().connect(uri); db = mongoClient.getDB(uri.getDatabase()); } catch (final Exception e) { throw BatchMessages.MESSAGES.invalidConfigProperty(e, JdbcRepository.DB_URL_KEY, dbUrl); } } } if (!db.collectionExists(TableColumns.SEQ)) { seqCollection = db.createCollection(TableColumns.SEQ, new BasicDBObject()); final DBObject jobInstanceDbo = new BasicDBObject(TableColumns._id, TableColumns.JOBINSTANCEID); jobInstanceDbo.put(TableColumns.SEQ, 1L); final DBObject jobExecutionDbo = new BasicDBObject(TableColumns._id, TableColumns.JOBEXECUTIONID); jobExecutionDbo.put(TableColumns.SEQ, 1L); final DBObject stepExecutionDbo = new BasicDBObject(TableColumns._id, TableColumns.STEPEXECUTIONID); stepExecutionDbo.put(TableColumns.SEQ, 1L); seqCollection.insert(jobInstanceDbo, jobExecutionDbo, stepExecutionDbo); } else { seqCollection = db.getCollection(TableColumns.SEQ); } }
From source file:org.jberet.support.io.MongoClientObjectFactory.java
License:Open Source License
/** * Gets an instance of {@code com.mongodb.MongoClient} based on the resource configuration in the application server. * The parameter {@code environment} contains MongoDB client connection properties, and accepts the following property: * <ul>//ww w . j a v a 2 s .c o m * <li>uri: uri to connect to MongoDB instance * <li>host: single host and port, or multiple host and port specification in the format * host1[:port1][,host2[:port2],...[,hostN[:portN]]] * <li>database: MongoDB database name, e.g., testData * <li>collection: MongoDB collection name, e.g., movies * <li>options: MongoDB client options, e.g., safe=true&wtimeout=1000 * <li>user: MongoDB username * <li>password: MongoDB password * </ul> * See also <a href="http://api.mongodb.org/java/2.12/com/mongodb/MongoClientURI.html">MongoClientURI javadoc.</a> * * @param obj the JNDI name of {@code com.mongodb.MongoClient} resource * @param name always null * @param nameCtx always null * @param environment a {@code Hashtable} of configuration properties for {@code com.mongodb.MongoClient} * @return an instance of {@code com.mongodb.MongoClient} * @throws Exception any exception occurred */ @Override public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception { final MongoClient mongoClient; final MongoClientURI clientURI; final String uri = (String) environment.get("uri"); if (uri != null) { clientURI = new MongoClientURI(uri); } else { clientURI = createMongoClientURI((String) environment.get("host"), (String) environment.get("database"), (String) environment.get("collection"), (String) environment.get("options"), (String) environment.get("user"), (String) environment.get("password")); } mongoClient = (MongoClient) Mongo.Holder.singleton().connect(clientURI); SupportLogger.LOGGER.tracef("getObjectInstance obtained MongoClient %s with uri %s%n", mongoClient, clientURI); return mongoClient; }
From source file:org.jberet.support.io.MongoClientObjectFactory.java
License:Open Source License
/** * Creates a {@code com.mongodb.MongoClientURI} with connection properties host, database, collection, options, user, * and password. Host property is mandatory and others are optional. * * @param host single host and port, or multiple host and port specification in the format * host1[:port1][,host2[:port2],...[,hostN[:portN]]] * @param database MongoDB database name, e.g., testData * @param collection MongoDB collection name, e.g., movies * @param options MongoDB client options, e.g., safe=true&wtimeout=1000 * @param user MongoDB username/*from w w w.jav a 2 s . co m*/ * @param password MongoDB password * @return {@code com.mongodb.MongoClientURI} created from passed parameters * @throws Exception any exception during the creation of {@code MongoClientURI} */ static MongoClientURI createMongoClientURI(final String host, final String database, final String collection, final String options, final String user, final String password) throws Exception { if (host == null) { throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, null, "host"); } //The format of the URI is: //mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]] final StringBuilder uriVal = new StringBuilder("mongodb://"); if (user != null) { uriVal.append(user).append(':').append(password == null ? "" : password).append('@'); } uriVal.append(host).append('/'); if (database != null) { uriVal.append(database); if (collection != null) { uriVal.append('.').append(collection); } } if (options != null) { uriVal.append('?').append(options); } return new MongoClientURI(uriVal.toString()); }
From source file:org.jberet.support.io.MongoItemReaderWriterBase.java
License:Open Source License
protected void init() throws Exception { if (beanType == null) { throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, null, "beanType"); }//from w w w . j av a 2s . c om if (mongoClientLookup == null) { final MongoClientURI clientURI; if (uri != null) { clientURI = new MongoClientURI(uri); if (database == null) { database = clientURI.getDatabase(); } if (collection == null) { collection = clientURI.getCollection(); } } else { clientURI = MongoClientObjectFactory.createMongoClientURI(host, database, collection, options, user, password); } mongoClient = (MongoClient) Mongo.Holder.singleton().connect(clientURI); } else { mongoClient = InitialContext.doLookup(mongoClientLookup); } db = mongoClient.getDB(database); jacksonCollection = JacksonDBCollection.wrap(db.getCollection(collection), beanType, String.class); }
From source file:org.jboss.javaee.mongodb.MongoExtension.java
License:Apache License
/** * If the application has a {@link MongoClientDefinition} register the bean for it unless user has defined a bean or a * producer for a <code>MongoClient</code> *///from w w w. ja va 2 s .c o m void registerDataSourceBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) { if (mongoDef != null) { if (bm.getBeans(MongoClient.class, DefaultLiteral.INSTANCE).isEmpty()) { log.log(Level.INFO, "Registering bean for MongoDB datasource {0}", mongoDef.name()); MongoClientURI uri = new MongoClientURI(mongoDef.url()); abd.addBean(bm.createBean( new MongoClientBeanAttributes( bm.createBeanAttributes(bm.createAnnotatedType(MongoClient.class))), MongoClient.class, new MongoClientProducerFactory(uri))); } else { log.log(Level.INFO, "Application contains a default MongoClient Bean, automatic registration will be disabled"); } } }
From source file:org.jumlabs.jcr.oak.rpc.AppConfiguration.java
@Bean public MongoConnection mongoConnection() { MongoClientURI uri = new MongoClientURI( "mongodb://" + connectionSettings.getMongoHost() + "/" + connectionSettings.getMongoDB()); MongoConnection mongo = null;/*from ww w. jav a 2 s . co m*/ try { mongo = new MongoConnection(uri.getURI()); } catch (UnknownHostException ex) { logger.error(ex.getMessage(), ex); } return mongo; }