开发者

Global variables in Eclipse RCP

how do i solve this:

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 need开发者_运维问答s a nodename which needs to be globally available.

Are there any possibilities to realize global variables?

Thanks!


There are a few options.


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.


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.


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).


I realize this question was posed long ago, but I haven't found a more recent discussion about this issue.

I am new to RCP development, but why not use IEclipsePreferences? Write:

IEclipsePreferences rootNode = Platform.getPreferencesService().getRootNode();
rootNode.put("currentFileName", "my.file.name.txt");

Read somewhere else:

IEclipsePreferences rootNode = Platform.getPreferencesService().getRootNode();
String currentFileName = rootNode.get("currentFileName", null);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜