eclipse-equinox / equinox.bundles

Eclipse Public License 2.0
8 stars 16 forks source link

Publish the a "BundleStateScope" as an IScopeContext service (factory) #24

Closed laeubi closed 2 years ago

laeubi commented 2 years ago

Currently a common patterns is that one acquires the instance scope through a static access InstanceScope.INSTANCE and then pass the ID of the bundle to listen for preference changes.

Another similar call is Platform.getStateLocation(...) or Plugin.getStateLocation to get an IPath to store bundle specific data.

Both calls suffer from the fact (due to their static access) that they are prone to initialization order problems, e.g. if the workspace location is not set. Beside that, a user of these must in one way or another bind itself to the Platform and/or Plugin activator to work where a reference to IScopeContext / IEclipsePreferences would be enough.

I'd thus would suggest the following:

That way a component that want to use either of both could simply use:

@Reference(target = "(context=bundle)")
IScopeContext scopeContext;

to get a reference and then call to add a preference listener to the root: scopeContext.getNode("").addPreferenceChangeListener(this); or IPath myLocation = scopeContext.getLocation(); to get hold of its state location.

Given that we delay the registration of the service factory until a suitable state is reached (e.g. workspace URL set) such a component is resistant to start-order dependencies or to early bundle activation.