Highlighted

Error in getServiceResourceResolver method

Samer_Yadav

24-11-2018

Hi Experts,

We are creating one custom workflow to move assets from draft folder to final folder in AEM 6.4. When creating the resourceResolver object then getting below error. I have also attached the code snippet.

Error:

com.x.x.core.workflow.DraftToFinalMove exceptionb::::

java.lang.NullPointerException: null

at com.x.x.core.workflow.DraftToFinalMove.execute(DraftToFinalMove.java:72) [com.cox.core:0.0.1.SNAPSHOT]

at com.adobe.granite.workflow.core.job.HandlerBase.executeProcess(HandlerBase.java:198) [com.adobe.granite.workflow.core:2.0.168]

at com.adobe.granite.workflow.core.job.JobHandler.process(JobHandler.java:261) [com.adobe.granite.workflow.core:2.0.168]

at org.apache.sling.event.impl.jobs.JobConsumerManager$JobConsumerWrapper.process(JobConsumerManager.java:502) [org.apache.sling.event:4.2.10]

at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.startJob(JobQueueImpl.java:293) [org.apache.sling.event:4.2.10]

at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.access$100(JobQueueImpl.java:60) [org.apache.sling.event:4.2.10]

at org.apache.sling.event.impl.jobs.queues.JobQueueImpl$1.run(JobQueueImpl.java:229) [org.apache.sling.event:4.2.10]

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

Can anyone guide us like what we are missing.

Thanks

Samer

Replies

Highlighted

Samer_Yadav

24-11-2018

package com.x.x.core.workflow;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import javax.jcr.Node;

import javax.jcr.NodeIterator;

import javax.jcr.Session;

import javax.jcr.query.Query;

import javax.jcr.query.QueryManager;

import javax.jcr.query.QueryResult;

//import org.apache.felix.scr.annotations.Component;

import org.osgi.service.component.annotations.Component;

import org.apache.felix.scr.annotations.Properties;

import org.apache.felix.scr.annotations.Property;

import org.apache.felix.scr.annotations.Service;

import org.osgi.framework.Constants;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;

import com.adobe.granite.workflow.WorkflowSession;

import com.adobe.granite.workflow.exec.WorkItem;

import com.adobe.granite.workflow.exec.WorkflowProcess;

import com.adobe.granite.workflow.metadata.MetaDataMap;

import org.apache.felix.scr.annotations.Reference;

//Sling Imports

import org.apache.sling.api.resource.ResourceResolverFactory ;

import org.apache.sling.api.resource.ResourceResolver;

import com.day.cq.dam.api.AssetManager;

//This is a component so it can provide or consume services

@Properties({

    @Property(name = Constants.SERVICE_DESCRIPTION, value = "Draft to Final Move Assets Description"),

    @Property(name = Constants.SERVICE_VENDOR, value = "Adobe"),

    @Property(name = "process.label", value = "Draft to Final Move Assets Label") })

@Component(

    immediate = true,

    service = WorkflowProcess.class,

    configurationPid = "com.x.x.core.workflow.DraftToFinalMove"

)

public class DraftToFinalMove implements WorkflowProcess{

/** Default log. */

protected final Logger log = LoggerFactory.getLogger(this.getClass());

private Session session;         

//Inject a Sling ResourceResolverFactory

@Reference

private ResourceResolverFactory resolverFactory;

@Override

public void execute(WorkItem item, WorkflowSession wfsession,MetaDataMap args) throws WorkflowException {

log.info("**** execute method here");

Map<String, Object> param = new HashMap<String, Object>();

        param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");

        ResourceResolver resourceResolver = null;

try{

//get Current assinee from workflow

String currentAssinee = item.getCurrentAssignee();

log.info("currentAssinee::"+currentAssinee);

String copyPath = null;

String assetPath = null;

ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(param);

session = resourceResolver.adaptTo(Session.class);

com.adobe.granite.asset.api.AssetManager assetManager = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);

QueryManager qm = session.getWorkspace().getQueryManager();

Query q = qm.createQuery("/jcr:root/content/dam/dal/bos-workflow/bga/draft//*[jcr:primaryType='dam:Asset']", "xpath");

QueryResult result = q.execute();

NodeIterator nIt = result.getNodes();

StringBuilder output = new StringBuilder();

log.info("assetPath:2: ");

while (nIt.hasNext()) {

Node node = nIt.nextNode();

assetPath = "/content/dam/dal/bos-workflow/bga/draft/"+node.getName();

copyPath = "/content/dam/dal/bos-workflow/bga/final/"+node.getName();

assetManager.moveAsset(assetPath, copyPath);

}

// Log out

session.save();

session.logout();

log.info("Successfully Moved the Assets!!!");

}

catch(Exception e)

{

log.error("exceptionb::::",e);

}

}

}

Highlighted

vipins5188

24-11-2018

Sameer,

Have you created system user to get service resource resolver with name datawrite ??

if not make sure to create system user.

After creating system user you need to register your service using Apache Sling Service User Mapper Service

You can refer below article for creating system user for example ;

How to get Service Resource Resolver? – Rahul Mengji

Highlighted

Samer_Yadav

24-11-2018

Hi Vipin,

Yes i have configured the system user and also added the entry into user mapper, and restarted the instance but still same error.

it is failing at line

ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(param);

And throwing the same error.

Thanks

Samer

Highlighted

vipins5188

25-11-2018

Sameer,

Could you put a debugger and see if you are getting resolverFactory as null , if yes than keep setter method (like any other java normal getter/setter method) .

Or if resolverFactory is not null than it is an issue with your system user and its permissions. Assign system user to administrators group and try.

Highlighted

Feike_Visser1

Employee

25-11-2018

Also you are mixing annotations (osgi and felix)

import org.osgi.service.component.annotations.Component;

import org.apache.felix.scr.annotations.Properties;

import org.apache.felix.scr.annotations.Property;