/**
  * Safely delete file on remote node.
  *
  * @param connection the SFTP URL Connection
  * @param fileName the file name
  */
 private void deleteFile(Sftp3gppUrlConnection connection, String fileName) {
   try {
     connection.deleteFile(fileName);
   } catch (Exception e) {
     LOG.warn(
         "Can't delete file {} from {} because {}",
         fileName,
         connection.getURL().getHost(),
         e.getMessage());
   }
 }
 /**
  * Safely delete file on remote node.
  *
  * @param connection the SFTP URL Connection
  * @param fileName the file name
  */
 private void deleteFile(Sftp3gppUrlConnection connection, String fileName) {
   try {
     connection.deleteFile(fileName);
   } catch (Exception e) {
     log()
         .warn(
             "Can't delete file "
                 + fileName
                 + " from "
                 + connection.getURL().getHost()
                 + " because "
                 + e.getMessage());
   }
 }
  /* (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);
    }
  }