How to assign dynamic property to a service when using DS? [message #716374] |
Wed, 17 August 2011 08:34 |
Marton Sigmond Messages: 73 Registered: July 2009 Location: Hungary |
Member |
|
|
Hi,
is there any way how I could assign a property dynamically to a declared service?
For example I declare the below service:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="remote.service.provider">
<implementation class="remote.service.provider.EchoService"/>
<service>
<provide interface="remote.service.IEchoService"/>
</service>
<property name="hostname" type="String" value="???"/>
</scr:component>
Wherever I deploy my bundle, the 'hostname' property should always be set to the actual host running the OSGi container.
Is there any way I could do that?
Or workaround?
Thanks,
Marton
Best Regards,
Marton Sigmond
Senior Software Engineer
|
|
|
(no subject) [message #716553 is a reply to message #716374] |
Wed, 17 August 2011 16:55 |
Peter Nehrer Messages: 241 Registered: July 2009 |
Senior Member |
|
|
Marton, you can use the ConfigurationAdmin service to configure these
properties on your components/services. If you don't have a default, you
can set up your component to require external configuration before it
can be activated (e.g., configuration-policy="require").
Then, of course, you need something else (like another component) to
start, read some external configuration (in your favorite format) and
use ConfigurationAdmin to create configurations for other components
that use or require them.
--Peter
On 8/17/11 4:34 AM, Marton Sigmond wrote:
> Hi,
>
> is there any way how I could assign a property dynamically to a declared
> service?
>
> For example I declare the below service:
> <?xml version="1.0" encoding="UTF-8"?>
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
> name="remote.service.provider">
> <implementation class="remote.service.provider.EchoService"/>
> <service>
> <provide interface="remote.service.IEchoService"/>
> </service>
> <property name="hostname" type="String" value="???"/>
> </scr:component>
>
> Wherever I deploy my bundle, the 'hostname' property should always be
> set to the actual host running the OSGi container.
>
> Is there any way I could do that?
> Or workaround?
>
> Thanks,
> Marton
>
|
|
|
Re: (no subject) [message #716815 is a reply to message #716553] |
Thu, 18 August 2011 14:11 |
Marton Sigmond Messages: 73 Registered: July 2009 Location: Hungary |
Member |
|
|
Thank you for pointing me to the right direction, Peter.
Since I have not found any decent article that would describe how to achieve this with the ConfigurationAdmin service, let me describe what I did:
How to use ConfigurationAdmin service with Declarative Services (DS)
After removing the dynamically provided property and setting configuration-policy="require", my component definition looked like this:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" configuration-policy="require" name="remote.service.provider">
<implementation class="remote.service.provider.EchoService"/>
<service>
<provide interface="remote.service.IEchoService"/>
</service>
</scr:component>
Then I created another bundle. The following code runs during the activation of this bundle:
ConfigurationAdmin confAdmin = getConfigurationAdminService();
Configuration config = confAdmin.createFactoryConfiguration("remote.service.provider");
Dictionary<String, String> props = new Hashtable<String, String>();
props.put("hostname", getHostName());
config.update(props);
This code creates one instance of the service, but multiple instances can be created by repeatably executing the above.
Finally, please add the org.eclipse.equinox.cm bundle to your target platform.
Cheers,
Marton
Best Regards,
Marton Sigmond
Senior Software Engineer
|
|
|
Powered by
FUDForum. Page generated in 0.02826 seconds