Ejemplo n.º 1
0
  /**
   * get an available connection and set the boolean flag to true (unavailable) in the connection
   * pool. Also removes any stale connections, since this method gets called more than add or
   * remove.
   *
   * @param callerSecurityToken The security token of the caller
   * @param p_protocol The protocol for the connection
   * @param p_host The Hostname for the connection
   * @param p_port The port number for the connection
   * @return A stream connection element or null if not found
   */
  public synchronized StreamConnectionElement get(
      SecurityToken callerSecurityToken, String p_protocol, String p_host, int p_port) {

    StreamConnectionElement result = null;
    long c_time = System.currentTimeMillis();
    Enumeration cons = m_connections.elements();

    callerSecurityToken.checkIfPermissionAllowed(Permissions.MIDP);

    while (cons.hasMoreElements()) {
      StreamConnectionElement sce = (StreamConnectionElement) cons.nextElement();

      if ((c_time - sce.m_time) > m_connectionLingerTime) {
        if (!sce.m_in_use) {
          sce.close();
        } else {
          // signal returnToUse() to close
          sce.m_removed = true;
        }

        m_connections.removeElement(sce);
        continue;
      }

      if (p_host.equals(sce.m_host)
          && p_port == sce.m_port
          && p_protocol.equals(sce.m_protocol)
          && !sce.m_in_use) {
        result = sce;

        // do not break out so old connections can be removed
        continue;
      }
    }

    if (result != null) {
      result.m_in_use = true;
    }

    return result;
  }