public OMElement getCEPPublisher(OMElement request) throws XMLStreamException {
    request.build();
    request.detach();

    OMElement tenantId =
        request.getFirstChildWithName(
            new QName(
                ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_TENANT_ID));
    OMElement executionPlan =
        request.getFirstChildWithName(
            new QName(
                ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_EXEC_PLAN));
    OMElement requesterIp =
        request.getFirstChildWithName(
            new QName(
                ManagerServiceConstants.NAMESPACE, ManagerServiceConstants.ELEMENT_REQUESTER_IP));
    String key = getKey(executionPlan.getText(), tenantId.getText());
    log.info("CEP Publisher requested for  " + key);

    Set<Endpoint> endpointSet = cepPublishers.get(key);
    Endpoint selectedEndpoint = selectEndpoint(endpointSet, requesterIp.getText());
    OMElement response =
        factory.createOMElement(
            ManagerServiceConstants.ELEMENT_CEP_PUBLISHER_RESPONSE, OMNamespace);
    if (selectedEndpoint != null) {
      OMElement hostNameElement =
          factory.createOMElement(ManagerServiceConstants.ELEMENT_HOST_NAME, OMNamespace);
      OMElement portElement =
          factory.createOMElement(ManagerServiceConstants.ELEMENT_PORT, OMNamespace);

      OMText hostNameText = factory.createOMText(hostNameElement, selectedEndpoint.getHostName());
      OMText portText =
          factory.createOMText(portElement, Integer.toString(selectedEndpoint.getPort()));

      hostNameElement.addChild(hostNameText);
      portElement.addChild(portText);
      response.addChild(hostNameElement);
      response.addChild(portElement);
      log.info(
          "Returning CEP Publisher:"
              + selectedEndpoint.getHostName()
              + ":"
              + selectedEndpoint.getPort());
    } else {
      log.warn("No CEP publishers registered " + key);
    }

    return response;
  }
  private Endpoint selectEndpoint(Set<Endpoint> endpointSet, String requesterIp) {
    Endpoint selectedEndpoint = null;

    if (endpointSet != null && !endpointSet.isEmpty()) {
      // If  there's a storm receiver/cep publisher in the same host as requester IP select it
      if ("".equals(requesterIp) == false) {
        for (Endpoint endpoint : endpointSet) {
          if (endpoint.getHostName().equals(requesterIp)) {
            selectedEndpoint = endpoint;

            if (log.isDebugEnabled()) {
              log.debug(
                  "Selecting"
                      + endpoint.toString()
                      + " since it's in the same host as the requester");
            }
            break;
          }
        }
      }
      // If there are no endpoints in the same host. Select the endpoint with lease number of
      // connections
      if (selectedEndpoint == null) {
        int minConnectionCount = Integer.MAX_VALUE;

        for (Endpoint endpoint : endpointSet) {
          if (log.isDebugEnabled()) {
            log.debug(
                "Endpoint "
                    + endpoint.toString()
                    + " has "
                    + endpoint.getConnectionCount()
                    + " connections.");
          }

          if (endpoint.getConnectionCount() < minConnectionCount) {
            minConnectionCount = endpoint.getConnectionCount();
            selectedEndpoint = endpoint;
          }
        }
      }

      selectedEndpoint.setConnectionCount(selectedEndpoint.getConnectionCount() + 1);
    }
    return selectedEndpoint;
  }