private void generate()

in src/main/java/org/apache/sling/sitemap/impl/SitemapGeneratorExecutor.java [133:189]


    private void generate(Resource res, String name, SitemapGenerator generator, JobExecutionContext jobCtxt)
            throws SitemapException, IOException {
        try {
            CopyableByteArrayOutputStream buffer = new CopyableByteArrayOutputStream();
            Context genCtxt = new Context();

            // prefill the buffer with existing data from storage
            ValueMap state = storage.getState(res, name);
            InputStream existingData = state.get(JcrConstants.JCR_DATA, InputStream.class);
            if (existingData != null) {
                IOUtils.copy(existingData, buffer);
            }
            // prefill the state from storage
            for (Map.Entry<String, Object> entry : state.entrySet()) {
                if (entry.getKey().indexOf(':') < 0) {
                    genCtxt.data.put(entry.getKey(), entry.getValue());
                }
            }
            // get the file index, if any
            int fileIndex = state.get(SitemapStorage.PN_SITEMAP_FILE_INDEX, 1);
            int urlCount = state.get(SitemapStorage.PN_SITEMAP_ENTRIES, 0);

            MultiFileSitemap sitemap = new MultiFileSitemap(res, name, fileIndex, buffer, genCtxt, jobCtxt);
            sitemap.currentSitemap.urlCount = urlCount;

            generator.generate(res, name, sitemap, genCtxt);

            sitemap.close();

            if (LOG.isDebugEnabled()) {
                LOG.debug("Generated sitemaps: {}", String.join(", ", sitemap.files));
            }

            // when the max(fileIndex) is smaller than in previous iterations, cleanup old files.
            Collection<String> purgedFiles = storage.deleteSitemaps(res, name, i -> i.getFileIndex() >= sitemap.fileIndex);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Purged obsolete sitemaps: {}", String.join(", ", purgedFiles));
            }
        } catch (JobAbandonedException ex) {
            throw ex;
        } catch (JobStoppedException ex) {
            LOG.debug("Job stopped, removing state", ex);
            storage.deleteState(res, name);
        } catch (RuntimeException | SitemapException | IOException ex) {
            storage.deleteState(res, name);
            if (ex instanceof IOException) {
                throw (IOException) ex;
            } else if (ex.getCause() instanceof IOException) {
                throw (IOException) ex.getCause();
            } else if (ex instanceof RuntimeException) {
                throw new SitemapException(ex);
            } else {
                throw ex;
            }
        }
    }