private static String readHostName(OMElement transport) {
   OMElement receiverHostName =
       transport.getFirstChildWithName(new QName(ConfigurationConstants.HOST_NAME_ELEMENT));
   String hostName = null;
   if (receiverHostName != null
       && receiverHostName.getText() != null
       && !receiverHostName.getText().trim().equals("")) {
     hostName = receiverHostName.getText();
   }
   if (hostName == null) {
     try {
       hostName = Utils.findAddress("localhost");
     } catch (SocketException e) {
       log.error("Unable to find the address of localhost.", e);
     }
   }
   return hostName;
 }
  private static DistributedConfiguration getDistributedConfiguration(OMElement processingElement) {

    DistributedConfiguration stormDeploymentConfig = new DistributedConfiguration();

    // Reading storm managers
    OMElement management =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.MANAGEMENT_ELEMENT));
    OMElement managers =
        management.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_MANAGERS_ELEMENT));
    Iterator<OMElement> iterator = managers.getChildElements();
    if (!iterator.hasNext()) {
      try {
        String hostName = Utils.findAddress("localhost");
        int port = 8904;
        stormDeploymentConfig.addManager(hostName, port);
        log.info(
            "No storm managers are provided. Hence automatically electing "
                + hostName
                + ":"
                + port
                + " node as "
                + "manager");
      } catch (SocketException e) {
        log.error(
            "Error while automatically populating storm managers. Please check the event-processor.xml"
                + " at CARBON_HOME/repository/conf",
            e);
        return null;
      }
    }
    while (iterator.hasNext()) {
      OMElement manager = iterator.next();
      String hostName =
          manager
              .getFirstChildWithName(new QName(ConfigurationConstants.HOST_NAME_ELEMENT))
              .getText();
      int port =
          Integer.parseInt(
              manager
                  .getFirstChildWithName(new QName(ConfigurationConstants.PORT_ELEMENT))
                  .getText());
      stormDeploymentConfig.addManager(hostName, port);
    }

    if (management.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_HEARTBEAT_INTERVAL_ELEMENT))
        != null) {
      stormDeploymentConfig.setManagementHeartbeatInterval(
          Integer.parseInt(
              management
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants
                              .DISTRIBUTED_NODE_CONFIG_HEARTBEAT_INTERVAL_ELEMENT))
                  .getText()));
    } else {
      log.info(
          "No heartbeat interval provided. Hence using default heartbeat interval "
              + stormDeploymentConfig.getManagementHeartbeatInterval());
    }
    if (management.getFirstChildWithName(
            new QName(ConfigurationConstants.RECONNECTION_INTERVAL_ELEMENT))
        != null) {
      stormDeploymentConfig.setManagementReconnectInterval(
          Integer.parseInt(
              management
                  .getFirstChildWithName(
                      new QName(ConfigurationConstants.RECONNECTION_INTERVAL_ELEMENT))
                  .getText()));
    } else {
      log.info(
          "No reconnection interval provided. Hence using default reconnection interval "
              + stormDeploymentConfig.getManagementReconnectInterval());
    }
    if (management.getFirstChildWithName(
            new QName(
                ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TOPOLOGY_RESUBMIT_INTERVAL_ELEMENT))
        != null) {
      stormDeploymentConfig.setTopologySubmitRetryInterval(
          Integer.parseInt(
              management
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants
                              .DISTRIBUTED_NODE_CONFIG_TOPOLOGY_RESUBMIT_INTERVAL_ELEMENT))
                  .getText()));
    } else {
      log.info(
          "No topology resubmit interval provided. Hence using default topology resubmit interval "
              + stormDeploymentConfig.getTopologySubmitRetryInterval());
    }

    // Reading transport
    OMElement transport =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.TRANSPORT_ELEMENT));
    OMElement portRange =
        transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_PORT_RANGE_ELEMENT));
    if (portRange != null) {
      stormDeploymentConfig.setTransportMaxPort(
          Integer.parseInt(portRange.getFirstChildWithName(new QName("max")).getText()));
      stormDeploymentConfig.setTransportMinPort(
          Integer.parseInt(portRange.getFirstChildWithName(new QName("min")).getText()));
    } else {
      log.info(
          "No port information provided. Hence using default port range "
              + stormDeploymentConfig.getTransportMinPort()
              + " - "
              + stormDeploymentConfig.getTransportMaxPort());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.RECONNECTION_INTERVAL_ELEMENT))
        != null) {
      stormDeploymentConfig.setTransportReconnectInterval(
          Integer.parseInt(
              transport
                  .getFirstChildWithName(
                      new QName(ConfigurationConstants.RECONNECTION_INTERVAL_ELEMENT))
                  .getText()));
    } else {
      log.info(
          "No transport reconnection interval provided. Hence using default reconnection interval "
              + stormDeploymentConfig.getTransportReconnectInterval());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CEP_RECEIVER_QUEUE_SIZE))
        != null) {
      int queueSize =
          Integer.parseInt(
              transport
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CEP_RECEIVER_QUEUE_SIZE))
                  .getText());

      if (isPowerOfTwo(queueSize)) {
        stormDeploymentConfig.setCepReceiverOutputQueueSize(queueSize);
      } else {
        // Disruptor queue size only allows powers of two
        throw new IllegalArgumentException(
            ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CEP_RECEIVER_QUEUE_SIZE
                + " must be a power of two.");
      }

    } else {
      log.info(
          "No CEP receiver output queue size specified. Hence using default queue size "
              + stormDeploymentConfig.getCepReceiverOutputQueueSize());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STORM_PUBLISHER_QUEUE_SIZE))
        != null) {
      int queueSize =
          Integer.parseInt(
              transport
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants
                              .DISTRIBUTED_NODE_CONFIG_STORM_PUBLISHER_QUEUE_SIZE))
                  .getText());

      if (isPowerOfTwo(queueSize)) {
        stormDeploymentConfig.setStormPublisherOutputQueueSize(queueSize);
      } else {
        // Disruptor queue size only allows powers of two
        throw new IllegalArgumentException(
            ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CEP_RECEIVER_QUEUE_SIZE
                + " must be a power of two.");
      }

    } else {
      log.info(
          "No storm publisher output queue size specified. Hence using default queue size "
              + stormDeploymentConfig.getStormPublisherOutputQueueSize());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_BUFFER_SIZE))
        != null) {
      int bufferSize =
          Integer.parseInt(
              transport
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_BUFFER_SIZE))
                  .getText());
      stormDeploymentConfig.setTransportPublisherTcpSendBufferSize(bufferSize);
    } else {
      log.info(
          "No TCP publisher buffer size not specified. Hence using default buffer size "
              + stormDeploymentConfig.getTransportPublisherTcpSendBufferSize());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_CHAR_SET))
        != null) {
      String tcpEventPublisherCharSet =
          transport
              .getFirstChildWithName(
                  new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_CHAR_SET))
              .getText();
      stormDeploymentConfig.setTransportPublisherCharSet(tcpEventPublisherCharSet);
    } else {
      log.info(
          "TCP event publisher Char-Set not set. Hence using default value "
              + stormDeploymentConfig.getTransportPublisherCharSet());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STORM_SPOUT_BUFFER_SIZE))
        != null) {
      int bufferSize =
          Integer.parseInt(
              transport
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STORM_SPOUT_BUFFER_SIZE))
                  .getText());
      stormDeploymentConfig.setStormSpoutBufferSize(bufferSize);
    } else {
      log.info(
          "No Storm Spout buffer size not specified. Hence using default buffer size "
              + stormDeploymentConfig.getStormSpoutBufferSize());
    }
    if (transport.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CON_STATUS_CHECK_INTERVAL))
        != null) {
      int connectionStatusCheckInterval =
          Integer.parseInt(
              transport
                  .getFirstChildWithName(
                      new QName(
                          ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CON_STATUS_CHECK_INTERVAL))
                  .getText());
      stormDeploymentConfig.setTransportPublisherConnectionStatusCheckInterval(
          connectionStatusCheckInterval);
    } else {
      log.info(
          "No transport connection status check interval specified. Hence using default interval "
              + stormDeploymentConfig.getTransportPublisherConnectionStatusCheckInterval()
              + "ms");
    }

    // Reading node info
    OMElement node =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_ELEMENT));
    if (node != null) {
      OMElement worker =
          node.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_WORKER_ELEMENT));
      if ("true"
          .equalsIgnoreCase(
              worker.getAttributeValue(new QName(ConfigurationConstants.ENABLE_ATTRIBUTE)))) {
        stormDeploymentConfig.setWorkerNode(true);
      }

      OMElement manager =
          node.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_MANAGER_ELEMENT));
      if ("true"
          .equalsIgnoreCase(
              manager.getAttributeValue(new QName(ConfigurationConstants.ENABLE_ATTRIBUTE)))) {
        stormDeploymentConfig.setManagerNode(true);
        String hostName =
            manager
                .getFirstChildWithName(new QName(ConfigurationConstants.HOST_NAME_ELEMENT))
                .getText();
        int port =
            Integer.parseInt(
                manager
                    .getFirstChildWithName(new QName(ConfigurationConstants.PORT_ELEMENT))
                    .getText());
        stormDeploymentConfig.setLocalManagerConfig(hostName, port);
      }

      OMElement presenter =
          node.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_PRESENTER_ELEMENT));
      if ("true"
          .equalsIgnoreCase(
              presenter.getAttributeValue(new QName(ConfigurationConstants.ENABLE_ATTRIBUTE)))) {
        stormDeploymentConfig.setPresenterNode(true);
        String hostName =
            presenter
                .getFirstChildWithName(new QName(ConfigurationConstants.HOST_NAME_ELEMENT))
                .getText();
        int port =
            Integer.parseInt(
                presenter
                    .getFirstChildWithName(new QName(ConfigurationConstants.PORT_ELEMENT))
                    .getText());
        stormDeploymentConfig.setLocalPresenterConfig(hostName, port);
      }
    } else {
      log.info(
          "No node type configurations provided. Hence using default node type configurations");
    }

    OMElement distributedUI =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_DISTRIBUTED_UI_URL_ELEMENT));
    if (distributedUI != null) {
      String url = distributedUI.getText();
      stormDeploymentConfig.setDistributedUIUrl(url);
    }

    OMElement memberUpdateCheckInterval =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_MEMBER_UPDATE_CHECK_INTERVAL));
    if (memberUpdateCheckInterval != null) {
      int interval = Integer.parseInt(memberUpdateCheckInterval.getText());
      stormDeploymentConfig.setMemberUpdateCheckInterval(interval);
    } else {
      log.info(
          "No member update check interval specified. Hence using default interval "
              + stormDeploymentConfig.getMemberUpdateCheckInterval());
    }

    // Get Jar name
    OMElement jar =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STORM_JAR_ELEMENT));
    stormDeploymentConfig.setJar(jar.getText());

    OMElement presentation =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_PRESENTATION_ELEMENT));
    if (presentation != null) {

      if (presentation.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_PRESENTER_QUEUE_SIZE))
          != null) {
        int queueSize =
            Integer.parseInt(
                presentation
                    .getFirstChildWithName(
                        new QName(
                            ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_PRESENTER_QUEUE_SIZE))
                    .getText());

        if (isPowerOfTwo(queueSize)) {
          stormDeploymentConfig.setPresentationOutputQueueSize(queueSize);
        } else {
          // Disruptor queue size only allows powers of two
          throw new IllegalArgumentException(
              ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_PRESENTER_QUEUE_SIZE
                  + " must be a power of two.");
        }
      } else {
        log.info(
            "No presentation output queue size provided. Hence using default queue size "
                + stormDeploymentConfig.getPresentationOutputQueueSize());
      }

      if (presentation.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_BUFFER_SIZE))
          != null) {
        int bufferSize =
            Integer.parseInt(
                transport
                    .getFirstChildWithName(
                        new QName(
                            ConfigurationConstants
                                .DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_BUFFER_SIZE))
                    .getText());
        stormDeploymentConfig.setPresentationPublisherTcpSendBufferSize(bufferSize);
      } else {
        log.info(
            "No TCP publisher buffer size not specified for presenter. Hence using default buffer size "
                + stormDeploymentConfig.getPresentationPublisherTcpSendBufferSize());
      }

      if (presentation.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_CHAR_SET))
          != null) {
        String tcpEventPublisherCharSet =
            transport
                .getFirstChildWithName(
                    new QName(
                        ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_TCP_PUBLISHER_CHAR_SET))
                .getText();
        stormDeploymentConfig.setPresentationPublisherCharSet(tcpEventPublisherCharSet);
      } else {
        log.info(
            "TCP event publisher Char-Set not set for presenter. Hence using default value "
                + stormDeploymentConfig.getPresentationPublisherCharSet());
      }

      if (presentation.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_CON_STATUS_CHECK_INTERVAL))
          != null) {
        int connectionStatusCheckInterval =
            Integer.parseInt(
                transport
                    .getFirstChildWithName(
                        new QName(
                            ConfigurationConstants
                                .DISTRIBUTED_NODE_CONFIG_CON_STATUS_CHECK_INTERVAL))
                    .getText());
        stormDeploymentConfig.setPresentationPublisherConnectionStatusCheckInterval(
            connectionStatusCheckInterval);
      } else {
        log.info(
            "No transport connection status check interval specified for presenter. Hence using default interval "
                + stormDeploymentConfig.getPresentationPublisherConnectionStatusCheckInterval()
                + "ms");
      }
    } else {
      log.info("No presentation configurations provided. Hence using default configurations");
    }

    // Reading Status Monitor Info
    OMElement statusMonitor =
        processingElement.getFirstChildWithName(
            new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STATUS_MONITOR_ELEMENT));
    if (statusMonitor != null) {
      OMElement lockTimeoutElement =
          statusMonitor.getFirstChildWithName(
              new QName(
                  ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STATUS_MONITOR_LOCK_TIMEOUT));
      if (lockTimeoutElement != null) {
        int lockTimeout = Integer.parseInt(lockTimeoutElement.getText());
        stormDeploymentConfig.setStatusLockTimeout(lockTimeout);
      } else {
        log.info(
            "No lockTimeout value specified in Status Monitor configurations. Hence using default lock timeout value: "
                + stormDeploymentConfig.getStatusLockTimeout()
                + " seconds.");
      }
      OMElement updateRateElement =
          statusMonitor.getFirstChildWithName(
              new QName(ConfigurationConstants.DISTRIBUTED_NODE_CONFIG_STATUS_MONITOR_UPDATE_RATE));
      if (updateRateElement != null) {
        int updateRate = Integer.parseInt(updateRateElement.getText());
        stormDeploymentConfig.setStatusUpdateInterval(updateRate);
      } else {
        log.info(
            "No updateRate value specified in Status Monitor configurations. Hence using default update rate: "
                + stormDeploymentConfig.getStatusUpdateInterval()
                + " milliseconds.");
      }
    } else {
      log.info(
          "No Status Monitor configurations provided. Hence using default Status Monitor configurations. Lock timeout: "
              + stormDeploymentConfig.getStatusLockTimeout()
              + " seconds, Update rate: "
              + stormDeploymentConfig.getStatusUpdateInterval()
              + " milliseconds.");
    }

    return stormDeploymentConfig;
  }