gpt4 book ai didi

java - OSGi 读取配置

转载 作者:行者123 更新时间:2023-11-30 07:57:01 25 4
gpt4 key购买 nike

我正在尝试编写一个可以使用多种配置启动的 OSGi 包。我的 bundle 的目的是重写 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
}

问题:我无法访问 bundle 中的配置。我可以在 Felix 控制台中创建多组配置。但 @Activate 方法仅在 bundle 安装时调用。在链接转换 Activity 期间,仅调用 init() 方法。因此我无法掌握配置。谁能告诉我如何获取配置?

最佳答案

上述方法的问题是在同一类中实现不同的接口(interface)。感谢@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 类,无需任何组件或服务注释。

关于java - OSGi 读取配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32529795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com