我正在尝试编写可以使用多个配置启动的OSGi包。我的包的目的是重写html中的静态链接并将其重定向到CDN URL。我正在使用org.apache.sling.rewriter.Transformer来实现这一目标。
@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
}
问题:我无法访问捆绑包中的配置。我可以在Felix控制台中创建多组配置。但是@Bctivate方法仅在捆绑安装时调用。在链接转换激活期间,仅调用init()方法。因此我无法掌握配置。谁能告诉我如何获得配置?
答案 0 :(得分:1)
上述方法的问题是实现同一类中的不同接口。感谢@Balazs Zsoldos,您可以查看答案here
在这里,我所要做的就是分别实施Transformer和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可以实现为普通的java类,不需要任何组件或服务注释。