Example usage for java.io Console readline

List of usage examples for java.io Console readline

Introduction

In this page you can find the example usage for java.io Console readline.

Prototype

private char[] readline(boolean zeroOut) throws IOException 

Source Link

Usage

From source file:org.jboss.as.quickstarts.datagrid.hotrod.FootballManager.java

public static void main(String[] args) {
    Console con = System.console();
    FootballManager manager = new FootballManager(con);
    con.printf(initialPrompt);// w  w w  .  ja v a  2s . c o  m

    while (true) {
        String action = con.readLine(">");
        if ("at".equals(action)) {
            manager.addTeam();
        } else if ("ap".equals(action)) {
            manager.addPlayers();
        } else if ("rt".equals(action)) {
            manager.removeTeam();
        } else if ("rp".equals(action)) {
            manager.removePlayer();
        } else if ("p".equals(action)) {
            manager.printTeams();
        } else if ("ar".equals(action)) {
            manager.addResidential(true);
        } else if ("arm".equals(action)) {
            manager.addResidential(false);
        } else if ("pR".equals(action)) {
            manager.printResidentials();
        } else if ("pr".equals(action)) {
            manager.printResidential();
        } else if ("rr".equals(action)) {
            con.printf("status:" + manager.removeResidential());
        } else if ("rR".equals(action)) {
            con.printf("status:" + manager.removeResidentials());
        } else if ("nani".equals(action)) {
            manager.insert7nani();
        } else if ("mr".equals(action)) {
            manager.massiveResidentialInsert(true);
        } else if ("mrm".equals(action)) {
            manager.massiveResidentialInsert(false);
        } else if ("help".equals(action)) {
            con.printf(initialPrompt);
        } else if ("k".equals(action)) {
            manager.printKeySet();
        } else if ("q".equals(action)) {
            manager.stop();
            break;
        }
    }
}

From source file:org.fracturedatlas.athena.admin.AthenaAdmin.java

public static void main(String[] args) {

    Console c = System.console();
    if (c == null) {
        System.exit(1);//from  w w  w  .j  a  va2s  . co  m
    }

    Properties props = new Properties();
    ClassPathResource cpr = new ClassPathResource("admin.properties");
    try {
        InputStream in = cpr.getInputStream();
        props.load(in);
        in.close();
    } catch (Exception e) {
        c.format("Could not read properties file admin.properties\n");
        System.exit(1);
    }

    ApplicationContext context = new ClassPathXmlApplicationContext("security.xml");
    JdbcUserDetailsManager userDao = (JdbcUserDetailsManager) context.getBean("userDao");
    Md5PasswordEncoder encoder = (Md5PasswordEncoder) context.getBean("passwordEncoder");

    //TODO: Props file
    String realmName = props.getProperty("athena.admin.realm");

    if (args.length == 0) {
        System.out.println("USAGE: admin [command]");
        System.out.println("Where [command] is one of: create-user");
        System.exit(1);
    }

    Boolean usernameGood = false;
    String login = null;
    while (!usernameGood) {
        login = c.readLine("Enter new username: ");
        if (StringUtils.isBlank(login)) {
            c.format("username cannot be blank, please try again\n");
        } else {
            usernameGood = true;
        }
    }
    Boolean match = false;
    char[] password = null;
    char[] confirmedPassword = null;
    while (!match) {
        password = c.readPassword("Enter password: ");
        confirmedPassword = c.readPassword("Enter password again: ");
        match = Arrays.equals(password, confirmedPassword);
        if (!match) {
            c.format("Passwords do not match please try again\n");
        }
    }

    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new GrantedAuthorityImpl("ROLE_CLIENT_APPLICATION"));
    String clearPassword = new String(password);
    String saltedClearPassword = login + ":" + realmName + ":" + clearPassword;
    String encryptedPassword = encoder.encodePassword(saltedClearPassword, null);
    User user = new User(login, encryptedPassword, true, true, true, true, authorities);
    try {
        userDao.createUser(user);
    } catch (org.springframework.dao.DuplicateKeyException dke) {
        System.out.println("Username [" + user.getUsername() + "] already exists.");
        System.exit(1);
    }

    System.out.println("Successfully created [" + user.getUsername() + "]");
}

From source file:org.apache.atlas.utils.AuthenticationUtil.java

public static String[] getBasicAuthenticationInput() {
    String username = null;/*ww w  .j  a  va  2  s  .c  om*/
    String password = null;

    try {
        Console console = System.console();
        username = console.readLine("Enter username for atlas :- ");

        char[] pwdChar = console.readPassword("Enter password for atlas :- ");
        if (pwdChar != null) {
            password = new String(pwdChar);
        }

    } catch (Exception e) {
        System.out.print("Error while reading ");
        System.exit(1);
    }
    return new String[] { username, password };
}

From source file:com.docdoku.cli.helpers.FileHelper.java

public static boolean confirmOverwrite(String fileName) {
    Console c = System.console();
    String response = c.readLine(
            "The file '" + fileName + "' has been modified locally, do you want to overwrite it [y/N]?");
    return "y".equalsIgnoreCase(response);
}

From source file:nl.paston.bonita.importfile.Main.java

protected static String getConsoleInput(String displayText, String defaultValue, CommandLine cmd,
        String optionName) {//ww w.  ja va2s .c o  m
    if (cmd != null && cmd.hasOption(optionName)) {
        return cmd.getOptionValue(optionName);
    } else {
        Console console = System.console();
        String consoleInput = null;
        if (console != null) {
            consoleInput = console.readLine(displayText + " (" + defaultValue + "): ");

            if (!consoleInput.trim().isEmpty()) {
                return consoleInput.trim();
            } else {
                return defaultValue;
            }
        }
    }
    return defaultValue;
}

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);/*  w w  w .  j  a  v a2 s . c o m*/
    }
    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:chatbot.Chatbot.java

/** **************************************************************************************************
 * Run with a given file//from  www  .  j  av  a  2s .  c  om
 */
private static void run(String fname) throws IOException {

    List<String> documents = null;

    try {
        if (asResource)
            documents = TextFileUtil.readLines(fname, false);
        //documents = TextFileUtil.readFile(fname, false);
    } catch (IOException e) {
        System.out.println("Couldn't read document: " + fname + ". Exiting");
        return;
    }
    Chatbot cb;
    ResourceBundle resourceBundle = ResourceBundle.getBundle("corpora");
    if (asResource)
        cb = new Chatbot(documents, resourceBundle.getString("stopWordsDirectoryName"));
    else {
        cb = new Chatbot(resourceBundle.getString("stopWordsDirectoryName"));
        cb.readFile(fname);
    }

    System.out.println("Hi, I'm Cloudio, tell/ask me something. Type 'quit' to exit");

    if (isDevelopment) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("User: ");
            String input = scanner.nextLine();
            if (input.toLowerCase().trim().equals("quit"))
                break;
            System.out.print("Cloudio: ");
            System.out.println(cb.matchBestInput(input));
        }
    } else {
        while (true) {
            Console c = System.console();
            if (c == null) {
                System.err.println("No console.");
                System.exit(1);
            }
            String input = c.readLine("> ");
            if (input.toLowerCase().trim().equals("quit"))
                System.exit(1);
            System.out.println("Cloudio:" + cb.matchBestInput(input));
        }
    }
}

From source file:com.geeksaga.light.console.Main.java

private void doConsole() throws Exception {
    Console console = System.console();

    String command = console.readLine("> ");

    System.out.println(command);//  w w  w. ja  v  a 2 s  .c  o m
}

From source file:DateServer.java

private void runServer() {
    Console console = System.console();
    Handler h = new Handler(ss);
    h.start();//  www  .j a  va 2 s.c om

    while (true) {
        String cmd = console.readLine(">");
        if (cmd == null)
            continue;
        if (cmd.equals("exit"))
            System.exit(0);
    }
}

From source file:org.overlord.sramp.shell.FileShellCommandReader.java

/**
 * @see org.overlord.sramp.shell.ShellCommandReader#promptForInput(java.lang.String)
 *//*  w ww .  ja  v a2s . c  o  m*/
@Override
public String promptForInput(String prompt) {
    Console console = System.console();
    if (console != null) {
        return console.readLine(prompt);
    } else {
        throw new RuntimeException(Messages.i18n.format("FileShellCommandReader.NoConsole")); //$NON-NLS-1$
    }
}