简体   繁体   English

OSGI-选择要从另一个Component类激活的捆绑软件

[英]OSGI - Choose which bundle to activate from another Component class

I want to develop OSGI storage service so I created an interface called Store . 我想开发OSGI存储服务,所以我创建了一个名为Store的接口。 Now, I have two classes that implement this interface. 现在,我有两个实现此接口的类。

SqlStorage.java SqlStorage.java

@Component(immediate = true)
@Service
public class SqlStorage implements Store {
    //some code here
}

MongoStorage.java MongoStorage.java

@Component(immediate = true)
@Service
public class MongoStorage implements Store {
    //some code here
}

Now, I have another bundle that depends on the Store . 现在,我还有另一个依赖于Store捆绑包。

@Component(immediate = true)
@Service
public class StoreManager implements StoreService {
    // how can I select one of the implmentation here
    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    public Store store;
}

How can the StoreManager choose which implementation of Store to be used? StoreManager如何选择要使用的Store实现?

Suppose, StoreManager has the ability to choose whether to use SQL or MongoDB as a storage. 假设StoreManager能够选择使用SQL还是MongoDB作为存储。

Is this kind of scenario doable? 这种情况可行吗?

How does your store manager want to decide which implementation of Store to use? 请问你的店经理决定使用哪种存储的实现? As you have currently defined them, the Store implementations are equivalent and equally good answers, and so OSGi will choose one arbitrarily. 正如您当前对它们的定义一样,Store实现是等效的且同样是不错的答案,因此OSGi将任意选择一个。

If that's not what you want then you need to add service properties to distinguish the Stores. 如果这不是您想要的,则需要添加服务属性以区分商店。 For example: 例如:

@Component(property = { "region=NorthAmerica", "language=en_US" })
@Component(property = { "region=Europe", "language=en_GB" })
@Component(property = { "region=Asia", "language=jp" })

Then your store manager can select the store it wants using a target filter. 然后,您的商店经理可以使用目标过滤器选择所需的商店。 For example if it wants an English-language store (but doesn't care about American vs British English) then it can use the following filter: 例如,如果它想要一家英语商店(但不关心美国英语和英国英语),则可以使用以下过滤器:

@Reference(target = "(language=en*)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM