Hi
How I can initialize Resourceresolver reference variable without @reference annotation.
Solved! Go to Solution.
Views
Replies
Total Likes
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
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(); } } }
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.
I believe you have added bundle symbolic name, subServiceName and system user mapping like the below one -
You have created system user and given requisite permission also -
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:
If you can explain your use case, maybe it can be done without resource resolver.
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
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
@BrianKasingli ,
I guess, you missed something in url. Let me add correct url.
Views
Likes
Replies
Views
Likes
Replies