resultlogmanager.java

来自「一个小公司要求给写的很简单的任务管理系统。」· Java 代码 · 共 537 行 · 第 1/2 页

JAVA
537
字号
/* * ResultLogManager.java * * Created on July 17, 2006, 1:15 PM * * @(#)ResultLogManager.java	1.3 06/08/02 * * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this *  list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, *  this list of conditions and the following disclaimer in the documentation *  and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */package com.sun.jmx.examples.scandir;import static com.sun.jmx.examples.scandir.ScanManager.getNextSeqNumber;import com.sun.jmx.examples.scandir.config.ResultLogConfig;import com.sun.jmx.examples.scandir.config.XmlConfigUtils;import com.sun.jmx.examples.scandir.config.ResultRecord;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Collections;import java.util.LinkedList;import java.util.List;import java.util.logging.Logger;import javax.management.MBeanNotificationInfo;import javax.management.MBeanRegistration;import javax.management.MBeanServer;import javax.management.Notification;import javax.management.NotificationBroadcasterSupport;import javax.management.ObjectName;import javax.xml.bind.JAXBException;/** * The <code>ResultLogManager</code> is in charge of managing result logs. * {@link DirectoryScanner DirectoryScanners} can be configured to log a * {@link ResultRecord} whenever they take action upon a file that * matches their set of matching criteria.  * The <code>ResultLogManagerMXBean</code> is responsible for storing these  * results in its result logs. * <p>The <code>ResultLogManagerMXBean</code> can be configured to log * these records to a flat file, or into a log held in memory, or both.  * Both logs (file and memory) can be configured with a maximum capacity.  * <br>When the maximum capacity of the memory log is reached - its first  * entry (i.e. its eldest entry) is removed to make place for the latest.  * <br>When the maximum capacity of the file log is reached, the file is * renamed by appending a tilde '~' to its name and a new result log is created. *  *  * @author Sun Microsystems, 2006 - All rights reserved. */public class ResultLogManager extends NotificationBroadcasterSupport        implements ResultLogManagerMXBean, MBeanRegistration {        /**     * The default singleton name of the {@link ResultLogManagerMXBean}.     **/    public static final ObjectName RESULT_LOG_MANAGER_NAME =            ScanManager.makeSingletonName(ResultLogManagerMXBean.class);        /**     * A logger for this class.     **/    private static final Logger LOG =            Logger.getLogger(ResultLogManager.class.getName());        // The memory log    //    private final List<ResultRecord> memoryLog;        // Whether the memory log capacity was reached. In that case every    // new entry triggers the deletion of the eldest one.    //    private volatile boolean memCapacityReached = false;        // The maximum number of record that the memory log can    // contain.    //    private volatile int memCapacity;        // The maximum number of record that the ResultLogManager can    // log in the log file before creating a new file.    //    private volatile long fileCapacity;        // The current log file.    //    private volatile File logFile;        // The OutputStream of the current log file.    //    private volatile OutputStream logStream = null;        // number of record that this object has logged in the log file    // since the log file was created. Creating a new file or clearing    // the log file reset this value to '0'    //    private volatile long logCount = 0;        // The ResultLogManager config - modified whenever    // ScanManager.applyConfiguration is called.    //    private volatile ResultLogConfig config;        /**     * Create a new ResultLogManagerMXBean. This constructor is package     * protected: only the {@link ScanManager} can create a     * <code>ResultLogManager</code>.     **/    ResultLogManager() {        // Instantiate the memory log - override the add() method so that        // it removes the head of the list when the maximum capacity is        // reached. Note that add() is the only method we will be calling,        // otherwise we would have to override all the other flavors        // of adding methods. Note also that this implies that the memoryLog        // will *always* remain encapsulated in this object and is *never*        // handed over (otherwise we wouldn't be able to ensure that        // add() is the only method ever called to add a record).        //        memoryLog =                Collections.synchronizedList(new LinkedList<ResultRecord>() {            public synchronized boolean add(ResultRecord e) {                final int max = getMemoryLogCapacity();                while (max > 0 && size() >= max) {                    memCapacityReached = true;                    removeFirst();                }                return super.add(e);            }        });                // default memory capacity        memCapacity = 2048;                // default file capacity: 0 means infinite ;-)        fileCapacity = 0;                // by default logging to file is disabled.        logFile = null;                // Until the ScanManager apply a new configuration, we're going to        // work with a default ResultLogConfig object.        config = new ResultLogConfig();        config.setMemoryMaxRecords(memCapacity);        config.setLogFileName(getLogFileName(false));        config.setLogFileMaxRecords(fileCapacity);    }            /**     * Allows the MBean to perform any operations it needs before being     * registered in the MBean server.      * <p>If the name of the MBean is not     * specified, the MBean can provide a name for its registration. If     * any exception is raised, the MBean will not be registered in the     * MBean server.</p>     * <p>The {@code ResultLogManager} uses this method to supply its own     * default singleton ObjectName (if <var>name</var> parameter is null).     * @param server The MBean server in which the MBean will be registered.     * @param name The object name of the MBean. This name is null if the     * name parameter to one of the createMBean or registerMBean methods in     * the MBeanServer interface is null. In that case, this method must     * return a non-null ObjectName for the new MBean.     * @return The name under which the MBean is to be registered. This value     * must not be null. If the name parameter is not null, it will usually     * but not necessarily be the returned value.     * @throws Exception This exception will be caught by the MBean server and     * re-thrown as an MBeanRegistrationException.     */    public ObjectName preRegister(MBeanServer server, ObjectName name)    throws Exception {        if (name == null)            name = RESULT_LOG_MANAGER_NAME;        objectName = name;        mbeanServer = server;        return name;    }        /**     * Allows the MBean to perform any operations needed after having     * been registered in the MBean server or after the registration has     * failed.     * <p>This implementation does nothing.</p>     * @param registrationDone Indicates whether or not the MBean has been     * successfully registered in the MBean server. The value false means     * that the registration has failed.     */    public void postRegister(Boolean registrationDone) {        // Don't need to do anything here.    }        /**     * Allows the MBean to perform any operations it needs before being     * unregistered by the MBean server.     * <p>This implementation does nothing.</p>     * @throws Exception This exception will be caught by the MBean server and     * re-thrown as an MBeanRegistrationException.     */    public void preDeregister() throws Exception {        // Don't need to do anything here.    }        /**     * Allows the MBean to perform any operations needed after having been     * unregistered in the MBean server.     * <p>Closes the log file stream, if it is still open.</p>     */    public void postDeregister() {        try {            if (logStream != null) {                synchronized(this)  {                    logStream.flush();                    logStream.close();                    logFile = null;                    logStream = null;                }            }        } catch (Exception x) {            LOG.finest("Failed to close log properly: "+x);        }    }        /**     * Create a new empty log file from the given basename, renaming     * previously existing file by appending '~' to its name.      **/    private File createNewLogFile(String basename) throws IOException {        return XmlConfigUtils.createNewXmlFile(basename);    }        /**     * Check whether a new log file should be created.     * If a new file needs to be created, creates it, renaming     * previously existing file by appending '~' to its name.     * Also reset the log count and file capacity.     * Sends a notification indicating that the log file was changed.     * Returns the new log stream;     * Creation of a new file can be forced by passing force=true.     **/    private OutputStream checkLogFile(String basename, long maxRecords, 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?