[
https://issues.apache.org/jira/browse/FELIX-5213?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15210281#comment-15210281
]
David Jencks commented on FELIX-5213:
-------------------------------------
Well, the service certainly _doesn't_ exist yet, since the activate method has
not yet completed so it certainly hasn't been exposed to anything.
I don't see how to fix this. We get the ServiceReference to return from the
ServiceRegistration, however in your scenario the consuming component is
obtaining the service from the service reference before the registerService
call has returned the ServiceRegistration. Is there another way to reliably
find the ServiceReference for a particular service?
> ComponentContext.getServiceReference returns null though service registered
> ---------------------------------------------------------------------------
>
> Key: FELIX-5213
> URL: https://issues.apache.org/jira/browse/FELIX-5213
> Project: Felix
> Issue Type: Bug
> Components: Declarative Services (SCR)
> Affects Versions: scr-2.0.2
> Reporter: Neil Bartlett
>
> In a component that provides a service and is not immediate, the service is
> registered before activation. Therefore the service reference of the
> registered service should be available to the component, from
> ComponentContext.getServiceReference(), during activation. However the
> following code sample throws NPE:
> {code}
> import org.osgi.framework.Constants;
> import org.osgi.framework.ServiceReference;
> import org.osgi.service.component.ComponentContext;
> import org.osgi.service.component.annotations.Activate;
> import org.osgi.service.component.annotations.Component;
> @Component(service = Object.class, property = "name=scrbug")
> public class Example {
> @Activate
> void activate(ComponentContext context) {
> ServiceReference<?> reference = context.getServiceReference();
> System.out.printf("My service ID is %d%n",
> reference.getProperty(Constants.SERVICE_ID));
> }
> }
> {code}
> Note that the service certainly exists, because I have injected it into a
> second component, in order to force activation of the above.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)