All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fiftyone.mobile.detection.LicenceKey Maven / Gradle / Ivy

The newest version!
/* *********************************************************************
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.
 * 
 * If a copy of the MPL was not distributed with this file, You can obtain
 * one at http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0.
 * ********************************************************************* */
package fiftyone.mobile.detection;

import java.util.regex.Matcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * Class to hold and validate a 51Degrees.mobi Licence key.
 *
 * @author 51Degrees.mobi
 * @version 2.2.9.1
 */
public class LicenceKey {

    /**
     * String to hold the licence key.
     */
    private static String key = null;
    
    /**
     * Creates a logger for this class
     */
    private static final Logger _logger = LoggerFactory.getLogger(LicenceKey.class);

    /**
     * Assigns the given license key to the License key object if it is a valid
     * key.
     *
     * @param licenceKey The license key to add to the collection.
     */
    public static void setKey(final String licenceKey) {
        if (licenceKey != null) {
            if (validate(licenceKey)) {
                key = licenceKey;
            } else {
                _logger.info(
                        String.format(
                        "Licence key '%s' was invalid.",
                        licenceKey));               
            }
        }
    }

    /**
     * Returns the license key held by this object, or null if no valid key has
     * been set.
     *
     * @return The license key as a string.
     */
    public static String getKey() {
        return key;
    }

    /**
     *
     * Method to validate a license key before it is added to the Object.
     *
     * @param licenceKey the License key to validate.
     * @return true if valid, else false.
     */
    private static boolean validate(final String licenceKey) {
        if (licenceKey != null) {
            final Matcher m = Constants.LICENSE_KEY_VALIDATION_REGEX.matcher(licenceKey);
            return m.matches();
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy