Eclipse RCP中的全局变量 [英] Global variables in Eclipse RCP

查看:317
本文介绍了Eclipse RCP中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何解决这个问题:

我的主要RCP插件中有用户认证信息。所有其他插件也应该有这些信息。 Preference Store是不可能的,因为预置存储需要一个需要全局可用的节点名称。

I have usercredential informtion in my main RCP plugin. All other plugins should have that information as well. Preference Store is not possible, because also the presference store needs a nodename which needs to be globally available.

有没有可能实现全局变量?

Are there any possibilities to realize global variables?

谢谢!

推荐答案

有几个选项。

快速而肮脏的方法是将全局变量的getter放入插件之一的激活器中。那么你只需要得到你的全局:

The quick and dirty approach is to put a getter for your global variable into the activator of one of your plug-ins. Then you just get your global like:

SomePluginActivator.getDefault()。getGlobalData()

这使您的插件紧密耦合,这有点违反了插件架构的精神。

This has the downside of making your plug-ins tightly coupled, which somewhat goes against the spirit of a plug-in architecture.

第二种方法是使用工作台服务。这是一个更好的一点,虽然它引入了对您的插件的 org.eclipse.ui 的依赖。在提供全局数据的插件中,创建一个 org.eclipse.ui.services 的扩展名。您定义一个服务接口和服务工厂。工厂应该返回一个接口的实现。您的客户端通过向服务定位器询问服务接口的实例来检索全局服务的实例。该平台使用您的工厂来创建服务实例:

The second approach is to use a workbench service. This is a bit better, although it introduces a dependency on org.eclipse.ui into your plug-ins. In the plug-in that provides the global data, create an extension of org.eclipse.ui.services. You define a service interface and service factory. The factory should return an implementation of the interface. Your clients retrieve an instance of the global service by asking a service locator for an instance of the service interface. The platform uses your factory to create the instance of the service:

IMyGlobalService service =(IMyGlobalService)PlatformUI.getWorkbench()。getService(IMyGlobalService。 class);

这是熟悉的服务定位器模式。这种方法的好处是,一些全球数据的消费者不需要知道它来自哪里。为了增加灵活性,您可以在一个插件中声明该接口,并在另一个插件中声明该接口,并将其实现在另一个插件中,以便可以切换实现。

This is the familiar service locator pattern. The benefit of this approach is that the consumers of some global data don't need to know where it is coming from. For added flexibility, you can declare the interface in one plug-in and the factory and implementation in another, so that you can swap out implementations.

第三种方法是使用扩展点。我没有自己尝试过,但是您似乎应该能够在插件中声明一个扩展点,该扩展点提供全局数据,然后将全局数据注入扩展它的插件。

The third approach is to use an extension point. I haven't tried this myself, but it seems like you should be able to declare an extension point in a plug-in that provides global data, then inject the global data into the plug-ins that extend it.

最后一种方法是使用OSGi服务。我最不熟悉这种方法。您将编写OSGi服务,其他插件将使用OSGi框架来定位服务。这与上面的工作台服务类似,但它不直接使用Eclipse RCP。您还应该阅读白板图案(警告:PDF链接)。

The last approach is to use an OSGi service. I'm least familiar with this approach. You'd write an OSGi service, and other plug-ins would use the OSGi framework to locate the service. This is similar to the workbench services above, but it doesn't use Eclipse RCP directly. You should also read about the whiteboard pattern (warning: PDF link).

这篇关于Eclipse RCP中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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