Expand my Community achievements bar.

SOLVED

Null sling repository

Avatar

Level 8

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.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

2 Replies

Avatar

Level 10

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. 

Avatar

Correct answer by
Employee Advisor

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