2014-02-26 17 views
1

我的應用程序當前正在使用equinox的IProvisioningAgent查找捆綁軟件並將其供應到正在運行的配置文件中。代碼符合以下內容;如何在配置到運行配置文件後解析並啓動捆綁軟件

// Look up agent provider using OSGI service 
IProvisioningAgentProvider provider = ...; 
IProvisioningAgent = provider.createAgent(null); // Use currently running agent 

// Define IU 
IInstallableUnit iu = ...; // found using remote repository queries 

// Find profile 
IProfileRegistry registry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME); 
IProfile profile = registry.getProfile(IProfileRegistry.SELF); 

// Create change request 
IPlanner planner = (IPlanner) agent.getService(IPlanner.SERVICE_NAME); 
IProfileChangeRequest request = planner.createChangeRequest(profile); 
request.add(iu); 

// Create plan and perform 
IProvisioningPlan plan = planner.getProvisioningPlan(request, ctx, monitor); 
IEngine engine = (IEngine) agent.getService(IEngine.SERVICE_NAME); 
IStatus status = engine.perform(plan, PhaseSetFactory.createDefaultPhaseSet(), monitor); 

這工作正常,我可以看到IU(具有依賴關係)已安裝在磁盤上。

我現在需要在不重新啓動的情況下將軟件包安裝到運行環境中。我可以在網上找到的所有例子只是重新啓動不適合在這種情況下的平臺。我以前使用BundleContext.installBundle(),但似乎太低級別,我無法找到如何從配置API獲取URL。

我可以使用配置API的另一部分嗎?我已閱讀使用org.eclipse.equinox.internal.provisional.configurator.Configurator,但它是內部的,似乎無法解決問題。

我的問題是:什麼是正確安裝,解決和啓動剛剛設置的捆綁包而不重新啓動的步驟。

回答

0

我發現,暴露了IU的文物爲java.io.File所以可以代替伊利亞的特別臭部分的額外服務。

IAgentLocation location = (IAgentLocation) agent.getService(IAgentLocation.SERVICE_NAME); 
URI bundlePool = location.getDataArea(ECLIPSE_TOUCHPOINT_ID); 
IArtifactRepositoryManager manager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME); 
// XXX Bit of a smell to cast here, but I know they are files now. 
IFileArtifactRepository repository = (IFileArtifactRepository) manager.loadRepository(
       bundlePool, monitor); 


// I can get the artifact and then query the repository 
for (IArtifactKey artifact : iu.getArtifacts()) { 
    File file = repository.getArtifactFile(artifact); 
    URI uri = file.toURI(); 

    // Install the bundle 
    ctx.installBundle(uri.toString()); 
} 

對於大多數情況下,這個作品。這裏的代碼已經被刪除了錯誤處理,並且可能不會在沒有調整的情況下編譯。它應該適用於不同的配置文件和不同的代理。

我相信有一個更好的解決方案,它涉及使用eclipse接觸點以正確的階段自動安裝軟件包。如果我找到更好的解決方案,我會盡力在此更新。

+0

ECLIPSE_TOUCHPOINT_ID的值是多少? –

1

P2中沒有API用於在運行平臺中安裝/更新軟件包。

對於這種情況,我們也使用BundleContext#installBundle(location)。下面的代碼示例說明了我們的做法。我們獲取要安裝的軟件包的URL的方式不是猶太教,如果你有更好的解決方案,請提出。

IPath bundlePoolPath=...; 
String iuFullName=...; //this you get from P2 

Bundle bundle = null; 

//XXX especially stinky part 
IPath bundleAsJar = bundlePoolPath.append("plugins/" + iuFullName + ".jar"); 

URL bundleURL = bundleAsJar.toFile().toURI().toURL(); 

try { 
    bundle = ctx.installBundle(bundleLocationURL.toExternalForm()); 
} 
catch (BundleException e) { 
    // may fail if the bundle is extracted to dir (hello, P2) 
    IPath bundleAsDir = bundlePoolPath.append("plugins/" + iuFullName); 
    bundleURL = bundleAsDir.toFile().toURI().toURL(); 
    bundle = ctx.installBundle(bundleURL.toExternalForm()); 
} 
+0

感謝您的支持;我已經添加了我自己的答案,因爲我相信它改進了符號名稱到URI的問題。由於我沒有足夠的聲譽,我無法評價您的答案。 –

0

我想分享一個完整的工作解決方案 - https://github.com/Nasdanika/server/blob/master/org.nasdanika.provisioning/src/org/nasdanika/provisioning/AutoUpdateComponent.java

此實現通過遍歷所有可用配置文件(以下情形中的一個)和系統存儲庫(以下情形中的兩個)來避免使用硬編碼配置文件和捆綁軟件位置名稱。我不知道它是否適用於所有情況,但它在下面解釋的情況下有效。

使用場景:

有服務在EPackage.Registry.INSTANCE註冊ECore模型文檔的春分/基於OSGi的Web應用程序(https://server-side-java-development-for-innovators.books.nasdanika.org/chapter-0-setup/documentation-system-overview.html,包款)。

模型包定期更新。如果沒有AutoUpdateComponent手冊,則需要重新部署Web應用程序才能使模型文檔保持最新。

AutoUpdateComponent配置了感興趣模型發佈的存儲庫位置。該組件安裝在存儲庫中找到的新捆綁軟件並更新現有捆綁軟件。因此,模型文檔會自動重新發布。