osgi.configuration.area and FileSystemResource

0

源起

程式都需要一些設定檔或是資料庫檔,然而這些檔案的安裝位置並不一定是絕對的, 所以執行時需要取得當時環境的資訊來調整,往往是根據安裝時放置的相對位置來決定, 像要練習如何取得 OSGi 執行時期的檔案位置,以及處理 Spring-OSGi 不支援 Resource 注入的替代方案。

參考

  1. Eclipse Runtime Options
  2. Can’t Find Resources outside of the bundle

Eclipse runtime options

使用 Equinox osgi 執行時會提供一些環境參數 ,程式中可以用 System.getProperty() 取得執行時期一些 檔案路徑來使用,下面為一個 bundle 在 Eclipse PDE 平台 的測試的範例。

  1. osgi.configuration.area= file:/C:/EclipseInstall/eclipse-3.2/configuration/
  2. osgi.install.area= file:/C:/EclipseInstall/eclipse-3.2/
  3. osgi.instance.area= file:/C:/myproject/Haha2007/
  4. osgi.instance.area.default= file:/C:/Documents and Settings/Foo/workspace/

File Resource

之前使用 Spring Resource 注入的 bean,放到 OSGi 環境會出現檔案問題,根據參考連結的解法,需要自行產出 FileSystemResource 來使用。

<bean 
class="org.sf.beans.factory.config.PropertyPlaceholderConfigurer" />
<bean id="haha" class="xx.yy.zz">
<property name="fooFile">
   <bean class="org.sf.core.io.FileSystemResource">
      <constructor-arg>${osgi.configuration.area}</constructor-arg>
   </bean>
</property>
</bean> 

不過 FileSystemResource 不吃 file: 開頭的字串,乾脆轉成下面做法。

<bean 
  class="org.sf.beans.factory.config.PropertyPlaceholderConfigurer" />
<bean id="haha" class="xx.yy.zz" 
  p:osgiConfArea="${osgi.configuration.area}"/>

觀察

  1. osgi.configuration.area 不清楚是否 OSGi 規格有提到,如果沒有,其他執行平台不一定會支援。

Comments

(leave url/email »)

   Preview comment