Florian Baierl
Florian Baierl

Reputation: 2481

OSGi annotations (Activate, Reference, Component) in Scala

I am trying to write an OSGi service in Scala (most other services/bundles are written in Java) and I struggle a bit with the syntax.

Normally in Java one can use the @Activate annotation on a constructor like this:

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

@Component(configurationPid = "PID", service=AgentService.class, immediate=true)
public class AgentServiceImpl implements AgentService {
   @Activate
    public AgentServiceImpl(@Reference Service1 service1, @Reference Service2 service2) {
  // ...
}

In Scala it should look somewhat like this:

import org.osgi.service.component.annotations.{Activate, Component, Deactivate, Reference}

@Component(
  configurationPid = "PID",
  service = Array(classOf[AgentService]),
  immediate = true)
class AgentServiceImpl @Activate() (@Reference service1: Service1, 
                                    @Reference service2: Service2) implements AgentService {
   // ...
}

When I try compiling this Scala code (with gradle) I geht the following error message:

error  : In component xxx.xxxx.xx.xx.agent.AgentServiceImpl , multiple references with the same name: service1. Previous def: xxx.xxxx.xx.xx.service.Service1, this def:
error  : In component xxx.xxxx.xx.xx.agent.AgentServiceImpl , multiple references with the same name: service2. Previous def: xxx.xxxx.xx.xx.service.Service2, this def:

Is this happening because my syntax concerning the annotations is wrong? I am particularly not too sure about this @Activate() bit. In Java I don't need to use brackets here - but it does not compile without in Scala.

Does anyone know a sample project trying to do something similar?

Upvotes: 2

Views: 934

Answers (1)

Florian Baierl
Florian Baierl

Reputation: 2481

I found the solution:

Compilation succeeds after adding val before the constructor paramters:

import org.osgi.service.component.annotations.{Activate, Component, Deactivate, Reference}

@Component(
  configurationPid = "PID",
  service = Array(classOf[AgentService]),
  immediate = true)
class AgentServiceImpl @Activate() (@Reference val service1: Service1, 
                                    @Reference val service2: Service2) implements AgentService {
   // ...
}

Probably this is because OSGi cannot properly deal with the automatically generated setter methods of service1 and service2.

Upvotes: 2

Related Questions