Expand my Community achievements bar.

SOLVED

AEM 6 SlingBindings object is null

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 7

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

View solution in original post

1 Reply

Avatar

Correct answer by
Level 7

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