1

バンドル間での OSGi でのサービスの登録/使用について質問があります。バンドル A - 複数の「ハンドラー」をマップ (ファクトリに似ています) にバインドするコア サービスと、追加のハンドラー インターフェイスを実装するバンドル B の 2 つのバンドルがあります。

私の期待は、他のバンドルがインターフェイスを実装でき、バンドル A の単一のサービスに登録できるため、そのコア サービスを呼び出すときに適切なハンドラーを検索して処理できることでした。ハンドラーの実装を他のバンドルから提供したい。

私はこの例に取り組んでいます:

SCR アノテーションを使用した複数 のカーディナリティ OSGI @References

コア バンドル内で、ハンドラーの実装が適切にバインドされます。私の他のバンドルでは、サービスはコアサービスにバインドされていないことによって登録されています。

だから、私の質問は、このようにバンドル A の他のバンドルにあるさまざまな実装とインターフェイスを登録できますか?

バンドル A - ハンドラー サービス:

@Component
@Service(value = IContentService.class)
public class ContentService implements IContentService
{
    @Reference(
        referenceInterface = IContentHandler.class,
        cardinality = ReferenceCardinality.MANDATORY_MULTIPLE,
        bind = Constants.OSGI_BIND_METHOD,
        unbind = Constants.OSGI_UNBIND_METHOD,
        policy = ReferencePolicy.DYNAMIC)
    private final Map<String, IContentHandler> handlers = Maps.newHashMap();


    protected void bind(final IContentHandler handler)
    {
        final Content h = handler.getClass().getAnnotation(Content.class);
        if (h != null)
        {
            final String id = getHandlerId(h);
            this.handlers.put(id, handler);
        }
    }

バンドル A - ハンドラの実装 (正常に登録):

@Component
@Service(value = IContentHandler.class)
@Content(name = "bean")
public class BeanContentHandler implements IContentHandler
{
    public IContentBean process(final Resource resource, final ContentOptions options) throws ContentException
    {
        ...

バンドル B - ハンドラの実装 (登録しない):

@Component
@Service(value = IContentHandler.class)
@Content(name = "sample")
public class SampleHandler implements IContentHandler
{
    @Override
    public IContentBean process(Resource resource, ContentOptions options) throws ContentException
    {
        ...
4

0 に答える 0