2

I have defined a service:

public interface TimeProvider {
     int getCurrentTime();
}

And an implementation:

@Component
@Provides
@Instantiate
public class FooTimeProvider implements TimeProvider {
    ...
}

In another class (actually an Activator class), I refer to the service like this:

@Component
@Provides
public class Activator implements BundleActivator {

    @Requires
    private TimeProvider timeProvider;

    ...

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        ...
        timeProvider.getCurrentTime();
    }
    ...
}

But I get a NullPointerException

at package.Activator.__M_start(Activator.java:60)

I installed Arch Gogo and these are the results:

ipojo:instances
Instance org.apache.felix.ipojo.IPOJOURLHandler-0 -> valid 
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -> valid 
Instance package.FooTimeProvider-0 -> valid 

ipojo:handlers
Handler org.apache.felix.ipojo:controller (VALID)
Handler org.apache.felix.ipojo:provides (VALID)
Handler org.apache.felix.ipojo:properties (VALID)
Handler org.apache.felix.ipojo:callback (VALID)
Handler org.apache.felix.ipojo:requires (VALID)
Handler org.apache.felix.ipojo:architecture (VALID)

ipojo:instance package.FooTimeProvider-0
instance name="package.FooTimeProvider-0" state="valid" bundle="16" component.type="package.FooTimeProvider"
handler name="org.apache.felix.ipojo:provides" state="valid"
    provides specifications="[anotherPackage.TimeProvider]" state="registered" service.id="44"
        property name="factory.name" value="package.FooTimeProvider"
        property name="instance.name" value="package.FooTimeProvider-0"
handler name="org.apache.felix.ipojo:architecture" state="valid"
object name="package.FooTimeProvider@6c7e1f14"

What am I missing?

(The byte code manipulation is done by an Ant task, btw)

Tim
  • 21
  • 2

1 Answers1

0

Instead of implementing BundleActivator just use:

@Validate
public void start() throws Exception {
    ...
    timeProvider.getCurrentTime();
}
Clement
  • 2,817
  • 1
  • 12
  • 11
  • OK, if I get this right the service will be available when the @Validate method is called. I thought the service would be injected when I first try to access it? So I won't be able to use it in the start method of the BundleActivator? – Tim Jun 05 '13 at 08:13