0

I'm trying to configure the osgi-jax-rs-connector in my RAP application.

The README says to use the Configuration Admin Service for configuration.

ServiceReference caRef = context
    .getServiceReference(ConfigurationAdmin.class.getName());

The code above always returns null for the ServiceReference. What's the correct way to obtain a reference to the ConfigurationAdmin. Does another bundle needs to be started before?

RĂ¼diger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Saskia
  • 1,046
  • 1
  • 7
  • 23

2 Answers2

1

If you run Equinox please make sure that the Config Admin bundle (org.eclipse.equinox.cm) is installed and started.

Gunnar
  • 2,264
  • 17
  • 31
1

Trying to get a ServiceReference this way is setting yourself up for disaster. This code can't handle 99% of the cases of what happens in OSGi: the config admin might not be there, the config admin bundle is started after you, the config admin bundle is in another start level, the config admin bundle is stopped, and the config admin is updated. The core OSGi API is very powerful, and is used by much middleware, but should not ever be used for application code since it requires way to much understanding of the underlying model and is very error prone.

By far the easiest and most reliable solution is to use Declarative Services (DS) with the annotations:

 @Component
 public class MyClass implements MyService {
    ConfigurationAdmin ca;
    @Reference void setCA(ConfigurationAdmin ca) { this.ca = ca; }

    public void doMyService() {
       // ... you can safely use ca
    }
 }

And Gunnar might be right, maybe have not installed a Configuration Admin service in the first place. However, with your current snippet your code is going to blow up anyway at another time.

Peter Kriens
  • 15,196
  • 1
  • 37
  • 55