/**
   * Checks if the given {@linkplain JID} supports the requested feature. The JID may be non
   * resource qualified in which case all presences belonging to that JID are checked.
   *
   * <p>This method does <b>not</b> perform any I/O operation and will return immediately.
   *
   * <p><b>Please note the return value: <code>if(isFeatureSupported(foo, bar)) ... </code> is
   * likely to produce a {@link NullPointerException NPE}.</b>
   *
   * @param jid {@link JID} to query support for
   * @param namespace the namespace of the feature
   * @return <code>true</code> if the given feature is supported, <code>false</code> if it is not
   *     supported or <b><code>null</code> </b> if no information is available
   * @see #queryFeatureSupport(JID, String, boolean)
   */
  public Boolean isFeatureSupported(final JID jid, final String namespace) {
    checkJID(jid);

    Boolean supported = null;

    final List<JID> jidsToQuery = new ArrayList<JID>();

    if (jid.isBareJID()) jidsToQuery.addAll(rosterTracker.getAvailablePresences(jid));
    else jidsToQuery.add(jid);

    for (JID rqJID : jidsToQuery) {

      DiscoverInfoWrapper info = cache.get(rqJID.toString());
      if (info == null) continue;

      DiscoverInfo disco = info.item;

      if (disco == null) continue;

      supported = disco.containsFeature(namespace);

      if (supported) break;
    }

    return supported;
  }
  /**
   * Perform a service discovery and check if the given feature is among the features supported by
   * the given recipient. All registered listeners will be notified about the result.
   *
   * @param jid A RQ-JID (user@host/resource) of the user to query support for or a non RQ-JID to
   *     query all presences for this JID.
   * @blocking This method blocks until the ServiceDiscovery returns.
   * @reentrant This method can be called concurrently.
   * @caching If results are available in the cache, they are used instead of querying the server.
   */
  private Boolean queryFeatureSupport(JID jid, String namespace) {

    Boolean supported = null;

    checkJID(jid);

    DiscoverInfoWrapper wrapper;

    final List<JID> jidsToQuery = new ArrayList<JID>();

    if (jid.isBareJID()) jidsToQuery.addAll(rosterTracker.getAvailablePresences(jid));
    else jidsToQuery.add(jid);

    for (JID rqJID : jidsToQuery) {

      // add dummy
      synchronized (cache) {
        wrapper = cache.get(rqJID.toString());
        if (wrapper == null) {
          wrapper = new DiscoverInfoWrapper();
          cache.put(rqJID.toString(), wrapper);
        }
      }

      DiscoverInfo disco = null;

      // wait if there is already a discovery for the JID in progress
      synchronized (wrapper) {
        if (wrapper.isAvailable()) disco = wrapper.item;
        else {
          disco = wrapper.item = performServiceDiscovery(rqJID);
          if (disco != null) LOG.debug("Inserted DiscoveryInfo into Cache for: " + rqJID);
        }
      }

      // Null means that the discovery failed
      if (disco == null) {
        // and so we do not know if the feature is supported
        // notifyFeatureSupportUpdated(jid, namespace, false);
        continue;
      }

      notifyFeatureSupportUpdated(rqJID, namespace, disco.containsFeature(namespace));

      /*
       * loop through all presence regardless if we already know that the
       * feature is supported to notify the listener for every current
       * presence
       */
      if (supported != null) supported |= disco.containsFeature(namespace);
      else supported = disco.containsFeature(namespace);
    }

    return supported;
  }
  /**
   * Perform a ServiceDiscovery [1] and check if the given resource is among the features supported
   * by the given recipient.
   *
   * <p>[1] XEP-0030 http://xmpp.org/extensions/xep-0030.html
   *
   * @param jid The JID must have a resource identifier (user@host/resource), otherwise you get a
   *     blame StackTrace in your logs.
   * @return DiscoverInfo from recipient or null if an XMPPException was thrown.
   * @blocking This method blocks until the ServiceDiscovery returns.
   * @reentrant This method can be called concurrently.
   * @nonCaching This method does not use a cache, but queries the server directly.
   */
  private DiscoverInfo performServiceDiscovery(final JID jid) {

    if (jid.isBareJID()) {
      LOG.warn(
          "cannot perform service discovery on a non resource qualified jid: " + jid.toString(),
          new StackTrace());
      return null;
    }

    final Connection connection = connectionService.getConnection();

    if (connection == null) {
      LOG.warn("cannot not perform a service discovery because not connected to a XMPP server");
      return null;
    }

    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);

    try {
      return sdm.discoverInfo(jid.toString());
    } catch (XMPPException e) {

      LOG.warn(
          "Service Discovery failed on recipient "
              + jid.toString()
              + " server: "
              + connection.getHost(),
          e);

      /*
       * FIXME handle timeouts and error conditions differently ! see
       * http://xmpp.org/extensions/xep-0030.html#errors
       */
      return null;
    }
  }