Example #1
0
  /**
   * Returns a channel from the ChannelManager's cache with the given name If multiple channels are
   * found, returns the most recently CREATED one. If two channels with the very same date exist,
   * avoid HUNGUP ones.
   *
   * @param name the name of the requested channel.
   * @return the (most recent) channel if found, in any state, or null if none found.
   */
  AsteriskChannelImpl getChannelImplByName(String name) {
    Date dateOfCreation = null;
    AsteriskChannelImpl channel = null;

    if (name == null) {
      return null;
    }

    synchronized (channels) {
      for (AsteriskChannelImpl tmp : channels) {
        if (tmp.getName() != null && tmp.getName().equals(name)) {
          // return the most recent channel or when dates are similar, the active one
          if (dateOfCreation == null
              || tmp.getDateOfCreation().after(dateOfCreation)
              || (tmp.getDateOfCreation().equals(dateOfCreation)
                  && tmp.getState() != ChannelState.HUNGUP)) {
            channel = tmp;
            dateOfCreation = channel.getDateOfCreation();
          }
        }
      }
    }
    return channel;
  }