本文整理了Java中com.typesafe.config.Config.checkValid()
方法的一些代码示例,展示了Config.checkValid()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Config.checkValid()
方法的具体详情如下:
包路径:com.typesafe.config.Config
类名称:Config
方法名:checkValid
[英]Validates this config against a reference config, throwing an exception if it is invalid. The purpose of this method is to "fail early" with a comprehensive list of problems; in general, anything this method can find would be detected later when trying to use the config, but it's often more user-friendly to fail right away when loading the config.
Using this method is always optional, since you can "fail late" instead.
You must restrict validation to paths you "own" (those whose meaning are defined by your code module). If you validate globally, you may trigger errors about paths that happen to be in the config but have nothing to do with your module. It's best to allow the modules owning those paths to validate them. Also, if every module validates only its own stuff, there isn't as much redundant work being done.
If no paths are specified in checkValid()
's parameter list, validation is for the entire config.
If you specify paths that are not in the reference config, those paths are ignored. (There's nothing to validate.)
Here's what validation involves:
If you want to allow a certain setting to have a flexible type (or otherwise want validation to be looser for some settings), you could either remove the problematic setting from the reference config provided to this method, or you could intercept the validation exception and screen out certain problems. Of course, this will only work if all other callers of this method are careful to restrict validation to their own paths, as they should be.
If validation fails, the thrown exception contains a list of all problems found. See ConfigException.ValidationFailed#problems. The exception's getMessage()
will have all the problems concatenated into one huge string, as well.
Again, checkValid()
can't guess every domain-specific way a setting can be invalid, so some problems may arise later when attempting to use the config. checkValid()
is limited to reporting generic, but common, problems such as missing settings and blatant type incompatibilities.
[中]根据引用配置验证此配置,如果无效则引发异常。这种方法的目的是“提前失败”,列出一系列问题;通常,此方法可以找到的任何内容都将在稍后尝试使用配置时被检测到,但在加载配置时立即失败通常更方便用户。
使用此方法始终是可选的,因为您可以改为“延迟失败”。
必须将验证限制为“拥有”的路径(其含义由代码模块定义的路径)。如果全局验证,则可能会触发有关路径的错误,这些路径恰好位于配置中,但与模块无关。最好允许拥有这些路径的模块验证它们。此外,如果每个模块只验证自己的东西,那么就不会有太多的冗余工作要做。
如果checkValid()
的参数列表中未指定任何路径,则对整个配置进行验证。
如果指定的路径不在引用配置中,则将忽略这些路径。(没有什么可验证的。)
以下是验证所涉及的内容:
*在此配置中必须存在引用配置中找到的所有路径,否则将引发异常。
*从引用配置到此配置的某些类型更改将导致引发异常。并不是所有潜在的类型问题都被检测到,特别是假设字符串与除对象和列表之外的所有对象都兼容。这是因为字符串类型通常是“真正的”其他类型(系统属性总是以字符串开头,或者像“5ms”这样的字符串可以与#getmillizes一起使用)。此外,还允许将任何类型设置为null或使用任何类型覆盖null。
*此配置中任何未解决的替换都将导致验证失败;在验证之前,应解析引用配置和此配置。如果引用配置未解析,则此方法的调用程序中存在错误。
如果希望允许某个设置具有灵活的类型(或者希望某些设置的验证更宽松),可以从提供给此方法的引用配置中删除有问题的设置,或者截获验证异常并筛选出某些问题。当然,只有当此方法的所有其他调用方都小心地将验证限制到自己的路径时,这才有效,因为它们应该这样做。
如果验证失败,抛出的异常将包含发现的所有问题的列表。请参阅ConfigException。验证失败#问题。异常的getMessage()
也会将所有问题连接到一个大字符串中。
同样,checkValid()
无法猜测设置无效的每种特定于域的方式,因此稍后尝试使用配置时可能会出现一些问题。checkValid()
仅限于报告通用但常见的问题,如缺少设置和明显的类型不兼容。
代码示例来源:origin: apache/drill
@Override
public void checkValid(Config reference, String... restrictToPaths) {
c.checkValid(reference, restrictToPaths);
}
代码示例来源:origin: dremio/dremio-oss
@Override
public void checkValid(Config reference, String... restrictToPaths) {
config.checkValid(reference, restrictToPaths);
}
代码示例来源:origin: org.apache.drill/drill-common
@Override
public void checkValid(Config reference, String... restrictToPaths) {
c.checkValid(reference, restrictToPaths);
}
代码示例来源:origin: net.nikore.gozer/marathon
@Inject
public DefaultMarathonConfig(Config config) {
this.config = config;
config.checkValid(ConfigFactory.defaultReference(), "gozer-marathon");
}
代码示例来源:origin: com.aliyun.openservices/iot-as-bridge-sdk-core
public BridgeConfigManagerImpl(Config load) {
this.config = load;
config.checkValid(ConfigFactory.defaultReference(), new String[] {});
}
代码示例来源:origin: Karumi/Reddo
public TypesafeHubReddoConfig() {
File configFile = getConfigFile();
this.config = ConfigFactory.
parseFile(configFile).withFallback(ConfigFactory.load(DEFAULT_CONFIG_PATH));
config.checkValid(config, CONFIG_NAME);
}
代码示例来源:origin: dremio/dremio-oss
private void check(){
final Config inner = getInnerConfig();
final Config ref = reference.resolve();
// make sure types are right
inner.checkValid(ref);
// make sure we don't have any extra paths. these are typically typos.
List<String> invalidPaths = new ArrayList<>();
for(Entry<String, ConfigValue> entry : inner.entrySet()){
if(!ref.hasPath(entry.getKey())){
invalidPaths.add(entry.getKey());
}
}
if(!invalidPaths.isEmpty()){
StringBuilder sb = new StringBuilder();
sb.append("Failure reading configuration file. The following properties were invalid:\n");
for(String s : invalidPaths){
sb.append("\t");
sb.append(s);
sb.append("\n");
}
throw new RuntimeException(sb.toString());
}
}
代码示例来源:origin: kite-sdk/kite
/** Loads the given config file from the local file system */
public Config parse(File file, Config... overrides) throws IOException {
if (file == null || file.getPath().trim().length() == 0) {
throw new MorphlineCompilationException("Missing morphlineFile parameter", null);
}
if (!file.exists()) {
throw new FileNotFoundException("File not found: " + file);
}
if (!file.canRead()) {
throw new IOException("Insufficient permissions to read file: " + file);
}
Config config = ConfigFactory.parseFile(file);
for (Config override : overrides) {
config = override.withFallback(config);
}
synchronized (LOCK) {
ConfigFactory.invalidateCaches();
config = ConfigFactory.load(config);
config.checkValid(ConfigFactory.defaultReference()); // eagerly validate aspects of tree config
}
return config;
}
代码示例来源:origin: org.kitesdk/kite-morphlines-core
/** Loads the given config file from the local file system */
public Config parse(File file, Config... overrides) throws IOException {
if (file == null || file.getPath().trim().length() == 0) {
throw new MorphlineCompilationException("Missing morphlineFile parameter", null);
}
if (!file.exists()) {
throw new FileNotFoundException("File not found: " + file);
}
if (!file.canRead()) {
throw new IOException("Insufficient permissions to read file: " + file);
}
Config config = ConfigFactory.parseFile(file);
for (Config override : overrides) {
config = override.withFallback(config);
}
synchronized (LOCK) {
ConfigFactory.invalidateCaches();
config = ConfigFactory.load(config);
config.checkValid(ConfigFactory.defaultReference()); // eagerly validate aspects of tree config
}
return config;
}
代码示例来源:origin: es.upv.grycap.coreutils/coreutils-common
/**
* Loads and merges application configuration with default properties.
* @param confname - optional configuration filename
* @param rootPath - only load configuration properties underneath this path that this code
* module owns and understands
* @return Configuration loaded from the provided filename or from default properties.
*/
public Config loadConfig(final @Nullable String confname, final String rootPath) {
// load configuration properties
Config config;
final String confname2 = trimToNull(confname);
if (confname2 != null) {
final ConfigParseOptions options = ConfigParseOptions.defaults().setAllowMissing(false);
final Config customConfig = ConfigFactory.parseFileAnySyntax(new File(confname2), options);
final Config regularConfig = ConfigFactory.load();
final Config combined = customConfig.withFallback(regularConfig);
config = ConfigFactory.load(combined);
} else {
config = ConfigFactory.load();
}
// validate
config.checkValid(ConfigFactory.defaultReference(), rootPath);
return config;
}
代码示例来源:origin: com.github.mwegrz/logback-hocon
void configure(LoggerContext context, Config config) {
config.checkValid(ConfigFactory.defaultReference(), "logback");
代码示例来源:origin: joliciel-informatique/talismane
config.checkValid(ConfigFactory.defaultReference().getConfig("talismane.machine-learning"));
MachineLearningAlgorithm algorithm = MachineLearningAlgorithm.valueOf(config.getString("algorithm"));
ClassificationModelTrainer modelTrainer = null;
代码示例来源:origin: joliciel-informatique/talismane
this.config = config;
config.checkValid(ConfigFactory.defaultReference(), "talismane.core." + sessionId);
内容来源于网络,如有侵权,请联系作者删除!