Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » How to assign dynamic property to a service when using DS?(<property name="hostname" type="String" value="???"/>)
How to assign dynamic property to a service when using DS? [message #716374] Wed, 17 August 2011 08:34 Go to next message
Marton Sigmond is currently offline Marton SigmondFriend
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 Go to previous messageGo to next message
Peter Nehrer is currently offline Peter NehrerFriend
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 Go to previous message
Marton Sigmond is currently offline Marton SigmondFriend
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
Previous Topic:Controlling Update site category "Details"
Next Topic:Target Provisioning for multiple platforms in 3.7
Goto Forum:
  


Current Time: Fri Apr 19 07:08:33 GMT 2024

Powered by FUDForum. Page generated in 0.02826 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top