Getting error while trying to refer OSGi Service or ResourceResolverFactory inside Custom Sling Rewriter class | Community
Skip to main content
Hemalatha
Level 4
August 11, 2020
Solved

Getting error while trying to refer OSGi Service or ResourceResolverFactory inside Custom Sling Rewriter class

  • August 11, 2020
  • 2 replies
  • 1771 views

Hi all,

 

I am writing a custom sling rewriter which implements Transformer Class. Usually, we will use it to parse the html and rewrite the needed tag elements. In my case, I have to get some dynamic values by accessing JCR and need to rewrite them as URLs. I am not able to Refer OSGi service or ResourceResolverFactory inside transformer class. It gives me either 500 error or null pointer exception.

 

When I checked on samples in the web, they did a static rewrite. Could anyone help me to confirm whether shall I use OSGi services inside Custom Sling rewriter class.

 

Regards,

Hema

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 Vijayalakshmi_S

Hi @hemalatha,

Create a custom implementation for TransformerFactory and reference the desired OSGI service there. 

In the createTransformer() method of TransformerFactory, invoke your custom Transformer class (Passing the referenced OSGI service via Parameterized constructor)

Below sample skeleton for reference. (Haven't tried for actual implementation, below is just the sample structure)

CustomTransformerFactory.class

import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.rewriter.Transformer; import org.apache.sling.rewriter.TransformerFactory; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component(service = TransformerFactory.class, property = { "pipeline.mode=global", "pipeline.paths=/content/learnings", "pipeline-type=linkrewriter" }, immediate = true) public class CustomTransformerFactory implements TransformerFactory { @Reference //if this is not working try bind/unbind reference private ResourceResolverFactory rescFactory; @Override public Transformer createTransformer() { return new CustomTransformer(rescFactory); } }

CustomTransformer.class

public class CustomTransformer implements Transformer { private ResourceResolverFactory rescFactory; // This instance variable is initialized in the below constructor(as invoked from Factory) can then be used in the overridden methods of Transformer. public CustomTransformer(ResourceResolverFactory rescFactory) { this.rescFactory = rescFactory; } }

 

2 replies

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
August 11, 2020

Hi @hemalatha,

Create a custom implementation for TransformerFactory and reference the desired OSGI service there. 

In the createTransformer() method of TransformerFactory, invoke your custom Transformer class (Passing the referenced OSGI service via Parameterized constructor)

Below sample skeleton for reference. (Haven't tried for actual implementation, below is just the sample structure)

CustomTransformerFactory.class

import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.rewriter.Transformer; import org.apache.sling.rewriter.TransformerFactory; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component(service = TransformerFactory.class, property = { "pipeline.mode=global", "pipeline.paths=/content/learnings", "pipeline-type=linkrewriter" }, immediate = true) public class CustomTransformerFactory implements TransformerFactory { @Reference //if this is not working try bind/unbind reference private ResourceResolverFactory rescFactory; @Override public Transformer createTransformer() { return new CustomTransformer(rescFactory); } }

CustomTransformer.class

public class CustomTransformer implements Transformer { private ResourceResolverFactory rescFactory; // This instance variable is initialized in the below constructor(as invoked from Factory) can then be used in the overridden methods of Transformer. public CustomTransformer(ResourceResolverFactory rescFactory) { this.rescFactory = rescFactory; } }

 

arunpatidar
Community Advisor
Community Advisor
August 11, 2020

I think it should work, It is just like another osgi services

 

@8220494(service = TransformerFactory.class, name = "Custom Link Transformer", immediate = true, property = { "pipeline.type=append-version", "pipeline.mode=global" })

 

please let us know if it does not work.

Arun Patidar
Hemalatha
HemalathaAuthor
Level 4
August 12, 2020

This is my class, which is similar as you mentioned, seems the issue is that the existing code base is still using SCR annotations. Even If I declare OSGi R7 dependencies and tried to use it in my class. It's not getting listed in the OSGi components list if I use OSGI annotation instead of SCR annotations. (If I rewrite using SCR annotation, it's showing as OSGi component but I am not able to refer any OSGi service inside that class)

 

@8220494(property = { "pipeline.type=dmrewriter" }, service = { TransformerFactory.class })
public class DMUrlTransformerFactory implements TransformerFactory {
public Transformer createTransformer() {
return new DMRewriterTransformer();
}

}