001/*
002 * #%L
003 * Netarchivesuite - monitor
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.monitor.logging;
025
026import java.lang.management.ManagementFactory;
027
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029import dk.netarkivet.common.exceptions.IOFailure;
030import dk.netarkivet.common.management.SingleMBeanObject;
031
032/**
033 * Cached log entry and JMX bean in one.
034 */
035public class CachingSLF4JLogRecord implements SingleLogRecord {
036
037        /** Index in cached list. */
038    private final int index;
039
040    /** Caching appender that created this caching log entry. */
041    private final CachingSLF4JAppender cachingSLF4JAppender;
042
043    /** JMX bean object. */
044    private SingleMBeanObject<SingleLogRecord> singleMBeanObject;
045
046    /**
047     * Make a caching log record, that exposes a log record at a given index as an MBean.
048     *
049     * @param index The index of this log record, counted from the top of the list.
050     * @param cachingSLF4JAppender The caching log handler this is an exposing view on.
051     * @throws IOFailure on any trouble registering.
052     */
053    public CachingSLF4JLogRecord(int index, CachingSLF4JAppender cachingSLF4JAppender) {
054        ArgumentNotValid.checkNotNull(cachingSLF4JAppender, "CachingSLF4JAppender cachingSLF4JAppender");
055        this.index = index;
056        this.cachingSLF4JAppender = cachingSLF4JAppender;
057        register();
058    }
059
060    @Override
061    public String getRecordString() {
062        String logMsg = cachingSLF4JAppender.getNthLogRecord(this.index);
063        if (logMsg == null) {
064            return "";
065        } else {
066            return logMsg;
067        }
068    }
069
070    /**
071     * Registers this object as an mbean.
072     */
073    private void register() {
074        singleMBeanObject = new SingleMBeanObject<>("dk.netarkivet.common.logging", this,
075                SingleLogRecord.class, ManagementFactory.getPlatformMBeanServer());
076        singleMBeanObject.getNameProperties().put("index", Integer.toString(this.index));
077        singleMBeanObject.register();
078    }
079
080    /**
081     * Unregisters this object as an mbean.
082     */
083    private void unregister() {
084        if (singleMBeanObject != null) {
085            singleMBeanObject.unregister();
086            singleMBeanObject = null;
087        }
088    }
089
090    /**
091     * Unregisters this object as an mbean.
092     */
093    public void close() {
094        unregister();
095    }
096
097}