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

Gaetan Boismal
25.53.2016 db3ffddbfab6dc0d5219247d432ef36c1a3a2055
Code cleanup

Add useful methods to handle arguments conflicts.
2 files modified
133 ■■■■■ changed files
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConnectionFactoryProvider.java 54 ●●●● patch | view | raw | blame | history
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java 79 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/ConnectionFactoryProvider.java
@@ -20,6 +20,7 @@
import static com.forgerock.opendj.cli.CliConstants.DEFAULT_LDAP_PORT;
import static com.forgerock.opendj.cli.CliMessages.*;
import static com.forgerock.opendj.cli.Utils.getHostNameForLdapUrl;
import static com.forgerock.opendj.cli.Utils.throwIfArgumentsConflict;
import static org.forgerock.opendj.ldap.LDAPConnectionFactory.AUTHN_BIND_REQUEST;
import static org.forgerock.opendj.ldap.LDAPConnectionFactory.CONNECT_TIMEOUT;
import static org.forgerock.opendj.ldap.LDAPConnectionFactory.SSL_CONTEXT;
@@ -447,45 +448,12 @@
     *             an argument exception is thrown.
     */
    private void checkForConflictingArguments() throws ArgumentException {
        // Couldn't have at the same time bindPassword and bindPasswordFile
        if (bindPasswordArg.isPresent() && bindPasswordFileArg.isPresent()) {
            final LocalizableMessage message =
                    ERR_TOOL_CONFLICTING_ARGS.get(bindPasswordArg.getLongIdentifier(),
                            bindPasswordFileArg.getLongIdentifier());
            throw new ArgumentException(message);
        }
        /*
         * Couldn't have at the same time trustAll and trustStore related arg
         */
        if (trustAllArg.isPresent() && trustStorePathArg.isPresent()) {
            final LocalizableMessage message =
                    ERR_TOOL_CONFLICTING_ARGS.get(trustAllArg.getLongIdentifier(),
                            trustStorePathArg.getLongIdentifier());
            throw new ArgumentException(message);
        }
        if (trustAllArg.isPresent() && trustStorePasswordArg.isPresent()) {
            final LocalizableMessage message =
                    ERR_TOOL_CONFLICTING_ARGS.get(trustAllArg.getLongIdentifier(),
                            trustStorePasswordArg.getLongIdentifier());
            throw new ArgumentException(message);
        }
        if (trustAllArg.isPresent() && trustStorePasswordFileArg.isPresent()) {
            final LocalizableMessage message =
                    ERR_TOOL_CONFLICTING_ARGS.get(trustAllArg.getLongIdentifier(),
                            trustStorePasswordFileArg.getLongIdentifier());
            throw new ArgumentException(message);
        }
        /*
         * Couldn't have at the same time trustStorePasswordArg and trustStorePasswordFileArg
         */
        if (trustStorePasswordArg.isPresent() && trustStorePasswordFileArg.isPresent()) {
            final LocalizableMessage message =
                    ERR_TOOL_CONFLICTING_ARGS.get(trustStorePasswordArg.getLongIdentifier(),
                            trustStorePasswordFileArg.getLongIdentifier());
            throw new ArgumentException(message);
        }
        throwIfArgumentsConflict(bindPasswordArg, bindPasswordFileArg);
        throwIfArgumentsConflict(trustAllArg, trustStorePathArg);
        throwIfArgumentsConflict(trustAllArg, trustStorePasswordArg);
        throwIfArgumentsConflict(trustAllArg, trustStorePasswordFileArg);
        throwIfArgumentsConflict(trustStorePasswordArg, trustStorePasswordFileArg);
        throwIfArgumentsConflict(useStartTLSArg, useSSLArg);
        if (trustStorePathArg.isPresent()) {
            // Check that the path exists and is readable
@@ -504,14 +472,6 @@
                throw new ArgumentException(message);
            }
        }
        // Couldn't have at the same time startTLSArg and useSSLArg
        if (useStartTLSArg.isPresent() && useSSLArg.isPresent()) {
            final LocalizableMessage message =
                    ERR_TOOL_CONFLICTING_ARGS.get(useStartTLSArg.getLongIdentifier(), useSSLArg
                            .getLongIdentifier());
            throw new ArgumentException(message);
        }
    }
    /**
opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
@@ -31,6 +31,7 @@
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.TimeZone;
@@ -658,4 +659,82 @@
            }
        };
    }
    /**
     * Throws an {@link ArgumentException} if both provided {@link Argument} are presents in the command line arguments.
     *
     * @param arg1
     *         The first {@link Argument} which should not be present if {@literal arg2} is.
     * @param arg2
     *         The second {@link Argument} which should not be present if {@literal arg1} is.
     * @throws ArgumentException
     *         If both provided {@link Argument} are presents in the command line arguments
     */
    public static void throwIfArgumentsConflict(final Argument arg1, final Argument arg2) throws ArgumentException {
        if (argsConflicts(arg1, arg2)) {
            throw new ArgumentException(conflictingArgsErrorMessage(arg1, arg2));
        }
    }
    /**
     * Adds a {@link LocalizableMessage} to the provided {@link Collection<LocalizableMessage>}
     * if both provided {@link Argument} are presents in the command line arguments.
     *
     * @param errors
     *         The {@link Collection<LocalizableMessage>} to use to add the conflict error (if occurs).
     * @param arg1
     *         The first {@link Argument} which should not be present if {@literal arg2} is.
     * @param arg2
     *         The second {@link Argument} which should not be present if {@literal arg1} is.
     */
    public static void addErrorMessageIfArgumentsConflict(
            final Collection<LocalizableMessage> errors, final Argument arg1, final Argument arg2) {
        if (argsConflicts(arg1, arg2)) {
            errors.add(conflictingArgsErrorMessage(arg1, arg2));
        }
    }
    /**
     * Return {@code true} if provided {@link Argument} are presents in the command line arguments.
     * <p>
     * If so, adds a {@link LocalizableMessage} to the provided {@link LocalizableMessageBuilder}.
     *
     * @param builder
     *         The {@link LocalizableMessageBuilder} to use to write the conflict error (if occurs).
     * @param arg1
     *         The first {@link Argument} which should not be present if {@literal arg2} is.
     * @param arg2
     *         The second {@link Argument} which should not be present if {@literal arg1} is.
     * @return {@code true} if provided {@link Argument} are presents in the command line arguments.
     */
    public static boolean appendErrorMessageIfArgumentsConflict(
        final LocalizableMessageBuilder builder, final Argument arg1, final Argument arg2) {
        if (argsConflicts(arg1, arg2)) {
            if (builder.length() > 0) {
                builder.append(LINE_SEPARATOR);
            }
            builder.append(conflictingArgsErrorMessage(arg1, arg2));
            return true;
        }
        return false;
    }
    private static boolean argsConflicts(final Argument arg1, final Argument arg2) {
        return arg1.isPresent() && arg2.isPresent();
    }
    /**
     * Returns a {@link LocalizableMessage} which explains to the user
     * that provided {@link Argument}s can not be used together on the command line.
     *
     * @param arg1
     *         The first {@link Argument} which conflicts with {@literal arg2}.
     * @param arg2
     *         The second {@link Argument} which conflicts with {@literal arg1}.
     * @return A {@link LocalizableMessage} which explains to the user that arguments
     *         can not be used together on the command line.
     */
    public static LocalizableMessage conflictingArgsErrorMessage(final Argument arg1, final Argument arg2) {
        return ERR_TOOL_CONFLICTING_ARGS.get(arg1.getLongIdentifier(), arg2.getLongIdentifier());
    }
}