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:io.github.benas.jql.shell.Shell.java

private static Console getConsole() {
    Console console = System.console();
    if (console == null) {
        System.err.print("No console available.");
        System.exit(1);// w  w w.  j a  v a2  s  .c o m
    }
    return console;
}

From source file:com.meltmedia.cadmium.cli.UndeployCommand.java

@Override
public void execute() throws Exception {
    // TODO add param to provide selection in cli
    if (System.console() == null) {
        System.err.println("Unable to run with out a console");
        System.exit(1);//  w  ww  . j a v  a  2 s .c om
    }
    if (args == null || args.size() != 1) {
        System.err.println("Please specify the url to a deployer.");
        System.exit(1);
    }
    try {
        String site = getSecureBaseUrl(args.get(0));
        List<String> deployed = getDeployed(site, token);
        if (deployed == null || deployed.isEmpty()) {
            System.out.println("There are no cadmium wars currently deployed.");
            return;
        }
        int selectedIndex = -1;
        while (selectedIndex < 0) {
            int index = 0;
            System.out.format("%5s |  %s\n", "index", "Cadmium App");
            for (String app : deployed) {
                System.out.format("%5d | \"%s\"\n", index++, app);
            }

            System.out.println("Enter index or x to exit: ");
            String selection = null;
            try {
                selection = System.console().readLine();
            } catch (Throwable e) {
            }

            if (selection != null && "x".equalsIgnoreCase(selection)) {
                return;
            } else if (selection != null && selection.matches("\\d+")) {
                int theIdx = Integer.parseInt(selection);
                if (theIdx >= 0 && theIdx < deployed.size()) {
                    selectedIndex = theIdx;
                } else {
                    System.err.println(theIdx + " is not a valid choice.");
                }
            } else {
                System.err.println(selection + " is not a valid choice.");
            }
        }

        String undeploy = deployed.get(selectedIndex);

        System.out.println("Undeploying " + undeploy + " from " + site);

        if (!undeploy.isEmpty()) {
            undeploy(site, undeploy, token);

        } else {
            System.err.println("Invalid app name: " + undeploy);
            System.exit(1);
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:org.wso2.ppaas.tools.artifactmigration.ConversionTool.java

/**
 * Method to validate configuration inputs and redirect to console inputs
 *
 * @param propertyConstant// w  ww .  j av  a  2  s  .  c  om
 * @param propertyName
 */
private static void validateConfigurationInputs(String propertyConstant, String propertyName) {
    Console console = System.console();
    if (System.getProperty(propertyConstant) == null || System.getProperty(propertyConstant).isEmpty()) {
        System.out.print("Enter the " + propertyName);
        if (propertyName.contains("Password")) {
            char[] passwordChars = console.readPassword();
            System.setProperty(propertyConstant, new String(passwordChars));
        } else {
            System.setProperty(propertyConstant, console.readLine());
        }
        System.out.println();
    }
}

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

/**
 * Connect to iBIOMES/*from  w  ww .ja v  a  2  s .c  o  m*/
 * @param password Password
 * @return Connection
 * @throws IOException
 * @throws CompressorException 
 */

public static IRODSConnector getConnection(String username, String password, String server)
        throws IOException, JargonException, CompressorException {
    IRODSEnvironmentFile envFile = null;
    IRODSConnector cnx = null;

    Console objConsole = System.console();
    if (objConsole == null) {
        System.err.println("Console Object is not available.");
        System.exit(1);
    }

    System.out.println("Authentication... ");
    try {
        envFile = IRODSEnvironmentFile.instance();
    } catch (IOException e) {
        System.out.println("IRODS user info file not found!");
        System.out.println("Make sure your .irodsEnv file exists in $HOME/.irods or $IBIOMES_HOME");
        System.exit(1);
    }

    //while login fails
    while (cnx == null) {
        //retrieve iRODS password
        if (password == null) {
            if (username == null)
                username = envFile.getIrodsUserName();
            System.out.println("iBIOMES password for " + username + ":");
            char[] pwdArray = objConsole.readPassword();
            password = String.copyValueOf(pwdArray);
        }

        //try to connect to iRODS system using default settings
        try {
            cnx = new IRODSConnector(username, password, server);
            try {
                //try to login
                IRODSAccessObjectFactory aoFactory = cnx.getFileSystem().getIRODSAccessObjectFactory();
                aoFactory.getUserAO(cnx.getAccount());
            } catch (JargonException exc) {
                System.out.println("\nLogin failed. Try again.");
                cnx = null;
            }
        } catch (IOException ioe) {
            System.out.println("\nCould not find login info...");
            cnx = null;
        } catch (AuthenticationException ae) {
            System.out.println("\nLogin failed. Try again.");
            cnx = null;
        }
        password = null;
    }
    System.out.println("Authentication successful!");
    return cnx;
}

From source file:uk.ac.gate.cloud.cli.Main.java

private static void doConfigure() {
    File configFile = findConfigFile();
    Console console = System.console();
    if (console == null) {
        System.err.println("Could not get java.io.Console - create configuration manually.");
        configFileUsage(configFile);/*from ww w .ja v a2  s  .c  om*/
    }
    System.out.println("Configuration");
    System.out.println("-------------");
    System.out.println();
    System.out.println("This client requires an API Key to authenticate to the GATE Cloud");
    System.out.println("APIs.  You can generate one from your account settings page on");
    System.out.println("https://cloud.gate.ac.uk");
    System.out.println();
    String apiKeyId = console.readLine("API key id: ");
    char[] password = console.readPassword("API key password: ");
    System.out.println("Writing configuration to " + configFile.getAbsolutePath());
    Properties config = new Properties();
    if (configFile.canRead()) {
        try {
            FileInputStream confIn = FileUtils.openInputStream(configFile);
            try {
                config.load(confIn);
            } finally {
                IOUtils.closeQuietly(confIn);
            }
        } catch (IOException e) {
            // ignore for the moment
        }
    }
    config.setProperty("keyId", apiKeyId);
    config.setProperty("password", new String(password));
    File newConfigFile = new File(configFile.getAbsolutePath() + ".new");
    try {
        FileOutputStream out = FileUtils.openOutputStream(newConfigFile);
        try {
            config.store(out, "Generated by GATE Cloud command line tools");
        } finally {
            IOUtils.closeQuietly(out);
        }
        configFile.delete();
        FileUtils.moveFile(newConfigFile, configFile);
        System.out.println("Configuration saved successfully.");
        System.exit(0);
    } catch (IOException e) {
        System.err.println("Could not write config file - please configure manually.");
        configFileUsage(configFile);
    }
}

From source file:org.olamy.puzzle.robot.input.cli.RobotMoverInputBuilderCli.java

@Override
public RobotMoverInput getRobotMoverInput() throws RobotMoverInputException {
    try {//from w  w w . jav a 2s. c  o m
        Console console = System.console();
        PrintWriter writer = console.writer();
        writer.write("Hello in robot mover, have Fun ! " + SystemUtils.LINE_SEPARATOR);
        // so we could accept the user to configure the size of the table
        /**
         writer.write( "Initial Table (sample 5 5) : " );
         console.flush();
         String tablePos = console.readLine();
         Table table = RobotOrderUtils.buildTable( tablePos );
         */
        RobotMoverInput moverInput = new RobotMoverInput(Table.DEFAULT_TABLE);

        moverInput.setRobotOrder(getRobotOrder(console, writer, moverInput.getTable()));

        return moverInput;
    } catch (Exception e) {
        throw new RobotMoverInputException(e.getMessage(), e);
    }
}

From source file:com.thoughtworks.go.utils.encryption.Encrypter.java

private void process() throws IOException, InvalidCipherTextException {
    if (verifyCipher()) {
        System.out.println("Using cipher file at " + cipherLocation);
        System.out.println("Enter plain text password: ");
        char[] password = System.console().readPassword();
        System.out.println("Confirm plain text password: ");
        char[] confirmPassword = System.console().readPassword();
        if (validateConfirmation(password, confirmPassword)) {
            File cipherFile = new File(cipherLocation);
            String cipherText = cipher(FileUtils.readFileToByteArray(cipherFile),
                    String.valueOf(confirmPassword));
            System.out.println(String.format("Encrypted text: %s", cipherText));
        } else {//from  ww  w.ja  va2  s.  com
            System.err.println("Password and confirmation do not match. Aborting...");
        }
    } else {
        throw new RuntimeException("Could not find cipher file at " + cipherLocation);
    }
}

From source file:fr.ortolang.diffusion.client.cmd.Command.java

protected String[] getCredentials(CommandLine cmd) throws OrtolangClientException {
    String username;//  w  w  w  . ja  v  a2s .c o m
    String password = null;
    if (cmd.hasOption("U")) {
        username = cmd.getOptionValue("U");
        if (cmd.hasOption("P")) {
            password = cmd.getOptionValue("P");
        } else {
            Console cons;
            char[] passwd;
            if ((cons = System.console()) != null
                    && (passwd = cons.readPassword("[%s]", "Password:")) != null) {
                password = new String(passwd);
            }
        }
        return new String[] { username, password };
    }
    throw new OrtolangClientException("Username and/or password missing");
}

From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulator.java

public static String readFromConsole(String prompt) throws IOException {
    return System.console().readLine(prompt);
}

From source file:de.whs.poodle.cli.PoodleCliCommandLineRunner.java

@Override
public void run(String... args) throws Exception {
    Console console = System.console();
    if (console == null) {
        System.err.println("failed to get console");
        return;//from   www. j ava2 s.c  o  m
    }

    if (args.length == 0) {
        showHelp();
        return;
    }

    argLoop: for (String arg : args) {
        switch (arg) {
        case "createInstructor":
            createInstructor(console);
            break argLoop;

        case "createStudent":
            createStudent(console);
            break argLoop;

        case "resetPassword":
            resetPassword(console);
            break argLoop;

        case "--help":
        case "-h":
            showHelp();
            break argLoop;

        default:
            System.err.println("unknown argument: " + arg);
            break argLoop;
        }
    }
}