Expand my Community achievements bar.

SOLVED

How to Read OSGi Configuration in OSGI Service class not in Activator Bundle Class?

Avatar

Level 4

 I want to read OSGI Configuration and following this blog   http://www.wemblog.com/2012/10/how-to-work-with-configurations-in-cq.html

@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)
private ConfigurationAdmin configAdmin;
private Configuration conf = null;
private Logger logger = LoggerFactory.getLogger(ConfigurationUtilImpl.class);
public String getConfig(String pid,String key){
try {
if(configAdmin!=null){
conf = configAdmin.getConfiguration(pid);
if(conf!=null){
if(null!=conf.getProperties() && null!=conf.getProperties().get(key)){
return conf.getProperties().get(key).toString();

 

}

 

I always get ConfigurationAdmin null.

 

Servlet and JSP can retrieve this his way but i need it OSGI JAVA Service.

<%@page import="org.osgi.service.cm.ConfigurationAdmin"%>%@page import="org.osgi.service.cm.Configuration"%>

Configuration conf = sling.getService(org.osgi.service.cm.ConfigurationAdmin.class).getConfiguration("com.tr.openweb.core");
String myProp = (String) conf.getProperties().get("page.properties.list");

But I want it in OSGI service class even I can get it in start method of Bundle Activator.

private ServiceRegistration ppcService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.DYNAMIC)
    private ConfigurationAdmin configAdmin;
    private Configuration conf = null;
public void start(BundleContext context) {
        LOG.info("Open Web Starting bundle", context.getBundle()
                .getSymbolicName());
        ServiceReference configurationAdminReference = context
                .getServiceReference(ConfigurationAdmin.class.getName());       

        configAdmin = (ConfigurationAdmin) context
                .getService(configurationAdminReference);

        if (null != configAdmin) {      

            try {
                conf = configAdmin.getConfiguration("Config PID");
                if (conf != null) {

                    LOG.info("****************** "

                            + conf.getProperties().get("page.properties.list"));

                }

            } catch (Exception ex) {
                ex.printStackTrace();            }

        }

Pom XML has entry for configuration admin Dependency and scr.mvn.plugin.

Any Idea?

 

Thanks Chandra

1 Accepted Solution

Avatar

Correct answer by
Level 2

Use:

 BundleContext bundleContext = FrameworkUtil.getBundle(MyClass.class).getBundleContext();
     ServiceReference<?> configurationAdminReference = 
                bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
     ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

Use:

 BundleContext bundleContext = FrameworkUtil.getBundle(MyClass.class).getBundleContext();
     ServiceReference<?> configurationAdminReference = 
                bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
     ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);