【发布时间】:2023-03-25 07:53:01
【问题描述】:
我是 OSGi 的新手,很快就被它的复杂性弄得不知所措。我相信这应该相当简单,但我无法找到一个完整的工作示例来说明我正在努力实现的目标。
我有一个 Java 类 Foo,其中包含一组服务。这些服务需要根据特定于Foo 的特定实例的值进行过滤。 Foo 可以有多个实例,但每个实例都应该有自己的一组过滤服务。
为了说明,请考虑以下示例(受Apache Felix tutorials 启发):
public interface DictionaryService {
public boolean check(String word);
}
@Component(property = "language=en")
public class EnglishDictionaryService implements DictionaryService {
private static final String[] WORDS = {"hi", "hello" /*...*/};
@Override
public boolean check(String word) {
if (word == null || word.isEmpty()) {
return true;
}
// super inefficient but you get the gist
return Arrays.stream(WORDS).anyMatch(entry -> word.equalsIgnoreCase(entry));
}
}
@Component(property = "language=en")
public class TexanDictionaryService implements DictionaryService {
private static final String[] WORDS = {"howdy" /*...*/};
//...
}
@Component(property = "language=en")
public class AustralianDictionaryService implements DictionaryService {
private static final String[] WORDS = {"g'day" /*...*/};
//...
}
@Component(property = "language=es")
public class SpanishDictionaryService implements DictionaryService {
private static final String[] WORDS = {"hola" /*...*/};
//...
}
@Component
public class SpellChecker {
@Reference
private volatile List<DictionaryService> dictionaryServices;
public SpellChecker(String language) {
// TODO: how to ensure my dictionaryServices match the given language code?
// dictionaryServices.target = "(language=" + language + ")"
}
public boolean check(String word) {
if (word == null || word.isEmpty()) {
return true;
}
List<DictionaryService> ds = dictionaryServices;
if (ds == null || ds.isEmpty()) {
return false;
}
return ds.stream().anyMatch(dictionary -> dictionary.check(word));
}
}
public static void main(String[] args) {
SpellChecker englishChecker = new SpellChecker("en");
SpellChecker spanishChecker = new SpellChecker("es");
// do stuff
}
阅读severalStackExchangeposts和一些otherarticles后,似乎可以使用ConfigurationAdmin来完成。但是,尚不清楚应该在何处以及如何使用ConfigurationAdmin,尤其是在声明式服务方面。我也反复阅读Configuration Admin Service Specification,但我很难应用这些概念。
谁能填补我理解的空白?
提前致谢!
编辑
Christian's answer 帮助我以不同的方式思考声明式服务。当我回顾我的研究时,我又遇到了Alan Hohn's blog posts on DZone。不幸的是,他似乎从未完成他承诺使用 DS 进行服务查找的系列。但是,他的example source code 包含以下内容:
public String greet(String language) {
BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
String filter = "(language=" + language + ")";
// Get ServiceReference(s) from OSGi framework, filtered for the specified value
ServiceReference[] refs = null;
try {
refs = context.getServiceReferences(Greeter.class.getName(), filter);
if (null != refs) {
Greeter greeter = (Greeter)context.getService(refs[0]);
return greeter.greet();
}
} catch (InvalidSyntaxException e) {
LOGGER.error("Invalid query syntax", e);
}
LOGGER.warn("No plugins found, making the default greeting");
return "Hello from the greeter manager!";
}
这看起来是一个可行的解决方案,但似乎没有使用 DS。这种方法有什么特别的考虑吗?我在 SO 和其他地方看到很多帖子声称 DS 是 BundleContext#getServiceReferences 的万灵药,所以我很好奇是否/如何将其重构为使用 DS。
【问题讨论】:
标签: java dynamic osgi declarative-services