You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This page presents how creating, reconfiguring and destroying iPOJO component instance with the OSGi Configuration Admin.

Configuration Admin

The Configuration Admin service is a configuration manager describe in the OSGi R4 Compendium. It allows an operator to set the configuration information of deployed applications.  The Configuration Admin defines the Configuration as the process of defining the configuration data of applications and assuring that those applications receive that data when they are running. The Configuration Admin is becoming an important piece on OSGi Gateway. It is become the standard way to configure applications on OSGi gateways.

Why using Configuration Admin with iPOJO

As the configuration admin offer an administration support, it seems reasonable to combine iPOJO and the Configuration Admin to control the gateway. Indeed, thanks to the configuration admin it should be possible to:

  • Create new component instances
  • Configuring / Reconfiguring these instances
  • Destroying these instances 

The configuration admin is persistent, so stored configuration will be reload it the framework restarts.
Moreover, using the configuration admin allows avoiding describing instances inside iPOJO metadata file. These instances are created by inserting new configurations in the configuration admin.

Combining iPOJO and the Configuration Admin

iPOJO has a component type concept. For each (public) component type, a ManagedServiceFactory is published. For each configurations matching with the component type from the Configuration Admin, a new component instance is created. Moreover, when this configuration is updated, the instance is dynamically reconfigured. If the configuration is removed, the instance is disposed.

If a new Configuration is created:

  • If the factory is available or an instance is create immediately,
  • Else the factory is not available and the instance will be created as soon as the factory appears.

Examples

This section presents 3 examples about the management of iPOJO instances with the configuration admin:

  • A simple instantiation example and destruction
  • An instantiation with property injection and dynamic reconfiguration
  • A property propagation example

All these examples are downloadable here.

Prerequisites

Let's take 4 Felix shell commands to manage configuration admin configurations:

  • Create_conf <type> <property-key=property-value>* allows to create a new Factory Configuration attached to the given type. The configuration contains the given properties.
  • Update_conf <configuration_name> < property-key=property-value>* allows to update the configuration with the given name with the given properties.
  • Delete_conf <configuration_name> allows deleting the configuration with the given name. If the name is 'all', delete all stored configurations.
  • List_conf allows listing all stored configuration.

Moreover iPOJO and an implementation of the Configuration Admin must be deployed on the gateway:

-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (0.9.0.incubator-SNAPSHOT)
[   1] [Active     ] [    1] Apache Felix Shell Service (0.9.0.incubator_SNAPSHOT)
[   2] [Active     ] [    1] Apache Felix Shell TUI (0.9.0.incubator_SNAPSHOT)
[   3] [Active     ] [    1] Apache Felix Bundle Repository (0.9.0.incubator_SNAPSHOT)
[   4] [Active     ] [    1] Apache Felix Configuration Admin Service (0.9.0.incubator_SNAPSHOT)
[   5] [Active     ] [    1] iPOJO (0.7.1.incubator_SNAPSHOT)
[   6] [Active     ] [    1] OSGi R4 Compendium Bundle (4)
[   7] [Active     ] [    1] javax.servlet-2.3 (0)

Simple Instantiation

Imagine the following dummy component implementation:

public class Hello1 {
    public Hello1() {
        System.out.println("Hello");
    }
}

The component type is defined with following metadata:

<component classname="org.apache.felix.ipojo.example.ca.component.Hello1" factory="hello1" immediate="true" architecture="true"/>

The defined component type(Hello1) just creates a Hello1 object when the instance is created (thanks to the immediate attribute).
So if we deploy this bundle and add a consistent configuration we obtain:

-> start file:/F:\dev\workspaces\iPOJO_dev\ConfigAdminExample\ConfigAdminExample.jar
-> create_conf hello1
Insert the configuration : {service.factoryPid=hello1}
Hello

So as predicted, the Hello message appears. To be really sure of the creating, we can ask for the instance architecture (the component type allows architecture introspection thank to the architecture attribute):

-> arch
Instance hello1.02f683ef-85b2-44fc-8ef5-719d40557c07 -> valid
->
-> arch hello1.02f683ef-85b2-44fc-8ef5-719d40557c07
instance name="hello1.02f683ef-85b2-44fc-8ef5-719d40557c07" state="valid" bundle="8" component.type="hello1"
	handler name="org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler" state="valid"
	handler name="org.apache.felix.ipojo.handlers.architecture.ArchitectureHandler" state="valid"
	object name="org.apache.felix.ipojo.example.ca.component.Hello1@16925b0"
->

So, the instance is correctly created. The name of the instance was created from the given one by the configuration admin. It could change according to your configuration admin implementation.

Note : The arch command is available on the Felix trunk

Then, we can delete the instance by removing the configuration from the configuration admin:

-> delete_conf hello1.02f683ef-85b2-44fc-8ef5-719d40557c07
Delete the configuration : hello1.02f683ef-85b2-44fc-8ef5-719d40557c07
-> arch
->

So, arch does no more displayed any instance, the created instance was disposed.

Reconfiguring instances with the Configuration Admin

Imagine the following component implementation:

public class Hello2 {
     String m_name;
    public void stop() {
        System.out.println("Good by " + m_name);
    }
    public void setName(String newName) {
        m_name = newName;
        System.out.println("Hello " + m_name);
    }

And the following metadata:

<component classname="org.apache.felix.ipojo.example.ca.component.Hello2" factory="hello2" immediate="true" architecture="true">
	<callback initial="VALID" final="INVALID" method="stop"/>
	<properties>
		<property field="m_name" name="to" method="setName"/>
	</properties>
</component>

The defined component type (Hello2) write "Hello + $name" when the property 'to' (attached to the field _m_name_) receive a new value. A value is necessary insert in the instance configuration. Moreover when killed, the instance will display a "Good By" message.
Let's play a simple scenario:

  • Create an Hello2 instance
  • Update the instance configuration
  • Kill the created instance
-> create_conf hello2 to=clement
Insert the configuration : {service.factoryPid=hello2, to=clement}
Hello clement
-> list_conf
hello2.e41713c3-9ff8-4bbb-a128-97d267962e58 : {service.pid=hello2.e41713c3-9ff8-4bbb-a128-97d267962e58, service.factorypid=hello2, to=clement}
-> update_conf hello2.e41713c3-9ff8-4bbb-a128-97d267962e58 to=Rick
Update the configuration : {service.pid=hello2.e41713c3-9ff8-4bbb-a128-97d267962e58, service.factorypid=hello2, to=Rick}
Hello Rick
-> delete_conf hello2.e41713c3-9ff8-4bbb-a128-97d267962e58
Delete the configuration : hello2.e41713c3-9ff8-4bbb-a128-97d267962e58
Good by Rick

In this simple scenario, we see that when updated the attached configuration, the instance receives the new value. The setName method is immediately invoked to inject the new value. Moreover, when the configuration is deleted, the instance is going to be killed: the "Good Bye" message appears and the instance is disposed.
Obviously it is possible to create several instance of the same type :

-> create_conf hello2 to=clement
Insert the configuration : {service.factoryPid=hello2, to=clement}
 Hello clement
-> create_conf hello2 to=rick
Insert the configuration : {service.factoryPid=hello2, to=rick}
 Hello rick
-> arch
Instance hello2.3d64ae54-98ae-43f4-845a-15956b2a78ce -> valid
Instance hello2.b5e3fc82-25bf-4191-bb56-f5afe516e2fa -> valid

The 'arch' command displays the two created instances.

Property Propagation

It is possible to propagate the instance configuration to the published service properties. To activate property propagation you need to write the 'configurable' attribute in the 'properties' element as in

<component classname="org.apache.felix.ipojo.example.ca.component.Hello3" factory="hello3" architecture="true">
	<provides/>
	<properties configurable="true">
		<property field="m_name" value="clement"/>
	</properties>
</component>

The defined type provides a service. Moreover it supports properties propagation. So all property, except listed one (m_name), will be published inside the provided services.
Imagine that we have created an instance of the previous component. Service information about this instance are:

factory.pid = hello3
objectClass = org.apache.felix.ipojo.example.ca.service.Hello
service.factoryPid = hello3
service.id = 287
service.pid = hello3.e964c4e0-029f-4e49-a7cd-a4d8880f20c1

Now, we update the instance configuration with a new property 'p1':

-> update_conf hello3.e964c4e0-029f-4e49-a7cd-a4d8880f20c1 p1=v1
Update the configuration : {p1=v1}
-> Remove : {service.pid=hello3.e964c4e0-029f-4e49-a7cd-a4d8880f20c1, service.factoryPid=hello3}
-> services 8
ConfigAdminExample (8) provides:
----
factory.pid = hello3
objectClass = org.apache.felix.ipojo.example.ca.service.Hello
p1 = v1
service.factoryPid = hello3
service.id = 287
service.pid = hello3.e964c4e0-029f-4e49-a7cd-a4d8880f20c1

Remark that the new property p1 is published.
Now we can remove this property by reconfiguring the instance with an empty configuration:

-> update_conf hello3.e964c4e0-029f-4e49-a7cd-a4d8880f20c1
Update the configuration : {}
-> services 8
ConfigAdminExample (8) provides:
----
factory.pid = hello3
objectClass = org.apache.felix.ipojo.example.ca.service.Hello
service.factoryPid = hello3
service.id = 287
service.pid = hello3.e964c4e0-029f-4e49-a7cd-a4d8880f20c1

The service does no more publish the p1 property.

Conclusion

This page has presented how to combine the configuration admin and iPOJO. If you have any comment or question, do not hesitate to send me an email

  • No labels