io.unravelling.ferguson.configuration.FergusonConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for io.unravelling.ferguson.configuration.FergusonConfiguration.java

Source

/*
 * Copyright 2017 Unravelling Technologies
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Contributors:
 * Tiago Veiga Lazaro
 */

package io.unravelling.ferguson.configuration;

import io.unravelling.ferguson.model.ServerType;

import java.util.Properties;

import org.apache.commons.lang3.EnumUtils;

/**
 * author Tiago Veiga Lazaro
 * version 1.0.0
 * since 1.0.0
 */
public final class FergusonConfiguration {

    /**
     * Type of the server (currently supported: EMS). See enum ServerType for more information.
     */
    public static final String SERVER_TYPE = "type";

    /**
     * Url to the server to be managed.
     */
    public static final String SERVER_URL = "url";

    /**
     * Username with administrative rights.
     */
    public static final String SERVER_USERNAME = "username";

    /**
     * Password for the supplied user. Ignores the warning of hardcoding password, as this is
     * the name of the property holding the password, rather than the password itself.
     */
    @SuppressWarnings({ "squid:S2068" })
    public static final String SERVER_PASSWORD = "password";

    /**
     * Private constructor for the class to disable instantiation.
     */
    private FergusonConfiguration() {
    }

    /**
     * Validates the received configuration for the Ferguson library.
     * @param configuration Configuration on the form of properties.
     * @return True if configuration is valid.
     */
    public static boolean validateConfiguration(final Properties configuration) {

        return configuration != null && configuration.size() > 0 && validateServerType(configuration)
                && validateServerUrl(configuration) && validateServerUsername(configuration)
                && validateServerPassword(configuration);
    }

    /**
     * Validates the server type that was supplied.
     * @param configuration Configuration of the server.
     * @return Boolean indicating if the server configuration is correct or not.
     */
    static boolean validateServerType(final Properties configuration) {

        return (configuration.containsKey(FergusonConfiguration.SERVER_TYPE) && EnumUtils
                .isValidEnum(ServerType.class, configuration.getProperty(FergusonConfiguration.SERVER_TYPE)));
    }

    /**
     * Validates the server URL that was supplied.
     * @param configuration Configuration of the server.
     * @return Boolean indicating if the server configuration is correct or not.
     */
    static boolean validateServerUrl(final Properties configuration) {

        return (configuration.containsKey(FergusonConfiguration.SERVER_URL)
                && !configuration.getProperty(FergusonConfiguration.SERVER_URL).isEmpty());
    }

    /**
     * Validates the server username that was supplied.
     * @param configuration Configuration of the server.
     * @return Boolean indicating if the server configuration is correct or not.
     */
    static boolean validateServerUsername(final Properties configuration) {

        return (configuration.containsKey(FergusonConfiguration.SERVER_USERNAME)
                && !configuration.getProperty(FergusonConfiguration.SERVER_USERNAME).isEmpty());
    }

    /**
     * Validates the server password that was supplied.
     * @param configuration Configuration of the server.
     * @return Boolean indicating if the server configuration is correct or not.
     */
    static boolean validateServerPassword(final Properties configuration) {

        return configuration.containsKey(FergusonConfiguration.SERVER_PASSWORD);
    }
}