Search code examples
eclipseosgibindservice-referencedeclarative-services

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


Solution

  • 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.