0

I have a OSGi Transformer component which is instantiated by sling. In my OSGi component I have the following annotations :

@Component(configurationFactory = true, metatype = true, policy =       ConfigurationPolicy.REQUIRE, label = "CDN Link Rewriter", description = "Rewrites links to all static files to use configurable CDN")
@Service(value = TransformerFactory.class)
public class StaticLinkTransformer implements Transformer,
    TransformerFactory

I have some properties which I have annotated as @Property

@Property(label = "CDN Url prefix", description = "CDN URL prefix", value = "")
private static final String CDN_URL_PREFIX = "cdn_url_prefix";

Now I am able to provide multiple configurations for this class using "+" sign in felix console. If I have "N" number of configurations, sling is instantiating N objects of my StaticLinkRewriter class.

Question : How do I get the proper configurations for the object instantiated ? I mean, when sling instantiates my objects, how can i get the configurations for which the object was instantiated ?

Prashant Onkar
  • 414
  • 3
  • 15

1 Answers1

0

I think this component is not instantiated by Sling but by Declarative Services.

You can get the configuration if you implement the activate method. E.g.:

@Activate
void activate(ComponentContext ctx) {
   Dictionary configuration = ctx.getProperties();
   // use your configuration
}

For more information, see the 112 Declarative Services Specification chapter of OSGi Compendium specification.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • Thanks for you answer, but activate method will be called only when i add new configurations. But i want to get the configurations when the object of my class is instantiated. There is an init() method in the code which gets called when the object is instantiated. In that method I am unable to get the configurations : init method is below : `@Override public void init( org.apache.sling.rewriter.ProcessingContext context, org.apache.sling.rewriter.ProcessingComponentConfiguration config)` – Prashant Onkar Sep 15 '15 at 07:27
  • Who calls that init method? Are you sure that init method is called before activate method? I can imagine the following order: Object is instantiated by DS, activate method is called, service is registered, init method is called by other technology due to the OSGi service is registered. If you save your config into a member variable in the activate method, you can access it in the init method as well. – Balazs Zsoldos Sep 15 '15 at 08:15
  • 1. I am trying to implement a custom Link rewriter by implementing Sling Transformer. If i have 3 set of configurations for my custom link rewriter, sling is creating 3 instances of my class. After instant creation sling calls my init method. In this, @Activate is not coming into picture anywhere. Hence I am unable to access configurations. 2. Even if i save the configurations in static variable when Activate is called, i will end up getting same configurations in all my three instances which defeats the whole purpose of config factory. I think I am missing some big chunk over here. – Prashant Onkar Sep 15 '15 at 09:42
  • I said member variable, not static variable. Activate is not a static but a member function. So you can save it into a member variable. If you implement the TransformerFactory that instantiates your Transformer that is called (I do not know as that part is not in the sample code snippet), you can pass the configuration to the new Transformer instance. I do not think it is a good idea to implement TransformerFactory and Transformer within the same class (although I do not know what these interface mean) – Balazs Zsoldos Sep 15 '15 at 12:31
  • Your suugestion worked. I seperated implementation and factory and used a member variable. It is working now. Thanks. – Prashant Onkar Sep 18 '15 at 13:27