com.typesafe.config.Config.resolve()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(340)

本文整理了Java中com.typesafe.config.Config.resolve()方法的一些代码示例,展示了Config.resolve()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Config.resolve()方法的具体详情如下:
包路径:com.typesafe.config.Config
类名称:Config
方法名:resolve

Config.resolve介绍

[英]Returns a replacement config with all substitutions (the ${foo.bar} syntax, see the spec) resolved. Substitutions are looked up using this Config as the root object, that is, a substitution ${foo.bar} will be replaced with the result of getValue("foo.bar").

This method uses ConfigResolveOptions#defaults(), there is another variant Config#resolve(ConfigResolveOptions) which lets you specify non-default options.

A given Config must be resolved before using it to retrieve config values, but ideally should be resolved one time for your entire stack of fallbacks (see Config#withFallback). Otherwise, some substitutions that could have resolved with all fallbacks available may not resolve, which will be potentially confusing for your application's users.

resolve() should be invoked on root config objects, rather than on a subtree (a subtree is the result of something like config.getConfig("foo")). The problem with resolve() on a subtree is that substitutions are relative to the root of the config and the subtree will have no way to get values from the root. For example, if you did config.getConfig("foo").resolve() on the below config file, it would not work:

common-value = 10 
foo { 
whatever = ${common-value} 
}

Many methods on ConfigFactory such as ConfigFactory#load() automatically resolve the loaded Config on the loaded stack of config files.

Resolving an already-resolved config is a harmless no-op, but again, it is best to resolve an entire stack of fallbacks (such as all your config files combined) rather than resolving each one individually.
[中]返回一个替换配置,并解析所有替换(${foo.bar}语法,请参见{the spec)。使用此Config作为根对象查找替换,也就是说,替换${foo.bar}将替换为getValue("foo.bar")的结果。
此方法使用ConfigResolveOptions#defaults(),还有另一个变量Config#ResolveOptions(ConfigResolveOptions),可用于指定非默认选项。
在使用给定配置检索配置值之前,必须对其进行解析,但理想情况下,应该对整个回退堆栈进行一次解析(请参见Config#withFallback)。否则,一些本来可以在所有可用回退情况下解决的替换可能无法解决,这可能会让应用程序的用户感到困惑。
resolve()应该在根配置对象上调用,而不是在子树上调用(子树是类似config.getConfig("foo")的结果)。子树上resolve()的问题是,替换是相对于配置的根的,子树将无法从根获取值。例如,如果您对下面的配置文件执行了config.getConfig("foo").resolve(),它将无法工作:

common-value = 10 
foo { 
whatever = ${common-value} 
}

ConfigFactory上的许多方法,如ConfigFactory#load()会自动解析已加载配置文件堆栈上已加载的Config
解析一个已经解析的配置是一个无害的禁止操作,但同样,最好是解析一个完整的回退堆栈(例如所有配置文件的组合),而不是单独解析每一个。

代码示例

代码示例来源:origin: kairosdb/kairosdb

public void resolve()
{
  m_config = m_config.resolve();
}

代码示例来源:origin: apache/drill

@Override
public Config resolve() {
 return c.resolve();
}

代码示例来源:origin: apache/drill

@Override
public Config resolve(ConfigResolveOptions options) {
 return c.resolve(options);
}

代码示例来源:origin: OryxProject/oryx

/**
 * @param serialized serialized form of configuration as JSON-like data
 * @return {@link Config} from the serialized config
 */
public static Config deserialize(String serialized) {
 return ConfigFactory.parseString(serialized).resolve().withFallback(DEFAULT_CONFIG);
}

代码示例来源:origin: apache/drill

/**
 * Creates a drill configuration using the provided config file.
 * @param config custom configuration file
 * @return {@link DrillConfig} instance
 */
public static DrillConfig create(Config config) {
 return new DrillConfig(config.resolve());
}

代码示例来源:origin: OryxProject/oryx

/**
 * @param overlay map of key-value pairs to add to default config. The map is converted
 *  to a string representation, as it were from a config file, and parsed accordingly.
 * @param underlying underlying config to overlay new settings on top of
 * @return default config but with key-value pairs added
 */
public static Config overlayOn(Map<String,?> overlay, Config underlying) {
 StringBuilder configFileString = new StringBuilder();
 overlay.forEach((k, v) -> configFileString.append(k).append('=').append(v).append('\n'));
 String configFile = configFileString.toString();
 log.debug("Overlaid config: \n{}", configFile);
 return ConfigFactory.parseString(configFile).resolve().withFallback(underlying);
}

代码示例来源:origin: apache/incubator-gobblin

private Config loadHoconFileAtPath(Path filePath, boolean allowUnresolved)
   throws IOException {
  ConfigResolveOptions options = ConfigResolveOptions.defaults().setAllowUnresolved(allowUnresolved);
  try (InputStream is = fs.open(filePath)) {
   return ConfigFactory.parseReader(new InputStreamReader(is, Charsets.UTF_8)).resolve(options);
  }
 }
}

代码示例来源:origin: apache/incubator-gobblin

public HOCONInputStreamFlowTemplate(InputStream inputStream, URI flowTemplateDirUri, FlowCatalogWithTemplates catalog)
  throws SpecNotFoundException, IOException, JobTemplate.TemplateException, URISyntaxException {
 this(ConfigFactory.parseReader(new InputStreamReader(inputStream, Charsets.UTF_8)).resolve(
   ConfigResolveOptions.defaults().setAllowUnresolved(true)), flowTemplateDirUri, catalog);
}

代码示例来源:origin: apache/incubator-gobblin

private static Config resolveConfig(JobSpec jobSpec, JobCatalog catalog)
  throws SpecNotFoundException, JobTemplate.TemplateException {
 Optional<URI> templateURIOpt = jobSpec.getTemplateURI();
 if (templateURIOpt.isPresent()) {
  JobCatalogWithTemplates catalogWithTemplates = new PackagedTemplatesJobCatalogDecorator(catalog);
  JobTemplate template = catalogWithTemplates.getTemplate(templateURIOpt.get());
  return template.getResolvedConfig(jobSpec.getConfig()).resolve();
 } else {
  return jobSpec.getConfig().resolve();
 }
}

代码示例来源:origin: apache/incubator-gobblin

protected Config addInstanceName(Config baseConfig, String instanceName) {
 Map<String, String> configMap = new HashMap<>();
 configMap.put(IntegrationBasicSuite.TEST_INSTANCE_NAME_KEY, instanceName);
 Config instanceConfig = ConfigFactory.parseMap(configMap);
 return instanceConfig.withFallback(baseConfig).resolve();
}

代码示例来源:origin: apache/incubator-gobblin

@Override
 protected Collection<Config> getWorkerConfigs() {
  Map<String, String> configMap = new HashMap<>();
  configMap.put(GobblinClusterConfigurationKeys.ENABLE_TASK_IN_SEPARATE_PROCESS, "true");
  Config config = ConfigFactory.parseMap(configMap);
  Config parent = super.getWorkerConfigs().iterator().next();
  return Lists.newArrayList(config.withFallback(parent).resolve());
 }
}

代码示例来源:origin: apache/incubator-gobblin

@Override
public Config getManagerConfig() {
 Map<String, String> configMap = new HashMap<>();
 configMap.put(GobblinClusterConfigurationKeys.DEDICATED_MANAGER_CLUSTER_ENABLED, "true");
 configMap.put(GobblinClusterConfigurationKeys.MANAGER_CLUSTER_NAME_KEY, "ManagerCluster");
 Config config = ConfigFactory.parseMap(configMap);
 return config.withFallback(super.getManagerConfig()).resolve();
}

代码示例来源:origin: apache/incubator-gobblin

@Override
protected Config getClusterConfig() {
 Map<String, String> configMap = new HashMap<>();
 configMap.put(GobblinClusterConfigurationKeys.DEDICATED_TASK_DRIVER_CLUSTER_ENABLED, "true");
 configMap.put(GobblinClusterConfigurationKeys.TASK_DRIVER_CLUSTER_NAME_KEY, "TaskDriverCluster");
 Config config = ConfigFactory.parseMap(configMap);
 return config.withFallback(super.getClusterConfig()).resolve();
}

代码示例来源:origin: apache/incubator-gobblin

@Override
 public Config getManagerConfig() {
  Map<String, String> configMap = new HashMap<>();
  configMap.put(GobblinClusterConfigurationKeys.DEDICATED_MANAGER_CLUSTER_ENABLED, "true");
  configMap.put(GobblinClusterConfigurationKeys.MANAGER_CLUSTER_NAME_KEY, "ManagerCluster");
  Config config = ConfigFactory.parseMap(configMap);
  return config.withFallback(super.getManagerConfig()).resolve();
 }
}

代码示例来源:origin: apache/incubator-gobblin

protected Collection<Config> getWorkerConfigs() {
 // worker config initialization
 URL url = Resources.getResource("BasicWorker.conf");
 Config workerConfig = ConfigFactory.parseURL(url);
 workerConfig = workerConfig.withFallback(getClusterConfig());
 return Lists.newArrayList(workerConfig.resolve());
}

代码示例来源:origin: apache/incubator-gobblin

public Config getManagerConfig() {
 // manager config initialization
 URL url = Resources.getResource("BasicManager.conf");
 Config managerConfig = ConfigFactory.parseURL(url);
 managerConfig = managerConfig.withFallback(getClusterConfig());
 return managerConfig.resolve();
}

代码示例来源:origin: apache/incubator-pinot

static io.vavr.collection.Map<String, ?> loadConfigFromString(String string) {
 Config config =
   ConfigFactory.parseString(string, ConfigParseOptions.defaults().prependIncluder(new ConfigIncluder() {
    private ConfigIncluder parent = null;
    public ConfigObject include(ConfigIncludeContext context, String what) {
     return ConfigFactory.parseFileAnySyntax(new File(what)).root();
    }
    public ConfigIncluder withFallback(ConfigIncluder fallback) {
     parent = fallback;
     return this;
    }
   }));
 config = config.resolve();
 return HashSet.ofAll(config.entrySet()).toMap(entry -> Tuple.of(entry.getKey(), entry.getValue().unwrapped()));
}

代码示例来源:origin: apache/incubator-gobblin

@Test
public void testValidConfigsInPushMode_withClusterResolve() throws Exception{
 Config c = ConfigFactory.parseResources(getClass().getClassLoader(), "replicationConfigTest/validCompleteDataset_PushMode.conf").resolve();
 ReplicationConfiguration rc = ReplicationConfiguration.buildFromConfig(c);
 this.checkReplicationConfig_Push(rc);
}

代码示例来源:origin: apache/incubator-gobblin

@Test
public void testValidConfigsInPushMode_withTopologyPicker() throws Exception{
 Config c = ConfigFactory.parseResources(getClass().getClassLoader(), "replicationConfigTest/validCompleteDataset_PushMode2.conf").resolve();
 ReplicationConfiguration rc = ReplicationConfiguration.buildFromConfig(c);
 this.checkReplicationConfig_Push(rc);
}

代码示例来源:origin: apache/incubator-gobblin

public void testSerDerWithEmptyRequester() throws IOException {
 List<ServiceRequester> list = new ArrayList<>();
 RequesterService rs = new NoopRequesterService(ConfigBuilder.create().build());
 String serialize = rs.serialize(list);
 Properties props = new Properties();
 props.put(RequesterService.REQUESTER_LIST, serialize);
 Config initConfig = ConfigBuilder.create().build();
 Config config = initConfig.withFallback(ConfigFactory.parseString(props.toString()).resolve());
 Properties props2 = ConfigUtils.configToProperties(config);
 String serialize2 = props2.getProperty(RequesterService.REQUESTER_LIST);
 Assert.assertTrue(serialize.equals(serialize2));
 List<ServiceRequester> list2 = rs.deserialize(serialize);
 Assert.assertTrue(list.equals(list2));
}

相关文章