Pierre De Rop created FELIX-6890:
------------------------------------

             Summary: DM cache: getServiceReferences(Class, String) throws NPE 
on no match
                 Key: FELIX-6890
                 URL: https://issues.apache.org/jira/browse/FELIX-6890
             Project: Felix
          Issue Type: Bug
          Components: Dependency Manager
            Reporter: Pierre De Rop
            Assignee: Pierre De Rop


When the {{ServiceRegistryCache}} is enabled, the {{BundleContext}} injected 
into DependencyManager components is a {{BundleContextInterceptor}}. Its 
{{getServiceReferences(Class<S>, String)}} method throws a 
{{NullPointerException}} when no service matches, instead of returning an empty 
collection as required by the OSGi specification.

The method delegates to {{getServiceReferences(String, String)}}, which returns 
{{null}} when nothing matches, and wraps the result in {{Arrays.asList(...)}}. 
{{Arrays.asList(null)}} throws an NPE. This happens for both the indexed and 
the non-indexed code paths.

h3. Example

{code:java}
public class MyComponent {
    volatile BundleContext m_ctx; // injected by DM

void start() throws InvalidSyntaxException {
    // no Foo service matches (type=premium)
    Collection<ServiceReference<Foo>> refs =
        m_ctx.getServiceReferences(Foo.class, "(type=premium)");

    if (refs.isEmpty()) {
        // fall back to default behavior
    }
}
}
{code}

{noformat}
java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java)
    at java.util.Arrays$ArrayList.<init>(Arrays.java)
    at java.util.Arrays.asList(Arrays.java)
    at 
org.apache.felix.dm.impl.index.BundleContextInterceptor.getServiceReferences(BundleContextInterceptor.java:177)
{noformat}

The stack trace is illustrative and was not captured from a run.

h3. Expected behavior

{{BundleContext.getServiceReferences(Class, String)}} returns an empty 
collection when no service matches, whether or not the {{ServiceRegistryCache}} 
is enabled. The same component code must behave identically in both cases.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to