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

org.nuiton.validator.NuitonValidatorProviders Maven / Gradle / Ivy

The newest version!
package org.nuiton.validator;

/*-
 * #%L
 * Validation :: API
 * %%
 * Copyright (C) 2021 - 2024 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.ServiceLoader;
import java.util.TreeMap;

/**
 * To get a {@link NuitonValidatorProvider}.
 *
 * 

* Created at 05/02/2024. * * @author Tony Chemit - [email protected] * @since 2.0.0 */ public class NuitonValidatorProviders { private static final Logger log = LogManager.getLogger(NuitonValidatorProviders.class); protected static String defaultProviderName; protected static Map providers; protected NuitonValidatorProviders() { // avoid instantiation of this factory } public static Map getProviders() { if (providers == null) { providers = new TreeMap<>(); ServiceLoader serviceLoader = ServiceLoader.load(NuitonValidatorProvider.class); for (NuitonValidatorProvider provider : serviceLoader) { if (log.isInfoEnabled()) { log.info("obtain validator provider " + provider.getName()); } providers.put(provider.getName(), provider); } } return providers; } public static NuitonValidatorProvider getProvider(String providerName) { if (providerName == null) { // take the default validator provider name throw new NullPointerException("providerName parameter can not be null."); } NuitonValidatorProvider provider = getProviders().get(providerName); if (provider == null) { throw new IllegalArgumentException(String.format("Could not find provider named '%s', existing providers are : %s", providerName, getProviders().keySet())); } return provider; } public static NuitonValidatorProvider getDefaultProvider() { String providerName = getDefaultProviderName(); return getProvider(providerName); } public static String getDefaultProviderName() { if (defaultProviderName == null) { // takes the first provider from existing provider Map providers = getProviders(); if (providers.isEmpty()) { throw new IllegalStateException("Could not find any provider of validator."); } defaultProviderName = "xwork2"/*FIXME Remove thisproviders.keySet().iterator().next()*/; if (log.isInfoEnabled()) { log.info("Set the default provider name to " + defaultProviderName); } } return defaultProviderName; } public static void setDefaultProviderName(String defaultProviderName) { if (defaultProviderName == null) { throw new NullPointerException("defaultProviderName can not be null."); } // check provider exists getProvider(defaultProviderName); NuitonValidatorProviders.defaultProviderName = defaultProviderName; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy