|
Page Edited :
FELIX :
Combining iPOJO and Configuration Admin
Combining iPOJO and Configuration Admin has been edited by Clement Escoffier (Apr 08, 2008). Content:This page presents how creating, reconfiguring and destroying iPOJO component instance with the OSGi Configuration Admin. Configuration AdminThe 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 iPOJOAs 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:
The configuration admin is persistent, so stored configuration will be reload it the framework restarts. Combining iPOJO and the Configuration AdminiPOJO 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:
ExamplesThis section presents 3 examples about the management of iPOJO instances with the configuration admin:
All these examples are downloadable here Java -jar bin/felix.jar Note When felix asks for a profile name, enter any name
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 (1.0.3)
[ 1] [Active ] [ 1] Apache Felix Shell Service (1.0.0)
[ 2] [Active ] [ 1] Apache Felix Shell TUI (1.0.0)
[ 3] [Active ] [ 1] Apache Felix Bundle Repository (1.0.2)
[ 4] [Active ] [ 1] iPOJO (0.7.6.SNAPSHOT)
[ 5] [Active ] [ 1] iPOJO Arch Felix Command (0.7.6.SNAPSHOT)
[ 6] [Active ] [ 1] Apache Felix Configuration Admin Service (1.0.1)
Simple InstanciationImagine the following very simple 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). -> start file:..\config.admin.tutorial\output\config.admin.tutorial.jar
-> create_conf hello1
Insert the configuration : {service.factoryPid=hello1}
-> Hello
Note: Debug messages from the configuration admin were removed -> arch Instance ArchCommand -> valid *Instance hello1.e40fe80a-2c0d-4c51-b00b-a82565874cd8 -> valid* -> -> arch -instance hello1.e40fe80a-2c0d-4c51-b00b-a82565874cd8 instance name="hello1.e40fe80a-2c0d-4c51-b00b-a82565874cd8" component.type="hello1" state="valid" bundle="7" object name="[EMAIL PROTECTED]" handler name="org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler" state="valid" handler name="org.apache.felix.ipojo.handlers.architecture.ArchitectureHandler" state="valid" -> So, the instance is correctly created. The name of the instance was created by the configuration admin. It could change according to your configuration admin implementation. -> delete_conf hello1.e40fe80a-2c0d-4c51-b00b-a82565874cd8 Delete the configuration : hello1.e40fe80a-2c0d-4c51-b00b-a82565874cd8 -> arch Instance ArchCommand -> valid So, arch does no more displayed any hello instances, the created instance was disposed. Reconfiguring instances with the Configuration AdminImagine 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 transition="validate" 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.
-> create_conf hello2 to=ipojo
Insert the configuration : {service.factoryPid=hello2, to=ipojo}
Created configuration: hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3
Hello ipojo
-> list_conf
hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3 : {service.pid=hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3, service.factorypid=hello2, to=ipojo}
-> update_conf hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3 to=felix
Update: pid=hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3
Update the configuration : {to=felix}
Hello felix
-> delete_conf hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3
Delete the configuration : hello2.75082279-9b4b-4c49-b0e0-8efb38b67aa3
Good by felix
-> list_conf
In this simple scenario, we see that when the configuration is updated, 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. -> create_conf hello2 to=ipojo
Insert the configuration : {service.factoryPid=hello2, to=ipojo}
Hello ipojo
-> create_conf hello2 to=felix
Insert the configuration : {service.factoryPid=hello2, to=felix}
Hello felix
-> arch
Instance ArchCommand -> valid
Instance hello2.aaf1927c-1a81-490d-bd7b-21b13d454987 -> valid
Instance hello2.9344fdbe-c35e-4afc-b839-f7ad0ea59a9d -> valid
The 'arch' command displays the two created instances. Note you can delete all created configurations with the delete_conf all command Property PropagationIt is possible to propagate the instance configuration to the published service properties. To activate property propagation you need to write the 'propagation' attribute in the 'properties' element as in <component classname="org.apache.felix.ipojo.example.ca.component.Hello3" factory="hello3" architecture="true"> <provides/> <properties propagation="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. -> create_conf hello3
Insert the configuration : {service.factoryPid=hello3}
Then, you can check provided services with the services 7 command -> services 7
// Factories and Managed Service factories //
----
factory.name = hello3
instance.name = hello3.a5ca5901-6e20-4636-8805-fbca2db1d68b
objectClass = org.apache.felix.ipojo.example.ca.service.Hello
service.factoryPid = hello3
service.id = 69
->
Now, we update the instance configuration with a new property 'p1': -> update_conf hello3.a5ca5901-6e20-4636-8805-fbca2db1d68b p1=v1
Update the configuration : {p1=v1}
-> services 7
config.admin.tutorial (7) provides:
// Factories and Managed Service factories //
----
factory.name = hello3
instance.name = hello3.a5ca5901-6e20-4636-8805-fbca2db1d68b
objectClass = org.apache.felix.ipojo.example.ca.service.Hello
p1 = v1
service.factoryPid = hello3
service.id = 69
Remark that the new property p1 is published. -> update_conf hello3.a5ca5901-6e20-4636-8805-fbca2db1d68b
Update the configuration : {}
-> services 7
ConfigAdminExample (8) provides:
// Factories and Managed Service factories //
----
factory.name = hello3
instance.name = hello3.a5ca5901-6e20-4636-8805-fbca2db1d68b
objectClass = org.apache.felix.ipojo.example.ca.service.Hello
service.factoryPid = hello3
service.id = 69
The service does no more publish the p1 property. ConclusionThis page has presented how to combine the configuration admin and iPOJO. Subscribe to the Felix users mailing list by sending a message to [[EMAIL PROTECTED]]; after subscribing, email questions or feedback to [[EMAIL PROTECTED]] |
Unsubscribe or edit your notifications preferences
