Osgi Config in Plain JAVA | Community
Skip to main content
Level 5
July 16, 2020
Solved

Osgi Config in Plain JAVA

  • July 16, 2020
  • 1 reply
  • 1356 views

Dear Team,

 

Is there any way to read OSGI Configuration values in a Plain JAVA Class (Not Service or Component) ?

 

Better if someone could help me out with example.

 

Thank you in advance.

 

Regards,

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ArpitVarshney

Hi @arvind-1 

Could you try below code:

 

BundleContext bundleContext = FrameworkUtil.getBundle(ServiceName.class).getBundleContext();
ServiceReference<?> configurationAdminReference =
bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);
Configuration config = confAdmin.getConfiguration("service-pid");
Dictionary<String, Object> properties = config.getProperties();
if (properties == null) {
LOGGER.error("No Properties found for the Config PID: {}", configPid);
} else {
String property = (String) properties.get("propertyName");
}

 

Note: Replace hardcoded service-pid and ServiceName with actual one.

Reference :https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-read-osgi-configuration-in-osgi-service-class-not-in/qaq-p/206360

 

Regards,

Arpit

1 reply

ArpitVarshney
Community Advisor
ArpitVarshneyCommunity AdvisorAccepted solution
Community Advisor
July 16, 2020

Hi @arvind-1 

Could you try below code:

 

BundleContext bundleContext = FrameworkUtil.getBundle(ServiceName.class).getBundleContext();
ServiceReference<?> configurationAdminReference =
bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);
Configuration config = confAdmin.getConfiguration("service-pid");
Dictionary<String, Object> properties = config.getProperties();
if (properties == null) {
LOGGER.error("No Properties found for the Config PID: {}", configPid);
} else {
String property = (String) properties.get("propertyName");
}

 

Note: Replace hardcoded service-pid and ServiceName with actual one.

Reference :https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-read-osgi-configuration-in-osgi-service-class-not-in/qaq-p/206360

 

Regards,

Arpit