More on OSGi DS

Bundle activation/deactivation

Now that bundles don't need to provide BundleActivator class, we still have opportunities to do something at bundle activation/deactivation time by implementing the following methods in component class:

    protected void activate(ComponentContext ctxt) {
        //initialze something...
    }
    
    protected void deactivate(ComponentContext ctxt) {
        //release resources...
    }

DS surely use reflection to invoke them at the right time.

DS properties

Some componenets need to retrieve customized properties/configurations at runtime. There are the following ways:
1. retrieve from OSGi Configuration Admin service. Equinox hasn't released Configration Admin service bundle.
2. properties specified in the component description. For example:

<component name="MySQLAccess">
    <implementation class="com.ibm.csdl.scakm.db.mysql.MySQLAccess"/>
    <service>
        <provide interface="com.ibm.csdl.scakm.db.DBAccess"/>
    </service>
    <properties entry="OSGI-INF/db.properties" />
</component>

Here, properties element points to a property file which can hold properties. Then in code the properties could be accessed by ComponentContext.getProperties(). For example:

    protected void activate(ComponentContext ctxt) {
            jdbcURL = (String) ctxt.getProperties().get("jdbcURL");
            user = (String) ctxt.getProperties().get("user");
            password = (String) ctxt.getProperties().get("password");
    }

The cons of this way is that the property file has to be packaged in the bundle and it's not convinent to modify it.

Actually we can specify properties in config.ini file, and in applications use BundleContext.getProperty() to retrieve them. However, this approach makes the properties global visible to all bundles.


Some tips

1. remember to set DS description property in bundle manifest file:
Service-Component: OSGI-INF/service.xml

2. In development, remember to put DS description file into binary build in build.properties so that the exported bundle jar can include the file:
bin.includes = META-INF/,\
               console.jar,\
               OSGI-INF/

3. Even DS cannot find required service reference for a bundle, the bundle can still run with activated status. Check console output to make sure no error happens.

4. Service implmenation class will only have one instance in an OSGi runtime, which is to be registered in service registry. If the service component should have multiple instances, each of which is created when a service is needed, a kind of factory component is provided by DS. Never used it, so just go to OSGi specification.

5. Equinox will cache bundle jar files so if bundles get updated and something wierd happens, try to delete configuration/org.eclipse.osgi/ directory and restart again. Avoid update bundle with same version number, and therefore upgrade version numbers as applications are developed.
 
6. Read OSGi specification to understand all the things in deep dive.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值