如何在激活器中获取IEclipseContext [英] How to get the IEclipseContext in an activator

查看:191
本文介绍了如何在激活器中获取IEclipseContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Eclipse 4 RCP应用程序的一个问题。我需要记录一些事件。我需要以某种方式获得对记录器的引用。我知道,如何使用 IEclipseContext ,但我找不到,如何获得 IEclipseContext 没有依赖注射,我不能在激活剂中使用。你知道怎么解决这个问题吗?

I got stuck on one problem with an Eclipse 4 RCP application. I need to log some events. I need obtain somehow a reference to the logger. I know, how to do that using IEclipseContext, but I've nowhere found, how to obtain IEclipseContext without the dependency injection, which I cannot use in the activator. Do you anybody know, how to sort it out this problem, please?

非常感谢

推荐答案

似乎很遗憾,没有使用注入就无法获得 IEclipseContext
有一个答案写在如何在未附加到应用程序模型的类中使用eclipse 4 DI

It seems regretably, that there is no way to obtain IEclipseContext without using injection. There is written in an answer to How to use eclipse 4 DI in classes that are not attached to the application model:


然而,问题是 IEclipseContext 已经需要将
注入到可以访问需要注入的对象的类中。

The problem is, however, that the IEclipseContext already needs to be injected into a class that can access the object that needs injection.

尽管如此,我已经解决了伐木问题和我的事情,这个原理一般都有效。总有一些服务提供您需要的东西。如果你不能使用依赖注入,你必须以某种方式(互联网和实验经常)获得适当的服务类名称。如果您已获得服务类名称,则可以从捆绑上下文中获取实例引用。幸运的是,可以在不使用注入的情况下访问bundle上下文。

Nevertheless I have already sorted out the problem of logging and I thing, the principle works generally. There is always some service providing things you need. If you cannot use the dependency injection, you've to get somehow (Internet and experiments are very often) an appropriate service class name. If you have got the service class name, then you can obtain an instance reference from the bundle context. Fortunately, the bundle context is accessible without using injection.

回到我们的日志记录问题。被搜索的类是 org.osgi.service.log.LogService

Back to our logging problem. The class being searched is org.osgi.service.log.LogService:

public class Activator implements BundleActivator {
    ...
    private static BundleContext context;
    ...

    public static BundleContext getContext() {
        return context;
    }
    ...
    public void start(BundleContext bundleContext) throws Exception {
        ServiceReference<?> logser = bundleContext.getServiceReference(LogService.class);
        LogService ls = (LogService)bundleContext.getService(logser);
        //print an error to test it (note, that info can be below the threshold)
        ls.log(LogService.LOG_ERROR, "The bundle is starting...");
        Activator.context = bundleContext;
    }
    ...
}

Etvoilà!

!ENTRY eu.barbucha.rcp-experiment.kernel 4 0 2013-08-20 07:32:32.347
!MESSAGE The bundle is starting...

这就是全部。稍后您可以使用 Activator.getContext()获取捆绑上下文,如果需要的话。

That's all. Later you can obtain the bundle context using Activator.getContext(), if it would be needed.

重要提示:很遗憾,您现在无法降低阈值。 JVM参数 -Declipse.log.level 不会影响OSGI日志服务,您现在只使用OSGI记录器。不幸的是,他们(可能暂时)硬编码了记录阈值(参见如何记录警告和日食中的信息3.7)。我发现,他们还没有修复它。在开普勒版本中都没有。但是你可以做出妥协。你可以这样做 注入方式 ,在可能的情况下。

Important note: Regretably you cannot decrease the threshold now. The JVM argument -Declipse.log.level does not affect the OSGI log service and you're using just the OSGI logger now. Unfortunately they (may have provisionally) hardcoded the logging threshold (see How to log warnings and infos in eclipse 3.7). I found out, that they haven't repair it yet. Neither in the Kepler release. However you can make a compromise. You can do that injection-way, where possible.

最终解决方案(以便在全球范围内捕获例外情况)

我扩展了我的激活器:

ServiceReference<?> logreser = bundleContext.getServiceReference(LogReaderService.class);
LogReaderService lrs = (LogReaderService) bundleContext.getService(logreser);   
lrs.addLogListener(new LogListener() {
    @Override
    public void logged(LogEntry entry) {
        System.err.println("Something was logged: " + entry.getMessage());
    }
});

以某事记录开头的文字真的出现了,whenewer就是某个地方登录。但最大的优点是,这个班级是我的。我可以控制它。日志条目还包含级别。我也可以轻松设置阈值。例如,在命令行上。

The text beginning with Something was logged really appears, whenewer is something somewhere logged. But the very advantage is, that this class is mine. I can control it. The log entry contains also the level. I can also easily set the threshold. For example on the command line.

这篇关于如何在激活器中获取IEclipseContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆