Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

How to get the New way of ResourceResolver object from Sling Mapper service

Avatar

Level 2

Hi ,

I need some help on where I am making the mistake in getting the ResourceResolver Object using the Apache Sling Service User Mapper Service.

I am trying to get the ResourceResolver Object from the Java service using the mapper service.

I am getting NULL Pointer Exception .

I had pasted the code snippet of the Service class below.

I had pasted the code snippet of the WCMUsePojo class below.

Created the System User with full permission for /content folder.

And mapper the System user with the service name and bundleID in the below format.

1566145_pastedImage_1.png

ServiceMapping= telstrahealth.core:datawrite=datawrite-service

BundleID or SymbolicName of the bundle folder= telstrahealth.core

Subservice Name=datawrite

System User created/mapped=datawrite-service

Please help to identify which step i am missing, as I am getting NULL Pointet Exception.

Any pointers in this regards is much appreciated.

thanks.

Sunil

Code snippet  of my WCMUsePojo class is below:

@Component(service=WCMUsePojo.class)

public class SolutionsComponent extends WCMUsePojo

{

   private final Logger logger = LoggerFactory.getLogger(getClass());

  private final String QUERY_PROP = "_property";
  private final String QUERY_PROP_VAL = "_property.value";

   List<TagCountBean> tagList = null;
   List<ProductTagCountBean> tagList2 = null;
  public void activate()

  {

  TagService tagSrvc= getSlingScriptHelper().getService(TagService.class);
   logger.info("tagSrvctagSrvctagSrvctagSrvctagSrvc"+tagSrvc);
   ResourceResolver resolver=tagSrvc.getResolverObject();

   logger.info("Inside Activate method---->>>");

}

}

I wrote a TagServiceImpl java service class  in the project.

Below is the code snippet:

import com.au.telstrahealth.core.service.TagService;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.osgi.service.component.annotations.*;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.ComponentContext;
import java.util.*;
import javax.jcr.Session;



//This is a component so it can provide or consume services
@Component(service=TagService.class,immediate = true)

public class TagServiceImpl implements TagService {

  @Reference

  ResourceResolverFactory resFactory;
  private Session adminSession;
  private final Logger logger = LoggerFactory.getLogger(getClass());

   //Define a class member named key
   private int key = 0 ;


   ResourceResolver resolver;


  public ResourceResolver getResolverObject(){

   try{

  Map<String, Object> param = new HashMap<String, Object>();
   param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");

   logger.info("ResourceResolver inside TagServiceImpl Just before getting in to try block--");
   resolver = resFactory.getServiceResourceResolver(param);

   logger.info("ResourceResolver in TagServiceImpl class associated System User--"+resolver.getUserID());


   }catch(Exception e){

  logger.info("ERROR" + e + e.getStackTrace().toString());

   e.printStackTrace();

   }

   return resolver;

   }

}

4 Replies

Avatar

Level 4

Did you ever figure this out?

Avatar

Community Advisor

Hey Guys,

Could you please make sure you followed all 3 steps to get service user session/resource

Step 1: Use in-built system user in OSGI configuration. In OSGI select Apache Sling Service User Mapper Service

e.g. AEM63App.core:readService=training-user

Step 2: Create system user(training-user) and assign that system user the permissions to access the desire folder/paths.

Step 3: In code, do below, as example:

Session session = null;
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put(ResourceResolverFactory.SUBSERVICE, "readService");
try {
resourceResolver = resourceFactory.getServiceResourceResolver(paramMap);
// logger.debug("User id inside {}", resourceResolver.getUserID());
session = resourceResolver.adaptTo(Session.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.debug("getSubserviceSession : Unable to Login : " + e);
}

Adobe Experience Manager Help | Querying Adobe Experience Manager 6 data using the Sling getServiceR...



Arun Patidar

Avatar

Level 1

This link seems expired (404) is there a new link? I'm trying to access the jcr from an apache felix osgi component.