我想阿帕奇菲利克斯嵌入到使用Maven簡單的Hello World Java項目,但我不能找到一種方式來獲得一個參考捆綁服務。我已經從jar中將org.apache.felix.bundlerepository包安裝到OSGi中,並將其作爲Maven依賴項添加到我的項目中。之後,我開始捆綁,從它獲取BundleContext,然後在該捆綁上下文中調用getServiceReference(RepositoryAdmin.class.getName())。我不開心的第一件事是我必須使用BundleContext從已安裝的bundle中,如果我使用框架的BundleContext,ServiceReference將始終爲空。這並不方便。
第二,更重要的問題是,當我終於接收到從bundlerepository束RepositoryAdmin服務的引用我不能將它轉換爲org.apache.felix.bundlerepository.RepositoryAdmin,執行以下代碼:
(RepositoryAdmin)admin.getBundleContext().getService(ref)
將拋出此異常:
java.lang.ClassCastException: org.apache.felix.bundlerepository.impl.RepositoryAdminImpl cannot be cast to org.apache.felix.bundlerepository.RepositoryAdmin
我知道這是一種類路徑問題,並可以通過接口不兼容引起的,但我使用相同版本的一個包JAR(2.0.2 )作爲我項目的maven依賴項。 我也知道Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA osgi配置選項,它應該強制捆綁使用來自主機應用程序的包,但它並沒有幫助我。
這是我的項目https://github.com/ArtemZ/osgi-study/blob/master/src/main/java/com/artemz/demo/Main.java的主要類,這是凌亂的,因爲我嘗試了不同的東西,以便獲得對服務的引用,但沒有一個真正爲我工作。
希望有人能幫助我,因爲我真的很擔心這個問題。
哇,這實際上幫助了我!非常感謝!我預計版本2.0.2的軟件包將導出與同一版本的軟件包...我錯了。再次感謝你的幫助! – 2014-10-28 03:40:20