4
votes

I am trying to get a reference to the ResourceResolver from the ResourceResolverFactory as follows:

@Reference
private ResourceResolverFactory resourceResolverFactory;

public void someMethod() {
    Map<String, Object> authenticationMap = new HashMap<String, Object>();
        authenticationMap.put(ResourceResolverFactory.USER, "user");
        authenticationMap.put(ResourceResolverFactory.PASSWORD, "pwd");

        //This line returns NullPointerException
        ResourceResolver resourceResolver =   resourceResolverFactory.getResourceResolver(authenticationMap); 
}

Can someone please tell me what I am doing wrong? The AEM API version v6.0.

1
can share something about errorPravin
Have you marked this class with @Component/@Service annotation?Oleksandr Tarasenko
Pravin - It is a NullPointerException. I am not able to get the resourceResolverFactory reference. Alex - Yes, the class is marked as @ComponentRaj
Can I assume your component/service is not coming up? Have you verified that the ResourceResolverFactory component is actually up and running?3xil3

1 Answers

4
votes

So what I did was to create an Activator class that is called when the bundle is deployed and started. The Activator class then gets the instance of org.apache.sling.jcr.api.SlingRepository which we can use to connect to the JCR. Here is the activator code:

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate = true, label = "Commons Activator")
public class Activator implements BundleActivator { 

@Reference
private SlingRepository repository;

private static final Logger logger = LoggerFactory.getLogger(Activator.class);

@Activate
@Override
public void start(BundleContext context) throws Exception {
    logger.info(context.getBundle().getSymbolicName() + " started");

    //My own factory class instance
    ResourceResolverDiscoveryService rrf = ResourceResolverDiscoveryService.getInstance();
    //Set the 'repository' in your factory class instance
    rrf.setSlingRepositoryFactory(repository);
}

@Deactivate
@Override
public void stop(BundleContext context) throws Exception {
    logger.info(context.getBundle().getSymbolicName() + " stopped");
}

}

Then in the class where I want to use JCR to store the data I did the following:

public class StoreInJCR {
  public void store(Quote quote) throws LoginException, RepositoryException {
        SlingRepository slingRepository = ResourceResolverDiscoveryService.getInstance().getSlingRepositoryFactory();

        // GOT IT!!! Mission Accomplished
        Session session = slingRepository.loginAdministrative(null);
        Node root = session.getRootNode();
        // Further code
        .
        .
  }
}

Hope someone finds this useful.