Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSGi Declarative Service - obtain ServiceReference in bind method

I'm using Eclipse 3.7 with Eclipse Gemini JPA / DBAccess. I implemented a declarative ServiceComponent which references EntityManagerBuilderFactory. The bind method looks like this:

public void bindEntityManagerFactoryBuilder(EntityManagerFactoryBuilder emfBuilder) {
    emfFactoryBuilders.add(emfBuilder);
}

However I need the ServiceReference to obtain the properties of this service, like:

    ServiceReference ref = ...
    String unitName = (String)ref.getProperty(EntityManagerFactoryBuilder.JPA_UNIT_NAME);

Do I have to search the ServiceRegistry for this ServiceReference, or is there a more elegant approach?

cheers, Muki

like image 798
Muki Avatar asked Dec 22 '22 09:12

Muki


2 Answers

If you are using Declarative Services 1.1 (the most recent spec version), you can change the signature of your bind method to:

public void bindEntityManagerFactoryBuilder(EntityManagerFactoryBuilder emfBuilder, Map properties)

The map will contain the service properties of the bound service.

like image 100
BJ Hargrave Avatar answered Apr 30 '23 12:04

BJ Hargrave


If you like to have access to ServiceReference object you can use this way (it's supported by 1.0 and 1.1 DS):

public void bindEntityManagerFactoryBuilder(ServiceReference reference)
like image 27
Dmytro Pishchukhin Avatar answered Apr 30 '23 11:04

Dmytro Pishchukhin