Example usage for java.lang System console

List of usage examples for java.lang System console

Introduction

In this page you can find the example usage for java.lang System console.

Prototype

public static Console console() 

Source Link

Document

Returns the unique java.io.Console Console object associated with the current Java virtual machine, if any.

Usage

From source file:org.apache.hadoop.gateway.services.security.impl.DefaultMasterService.java

private void displayWarning(boolean persisting) {
    Console c = System.console();
    if (c == null) {
        System.err.println("No console.");
        System.exit(1);//from w  w w  . ja  v  a 2  s.co m
    }
    if (persisting) {
        c.printf(
                "***************************************************************************************************\n");
        c.printf(
                "You have indicated that you would like to persist the master secret for this gateway instance.\n");
        c.printf("Be aware that this is less secure than manually entering the secret on startup.\n");
        c.printf("The persisted file will be encrypted and primarily protected through OS permissions.\n");
        c.printf(
                "***************************************************************************************************\n");
    } else {
        c.printf(
                "***************************************************************************************************\n");
        c.printf(
                "Be aware that you will need to enter your master secret for future starts exactly as you do here.\n");
        c.printf("This secret is needed to access protected resources for the gateway process.\n");
        c.printf("The master secret must be protected, kept secret and not stored in clear text anywhere.\n");
        c.printf(
                "***************************************************************************************************\n");
    }
}

From source file:edu.utah.bmi.ibiomes.cli.CLIUtils.java

/**
 * Connect to iRODS using parameters given in command-line interface.
 * @return Connection object/*  w  ww. j  a  v  a  2s  . c  om*/
 * @throws IOException
 * @throws JargonException
 */
public static IRODSConnector getConnectionFromCLI() throws IOException, JargonException {
    Console objConsole = System.console();
    if (objConsole == null) {
        System.err.println("Console Object is not available.");
        System.exit(1);
    }

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));

    System.out.println("Host:");
    String host = in.readLine();

    System.out.println("Port [1247]:");
    String iPort = in.readLine();

    int port = 1247;
    if (iPort != null && iPort.length() > 0)
        port = Integer.parseInt(iPort);

    System.out.println("Zone:");
    String zone = in.readLine();

    System.out.println("Default resource:");
    String server = in.readLine();

    System.out.println("Username:");
    String userName = in.readLine();

    //retrieve iRODS password
    System.out.println("Password:");
    char[] pwdArray = objConsole.readPassword();
    String password = String.copyValueOf(pwdArray);

    IRODSConnector cnx = new IRODSConnector(host, port, userName, password, "", zone, server);
    try {
        //try to login
        IRODSAccessObjectFactory aoFactory = cnx.getFileSystem().getIRODSAccessObjectFactory();
        aoFactory.getUserAO(cnx.getAccount());
        System.out.println("\nConnected! (" + cnx.getAccount().toURI(false) + ")");
    } catch (JargonException exc) {
        System.out.println("Login info: " + cnx.getAccount().getUserName() + "@" + cnx.getAccount().getHost()
                + ":" + cnx.getAccount().getPort() + ").");
        System.out.println("Login failed. Try again.");
        cnx = null;
    }
    return cnx;
}

From source file:de.hasait.clap.CLAP.java

/**
 * @param pNLS {@link ResourceBundle} used for messages.
 *///from   w w  w .  ja  v a 2 s  .c om
public CLAP(final ResourceBundle pNLS) {
    super();

    _nls = pNLS;

    _shortOptPrefix = '-';
    _longOptPrefix = "--"; //$NON-NLS-1$
    _longOptEquals = "="; //$NON-NLS-1$

    _converters = new HashMap<Class<?>, CLAPConverter<?>>();
    initDefaultConverters();

    _root = new CLAPNodeList(this);
    _root.setHelpCategory(1000, NLSKEY_DEFAULT_HELP_CATEGORY);
    _root.setUsageCategory(1000, NLSKEY_DEFAULT_USAGE_CATEGORY);

    if (System.console() != null) {
        _uiCallback = new SystemConsoleUICallback();
    } else {
        _uiCallback = new SwingDialogUICallback(null);
    }
}

From source file:net.ronoaldo.code.appenginetools.RemoteApiHelper.java

/**
 * Retrieve the user {@link Credentials} to connect with the AppEngine
 * Remote API.//from w w  w . j a v  a2  s  .c  o m
 * 
 * <p>
 * Currently, try to load a file from $HOME/.remoteapi.properties, a
 * {@link Properties} file that is suposed to store the email and password
 * for your Google Account. If this file is not found, ask for user
 * credentials from the command-line.
 * 
 * @return the informed user {@link Credentials}.
 */
protected Properties getCredentials() {
    Properties p = new Properties();
    File f = FileUtils.atHomeDir(".remoteapi.properties");
    if (f.exists()) {
        try {
            p.load(new FileReader(f));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        String email = new String(System.console().readLine("Email: "));
        String password = new String(System.console().readPassword("Password: "));
        p.setProperty("email", email);
        p.setProperty("password", password);
    }
    return p;
}

From source file:org.ldaptive.cli.AuthenticatorCli.java

/**
 * Initialize an authentication request with command line options.
 *
 * @param  line  parsed command line arguments
 *
 * @return  authentication request that has been initialized
 *///  w w  w. j a v a2s .  c o  m
protected AuthenticationRequest initAuthenticationRequest(final CommandLine line) {
    final AuthenticationRequest request = new AuthenticationRequest();
    final AuthenticationRequestPropertySource arSource = new AuthenticationRequestPropertySource(request,
            getPropertiesFromOptions(PropertyDomain.AUTH.value(), line));
    arSource.initialize();
    if (request.getUser() == null) {
        // prompt for a user name
        final String user = System.console().readLine("[Enter user name]: ");
        request.setUser(user);
    }

    if (request.getCredential() == null) {
        // prompt the user to enter a password
        final char[] pass = System.console().readPassword("[Enter password for %s]: ", request.getUser());
        request.setCredential(new Credential(pass));
    }

    return request;
}

From source file:edu.vt.middleware.ldap.cli.AuthenticatorCli.java

/**
 * Initialize an authentication request with command line options.
 *
 * @param  line  parsed command line arguments
 *
 * @return  authentication request that has been initialized
 *
 * @throws  Exception  if an authentication request cannot be created
 *///w  ww.  j  a va  2 s .com
protected AuthenticationRequest initAuthenticationRequest(final CommandLine line) throws Exception {
    final AuthenticationRequest request = new AuthenticationRequest();
    final AuthenticationRequestPropertySource arSource = new AuthenticationRequestPropertySource(request,
            getPropertiesFromOptions(PropertyDomain.AUTH.value(), line));
    arSource.initialize();
    if (request.getUser() == null) {
        // prompt for a user name
        final String user = System.console().readLine("[Enter user name]: ");
        request.setUser(user);
    }

    if (request.getCredential() == null) {
        // prompt the user to enter a password
        final char[] pass = System.console().readPassword("[Enter password for %s]: ", request.getUser());
        request.setCredential(new Credential(pass));
    }

    return request;
}

From source file:com.aerospike.examples.Main.java

/**
 * Parse command line parameters./* w ww. ja v  a  2 s.c o  m*/
 */
private static Parameters parseParameters(CommandLine cl) throws Exception {
    String host = cl.getOptionValue("h", "127.0.0.1");
    String portString = cl.getOptionValue("p", "3000");
    int port = Integer.parseInt(portString);
    String namespace = cl.getOptionValue("n", "test");
    String set = cl.getOptionValue("s", "demoset");

    if (set.equals("empty")) {
        set = "";
    }

    String user = cl.getOptionValue("U");
    String password = cl.getOptionValue("P");

    if (user != null && password == null) {
        java.io.Console console = System.console();

        if (console != null) {
            char[] pass = console.readPassword("Enter password:");

            if (pass != null) {
                password = new String(pass);
            }
        }
    }
    return new Parameters(host, port, user, password, namespace, set);
}

From source file:org.ldaptive.cli.AbstractCli.java

/**
 * Initialize a connection factory with command line options.
 *
 * @param  line  parsed command line arguments
 *
 * @return  connection factory that has been initialized
 *//*from  w  ww.  ja va2  s . c  o  m*/
protected ConnectionFactory initConnectionFactory(final CommandLine line) {
    final DefaultConnectionFactory factory = new DefaultConnectionFactory();
    final DefaultConnectionFactoryPropertySource cfSource = new DefaultConnectionFactoryPropertySource(factory,
            getPropertiesFromOptions(PropertyDomain.LDAP.value(), line));
    cfSource.initialize();

    final ConnectionInitializer ci = factory.getConnectionConfig().getConnectionInitializer();
    if (ci instanceof BindConnectionInitializer) {
        final BindConnectionInitializer bci = (BindConnectionInitializer) ci;
        if (bci.getBindDn() != null && bci.getBindCredential() == null) {
            // prompt the user to enter a password
            final char[] pass = System.console().readPassword("[Enter password for %s]: ", bci.getBindDn());
            bci.setBindCredential(new Credential(pass));
        }
    }
    return factory;
}

From source file:com.google.enterprise.connector.encryptpassword.EncryptPassword.java

/**
 * Returns the password to encrypt.  If a password was not supplied on the
 * command line, prompt for it.//from   w w  w  .  j  a va  2  s  . co  m
 *
 * @param password password option supplied on command line.
 * @return String password
 */
private char[] getPassword(String password) {
    if (password != null) {
        // If password was supplied on command line, return it.
        return password.toCharArray();
    }

    // Since we will be prompting, might as well display the version.
    printVersion();

    Console console = System.console();
    if (console == null) {
        System.err.println("Error: No Console");
        return null;
    }

    while (true) {
        char[] pw1 = console.readPassword("  Type Password: ");
        if ((pw1 == null) || (pw1.length == 0)) {
            System.exit(0);
        }
        char[] pw2 = console.readPassword("Retype Password: ");
        if (Arrays.equals(pw1, pw2)) {
            System.out.println("\nThe encrypted password is:");
            Arrays.fill(pw2, '\0');
            return pw1;
        }
        System.out.println("\007\nPasswords do not match.  Please try again.\n");
    }
}

From source file:org.elasticsearch.plugins.PluginCli.java

private Command parseInstallPluginCommand(CommandLine cli) {
    String[] args = cli.getArgs();
    if (args.length != 1) {
        return exitCmd(ExitStatus.USAGE, terminal, "Must supply a single plugin id argument");
    }/* ww  w . j a va2 s.c  o  m*/

    boolean batch = System.console() == null;
    if (cli.hasOption("b")) {
        batch = true;
    }

    return new InstallPluginCommand(terminal, args[0], batch);
}