public List<String> getAllSchemas() { return ImmutableList.copyOf(client.listDatabaseNames()); }
/** * Perform the given operation on the database with the given name, only if that database exists. * * @param client the MongoDB client; may not be null * @param dbName the name of the database; may not be null * @param dbOperation the operation to perform; may not be null */ public static void onDatabase(MongoClient client, String dbName, Consumer<MongoDatabase> dbOperation) { if (contains(client.listDatabaseNames(), dbName)) { dbOperation.accept(client.getDatabase(dbName)); } }
/** * Perform the given operation on each of the database names. * * @param client the MongoDB client; may not be null * @param operation the operation to perform; may not be null */ public static void forEachDatabaseName(MongoClient client, Consumer<String> operation) { forEach(client.listDatabaseNames(), operation); }
@Override public Config validate(Map<String, String> connectorConfigs) { Configuration config = Configuration.from(connectorConfigs); // First, validate all of the individual fields, which is easy since don't make any of the fields invisible ... Map<String, ConfigValue> results = config.validate(MongoDbConnectorConfig.EXPOSED_FIELDS); // Get the config values for each of the connection-related fields ... ConfigValue hostsValue = results.get(MongoDbConnectorConfig.HOSTS.name()); ConfigValue userValue = results.get(MongoDbConnectorConfig.USER.name()); ConfigValue passwordValue = results.get(MongoDbConnectorConfig.PASSWORD.name()); // If there are no errors on any of these ... if (hostsValue.errorMessages().isEmpty() && userValue.errorMessages().isEmpty() && passwordValue.errorMessages().isEmpty()) { // Try to connect to the database ... try (ConnectionContext connContext = new ConnectionContext(config)) { try ( MongoClient client = connContext.clientFor(connContext.hosts()) ) { client.listDatabaseNames(); } } catch (MongoException e) { hostsValue.addErrorMessage("Unable to connect: " + e.getMessage()); } } return new Config(new ArrayList<>(results.values())); } }
if (Iterables.contains(client.listDatabaseNames(), DBNAME)) { client.getDatabase(DBNAME).drop();
public List<MongoDatabase> loadDatabases(MongoServer mongoServer, ServerConfiguration configuration) { final List<MongoDatabase> mongoDatabases = new LinkedList<>(); TaskWithReturnedObject<List<MongoDatabase>> perform = mongoClient -> { String userDatabase = configuration.getUserDatabase(); if (StringUtils.isNotEmpty(userDatabase)) { com.mongodb.client.MongoDatabase database = mongoClient.getDatabase(userDatabase); MongoDatabase mongoDatabase = new MongoDatabase(database.getName(), mongoServer); mongoDatabases.add(createMongoDatabaseAndItsCollections(mongoDatabase, database)); } else { MongoIterable<String> databaseNames = mongoClient.listDatabaseNames(); for (String databaseName : databaseNames) { com.mongodb.client.MongoDatabase database = mongoClient.getDatabase(databaseName); MongoDatabase mongoDatabase = new MongoDatabase(database.getName(), mongoServer); mongoDatabases.add(createMongoDatabaseAndItsCollections(mongoDatabase, database)); } } return mongoDatabases; }; return executeTask(configuration, perform); }
@Override public void evaluate() throws Throwable { MongoDbAvailable mongoAvailable = method.getAnnotation(MongoDbAvailable.class); if (mongoAvailable != null) { try { MongoClientOptions options = new MongoClientOptions.Builder() .serverSelectionTimeout(100) .build(); MongoClient mongo = new MongoClient(ServerAddress.defaultHost(), options); mongo.listDatabaseNames() .first(); } catch (Exception e) { logger.warn("MongoDb is not available. Skipping the test: " + target.getClass().getSimpleName() + "." + method.getName() + "()"); return; } } base.evaluate(); } };
MongoClient mongoClient = new MongoClient(); MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator(); while(dbsCursor.hasNext()) { System.out.println(dbsCursor.next()); }
@Override public boolean exists(final String instanceName) { requireNonNull( instanceName ); for(final String dbName : adminClient.listDatabaseNames()) { if(dbName.equals(instanceName)) { return true; } } return false; } }
private List<String> listDatabaseNames() { List<String> databaseNames = new ArrayList<>(); for (String databaseName : syncClient.listDatabaseNames()) { databaseNames.add(databaseName); } return databaseNames; }
@Override public boolean exists(final String instanceName) { requireNonNull( instanceName ); for(final String dbName : adminClient.listDatabaseNames()) { if(dbName.equals(instanceName)) { return true; } } return false; } }
public List<String> getDatabaseNames(){ MongoClient mongoClient = new MongoClient(); //Maybe replace it with an already existing client List<String> dbs = new ArrayList<String>(); MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator(); while(dbsCursor.hasNext()) { dbs.add(dbsCursor.next()); } return dbs; }
@Override public Collection<String> getAvailableModels() { Set<String> availableModels = new HashSet<>(); for (String s : mongoClient.listDatabaseNames()) { if (s.endsWith(dbNameSuffix)) { availableModels.add(s); } } return availableModels; }
@Override public Collection<String> getAvailableModels() { Set<String> availableModels = new HashSet<>(); for (String s : mongoClient.listDatabaseNames()) { if (!s.equalsIgnoreCase("admin") || s.equalsIgnoreCase("local")) { availableModels.add(s); } } return availableModels; }
@Override public List<String> getDatabaseNames() { ArrayList<String> dbNames = new ArrayList<>(); client.listDatabaseNames().into(dbNames); return dbNames; }
private void dropAllDatabases() { for (String databaseName : syncClient.listDatabaseNames()) { if (databaseName.equals("admin") || databaseName.equals("local")) { continue; } syncClient.dropDatabase(databaseName); } }
@Test public void testListDatabaseNames() throws Exception { assertThat(readOnlyClient.listDatabaseNames()).isEmpty(); writeClient.getDatabase("testdb").getCollection("testcollection").insertOne(new Document()); assertThat(toArray(readOnlyClient.listDatabaseNames())).containsExactly("testdb"); writeClient.getDatabase("bar").getCollection("testcollection").insertOne(new Document()); assertThat(toArray(readOnlyClient.listDatabaseNames())).containsExactly("bar", "testdb"); }
@Override public Set<Relationship> detect() { Set<Relationship> result = new HashSet<>(); for (MongoClient client : clients) { for (String db : client.listDatabaseNames()) { result.add(Dependency.on(Component.of(db, ComponentType.MONGODB))); } } return result; }
@Override public void run() throws WebProtegeConfigurationException { try { mongoClient.listDatabaseNames().first(); } catch (MongoTimeoutException e) { throw new WebProtegeConfigurationException(getUnknownHostErrorMessage()); } }
private void removeTestDBs() throws UnknownHostException { log.info("Removing test databases"); MongoClient mongo = TestHelper.getMongo(); for (String dbName : mongo.listDatabaseNames()) { if (dbName.startsWith(TestHelper.TEST_DATABASE_NAME)) { mongo.getDatabase(dbName).drop(); } } }