/**
   * Get all configuration for a specified server type placed in repository. This would search the
   * servicemgr category inside repository and apply XPath to figure out configuration files
   * belonging to a specific server type.
   *
   * @param type
   * @return List containing file names.
   * @throws ProcessingException
   */
  public static List<String> getConfigCategory(String type) throws ProcessingException {

    NVPair[] pairs = null;
    try {
      pairs =
          RepositoryManager.getInstance()
              .listMetaData(ServerManagerFactory.MGR_CONFIG_CATEGORY, false, XML_FILE_SUFFIX);

      if (pairs == null || pairs.length == 0) {
        Debug.log(
            Debug.NORMAL_STATUS,
            "No XML file found in repository folder ["
                + ServerManagerFactory.MGR_CONFIG_CATEGORY
                + "]");

        return null;
      }
    } catch (Exception e) {
      Debug.error(
          "An exception occured while listing xml files from repository folder ["
              + ServerManagerFactory.MGR_CONFIG_CATEGORY
              + "]");

      Debug.logStackTrace(e);

      throw new ProcessingException(
          "An exception occured while listing xml files from repository folder  ["
              + ServerManagerFactory.MGR_CONFIG_CATEGORY
              + "]");
    }

    List<String> cfgFileLst = new ArrayList<String>();
    String fileNm = null;
    try {
      for (int j = 0; j < pairs.length; j++) {
        fileNm = pairs[j].name;
        Document document = getDocument(fileNm);
        ParsedXPath parseXpath = new ParsedXPath(serverType2Xpath.get(type));

        if (Debug.isLevelEnabled(Debug.NORMAL_STATUS))
          Debug.log(Debug.NORMAL_STATUS, " --> Evaluating file :" + fileNm + " for type :" + type);

        if (parseXpath.getBooleanValue(document.getDocumentElement())) cfgFileLst.add(fileNm);
      }

      if (Debug.isLevelEnabled(Debug.NORMAL_STATUS))
        Debug.log(
            Debug.NORMAL_STATUS,
            " --> returning list of files for type[" + type + "] : " + cfgFileLst);

      return cfgFileLst;
    } catch (Exception e) {
      Debug.log(
          Debug.NORMAL_STATUS,
          "Failed to parse and evaluate XPath on file :" + fileNm + "\n" + Debug.getStackTrace(e));

      throw new ProcessingException(
          "Failed to parse and evaluate XPath on file :" + fileNm + "\n" + Debug.getStackTrace(e));
    }
  }
  /**
   * @param serverType
   * @param classNm
   * @param fileFilterXpath
   * @throws FrameworkException
   */
  public static void addServerMapping(String serverType, String classNm, String fileFilterXpath)
      throws FrameworkException {

    try {
      Class.forName(classNm);
    } catch (Exception exp) {
      Debug.warning("Unable to load class :" + classNm);
      Debug.logStackTrace(exp);

      throw new FrameworkException("Unable to load class :" + classNm);
    }

    serverType2ClassNm.put(serverType, classNm);

    serverType2Xpath.put(serverType, fileFilterXpath);
  }
  /*
   * (non-Javadoc)
   *
   * @see com.nightfire.comms.servicemgr.ServerManagerBase#initialize()
   */
  @Override
  public void initialize() throws ProcessingException {

    if (Debug.isLevelEnabled(Debug.NORMAL_STATUS))
      Debug.log(Debug.NORMAL_STATUS, "initializing poll comm server : " + getType());

    // read configuration from repository
    // String categoryConfig =
    // ServerManagerFactory.getInstance().getConfigCategory(getType());
    String categoryConfig = ServerManagerFactory.MGR_CONFIG_CATEGORY;
    // String metaConfig =
    // ServerManagerFactory.getInstance().getConfigMeta(getType());

    List<String> configCategory = ServerManagerFactory.getInstance().getConfigCategory(getType());
    XMLMessageParser fileParser = null;

    Document aggregatedDoc;
    try {
      aggregatedDoc = XMLLibraryPortabilityLayer.getNewDocument(getType(), null);
    } catch (MessageException me) {
      Debug.logStackTrace(me);
      throw new ProcessingException(me);
    }

    ServiceIdFilter idFilter = new ServiceIdFilter();
    for (String fileNm : configCategory) {

      String xmlDescription;
      try {
        xmlDescription = RepositoryManager.getInstance().getMetaData(categoryConfig, fileNm);
        fileParser = new XMLMessageParser(xmlDescription);
        fileParser = idFilter.getFilteredDOM(fileParser, ServiceMgrConsts.POLL_COMM_SERVER_CONFIG);
        Element document = fileParser.getDocument().getDocumentElement();
        Node node = aggregatedDoc.importNode(document, true);
        aggregatedDoc.getDocumentElement().appendChild(node);

      } catch (Exception e) {
        Debug.error(
            "Unable to load and parse configuration from repository "
                + categoryConfig
                + " "
                + fileNm);
        throw new ProcessingException(e);
      }

      NodeList list = fileParser.getDocument().getElementsByTagName(ELEM_POLL_COMM_SERVER);
      for (int Ix = 0; Ix < list.getLength(); Ix++) {
        if (list.item(Ix) != null) {
          Element pollCommServerElement = (Element) list.item(Ix);

          String id, key, value, start, pollCommServerType;

          id =
              getConfigurationValue(
                  pollCommServerElement, ConfigType.ATTRIBUTE, ATTR_COMM_SERVER_ID, null);
          key =
              getConfigurationValue(
                  pollCommServerElement, ConfigType.ELEMENT, ELEM_COMM_SERVER_KEY, null);
          value =
              getConfigurationValue(
                  pollCommServerElement, ConfigType.ELEMENT, ELEM_COMM_SERVER_TYPE, null);
          start =
              getConfigurationValue(
                  pollCommServerElement, ConfigType.ATTRIBUTE, ATTR_COMM_SERVER_START, "true");
          pollCommServerType =
              getConfigurationValue(pollCommServerElement, ConfigType.ATTRIBUTE, "type", null);

          if (!StringUtils.hasValue(id)
              || !StringUtils.hasValue(key)
              || !StringUtils.hasValue(value)) {
            Debug.log(
                Debug.XML_ERROR,
                "Could not configure Poll Comm Server since mandatory property are not configured");
            Debug.log(Debug.DB_ERROR, ELEM_POLL_COMM_SERVER + "=" + getType());
            Debug.log(Debug.DB_ERROR, ATTR_COMM_SERVER_ID + "=" + id);
            Debug.log(Debug.DB_ERROR, ELEM_COMM_SERVER_KEY + "=" + key);
            Debug.log(Debug.DB_ERROR, ELEM_COMM_SERVER_TYPE + "=" + value);

            // skip to initialize FTP Poller server if not
            // configured correctly.
            continue;
          }

          boolean startFlag = "false".equalsIgnoreCase(start) ? false : true;

          CommServerConf commServerConf = new CommServerConf(key, value, pollCommServerType);
          commServerConf.setStarted(startFlag);

          NodeList paramList = pollCommServerElement.getElementsByTagName(ELEM_PARAM);
          if (paramList != null) {
            for (int i = 0; i < paramList.getLength(); i++) {
              Element paramElement = (Element) paramList.item(i);

              String paramNm =
                  getConfigurationValue(paramElement, ConfigType.ATTRIBUTE, "name", null);

              String paramVal =
                  getConfigurationValue(paramElement, ConfigType.ATTRIBUTE, "value", null);

              commServerConf.setParamValue(paramNm, paramVal);

              if (Debug.isLevelEnabled(Debug.OBJECT_LIFECYCLE))
                Debug.log(
                    Debug.OBJECT_LIFECYCLE,
                    "Setting param [" + paramNm + "] value [" + paramVal + "]");
            }
          }

          if (id == null)
            Debug.log(
                Debug.MSG_ERROR, "Could not initialize poll comm server with id [" + id + "].");
          else commServerConfMap.put(id, commServerConf);
        }
      }
    }

    try {
      parser = new XMLMessageParser(aggregatedDoc);
    } catch (Exception e) {
      throw new ProcessingException("Unable to create XMLMessageParser object " + e.getMessage());
    }

    if (Debug.isLevelEnabled(Debug.NORMAL_STATUS))
      Debug.log(
          Debug.NORMAL_STATUS,
          "done initializing poll comm servers of type [" + getType() + "].. ");
  }