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

ResourceResolverFactory resolverFactory is null outside of 'Activate' method when implementing Transformer and Transformerfactory

Avatar

Level 4

Hi.

Maybe I need to break down my service into a few services, but here is whats going on.

I am needing to modify certain links generated inside of AEM. To do this, I am working with SAX and the Transformer and TransformerFactory interfaces.

Within my service's activate method, everything works fine. I have access to the referenced ResourceResolverFactory:

@Reference


private ResourceResolverFactory resolverFactory;

@Activate
protected void activate(Map<String, Object> properties) {

   log.error("[[[ Inside of Activate ]]]]");

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

  try {

  resolver = resolverFactory.getServiceResourceResolver(param);
   log.error("UserID => " + resolver.getUserID());
   } catch (LoginException e) {

   log.error("LoginException",e);
   } finally {

  resolver.close();
   }

}

(Sorry, the syntax highlter doesn't like the copy paste job.

If I run this same code, inside of my custom method, resolverFactory is null.

private void BuildUrl(){

   log.error("[[[ Inside of Build URL ]]]]");

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

  try {

  resolver = resolverFactory.getResourceResolver(param);
  
   log.error("UserID => " + resolver.getUserID());
  
   } catch (LoginException e) {

   log.error("LoginException",e);
   }

}

Any ideas why resolverFactory is not Null during the @activate method, and then null during my custom method?

1 Accepted Solution

Avatar

Correct answer by
Level 10

It does not need to be in a specific method.

See this article - we use it within a custom method in the implementation class --

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

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

It does not need to be in a specific method.

See this article - we use it within a custom method in the implementation class --

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

Avatar

Level 4

Right, I actually looked at that implementation many times when trying to debug this to see if I was doing anything different.

I'm not.

I'm wondering if it has anything to do with the context of where my code is being called.

Within my class, as i am implementing a transformer, I have the following method:

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {

   log.error("Inside of Start Element - local name of = " + localName);
   BuildUrl();
   contentHandler.startElement(uri, localName, qName, atts);
}

Once insde that method, and in turn my custom method buildUrl(), ResolverFactory is null .:\

Avatar

Level 10

Try a test - whitelist the bundle and see if the old getAdmin method works in a custom method.