Null sling repository | Community
Skip to main content
Level 8
October 16, 2015
Solved

Null sling repository

  • October 16, 2015
  • 2 replies
  • 883 views

I have a class defined as follows:

@Component @Service public class CalendarServiceImpl implements CalendarService { static private final org.slf4j.Logger logger = LoggerFactory.getLogger(CalendarServiceImpl.class); @Reference private SlingRepository repository; public void bindRepository(SlingRepository repository) { this.repository = repository; } private Session _session; private Session getSession() { if (_session == null || !_session.isLive()) { try { _session = this.repository.loginAdministrative(null); } catch (RepositoryException e) { logger.error(e.getMessage()); } } return _session; } }

When i try to instantiate the class using the following code from a JSP: 

CalendarService calendarService = sling.getService(CalendarService.class);

And call a method which uses the Session variable, i receive and error telling me that the "repository" variable is NULL and therefore the component doesn't function as intended.

The strangest part about this is that it works fine on our Author instance, but doesn't work on the Publish instance.  

Any help would be appreciated.

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 joerghoh

Do you use the maven-scr-plugin to translate the annotations properly to meta information for the bundle?

Btw: in your code you don't need the bindRepository method, as it you just explicitly stated the default implementation for it :-)

Jörg

2 replies

smacdonald2008
Level 10
October 16, 2015

To get a Session within an OSGi bundle, you should inject a ResourceResolver into your bundle:

//Invoke the adaptTo method to create a Session 

ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);

 

For information on how to do this and call it from a JSP -- see:

http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

Hope this helps. 

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

Do you use the maven-scr-plugin to translate the annotations properly to meta information for the bundle?

Btw: in your code you don't need the bindRepository method, as it you just explicitly stated the default implementation for it :-)

Jörg