/** Match a set of connections from the pool */
  public ManagedConnection matchManagedConnections(
      Set connectionSet, Subject subject, ConnectionRequestInfo info) throws ResourceException {
    boolean trace = log.isTraceEnabled();

    // Get cred
    info = getInfo(info);
    JmsCred cred = JmsCred.getJmsCred(this, subject, info);

    if (trace) log.trace("Looking for connection matching credentials: " + cred);

    // Traverse the pooled connections and look for a match, return first
    // found
    Iterator connections = connectionSet.iterator();

    while (connections.hasNext()) {
      Object obj = connections.next();

      // We only care for connections of our own type
      if (obj instanceof JmsManagedConnection) {
        // This is one from the pool
        JmsManagedConnection mc = (JmsManagedConnection) obj;

        // Check if we even created this on
        ManagedConnectionFactory mcf = mc.getManagedConnectionFactory();

        // Only admit a connection if it has the same username as our
        // asked for creds

        // FIXME, Here we have a problem, jms connection
        // may be anonymous, have a user name

        if ((mc.getUserName() == null
                || (mc.getUserName() != null && mc.getUserName().equals(cred.name)))
            && mcf.equals(this)) {
          // Now check if ConnectionInfo equals
          if (info.equals(mc.getInfo())) {

            if (trace) log.trace("Found matching connection: " + mc);

            return mc;
          }
        }
      }
    }

    if (trace) log.trace("No matching connection was found");

    return null;
  }
  /* (non-Javadoc)
   * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set, javax.security.auth.Subject, javax.resource.spi.ConnectionRequestInfo)
   */
  public ManagedConnection matchManagedConnections(
      @SuppressWarnings("rawtypes") Set connectionSet,
      Subject subject,
      ConnectionRequestInfo cxRequestInfo)
      throws ResourceException {
    log(Level.FINEST, "matchManagedConnections");

    if ((null != connectionSet) && !connectionSet.isEmpty()) {
      for (Object con : (Set<?>) connectionSet) {
        if (con instanceof ManagedConnectionImpl) {
          ManagedConnectionImpl conn = (ManagedConnectionImpl) con;
          ConnectionRequestInfo otherCxRequestInfo = conn.getCxRequestInfo();
          if (null == otherCxRequestInfo || otherCxRequestInfo.equals(cxRequestInfo)) {
            return conn;
          }

        } else {
          log(Level.WARNING, "Unexpected element in list: " + con);
        }
      }
    }
    // null is interpreted as "create new one" by the container
    return null;
  }