felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Neil Bartlett (JIRA)" <j...@apache.org>
Subject [jira] [Reopened] (FELIX-5213) ComponentContext.getServiceReference returns null though service registered
Date Wed, 30 Mar 2016 18:08:25 GMT

     [ https://issues.apache.org/jira/browse/FELIX-5213?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

Neil Bartlett reopened FELIX-5213:

Reopening on the advice on BJ, who makes the following comment:

"The component activate method is being activated because someone wants the service object.
Since the ServiceRegistration is passed to the ServiceFactory.getService method, SCR can certainly
provide the ServiceReference in the call to context.getServiceReference in the activate method."

> 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

View raw message