ConfigReader
来源:互联网 发布:汉诺塔算法伪代码 编辑:程序博客网 时间:2024/06/05 03:29
/** * A helper class for reading config entries and performing variable substitution. * * If a config value contains variable references of the form "${prefix:variableName}", the * reference will be replaced with the value of the variable depending on the prefix. By default, * the following prefixes are handled: * * - no prefix: use the default config provider * - system: looks for the value in the system properties * - env: looks for the value in the environment * * Different prefixes can be bound to a `ConfigProvider`, which is used to read configuration * values from the data source for the prefix, and both the system and env providers can be * overridden. * * If the reference cannot be resolved, the original string will be retained. * * @param conf The config provider for the default namespace (no prefix). */
private[spark] class ConfigReader(conf: ConfigProvider) { def this(conf: JMap[String, String]) = this(new MapProvider(conf)) private val bindings = new HashMap[String, ConfigProvider]() bind(null, conf) bindEnv(new EnvProvider()) bindSystem(new SystemProvider()) /** * Binds a prefix to a provider. This method is not thread-safe and should be called * before the instance is used to expand values. */ def bind(prefix: String, provider: ConfigProvider): ConfigReader = { bindings(prefix) = provider this } def bind(prefix: String, values: JMap[String, String]): ConfigReader = { bind(prefix, new MapProvider(values)) } def bindEnv(provider: ConfigProvider): ConfigReader = bind("env", provider) def bindSystem(provider: ConfigProvider): ConfigReader = bind("system", provider)
/** * Reads a configuration key from the default provider, and apply variable substitution. */ def get(key: String): Option[String] = conf.get(key).map(substitute) /** * Perform variable substitution on the given input string. */ def substitute(input: String): String = substitute(input, Set()) private def substitute(input: String, usedRefs: Set[String]): String = { if (input != null) { ConfigReader.REF_RE.replaceAllIn(input, { m => val prefix = m.group(1) val name = m.group(2) val ref = if (prefix == null) name else s"$prefix:$name" require(!usedRefs.contains(ref), s"Circular reference in $input: $ref") val replacement = bindings.get(prefix) .flatMap(_.get(name)) .map { v => substitute(v, usedRefs + ref) } .getOrElse(m.matched) Regex.quoteReplacement(replacement) }) } else { input } }
0 0
- ConfigReader
- ConfigReader(六十)—— ConfigReader
- testNg之ConfigReader
- Commom(十)—— ConfigReader
- ConfigReader(一)—— MapConfig
- ConfigReader(二)—— MapLoadConfig
- ConfigReader(三)—— ReadAdvancedGuideConfig
- ConfigReader(四)—— ReadBuffConfig
- ConfigReader(五)—— ReadBuySkinConfig
- ConfigReader(六)—— ReadCombineConfig
- ConfigReader(七)—— ReadDailyBonusConfig
- ConfigReader(八)—— ReadDailyTaskConfig
- ConfigReader(九)—— ReadGuideAbsorbTaskConfig
- ConfigReader(十)—— ReadGuideBornNpcTaskConfig
- ConfigReader(十一)—— ReadGuideCameraTaskConfig
- ConfigReader(十二)—— ReadGuideClickButtonTaskConfig
- ConfigReader(十三)—— ReadGuideFlashTaskConfig
- ConfigReader(十四)—— ReadGuideHelpConfig
- DI【理解】【应用】【重点】
- 8.learningAction(行为库)3
- hadoop资料收集
- Vue2中的键盘事件
- Android指纹识别
- ConfigReader
- 2、scrapy使用步骤
- DrawerLayout不能滑动关闭的解决办法
- Win7下Nginx的安装与配置,win7nginx配置
- Matlab图像预处理命令
- android绘制动画实现方式
- iOS Apps – JSON-RPC – Go Servers
- JWT简介
- 网络编程-实验2-C与C通信