簡體   English   中英

如何獲取Eclipse RCP應用程序的OSGi BundleContext?

[英]How do I get the OSGi BundleContext for an Eclipse RCP application?

我剛剛開始使用Eclipse RCP應用程序,它基本上只是提供的“hello world”示例之一。

當應用程序啟動時,我想查看我的命令行參數並根據它們啟動一些服務。 我可以在IApplication.start中獲取命令行參數:

public Object start(IApplicationContext context) {
   String[] argv = (String[]) 
       context.getArguments().get(IApplicationContext.APPLICATION_ARGS)));
}

但是如何獲得BundleContext,以便我可以注冊服務? 它似乎不在IApplicationContext中。

剛剛進行了網絡搜索,並認為我將推廣新的標准OSGi R4.2方式(由Eclipse 3.5附帶的Equinox提供)。 如果您沒有激活器,並且不想僅僅為了緩存捆綁上下文而創建激活器,則可以使用FrameworkUtil.getBundle。 修改上一個示例:

import org.osgi.framework.FrameworkUtil;

public class ExportClassDigestApplication implements IApplication {
    public Object start(IApplicationContext context) throws Exception {
        context.applicationRunning();
        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
                                                   .getBundleContext();
    }
}

棘手的內部方式:

InternalPlatform.getDefault().getBundleContext()

能做到這。

您將在本課程中找到一個示例

public class ExportClassDigestApplication implements IApplication {

    public Object start(IApplicationContext context) throws Exception {
        context.applicationRunning();

        List<ExtensionBean> extensionBeans = ImpCoreUtil.loadExtensionBeans(&quot;com.xab.core.containerlaunchers&quot;);
        for (ExtensionBean bean : extensionBeans) {
            ILauncher launcher = (ILauncher) bean.getInstance();
            launcher.start();
        }
        ClassFilter classFilter = new ClassFilter() {
            public boolean isClassAccepted(Class clz) {
                return true;
            }
        };

        PrintWriter writer = new PrintWriter( new File( "C:/classes.csv"));

        Bundle[] bundles = InternalPlatform.getDefault().getBundleContext().getBundles();

適當的方式

每個插件都可以訪問自己的bundle上下文。

只需確保您的插件類覆蓋start(BundleContext)方法。 然后,您可以將其保存到插件中的地方類,可以輕松訪問

請注意,提供給插件的bundle上下文是特定於它的,不應該與其他插件共享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM