001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.site.xmlsitemap;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.types.I_CmsResourceType;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.scheduler.I_CmsScheduledJob;
037
038import java.util.List;
039import java.util.Map;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * Scheduled job for updating the XML sitemap cache.<p>
045 */
046public class CmsUpdateXmlSitemapCacheJob implements I_CmsScheduledJob {
047
048    /** The logger instance for this class. */
049    private static final Log LOG = CmsLog.getLog(CmsUpdateXmlSitemapCacheJob.class);
050
051    /**
052     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(org.opencms.file.CmsObject, java.util.Map)
053     */
054    public String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
055
056        long start = System.currentTimeMillis();
057        LOG.info("Starting job " + getClass().getName());
058
059        String parentFolder = parameters.get("folder");
060        if (parentFolder == null) {
061            parentFolder = "/";
062        }
063        I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(CmsXmlSeoConfiguration.SEO_FILE_TYPE);
064        List<CmsResource> resources = cms.readResources(
065            parentFolder,
066            CmsResourceFilter.DEFAULT_FILES.addRequireType(type));
067        LOG.info("Starting to process individual XML sitemap files...");
068        int i = 0;
069        for (CmsResource res : resources) {
070            i += 1;
071            try {
072                LOG.info("Processing file " + res.getRootPath() + " [" + i + "/" + resources.size() + "]");
073                String name = res.getName();
074                if (name.contains("robots") || name.contains("test")) {
075                    LOG.info("Ignoring file " + res.getRootPath());
076                    continue;
077                }
078                CmsXmlSeoConfiguration config = new CmsXmlSeoConfiguration();
079                config.load(cms, res);
080                CmsXmlSitemapGenerator generator = null;
081                if (config.usesCache()) {
082                    generator = CmsXmlSitemapActionElement.prepareSitemapGenerator(res, config);
083                }
084                if (generator != null) {
085                    String xml = generator.renderSitemap();
086                    CmsXmlSitemapCache.INSTANCE.put(res.getRootPath(), xml);
087                } else {
088                    LOG.info("Ignoring file " + res.getRootPath());
089                }
090            } catch (Exception e) {
091                LOG.error("Error processing file " + res.getRootPath() + ": " + e.getLocalizedMessage(), e);
092            }
093        }
094        long end = System.currentTimeMillis();
095        LOG.info("Finished processing XML sitemap files. Elapsed time: " + ((end - start) / 1000) + " seconds");
096        return "";
097    }
098}