OSGi Declarative Services Introduction

Declarative Services is a declarative model which provides a publish/find/bind model for using services. DS is a new service from OSGi R4.

DS simplifies the task of authoring services by performing the work of registering services and handling service dependencies.

DS helps de-couple service bundles from the OSGi framework and other service bundles. Service bundles don't need to provide a BundleActivator class to collaborate with other through the service registry.

To provide DS, a DS description file is necessary. I usually use OSGI-INF/service.xml.

Here is an example to declare a service and its referencing service:

<?xml version="1.0" encoding="UTF-8"?>
<component name="AuthServiceImpl">
    <implementation class="com.ibm.csdl.scakm.auth.impl.AuthServiceImpl"/>
    <reference name="useradmin" interface="org.osgi.service.useradmin.UserAdmin" bind="setUserAdmin" unbind="unsetUserAdmin"/>
    <service>
        <provide interface="com.ibm.csdl.scakm.auth.AuthService"/>
    </service>
</component>

A DS component has the following major parts:
1. service provided to service registry with specified interface (optional)
2. service implementation class or class factory (mandatory)
2. service references with specified interface (optional)

A bit like SCA model, huh?

The component class must implement the declared interface and at bundle startup time, the implementation will be registered in service registry to provide the capability of the interface. Sometimes the component needs to use services provided by other compnents, so it declares to find and bind those services by specified interfaces at startup time.

In the example, the component is implemented by AuthServiceImpl class, and offers service with com.ibm.csdl.scakm.auth.AuthService interface, and it references another service by interface org.osgi.service.useradmin.UserAdmin which is an OSGi standard service.

Let's see how another service uses the service in the example:

<?xml version="1.0" encoding="UTF-8"?>
<component name="AdminConsole">
    <implementation class="com.ibm.csdl.scakm.console.AdminCommandProvider"/>
    <reference name="auth" interface="com.ibm.csdl.scakm.auth.AuthService" bind="setAuthService" unbind="unsetAuthService"/>
</component>

Here the component doesn't provide a service to outside world, but only uses a service with interface com.ibm.csdl.scakm.auth.AuthService. At startup time, DS will resolve the reference between these two components, and set the AuthServiceImpl class instance to AdminCommandProvider.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值