ResourceResolverFactory is NULL (Adobe Experience Manager AEM)

ぃ、小莉子 提交于 2019-12-23 14:59:33

问题


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:


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.



来源:https://stackoverflow.com/questions/27005919/resourceresolverfactory-is-null-adobe-experience-manager-aem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!