Example usage for com.mongodb MongoClientURI getUsername

List of usage examples for com.mongodb MongoClientURI getUsername

Introduction

In this page you can find the example usage for com.mongodb MongoClientURI getUsername.

Prototype

@Nullable
public String getUsername() 

Source Link

Document

Gets the username

Usage

From source file:org.netbeans.modules.mongodb.native_tools.MongoDumpExecAction.java

License:Open Source License

private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) {
    if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) {
        options.put(MongoDumpOptions.USERNAME, uri.getUsername());
    }/*from   ww w.  j  a  v a2  s  . c  om*/
    if (uri.getPassword() != null && uri.getPassword().length > 0) {
        options.put(MongoDumpOptions.PASSWORD, new String(uri.getPassword()));
    }
    if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) {
        final String hostWithPort = uri.getHosts().get(0);
        final Pattern p = Pattern.compile("(.*)(:(\\d+))?");
        final Matcher m = p.matcher(hostWithPort);
        if (m.matches()) {
            final String host = m.group(1);
            final String port = m.group(3);
            if (host.isEmpty() == false) {
                options.put(MongoDumpOptions.HOST, host);
                if (port != null) {
                    options.put(MongoDumpOptions.PORT, port);
                }
            }
        }
    }
    if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) {
        options.put(MongoDumpOptions.DB, uri.getDatabase());
    }
    if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) {
        options.put(MongoDumpOptions.COLLECTION, uri.getCollection());
    }
}

From source file:org.netbeans.modules.mongodb.native_tools.MongoRestoreExecAction.java

License:Open Source License

private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) {
    if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) {
        options.put(MongoRestoreOptions.USERNAME, uri.getUsername());
    }//from   w  w  w.j  av  a2 s. c  om
    if (uri.getPassword() != null && uri.getPassword().length > 0) {
        options.put(MongoRestoreOptions.PASSWORD, new String(uri.getPassword()));
    }
    if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) {
        final String hostWithPort = uri.getHosts().get(0);
        final Pattern p = Pattern.compile("(.*)(:(\\d+))?");
        final Matcher m = p.matcher(hostWithPort);
        if (m.matches()) {
            final String host = m.group(1);
            final String port = m.group(3);
            if (host.isEmpty() == false) {
                options.put(MongoRestoreOptions.HOST, host);
                if (port != null) {
                    options.put(MongoRestoreOptions.PORT, port);
                }
            }
        }
    }
    if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) {
        options.put(MongoRestoreOptions.DB, uri.getDatabase());
    }
    if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) {
        options.put(MongoRestoreOptions.COLLECTION, uri.getCollection());
    }
}

From source file:org.netbeans.modules.mongodb.native_tools.MongoShellExecAction.java

License:Open Source License

private void parseOptionsFromURI(ProcessCreator.Builder builder, MongoClientURI uri) {
    if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) {
        builder.option("--username", uri.getUsername());
    }/*from www. j ava 2 s .com*/
    if (uri.getPassword() != null && uri.getPassword().length > 0) {
        builder.option("--password", new String(uri.getPassword()));
    }
    if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) {
        final String hostWithPort = uri.getHosts().get(0);
        final Pattern p = Pattern.compile("(.*)(:(\\d+))?");
        final Matcher m = p.matcher(hostWithPort);
        if (m.matches()) {
            final String host = m.group(1);
            final String port = m.group(3);
            if (host.isEmpty() == false) {
                builder.option("--host", host);
                if (port != null) {
                    builder.option("--port", port);
                }
            }
        }
    }
}

From source file:org.netbeans.modules.mongodb.native_tools.MongoTopExecAction.java

License:Open Source License

private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) {
    if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) {
        options.put(MongoDumpOptions.USERNAME, uri.getUsername());
    }//from  w w  w .ja  v  a 2s.  c o  m
    if (uri.getPassword() != null && uri.getPassword().length > 0) {
        options.put(MongoDumpOptions.PASSWORD, new String(uri.getPassword()));
    }
    if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) {
        final String hostWithPort = uri.getHosts().get(0);
        final Pattern p = Pattern.compile("(.*)(:(\\d+))?");
        final Matcher m = p.matcher(hostWithPort);
        if (m.matches()) {
            final String host = m.group(1);
            final String port = m.group(3);
            if (host.isEmpty() == false) {
                options.put(MongoDumpOptions.HOST, host);
                if (port != null) {
                    options.put(MongoDumpOptions.PORT, port);
                }
            }
        }
    }
}

From source file:org.netbeans.modules.mongodb.ui.components.MongoURIEditorPanel.java

License:Open Source License

public void setMongoURI(MongoClientURI uri) {
    hostsListModel.clear();//w  w  w.  j  a  va 2  s .  c  o  m
    optionsListModel.clear();
    for (String string : uri.getHosts()) {
        final String[] rawHost = string.split(":");
        final String hostname = urlDecode(rawHost[0]);
        final Integer port = rawHost.length > 1 ? Integer.parseInt(rawHost[1]) : null;
        hostsListModel.addElement(new Host(hostname, port));
    }
    usernameField.setText(uri.getUsername());
    if (uri.getPassword() != null) {
        passwordField.setText(new String(uri.getPassword()));
    }
    databaseField.setText(uri.getDatabase());
    final List<Option> options = decodeOptions(uri);
    optionsListModel.clear();
    for (Option option : options) {
        optionsListModel.addElement(option);
    }
    updateURIField();
}

From source file:rapture.mongodb.MongoDBFactory.java

License:Open Source License

private Mongo getMongoFromLocalConfig(String instanceName) {
    String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-" + instanceName);
    log.info("Host is " + mongoHost);
    if (StringUtils.isBlank(mongoHost)) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST,
                mongoMsgCatalog.getMessage("NoHost"));
    }/*  www .j a va 2s . co  m*/
    MongoClientURI uri = new MongoClientURI(mongoHost);
    log.info("Username is " + uri.getUsername());
    log.info("Host is " + uri.getHosts().toString());
    log.info("DBName is " + uri.getDatabase());
    log.info("Collection is " + uri.getCollection());
    try {
        MongoClient mongo = new MongoClient(uri);
        mongoDBs.put(instanceName, mongo.getDB(uri.getDatabase()));
        mongoDatabases.put(instanceName, mongo.getDatabase(uri.getDatabase()));
        mongoInstances.put(instanceName, mongo);
        return mongo;
    } catch (MongoException e) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, new ExceptionToString(e));
    }
}

From source file:rapture.repo.integration.LocalTestSetup.java

License:Open Source License

public static void createUser() throws IOException, InterruptedException {
    String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-integrationTest");
    log.info("Host is " + mongoHost);
    if (mongoHost != null) {
        MongoClientURI uri = new MongoClientURI(mongoHost);
        List<String> hosts = uri.getHosts();
        for (String host : hosts) {
            String[] cmdarray = createSetupCommand(host, uri.getDatabase(), uri.getUsername(),
                    new String(uri.getPassword()));
            Process process = Runtime.getRuntime().exec(cmdarray);
            int retVal = process.waitFor();
            log.info(String.format("retVal=%s", retVal));
            log.info("output is " + IOUtils.toString(process.getInputStream()));
            if (retVal != 0) {
                log.info("error is " + IOUtils.toString(process.getErrorStream()));
            }/*w  w w .  j av a2s  . c om*/
        }
    } else {
        log.error("mongo host is not defined!");
    }
}