Expand my Community achievements bar.

SOLVED

Osgi Config in Plain JAVA

Avatar

Level 6

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,

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @arvind 

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

 

Regards,

Arpit

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @arvind 

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

 

Regards,

Arpit