Java Code Examples for javax.smartcardio.TerminalFactory#terminals()

The following examples show how to use javax.smartcardio.TerminalFactory#terminals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From openjavacard-ndef with GNU General Public License v3.0 5 votes vote down vote up
public static final void main(String[] arguments) {
    PrintStream os = System.out;
    TerminalFactory tf = TerminalFactory.getDefault();
    CardTerminals terminals = tf.terminals();

    try {
        // default command is info
        String command = "info";

        // check if user specified a reader as first argument
        if(arguments.length >= 1) {
            // if yes then use that reader
            String terminalName = arguments[0];
            CardTerminal terminal = terminals.getTerminal(terminalName);
            if(terminal == null) {
                throw new RuntimeException("Could not find terminal \"" + terminalName + "\"");
            }
            // check if the user also specified a command
            if(arguments.length >= 2) {
                command = arguments[1];
            }
            // split off the command arguments
            String[] commandArguments = new String[0];
            if(arguments.length > 2) {
                commandArguments = Arrays.copyOfRange(arguments, 2, arguments.length);
            }
            // run the command
            runCommand(terminal, command, commandArguments);
        } else {
            // else list all readers
            os.println("Available terminals:");
            for (CardTerminal t : terminals.list()) {
                os.println("  \"" + t.getName() + "\"");
            }
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
}