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.
SOLVED

How to initialize ResourceResolver reference variable.

Avatar

Level 7

Hi 

How I can initialize Resourceresolver reference variable without @reference annotation.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @akshaybhujbale ,
Before you get ResourceResolver using service/system user. You must have
1. Must have service user created with needed permissions.
2. It should map with your bundle. if you use for specific project/operations.
You should not do any of these task manually. These should be done by code/configurations.
Created service user and adding permission can done by ACS Commons or Repository Initialization (repoinit). Recommended to use Repository Initialization (repoinit) for aem as a cloud service

 

Using ACS Commons- Create this OSGi config
com.adobe.acs.commons.users.impl.EnsureServiceUser
Or Using Repository Initialization
org.apache.sling.jcr.repoinit.RepositoryInitializer
Map service user to bundle using this OSGi config
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl

Once you are done with these configs. Get ResourceResolver using this service user

@Reference
ResourceResolverFactory resourceResolverFactory;

final Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put( ResourceResolverFactory.SUBSERVICE, "SERVICE_USER" );
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(paramMap);

Hope this will help



View solution in original post

7 Replies

Avatar

Employee Advisor

Are you looking for something like below one -

/**
 * 
 */
package com.aem.demo.core.services.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aem.demo.core.services.JcrUtility;

/**
 * @author debal
 *
 */
@Component(service = JcrUtility.class, immediate = true)
public class JcrUtilityImpl implements JcrUtility {

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

	@Reference
	ResourceResolverFactory resourceResolverFactory;

	@Override
	public ResourceResolver getResourceResolver() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(resourceResolverFactory.SUBSERVICE, "readWriteService");
		ResourceResolver serviceResourceResolver = null;
		try {

			serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(map);
		} catch (LoginException e) {
			logger.error("Could not get service user [ {} ]", "demoSystemUser", e.getMessage());
		}
		return serviceResourceResolver;

	}

	@Override
	public void closeResourceResolver(ResourceResolver resourceResolver) {
		if (Objects.nonNull(resourceResolver)) {
			resourceResolver.close();
		}

	}

}

Avatar

Level 7

Hi @DEBAL_DAS 

 

I have tried this code but getting nullpointer error at this line 

rerviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(map)

basically it is not initilizing resourceResolverFactory using @Refernece annotation.

Avatar

Employee Advisor

I believe you have added bundle symbolic name, subServiceName and system user mapping like the below one -

 

DEBAL_DAS_0-1654773965989.png

You have created system user and given requisite permission also -

DEBAL_DAS_1-1654774206597.png

 

Avatar

Community Advisor

Can you briefly explain your use-case. Like why do you need the resourceResolver outside model or without the @reference annotation.

This is usually not a good practive and created overhead.

 

Expanation:

https://stackoverflow.com/questions/41708758/aem-sling-how-to-create-resourceresolverfactory-in-a-st...

 

If you can explain your use case, maybe it can be done without resource resolver.

Avatar

Correct answer by
Community Advisor

Hi @akshaybhujbale ,
Before you get ResourceResolver using service/system user. You must have
1. Must have service user created with needed permissions.
2. It should map with your bundle. if you use for specific project/operations.
You should not do any of these task manually. These should be done by code/configurations.
Created service user and adding permission can done by ACS Commons or Repository Initialization (repoinit). Recommended to use Repository Initialization (repoinit) for aem as a cloud service

 

Using ACS Commons- Create this OSGi config
com.adobe.acs.commons.users.impl.EnsureServiceUser
Or Using Repository Initialization
org.apache.sling.jcr.repoinit.RepositoryInitializer
Map service user to bundle using this OSGi config
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl

Once you are done with these configs. Get ResourceResolver using this service user

@Reference
ResourceResolverFactory resourceResolverFactory;

final Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put( ResourceResolverFactory.SUBSERVICE, "SERVICE_USER" );
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(paramMap);

Hope this will help



Avatar

Community Advisor

@akshaybhujbale,

The best practice is to utilize the system user. If you like videos, take a look at this one. It's an easy to follow youtube video from AEM Geeks, https://www.youtube.com/watch?v=H_ljzX9B4Q