
org.nuiton.util.ApplicationConfigHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nuiton-utils Show documentation
Show all versions of nuiton-utils Show documentation
Library of usefull class to be used in any project.
/*
* #%L
* Nuiton Utils :: Nuiton Utils
*
* $Id: ApplicationConfigHelper.java 2513 2013-02-26 07:22:43Z tchemit $
* $HeadURL: http://svn.nuiton.org/svn/nuiton-utils/tags/nuiton-utils-2.6.10/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfigHelper.java $
* %%
* Copyright (C) 2004 - 2012 CodeLutin
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;
/**
* Helper about {@link ApplicationConfig}.
*
* @author tchemit
* @since 2.4.8
* @deprecated since 2.6.10 (replaced by org.nuiton.util.config.ApplicationConfigConfigHelper
* in nuiton-config module), will be removed in version 2.7.1.
*/
@Deprecated
public class ApplicationConfigHelper {
/** Logger. */
private static final Log log =
LogFactory.getLog(ApplicationConfigHelper.class);
protected ApplicationConfigHelper() {
// helper with no instance
}
/**
* Obtain all providers on class-path.
*
* @param classLoader optional classLoader used to seek for providers
* @param includes optional includes providers to use (if none then accept all providers)
* @param excludes optional excludes providers (if none the no reject)
* @param verbose verbose flag
* @return sets of providers
*/
public static Set getProviders(ClassLoader classLoader,
Set includes,
Set excludes,
boolean verbose) {
ServiceLoader loader;
if (classLoader == null) {
loader = ServiceLoader.load(ApplicationConfigProvider.class);
} else {
loader = ServiceLoader.load(ApplicationConfigProvider.class,
classLoader);
}
Set result =
new HashSet();
for (ApplicationConfigProvider configProvider : loader) {
String name = configProvider.getName();
if (includes != null && !includes.contains(name)) {
// reject by include
if (verbose) {
log.info("configuration named '" + name +
"' is rejected by includes.");
}
continue;
}
if (excludes != null && excludes.contains(name)) {
// reject by exclude
if (verbose) {
log.info("configuration named '" + name +
"' is rejected by excludes.");
}
continue;
}
if (verbose) {
log.info("configuration named '" + name +
"' will be generated.");
}
result.add(configProvider);
}
return result;
}
public static ApplicationConfigProvider getProvider(ClassLoader classLoader,
String name) {
Set providers = getProviders(
classLoader, null, null, false);
ApplicationConfigProvider result = null;
for (ApplicationConfigProvider provider : providers) {
if (name.equals(provider.getName())) {
result = provider;
break;
}
}
return result;
}
/**
* Load default options from all given config providers.
*
* @param config config where to add default options.
* @param providers providers to use
* @since 2.6.7
*/
public static void loadAllDefaultOption(ApplicationConfig config,
Set providers) {
for (ApplicationConfigProvider provider : providers) {
if (log.isInfoEnabled()) {
log.info("Load default options from configuration: " +
provider.getName());
}
if (log.isInfoEnabled()) {
for (ApplicationConfig.OptionDef optionDef : provider.getOptions()) {
log.info(" " + optionDef.getKey() +
" (" + optionDef.getDefaultValue() + ')');
}
}
config.loadDefaultOptions(provider.getOptions());
}
}
/**
* Gets all transient options from the given providers.
*
* @param providers providers to inspect
* @return the set of all options that are transient
* @see ApplicationConfig.OptionDef#isTransient()
* @since 2.6.7
*/
public static Set getTransientOptions(Set providers) {
Set result = new HashSet();
for (ApplicationConfigProvider provider : providers) {
for (ApplicationConfig.OptionDef def : provider.getOptions()) {
if (def.isTransient()) {
result.add(def);
}
}
}
return result;
}
/**
* Gets all final options from the given providers.
*
* @param providers providers to inspect
* @return the set of all options that are final
* @see ApplicationConfig.OptionDef#isFinal()
* @since 2.6.7
*/
public static Set getFinalOptions(Set providers) {
Set result = new HashSet();
for (ApplicationConfigProvider provider : providers) {
for (ApplicationConfig.OptionDef def : provider.getOptions()) {
if (def.isFinal()) {
result.add(def);
}
}
}
return result;
}
/**
* Get all option keys that should not be saved in the user config file
* from the given options providers.
*
* Such options are {@code transient} or {@code final}.
*
* @param providers providers to inspect
* @return the set of options key not to store in the config file
* @see ApplicationConfig.OptionDef#isFinal()
* @see ApplicationConfig.OptionDef#isTransient()
* @since 2.6.7
*/
public static Set getTransientOrFinalOptionKey(Set providers) {
Set result = new HashSet();
for (ApplicationConfig.OptionDef def : getTransientOptions(providers)) {
result.add(def.getKey());
}
for (ApplicationConfig.OptionDef def : getFinalOptions(providers)) {
result.add(def.getKey());
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy