Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Offer BundleContext#registerService without HashTable #43

Open
bjhargrave opened this issue Sep 2, 2019 · 4 comments
Open

Offer BundleContext#registerService without HashTable #43

bjhargrave opened this issue Sep 2, 2019 · 4 comments
Labels
publiccomment Public comment

Comments

@bjhargrave
Copy link
Member

Original bug ID: BZ#239
From: Lars Vogel <[email protected]>
Reported version: R7

@bjhargrave
Copy link
Member Author

Comment author: Lars Vogel <[email protected]>

The synchronized data structure HashTable is not recommened to be used, by several tools, e.g. Sonar shows this as warning. Would be nice if we could register services without creating warnings in our code.

Example:

Dictionary<String, String> props = new Hashtable<>();
	props.put(Constants.SERVICE_VENDOR, "Eclipse"); //$NON-NLS-1$
	props.put(Constants.SERVICE_PID, SimpleConfiguratorConstants.TARGET_CONFIGURATOR_NAME);
	ServiceFactory<?> configurationFactory = new SimpleConfiguratorFactory(context);
	configuratorRegistration = context.registerService(Configurator.class.getName(), configurationFactory, props);

@bjhargrave bjhargrave added the publiccomment Public comment label May 1, 2020
@HannesWell
Copy link

Comment author: Lars Vogel <[email protected]>

The synchronized data structure HashTable is not recommened to be used, by several tools, e.g. Sonar shows this as warning.

Even the Java-doc of Dictionary says that Map should be used instead.

Dictionary is referenced in the following classes (at least in the eclipse implementation org.eclipse.osgi) :

  • org.osgi.framework.Bundle
  • org.osgi.framework.BundleContext
  • org.osgi.framework.Constants
  • org.osgi.framework.Filter
  • org.osgi.framework.FrameworkUtil
  • org.osgi.framework.ServiceEvent
  • org.osgi.framework.ServiceReference
  • org.osgi.framework.ServiceRegistration
  • org.osgi.service.condpermadmin.Condition

Some of them define API using Dictionary, some only consume it. But all of them should be considered when Dictionary is replaced some day.

@bjhargrave
Copy link
Member Author

See the new FrameworkUtil.asDictionary(Map) helper method which can be used here. This method was recently added as a bridge from Maps to the OSGi API.


Map<String, String> props = new HashMap<>();
	props.put(Constants.SERVICE_VENDOR, "Eclipse"); //$NON-NLS-1$
	props.put(Constants.SERVICE_PID, SimpleConfiguratorConstants.TARGET_CONFIGURATOR_NAME);
	ServiceFactory<?> configurationFactory = new SimpleConfiguratorFactory(context);
	configuratorRegistration = context.registerService(Configurator.class.getName(), configurationFactory, FrameworkUtil.asDictionary(props));

@stbischof
Copy link

stbischof commented Jul 14, 2021

I can see your problems with the dictionary. But in most cases of using dictionaries it might be better to use newer patterns.
e.g. use @ Component and other ComponentPropertType annotations and do not try to register everything by hand in the BundleActivator.

The only way i really use an dictionary is in tests. But there I use the powerfully Dictionaries class. Maybe more of the functionally should be available in the framework package.

Btw.
The javadoc does not say that you should use maps instead of dictionarys. It says only to not do new implementations.

New implementations should implement the Map interface, rather than extending this class.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
publiccomment Public comment
Projects
None yet
Development

No branches or pull requests

3 participants