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.main;
029
030import java.util.LinkedHashSet;
031import java.util.Set;
032
033import org.apache.commons.lang3.builder.EqualsBuilder;
034import org.apache.commons.lang3.builder.HashCodeBuilder;
035import org.apache.commons.logging.Log;
036
037/**
038 * Class used to collect log messages and later write them to the actual log but with duplicates removed.
039 */
040public class CmsDuplicateRemovingLogBuffer {
041
042    /**
043     * Entry to write to the log.
044     */
045    public static class Entry {
046
047        /** The log channel. */
048        private String m_channel;
049
050        /** The log level. */
051        private String m_level;
052
053        /** The log message. */
054        private String m_message;
055
056        /**
057         * Creates a new entry.
058         *
059         * @param channel the log channel
060         * @param level the log level
061         * @param message the log message
062         */
063        public Entry(String channel, String level, String message) {
064
065            super();
066            m_channel = channel;
067            m_level = level;
068            m_message = message;
069        }
070
071        /**
072         * @see java.lang.Object#equals(java.lang.Object)
073         */
074        @Override
075        public boolean equals(Object obj) {
076
077            return EqualsBuilder.reflectionEquals(this, obj);
078        }
079
080        /**
081         * Gets the log channel.
082         *
083         * @return the log channel
084         */
085        public String getChannel() {
086
087            return m_channel;
088        }
089
090        /**
091         * Gets the log level.
092         *
093         * @return the log level
094         */
095        public String getLevel() {
096
097            return m_level;
098        }
099
100        /**
101         * Gets the log message.
102         *
103         * @return the log message
104         */
105        public String getMessage() {
106
107            return m_message;
108        }
109
110        /**
111         * @see java.lang.Object#hashCode()
112         */
113        @Override
114        public int hashCode() {
115
116            return HashCodeBuilder.reflectionHashCode(this);
117        }
118
119    }
120
121    /** The set of log entries collected so far. */
122    private Set<Entry> m_entries = new LinkedHashSet<>();
123
124    /**
125     * Adds a new log entry.
126     *
127     * @param channel the log channel
128     * @param level the log level
129     * @param message the log message
130     */
131    public void add(String channel, String level, String message) {
132
133        m_entries.add(new Entry(channel, level, message));
134    }
135
136    /**
137     * Dumps all collected log entries to their respective logs, with duplicates removed.
138     *
139     * <p>Also clears the set of collected log entries.
140     */
141    public void flush() {
142
143        for (Entry entry : m_entries) {
144            String level = entry.getLevel();
145            Log log = CmsLog.getLog(entry.getChannel());
146            if ("warn".equalsIgnoreCase(level)) {
147                log.warn(entry.getMessage());
148            } else if ("error".equalsIgnoreCase(level)) {
149                log.error(entry.getMessage());
150            } else if ("info".equalsIgnoreCase(level)) {
151                log.info(entry.getMessage());
152            } else if ("debug".equalsIgnoreCase(level)) {
153                log.debug(entry.getMessage());
154            } else {
155                log.info(entry.getMessage());
156            }
157        }
158        m_entries.clear();
159    }
160}