JCR Session comes back null in custom Workflow step implementation | Community
Skip to main content
marissaw9851252
Level 3
October 16, 2015
Solved

JCR Session comes back null in custom Workflow step implementation

  • October 16, 2015
  • 11 replies
  • 4227 views

I have created a custom Workflow step for AEM 6.0.  I created a class that implements com.adobe.granite.workflow.exec.WorkflowProcess.  I need to get the jcr session and session.adaptTo(Session.class) is returning null.  session is not null and its type is com.adobe.granite.workflow.WorkflowSession.  The example in the Extending Workflow Functionality documentation uses this call to get the jcr session. http://docs.adobe.com/docs/en/aem/6-0/develop/extending/workflows/wf-extending.html

Below is the class declaration and the first part of the execute method.  This custom Workflow step is set up in place of the Product Asset Upload step at the end of the DAM Update Asset Workflow. 

@Component
@Service(value = WorkflowProcess.class)
@Properties({
 @Property(name = "process.label", value = "Custom Product Assets Upload Process"),
 @Property(name = Constants.SERVICE_DESCRIPTION, value = "Custom Product Assets Upload Process"),
 @Property(name = Constants.SERVICE_VENDOR, value = "Test")
})
public class CustomProductAssetsUploadProcess implements com.adobe.granite.workflow.exec.WorkflowProcess{
 
 private static final String TYPE_JCR_PATH = "JCR_PATH";
    private static final Logger log = LoggerFactory.getLogger(CustomProductAssetsUploadProcess.class);
    public static final String ENABLE_WRITEBACK = "writebackEnable";
 
    @Reference(cardinality= ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)
    private ResourceResolverFactory resourceResolverFactory;
   
    public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException {

        try {
           String payloadPath = null;

            if (item.getWorkflowData().getPayloadType().equals(TYPE_JCR_PATH)) {
                payloadPath = item.getWorkflowData().getPayload().toString();
            }
                       
            log.info("payload path :"+payloadPath);
                       
            Session jcrSession = session.adaptTo(Session.class);

...

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by WillMc1

Hmm...

Is there any chance you are embedding a dependency jar inside your bundle?  If you have your own copy of the javax.jcr.Session class file inside your bundle the classloaders could be messed up.  

Sorry again just a guess.

Will

11 replies

smacdonald2008
Level 10
October 16, 2015

As of AEM 6, you get a session instance (required to work with the JCR API) by using a Sling method named getServiceResourceResolver().


 The following code shows use of the more secure getServiceResourceResolverAPI call.

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

try {
           
    //Invoke the adaptTo method to create a Session used to create a QueryManager
resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);

This code uses a specific AEM account with JCR read and write privileges to access the AEM 6 JCR. For more infor, see this community article

http://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html