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.

你可能感兴趣的:(eclipse,mysql,IBM,osgi,Go)