/**
   * Adds a service listener to the given bundle context under the specified filter given as a
   * String. The method will also retrieve <em>all</em> the services registered before the listener
   * registration (that match the given filter) and will inform the listener through service events
   * of type <code>REGISTERED</code>.
   *
   * <p>This might cause problems if a service is registered between the listener registration and
   * the retrieval of existing services since the listener will receive two events for the same
   * service. For most listeners implementations however, this should not be a problem
   *
   * @param context bundle context to register the listener with
   * @param listener service listener to be registered
   * @param filter OSGi filter (given as a String) for registering the listener (can be <code>null
   *     </code>)
   * @see BundleContext#getServiceReference(String)
   * @see BundleContext#getServiceReferences(String, String)
   */
  public static void addServiceListener(
      BundleContext context, ServiceListener listener, String filter) {
    registerListener(context, listener, filter);

    // now get the already registered services and call the listener
    // (the listener should be able to handle duplicates)
    dispatchServiceRegistrationEvents(
        OsgiServiceReferenceUtils.getServiceReferences(context, filter), listener);
  }
  /**
   * Adds a service listener to the given application context, under the specified filter given as a
   * String. The method will also retrieve the <em>best matching</em> service registered before the
   * listener registration and will inform the listener through a service event of type <code>
   * REGISTERED</code>. This is the only difference from {@link #addServiceListener(BundleContext,
   * ServiceListener, Filter)} which considers all services, not just the best match.
   *
   * <p>This might cause problems if a service is registered between the listener registration and
   * the retrieval of existing services since the listener will receive two events for the same
   * service. For most listeners implementations however, this should not be a problem
   *
   * @param context bundle context to register the listener with
   * @param listener service listener to be registered
   * @param filter OSGi filter (given as a String) for registering the listener (can be <code>null
   *     </code>)
   * @see BundleContext#getServiceReference(String)
   * @see BundleContext#getServiceReferences(String, String)
   */
  public static void addSingleServiceListener(
      BundleContext context, ServiceListener listener, String filter) {
    registerListener(context, listener, filter);

    // now get the already registered services and call the listener
    // (the listener should be able to handle duplicates)
    ServiceReference ref = OsgiServiceReferenceUtils.getServiceReference(context, filter);
    ServiceReference[] refs = (ref == null ? null : new ServiceReference[] {ref});
    dispatchServiceRegistrationEvents(refs, listener);
  }