views:

349

answers:

1

Hi,

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 needs a nodename which needs to be globally available.

Are there any possibilities to realize global variables?

Thanks!

+4  A: 

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

Mike Daniels