【问题标题】:How to get the IEclipseContext in an activator如何在激活器中获取 IEclipseContext
【发布时间】:2013-08-19 14:58:24
【问题描述】:

我遇到了 Eclipse 4 RCP 应用程序的一个问题。我需要记录一些事件。我需要以某种方式获得对记录器的引用。我知道,如何使用IEclipseContext 来做到这一点,但我无处可寻,如何在没有依赖注入的情况下获得IEclipseContext,我不能在激活器中使用它。请问有大神知道这个问题怎么解决吗?

非常感谢

【问题讨论】:

    标签: java eclipse-rcp e4


    【解决方案1】:

    您可以通过调用EclipseContextFactory.getServiceContext(bundleContext) 获得专门的IEclipseContext,这将允许访问OSGi 服务。

    【讨论】:

    • specialized 究竟是什么意思?我试过了,但是没有org.eclipse.e4.core.services.Logger类int这个专门的IEclipseContext的实例,怎么提到on this wiki page。如果我理解得很好,那也不可能。只有 OSGI 记录器用于这些低级别的东西,如激活器等。
    • 它是“专门的”,因为它只包含 OSGi 服务,而不包含通常在上下文中找到的其他东西。它确实包含 LogService 和 LogReaderService
    • 是的,你是对的。只有 OSGI 服务,因为激活器是 OSGI 级别的问题。
    • 如果您在这种情况下需要 e4 Logger,您可以尝试以下操作: eclipseContext.set(Logger.class, new WorkbenchLogger(Activator.PLUGIN_ID); 目前有“不鼓励访问”警告。
    【解决方案2】:

    似乎很遗憾,不使用注入就无法获得IEclipseContext。 回复How to use eclipse 4 DI in classes that are not attached to the application model有写:

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

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

    回到我们的日志记录问题。正在搜索的类是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;
        }
        ...
    }
    

    等等!

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

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

    重要提示:很遗憾,您现在无法降低阈值。 JVM 参数-Declipse.log.level 不会影响 OSGI 日志服务,您现在只使用 OSGI 记录器。不幸的是,他们(可能暂时)硬编码了日志记录阈值(参见How to log warnings and infos in eclipse 3.7)。我发现,他们还没有修复它。在开普勒版本中也没有。但是,您可以做出妥协。在可能的情况下,您可以这样做injection-way

    最终解决方案(也可以全局捕获异常)

    我扩展了我的激活器:

    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());
        }
    });
    

    Something was logged 开头的文本确实出现了,当某个地方记录了某事时。但最大的好处是,这门课是我的。我可以控制它。日志条目还包含级别。我还可以轻松设置阈值。例如在命令行上。

    【讨论】:

      【解决方案3】:

      可以从IWorkbench 作为服务获取“WorkbenchContext”:

      import org.eclipse.e4.core.contexts.IEclipseContext;
      import org.eclipse.ui.PlatformUI;
      
      public final class EclipseContextHelper {
      
          public static IEclipseContext getActiveContext(){
              IEclipseContext context = getWorkbenchContext();
              return context == null ? null : context.getActiveLeaf();
          }
      
          public static IEclipseContext getWorkbenchContext(){
              return PlatformUI.getWorkbench().getService(IEclipseContext.class);
          }
      }
      

      【讨论】:

      • PlatformUI.getWorkbench() 是 RCP 3 API。
      猜你喜欢
      • 1970-01-01
      • 2012-11-17
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多