@ConditionalOnMissingBean(com.mongodb.client.MongoClient.class) @ConditionalOnProperty(CONSTANT_MONGODBURI) @org.springframework.context.annotation.Bean public com.mongodb.client.MongoClient mongoClient(@Autowired(required = false) com.mongodb.MongoClient mongoClient) { if (mongoClient == null) { return MongoClients.create(this.environment.getProperty(CONSTANT_MONGODBURI)); } else { List<ServerAddress> addressList = mongoClient.getAllAddress(); StringBuilder ber = new StringBuilder(); for (int i = 0; addressList != null && i < addressList.size(); i++) { ServerAddress address = addressList.get(i); String host = address.getHost(); int port = address.getPort(); if (i == 0) { ber.append(host).append(":").append(port); } else { ber.append(",").append(host).append(":").append(port); } } return MongoClients.create(String.format("mongodb://%s", ber.toString())); } }
@ConditionalOnMissingBean(com.mongodb.client.MongoClient.class) @ConditionalOnProperty(CONSTANT_MONGODBURI) @org.springframework.context.annotation.Bean public com.mongodb.client.MongoClient mongoClient(@Autowired(required = false) com.mongodb.MongoClient mongoClient) { if (mongoClient == null) { return MongoClients.create(this.environment.getProperty(CONSTANT_MONGODBURI)); } else { List<ServerAddress> addressList = mongoClient.getAllAddress(); StringBuilder ber = new StringBuilder(); for (int i = 0; addressList != null && i < addressList.size(); i++) { ServerAddress address = addressList.get(i); String host = address.getHost(); int port = address.getPort(); if (i == 0) { ber.append(host).append(":").append(port); } else { ber.append(",").append(host).append(":").append(port); } } return MongoClients.create(String.format("mongodb://%s", ber.toString())); } }
@ConditionalOnMissingBean(com.mongodb.client.MongoClient.class) @ConditionalOnProperty(CONSTANT_MONGODBURI) @org.springframework.context.annotation.Bean public com.mongodb.client.MongoClient mongoClient(@Autowired(required = false) com.mongodb.MongoClient mongoClient) { if (mongoClient == null) { return MongoClients.create(this.environment.getProperty(CONSTANT_MONGODBURI)); } else { List<ServerAddress> addressList = mongoClient.getAllAddress(); StringBuilder ber = new StringBuilder(); for (int i = 0; addressList != null && i < addressList.size(); i++) { ServerAddress address = addressList.get(i); String host = address.getHost(); int port = address.getPort(); if (i == 0) { ber.append(host).append(":").append(port); } else { ber.append(",").append(host).append(":").append(port); } } return MongoClients.create(String.format("mongodb://%s", ber.toString())); } }
/** * Loads the oplog collection. * * @return the oplog collection */ private MongoCollection<Document> readCollection() { mongoClient = createMongoClient(); log.trace("Starting database reader with configuration: "); log.trace("addresses: {}", StringUtils.join(mongoClient.getAllAddress(), ",")); log.trace("db: {}", db); log.trace("start: {}", start); final MongoDatabase db = mongoClient.getDatabase("local"); return db.getCollection("oplog.rs"); }
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { Map properties = new HashMap<>(); List<Resource> addressStates = new ArrayList<>(); for (ServerAddress serverAddress : mongoClient.getAllAddress()) { addressStates.add(new ServerAddressState(this, serverAddress)); } properties.put(SERVERS, addressStates); List<Resource> credentialStates = new ArrayList<>(); for (MongoCredential mongoCredential: mongoClient.getCredentialsList()) { credentialStates.add(new CredentialState(this, mongoCredential)); } properties.put(CredentialState.ID, credentialStates); WriteConcernState writeConcern = new WriteConcernState(this, mongoClient.getWriteConcern()); properties.put(WriteConcernState.ID, writeConcern); ReadPreferenceState readPreference = new ReadPreferenceState(this, mongoClient.getReadPreference()); properties.put(ReadPreferenceState.ID, readPreference); MongoClientOptionsState mongoClientOptions = new MongoClientOptionsState(this, mongoClient.getMongoClientOptions()); properties.put(MongoClientOptionsState.ID, mongoClientOptions); return properties; }
@SuppressWarnings("unchecked") private List<ServerAddress> extractServerAddresses(MongoClient client) { if (ClassUtils.isPresent("com.mongodb.connection.Cluster", getClass().getClassLoader())) { Object cluster = ReflectionTestUtils.getField(client, "cluster"); Object clusterSettings = ReflectionTestUtils.getField(cluster, "settings"); Method getHostsMethod = ReflectionUtils.findMethod(clusterSettings.getClass(), "getHosts"); return (List<ServerAddress>) ReflectionUtils.invokeMethod(getHostsMethod, clusterSettings); } return client.getAllAddress(); }
/** * Initialises the MongoDB connection using the Mongo object provided to the endpoint * * @throws CamelMongoDbException */ public void initializeConnection() throws CamelMongoDbException { LOG.info("Initialising MongoDb endpoint: {}", this); if (database == null || (collection == null && !(MongoDbOperation.getDbStats.equals(operation) || MongoDbOperation.command.equals(operation)))) { throw new CamelMongoDbException("Missing required endpoint configuration: database and/or collection"); } mongoDatabase = mongoConnection.getDatabase(database); if (mongoDatabase == null) { throw new CamelMongoDbException("Could not initialise MongoDbComponent. Database " + database + " does not exist."); } if (collection != null) { if (!createCollection && !databaseContainsCollection(collection)) { throw new CamelMongoDbException("Could not initialise MongoDbComponent. Collection " + collection + " and createCollection is false."); } mongoCollection = mongoDatabase.getCollection(collection, BasicDBObject.class); LOG.debug("MongoDb component initialised and endpoint bound to MongoDB collection with the following parameters. Address list: {}, Db: {}, Collection: {}", new Object[]{mongoConnection.getAllAddress().toString(), mongoDatabase.getName(), collection}); try { if (ObjectHelper.isNotEmpty(collectionIndex)) { ensureIndex(mongoCollection, createIndex()); } } catch (Exception e) { throw new CamelMongoDbException("Error creating index", e); } } }