Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Loading DS service from XML file(Chose when multiple implementation are available)
Loading DS service from XML file [message #1110915] Tue, 17 September 2013 13:53 Go to next message
ALex W is currently offline ALex WFriend
Messages: 56
Registered: July 2012
Member
Hello,

I have a simple problem. I think it will be simple for most advanced users.
Is it possible to chose, for a given service interface, which implementation to use when 2 or more implementation are available (from separate bundles) ?

I need this to be customizable by the user (I can do the UI associated, no issue with that). So it need to be stored (in an Xml file or something). I do not need warm change : restarting the application is no issues.

Anyone have any idea ?

Thanks
Re: Loading DS service from XML file [message #1110937 is a reply to message #1110915] Tue, 17 September 2013 14:28 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You can use an LDAP-Filter to query for the objectclass if those 2
services come from different bundles.

Tom

On 17.09.13 15:53, ALex W wrote:
> Hello,
>
> I have a simple problem. I think it will be simple for most advanced users.
> Is it possible to chose, for a given service interface, which
> implementation to use when 2 or more implementation are available (from
> separate bundles) ?
>
> I need this to be customizable by the user (I can do the UI associated,
> no issue with that). So it need to be stored (in an Xml file or
> something). I do not need warm change : restarting the application is no
> issues.
>
> Anyone have any idea ?
>
> Thanks
Re: Loading DS service from XML file [message #1111539 is a reply to message #1110937] Wed, 18 September 2013 11:02 Go to previous messageGo to next message
ALex W is currently offline ALex WFriend
Messages: 56
Registered: July 2012
Member
Hello,

I didn't know LDAP filter.
I was reading how it works, and it seems like it only means that a service requires a specific service (one that matches the filter).
Unless there is something I'm missing, this is not what I want.

The user should have the choice to select a service.
Let's say I want to communicate with a physical device (arduino, Raspberry), and there are multiple available protocols (Serial, USB, or Ethernet)

The specific controller for the Arduino or the Raspberry Pi can use any of the communication, it's only a choice of the user, depending on which connection he chose.

I don't know if I'm being clear, but there is no technical constraint in the choice of the implementation, except the physical setup.
Re: Loading DS service from XML file [message #1111570 is a reply to message #1111539] Wed, 18 September 2013 11:56 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You can query the OSGi-Registry using the LDAP-Filter so let's assume
you have a service named DeviceService and you contribute those through DS:

> <?xml version="1.0" encoding="UTF-8"?>
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="usb.deviceservice">
> <implementation class="usb.USBDeviceService"/>
> <service>
> <provide interface="services.DeviceService"/>
> </service>
> <property name="deviceType" type="String" value="USB"/>
> </scr:component>

> <?xml version="1.0" encoding="UTF-8"?>
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="serial.deviceservice">
> <implementation class="serial.SerialDeviceService"/>
> <service>
> <provide interface="services.DeviceService"/>
> </service>
> <property name="deviceType" type="String" value="Serial"/>
> </scr:component>


If you know want to find all services who have deviceType==Serial you
query for them as:

BundleContext bc = ...;
Collection<ServiceReference<DeviceService>> ref =
bc.getServiceReferences(DeviceService.class, "(deviceType=Serial)");


if you know let the user choose the deviceType in the UI and e.g. store
it into your preferences all parts can reach out to the preference and
find the appropriate services

Tom


On 18.09.13 13:02, ALex W wrote:
> Hello,
>
> I didn't know LDAP filter. I was reading how it works, and it seems like
> it only means that a service requires a specific service (one that
> matches the filter).
> Unless there is something I'm missing, this is not what I want.
> The user should have the choice to select a service. Let's say I want to
> communicate with a physical device (arduino, Raspberry), and there are
> multiple available protocols (Serial, USB, or Ethernet)
>
> The specific controller for the Arduino or the Raspberry Pi can use any
> of the communication, it's only a choice of the user, depending on which
> connection he chose.
>
> I don't know if I'm being clear, but there is no technical constraint in
> the choice of the implementation, except the physical setup.
Re: Loading DS service from XML file [message #1111575 is a reply to message #1111539] Wed, 18 September 2013 12:05 Go to previous message
Mihael Schmidt is currently offline Mihael SchmidtFriend
Messages: 81
Registered: August 2010
Member
Yes, you are missing some points.

If you are using OSGi DS you add a property to the service like "protocol" : "USB".

If you are using DS to get a reference you can add the service property to the target attribute or the reference element like: target="(protocol=USB)"

If you are using a ServiceTracker to get the service you can use the Filter to select the service. As you can dynamically build the filter criteria this enables you to select the service specified by the user.

http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/Filter.html?is-external=true

Hope this helps a bit.

Mihael
Previous Topic:Workbench.xmi not created correctly
Next Topic:How to keep track of toggle state of a Command
Goto Forum:
  


Current Time: Tue Apr 16 21:06:05 GMT 2024

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

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

Back to the top