【问题标题】:Typesafe Config under OSGiOSGi 下的 Typesafe Config
【发布时间】:2015-01-14 14:51:05
【问题描述】:

使用 Typesafe Config 的库通常依赖于从类路径上的 /reference.conf 文件合并的一个大配置。

例如,Spray 期望在 ActorSystem 的 Typesafe Config 实例中找到其配置部分,但除非我创建 ActorSystem 的包导入 Spray 包,否则它们不可用。在我的应用程序中没有这样的导入,因为我有一个专用包,其唯一目的是将ActorSystem 作为服务发布。其他捆绑包使用它,其中一些依赖于 Spray,但不依赖于仅导出 AS 的捆绑包。

这让我想到了 Typesafe Config 在 OSGi 环境中查找 /reference.conf 文件的一般问题。我知道akka-osgiBundleDelegatingClassLoader 在捆绑包依赖链中查找资源,所以我想为什么不直接查看系统中的所有捆绑包以符合 Typesafe Config 的合并意识形态?

在 OSGi 下使用 Typesafe Config 的正确方法是什么?我将在答案中介绍我的通用解决方案,但我不是 OSGi 专家,我想知道这是否是错误的,为什么以及处理合并的更好方法是什么。

【问题讨论】:

    标签: osgi spray typesafe-config


    【解决方案1】:

    这使得 Typesafe Config 的 include 在所有已安装的包中查找给定资源:

    // create Typesafe Config
    
    val myConfig = ConfigFactory.parseFile(
      new File("myconfig.conf"),
      ConfigParseOptions.defaults().setClassLoader(new ClassLoader() {
        override def getResources(name: String): util.Enumeration[URL] = {
          val resources = context.getBundles.flatMap { bundle =>
            val found = JavaConversions.enumerationAsScalaIterator(
              Option(bundle.getResources(name)).getOrElse(Collections.emptyEnumeration())
            )
            found
          }
          JavaConversions.asJavaEnumeration(resources.toIterator)
        }
      })
    ).resolve()
    
    // create ActorSystem
    
    /* This could've been myconfig.getConfig("myconfig").withOnlyPath("akka") but
     * like I said, Spray expects to find "spray.*" section in the ActorSystem's config.
     */
    val factory = OsgiActorSystemFactory(context, myconfig.getConfig("myconfig"))
    val as = factory.createActorSystem("blah")
    

    在我使用的配置本身中:

    myconfig {
    
        // this will include reference.conf from every installed bundle in the container
        include classpath("reference.conf")
    
        // overrides
        akka.loglevel = INFO
        spray.version = "1.3.2"
    
        // etc ...
        other.stuff.for.my.app = 1
    }
    

    -Dconfig.trace=loads 显示:

    karaf@root()> feature:install myfeature
    Loading config from a file: C:\Users\YUUshakov\p\myconfig.conf
    Loading config from URL bundleresource://925.fwk875016237/reference.conf from class loader mytest.ConfigActivator$$anon$1@1131e4e4
    Loading config from URL bundleresource://931.fwk875016237/reference.conf from class loader mytest.ConfigActivator$$anon$1@1131e4e4
    Loading config from URL bundleresource://933.fwk875016237/reference.conf from class loader mytest.ConfigActivator$$anon$1@1131e4e4
    Loading config from URL bundleresource://934.fwk875016237/reference.conf from class loader mytest.ConfigActivator$$anon$1@1131e4e4
    Loading config from URL bundleresource://946.fwk875016237/reference.conf from class loader mytest.ConfigActivator$$anon$1@1131e4e4
    

    生成的配置实例获得了所有参考部分,如 myconfig.akkamyconfig.spray 等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2016-03-24
      • 2014-01-02
      • 2021-05-14
      • 2019-04-02
      • 1970-01-01
      相关资源
      最近更新 更多