AEM 6 SlingBindings object is null | Community
Skip to main content
New Participant
October 16, 2015
Solved

AEM 6 SlingBindings object is null

  • October 16, 2015
  • 1 reply
  • 1532 views

I have created sling:OsgiConfig nodes inside config folders like config.author, config.publish and so on. I am trying to fetch properties from these nodes by doing something like this:

public static List fetchTokenLinksFromOsgiConfig(final SlingHttpServletRequest slingRequest) throws IOException { List<String> tokenlinksList = new ArrayList<String>(); SlingBindings bindings = (SlingBindings) slingRequest.getAttribute(SlingBindings.class.getName()); log.info("=================inside fetchTokenLinksFromOsgiConfig======================"+bindings); SlingScriptHelper sling = bindings.getSling(); Configuration conf = sling.getService(org.osgi.service.cm.ConfigurationAdmin.class).getConfiguration("com.myuhc.TokenLinksConfig"); log.info("=================inside fetchTokenLinksFromOsgiConfig:::taking configurations======================"); String myuhcTokenId = (String) conf.getProperties().get("MyUHCTokenId"); String myuhcTokenSecret = (String) conf.getProperties().get("MyUHCTokenSecret"); String OAuthLink = (String) conf.getProperties().get("OAuthLink"); log.info("=================myuhcTokenId:::myuhcTokenSecret:::OAuthLink======================"+myuhcTokenId +" "+myuhcTokenSecret+" "+OAuthLink); if(!StringUtil.isEmpty(myuhcTokenId)) { tokenlinksList.add(myuhcTokenId); } if(!StringUtil.isEmpty(myuhcTokenSecret)) { tokenlinksList.add(myuhcTokenSecret); } if(!StringUtil.isEmpty(OAuthLink)) { tokenlinksList.add(OAuthLink); } return tokenlinksList; }

 

I am calling this method from a sling servlet like this: 

List tokenList = OsgiConfigUtil.fetchTokenLinksFromOsgiConfig(slingRequest);

but the bindings object of type SlingBindings is coming null. I have no idea how to work this out ?

Thanks in advance

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 Runal_Trivedi

Its hard to say why binding is coming up null, but if its null there is one more way of getting the service reference:

//serviceClass is the class object for which you need reference from OSGI BundleContext bundleContext = FrameworkUtil.getBundle(serviceClass).getBundleContext(); ServiceReference osgiRef = bundleContext.getServiceReference(serviceClass.getName()); serviceRef = (T) bundleContext.getService(osgiRef);

Imports are as follows:

import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference;

- Runal

1 reply

Runal_Trivedi
Runal_TrivediAccepted solution
New Participant
October 16, 2015

Its hard to say why binding is coming up null, but if its null there is one more way of getting the service reference:

//serviceClass is the class object for which you need reference from OSGI BundleContext bundleContext = FrameworkUtil.getBundle(serviceClass).getBundleContext(); ServiceReference osgiRef = bundleContext.getServiceReference(serviceClass.getName()); serviceRef = (T) bundleContext.getService(osgiRef);

Imports are as follows:

import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference;

- Runal