Search code examples
javaosgideclarative-services

OSGi reading configurations


I am trying to code a OSGi bundle which can be initiated using multiple configurations. Purpose of my bundle is to rewrite static links in html and redirect it to a CDN URL. I am using org.apache.sling.rewriter.Transformer to achieve this.

@Component(metatype = true, label = "CDN Link Rewriter", configurationFactory = true, immediate = true)
@Service(value = TransformerFactory.class)
public class LinkTransformer implements Transformer,
    TransformerFactory {

@Property(label = "Static URL Extensions", value = "js,jpg,png,css,gif")
private static final String STATIC_FILES_EXTNS = "static_file_extn";

@Property(label = "Domain Path", value = "")
private static final String DOMAIN_PATH = "domain_path";

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

@Property(label = "Tags to check", value = "a,img,link,script")
private static final String TAGS_TO_CHECK = "tags_to_check";

@Property(label = "Attributes to check", d value = "src,href")
private static final String ATTRS_TO_CHECK = "attrs_to_check";

@Property(value = "append-version", propertyPrivate = true)
private static final String PIPELINE_TYPE = "pipeline.type";

@Property(value = "global", propertyPrivate = true)
private static final String PIPELINE_MODE = "pipeline.mode";

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

    this.update(props);
}

@Modified
protected void update(final Map<String, Object> props) {

}
public LinkTransformer() {

}
@Override
public void init(org.apache.sling.rewriter.ProcessingContext context,
        org.apache.sling.rewriter.ProcessingComponentConfiguration config)
        throws IOException {

}
@Override
public final Transformer createTransformer() {
    return new LinkTransformer();

}
//some other methods
}

Problem: I am unable to access my configurations in my bundle. I am able to create multiple sets of configurations in Felix console. But @Activate method is called only at the time of bundle installation. During Link transformation activty only init() method is being called. Hence I am unable to get hold of configurations. Can anyone tell me how to get configurations ?


Solution

  • The problem with above approach is implementing to different interfaces in same class. Thanks to @Balazs Zsoldos you can check the answer here

    Here, All I had to do was seperately implement Transformer and TransformerFactory.

    @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 StaticLinkTransformerFactory implements TransformerFactory {
    
    //all property declarations as in question
    private Map<String, Object> map;
    @Activate
    void activate(Map<String, Object> map) {
        this.map = map;
    }
    @Override
    public Transformer createTransformer() {
        return new StaticLinkTransformer(map);
    }
    }
    

    StaticLinkTransformer can be implemented as plain java class without any component or service annotations.