001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.scheduler.jobs;
029
030import org.opencms.db.log.CmsLogFilter;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsLog;
033import org.opencms.scheduler.I_CmsScheduledJob;
034
035import java.util.Map;
036
037import org.apache.commons.logging.Log;
038
039/**
040 * A scheduled job which removes entries older than a given amount of time from the CMS_LOG table, to improve
041 * database performance.<p>
042 *
043 * This job has a single parameter named 'max-age', whose value consists of a number, followed by one or more spaces and
044 * finall a unit which is either 'hours', 'days', or 'weeks', which controls how old values have to be before they are
045 * deleted by the job.<p>
046 *
047 * To delete the CMS_LOG entries, this scheduled job needs to be executed as a user who has the role WORKPLACE_MANAGER.<p>
048 */
049public class CmsRemoveOldDbLogEntriesJob implements I_CmsScheduledJob {
050
051    /** The default max age. */
052    public static final int MAX_AGE_DEFAULT = 24 * 30 * 4;
053
054    /** The key for the max-age parameter. */
055    public static final String PARAM_MAX_AGE = "max-age";
056
057    /** The log object for this class. */
058    private static final Log LOG = CmsLog.getLog(CmsRemoveOldDbLogEntriesJob.class);
059
060    /**
061     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(org.opencms.file.CmsObject, java.util.Map)
062     */
063    public String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
064
065        String maxAgeStr = parameters.get(PARAM_MAX_AGE);
066        long maxAgeHours = parseMaxAge(maxAgeStr);
067        if (maxAgeHours > 0) {
068            long maxAgeMillis = maxAgeHours * 3600L * 1000L;
069            long now = System.currentTimeMillis();
070            CmsLogFilter filter = CmsLogFilter.ALL.filterTo(now - maxAgeMillis);
071            LOG.info("Removing all entries from CMS_LOG older than " + maxAgeHours + " hours...");
072            cms.deleteLogEntries(filter);
073        } else {
074            LOG.info("Not deleting any log entries because of a problem with the max-age format.");
075        }
076        return "remove old db log entries (max age : " + maxAgeStr + ")";
077    }
078
079    /**
080     * Parses the 'max-age' parameter and returns a value in hours.<p>
081     *
082     * @param maxAgeStr the value of the 'max-age' parameter
083     *
084     * @return the maximum age in hours
085     */
086    public int parseMaxAge(String maxAgeStr) {
087
088        if (maxAgeStr == null) {
089            showFormatError(maxAgeStr);
090            return -1;
091        }
092
093        maxAgeStr = maxAgeStr.toLowerCase().trim();
094        String[] tokens = maxAgeStr.split(" +");
095        if ((tokens.length != 2)) {
096            showFormatError(maxAgeStr);
097            return -1;
098        }
099        int number = 0;
100        try {
101            number = Integer.parseInt(tokens[0]);
102        } catch (NumberFormatException e) {
103            showFormatError(maxAgeStr);
104            return -1;
105        }
106
107        String unit = tokens[1];
108        if ("d".equals(unit) || unit.startsWith("day")) {
109            return 24 * number;
110        } else if ("h".equals(unit) || unit.startsWith("hour")) {
111            return number;
112        } else if ("w".equals(unit) || unit.startsWith("week")) {
113            return 7 * 24 * number;
114        } else {
115            showFormatError(maxAgeStr);
116            return -1;
117        }
118    }
119
120    /**
121     * Shows an error with the format of the 'max-age' parameter value.<p>
122     *
123     * @param paramValue the parameter value
124     */
125    private void showFormatError(String paramValue) {
126
127        LOG.error("Invalid value for the max-age parameter: '" + paramValue + "'");
128    }
129}