2015-01-14 75 views
0

使用Typesafe Config的庫通常依賴於從類路徑中/reference.conf文件合併的一個大配置。OSGi下的Typesafe Config

例如,Spray希望在ActorSystem的Typesafe Config實例中找到其配置節,但它們不可用,除非我創建ActorSystem的軟件包導入Spray軟件包。在我的應用程序中沒有這樣的導入,因爲我有一個專用包,其唯一目的是作爲服務發佈ActorSystem。其他bundle使用它,其中一些依賴於Spray,但不依賴於僅導出AS的bundle。

這導致我在OSGi環境中發現Typesafe Config的一般問題/reference.conf文件。我知道akka-osgiBundleDelegatingClassLoader查找捆綁依賴關係鏈中的資源,所以我想爲什麼不直接查看系統中的所有捆綁以符合Typesafe Config的合併idology?

在OSGi下使用Typesafe Config的正確方法是什麼?我將在一個答案中提出我的通用解決方案,但我不是OSGi專家,並且希望知道這是否是錯誤的,爲什麼以及什麼是更好的方式來處理合並。

回答

1

這使得類型安全配置的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顯示:

[email protected]()> 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 [email protected] 
Loading config from URL bundleresource://931.fwk875016237/reference.conf from class loader [email protected] 
Loading config from URL bundleresource://933.fwk875016237/reference.conf from class loader [email protected] 
Loading config from URL bundleresource://934.fwk875016237/reference.conf from class loader [email protected] 
Loading config from URL bundleresource://946.fwk875016237/reference.conf from class loader [email protected] 

並生成配置實例去t所有參考部分如myconfig.akkamyconfig.spray