/* (non-Javadoc)
  * @see java.lang.Object#equals(java.lang.Object)
  */
 @Override
 public boolean equals(Object obj) {
   if (obj instanceof XmlSource) {
     XmlSource other = (XmlSource) obj;
     return new EqualsBuilder()
         .append(getUrl(), other.getUrl())
         .append(
             getXmlGroups().toArray(OF_XML_GROUPS), other.getXmlGroups().toArray(OF_XML_GROUPS))
         .isEquals();
   }
   return false;
 }
  /* (non-Javadoc)
   * @see org.opennms.protocols.xml.collector.XmlCollectionHandler#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.protocols.xml.config.XmlDataCollection, java.util.Map)
   */
  @Override
  public XmlCollectionSet collect(
      CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters)
      throws CollectionException {
    // Create a new collection set.
    XmlCollectionSet collectionSet = new XmlCollectionSet();
    collectionSet.setCollectionTimestamp(new Date());
    collectionSet.setStatus(ServiceCollector.COLLECTION_UNKNOWN);

    // TODO We could be careful when handling exceptions because parsing exceptions will be treated
    // different from connection or retrieval exceptions
    try {
      File resourceDir =
          new File(getRrdRepository().getRrdBaseDir(), Integer.toString(agent.getNodeId()));
      for (XmlSource source : collection.getXmlSources()) {
        if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
          throw new CollectionException(
              "The 3GPP SFTP Collection Handler can only use the protocol "
                  + Sftp3gppUrlHandler.PROTOCOL);
        }
        String urlStr = parseUrl(source.getUrl(), agent, collection.getXmlRrd().getStep());
        Request request = parseRequest(source.getRequest(), agent);
        URL url = UrlFactory.getUrl(urlStr, request);
        String lastFile = getLastFilename(resourceDir, url.getPath());
        Sftp3gppUrlConnection connection = (Sftp3gppUrlConnection) url.openConnection();
        if (lastFile == null) {
          lastFile = connection.get3gppFileName();
          LOG.debug(
              "collect(single): retrieving file from {}{}{} from {}",
              url.getPath(),
              File.separatorChar,
              lastFile,
              agent.getHostAddress());
          Document doc = getXmlDocument(urlStr, source.getRequest());
          fillCollectionSet(agent, collectionSet, source, doc);
          setLastFilename(resourceDir, url.getPath(), lastFile);
          deleteFile(connection, lastFile);
        } else {
          connection.connect();
          List<String> files = connection.getFileList();
          long lastTs = connection.getTimeStampFromFile(lastFile);
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          factory.setIgnoringComments(true);
          boolean collected = false;
          for (String fileName : files) {
            if (connection.getTimeStampFromFile(fileName) > lastTs) {
              LOG.debug(
                  "collect(multiple): retrieving file {} from {}",
                  fileName,
                  agent.getHostAddress());
              InputStream is = connection.getFile(fileName);
              Document doc = builder.parse(is);
              IOUtils.closeQuietly(is);
              fillCollectionSet(agent, collectionSet, source, doc);
              setLastFilename(resourceDir, url.getPath(), fileName);
              deleteFile(connection, fileName);
              collected = true;
            }
          }
          if (!collected) {
            LOG.warn("collect: could not find any file after {} on {}", lastFile, agent);
          }
          connection.disconnect();
        }
      }
      collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
      return collectionSet;
    } catch (Exception e) {
      collectionSet.setStatus(ServiceCollector.COLLECTION_FAILED);
      throw new CollectionException(e.getMessage(), e);
    }
  }
 /* (non-Javadoc)
  * @see java.lang.Comparable#compareTo(java.lang.Object)
  */
 public int compareTo(XmlSource obj) {
   return new CompareToBuilder()
       .append(getUrl(), obj.getUrl())
       .append(getXmlGroups().toArray(OF_XML_GROUPS), obj.getXmlGroups().toArray(OF_XML_GROUPS))
       .toComparison();
 }