2016-02-10 89 views
1

您好我正在使用FeaturesService和BundleContext在運行時加載一些功能和捆綁。所有這些東西都成功加載。之後,如果我對bundlecontext對象進行操作,我會得到 java.lang.IllegalStateException:無效的BundleContext。Karaf PaxExam獲取java.lang.IllegalStateException:無效的BundleContext

@Inject 
FeaturesService service; 
@Before 
public void init() throws Exception{ 
    service.installFeature("hibernate"); 
    service.installFeature("hibernate-validator"); 
    service.installFeature("transaction"); 
    service.installFeature("jpa"); 
    service.installFeature("hibernate-envers"); 
    service.installFeature("hibernate-envers"); 
    bc.installBundle("wrap:mvn:com.oracle/ojdbc6/11.2.0").start(); 
    service.installFeature("DBHandler"); 
    bc.getBundle(); // Fails 
} 

經過大量的瀏覽,我明白你需要刷新捆綁。如何以編程方式執行並獲得重新引導的BundContext對象

回答

0

此代碼固定我的問題

public void refreshBundles() { 

     Bundle currentBundle = FrameworkUtil.getBundle(MyTest.class); 
     if (currentBundle == null) { 
      return; 
     } 

     BundleContext bundleContext = currentBundle.getBundleContext(); 
     if (bundleContext == null) { 
      return; 
     } 

     Bundle systemBundle = bundleContext.getBundle(0); 
     if (systemBundle == null) { 
      return; 
     } 

     FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class); 
     frameworkWiring.refreshBundles(null); 
     bc = frameworkWiring.getBundle().getBundleContext(); 
    } 
3

當您使用無效的軟件包時:它已被停止或刷新(刷新停止軟件包並啓動新實例)

安裝功能時,默認情況下,Karaf會嘗試定義要刷新的軟件包列表,因爲它具有新功能。例如,如果一個bundle對一個包有一個可選的依賴關係,並且新特性添加了這個包,那麼這個包將被刷新,以便更新他的連線。這是傳遞性的:所有依賴包也被刷新。

此外,當您使用「包裝」的協議,它通過導入創建從一個罐子一束都使用「可選」

在你的情況的決議,我想該功能「DBHandler」打包添加你的包使用的包。

您可以:

  • 安裝功能後,查找由SymbolicName你的捆綁,與BundleContext.getBundles():你將有一個正當的捆紮
  • 使用該選項NoAutoRefreshBundles到的實例安裝功能時禁用刷新(featureService.installFeature("..", EnumSet.of(FeatureService.NoAutoRefreshBundles)))。但是,這不是一個好主意,因爲一些包將不會看到新包
+0

Jermie我找到了一種方法來刷新和更新它把我的問題 – Charity

+0

的護理的BundleContext如果有人期待使用Jérémie解決方案,請在'FeaturesService.Option'中查找枚舉,而不是簡單地使用'FeaturesService' – buer