Combining iPOJO and Configuration AdminThis page presents how creating, reconfiguring and destroying iPOJO component instance with the OSGi Configuration Admin. Table of Contents |
---|
maxLevel | 4 |
---|
minLevel | 2 |
---|
class | toc |
---|
|
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: - Create new component instances
- Configuring / reconfiguring these instances
- Destroying these instances
- Reconfiguring instances by using Managed Services (not addressed in this page, see here for further information)
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 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: - 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.
ExamplesThis 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. The archive contains both the project sources and a pre-configured version of felix. To compile the project, launch ant from the config.admin.tutorial directory Then, you can launch Felix by launching the following command from the felix-1.0.3 directory: Code Block |
---|
Java -jar bin/felix.jar
|
Note When felix asks for a profile name, enter any name PrerequisitesLet's take 4 Felix shell commands to manage configuration admin configurations (available in the example archive): - 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: Code Block |
---|
-> 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 (1.0.0)
[ 5] [Active ] [ 1] iPOJO Arch Felix Command (1.0.0)
[ 6] [Active ] [ 1] Apache Felix Configuration Admin Service (1.0.1)
|
Simple InstantiationImagine the following very simple component implementation: Code Block |
---|
|
public class Hello1 {
public Hello1() {
System.out.println("Hello");
}
}
|
The component type is defined with following metadata: Code Block |
---|
|
<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 (note that bundle need to be already compiled): Code Block |
---|
-> 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 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): Code Block |
---|
-> 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="org.apache.felix.ipojo.example.ca.component.Hello1@120cc56"
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. Then, we can delete the instance by removing the configuration from the configuration admin: Code Block |
---|
-> 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: Code Block |
---|
|
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: Code Block |
---|
|
<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. Let's play a simple scenario: - Create a Hello2 instance
- Update the instance configuration
- Kill the created instance
Code Block |
---|
-> 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. Obviously it is possible to create several instance of the same type: Code Block |
---|
-> 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 Info |
---|
title | Delete configurations |
---|
| 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 Code Block |
---|
|
<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. So create an instance of the Hello3 component type as follow: Code Block |
---|
-> create_conf hello3
Insert the configuration : {service.factoryPid=hello3}
|
Then, you can check provided services with the services 7 command Code Block |
---|
-> 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': Code Block |
---|
-> 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. Now we can remove this property by reconfiguring the instance with an empty configuration: Code Block |
---|
-> 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 users-subscribe@felix.apache.org; after subscribing, email questions or feedback to users@felix.apache.org |