mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
15.35.2016 db1a3826ba28aa6231756ebd224f16677440ed2e
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
@@ -625,4 +625,37 @@
        Arrays.fill(str, charToRepeat);
        return new String(str);
    }
    /**
     * Return a {@link ValidationCallback<Integer>} which can be used to validate a port number.
     *
     * @param defaultPort
     *        The default value to suggest to the user.
     * @return a {@link ValidationCallback<Integer>} which can be used to validate a port number.
     */
    public static ValidationCallback<Integer> portValidationCallback(final int defaultPort) {
        return new ValidationCallback<Integer>() {
            @Override
            public Integer validate(ConsoleApplication app, String rawInput) throws ClientException {
                final String input = rawInput.trim();
                if (input.length() == 0) {
                    return defaultPort;
                }
                try {
                    int i = Integer.parseInt(input);
                    if (i < 1 || i > 65535) {
                        throw new NumberFormatException();
                    }
                    return i;
                } catch (NumberFormatException e) {
                    // Try again...
                    app.println();
                    app.println(ERR_BAD_PORT_NUMBER.get(input));
                    app.println();
                    return null;
                }
            }
        };
    }
}