How can I read factory configurations for my OSGI

2019-08-24 15:02发布

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 ?

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-24 15:41

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.

查看更多
登录 后发表回答