📄 scandirconfig.java
字号:
/* * ScanDirConfig.java * * Created on July 12, 2006, 7:43 PM * * @(#)ScanDirConfig.java 1.2 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 static com.sun.jmx.examples.scandir.ScanDirConfigMXBean.SaveState.*;import com.sun.jmx.examples.scandir.config.XmlConfigUtils;import com.sun.jmx.examples.scandir.config.DirectoryScannerConfig;import com.sun.jmx.examples.scandir.config.FileMatch;import com.sun.jmx.examples.scandir.config.ScanManagerConfig;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Date;import java.util.logging.Level;import java.util.logging.Logger;import javax.management.*;import javax.xml.bind.JAXBException;/** * <p>The <code>ScanDirConfig</code> MBean is in charge of the * <i>scandir</i> application configuration. * </p> * <p>The <code>ScanDirConfig</code> MBean is able to * load and save the <i>scandir</i> application configuration to and from an * XML file. * </p> * <p> * It will let you also interactively modify that configuration, which you * can later save to the file, by calling {@link #save}, or discard, by * reloading the file without saving - see {@link #load}. * </p> * <p> * There can be as many <code>ScanDirConfigMXBean</code> registered * in the MBeanServer as you like, but only one of them will be identified as * the current configuration of the {@link ScanManagerMXBean}. * You can switch to another configuration by calling {@link * ScanManagerMXBean#setConfigurationMBean * ScanManagerMXBean.setConfigurationMBean}. * </p> * <p> * Once the current configuration has been loaded (by calling {@link #load}) * or modified (by calling one of {@link #addDirectoryScanner * addDirectoryScanner}, {@link #removeDirectoryScanner removeDirectoryScanner} * or {@link #setConfiguration setConfiguration}) it can be pushed * to the {@link ScanManagerMXBean} by calling {@link * ScanManagerMXBean#applyConfiguration * ScanManagerMXBean.applyConfiguration(true)} - * <code>true</code> means that we apply the configuration from memory, * without first reloading the file. * </p> * <p> * The <code>ScanDirConfig</code> uses the XML annotated Java Beans defined * in the {@link com.sun.jmx.examples.scandir.config} package. * </p> * <p> * <u>Note:</u> The <code>ScanDirConfig</code> should probably use * {@code java.nio.channels.FileLock} and lock its configuration file so that * two <code>ScanDirConfig</code> object do not share the same file, but it * doesn't. Feel free to improve the application in that way. * </p> * @author Sun Microsystems, 2006 - All rights reserved. */public class ScanDirConfig extends NotificationBroadcasterSupport implements ScanDirConfigMXBean, MBeanRegistration { /** * A logger for this class. **/ private static final Logger LOG = Logger.getLogger(ScanDirConfig.class.getName()); // We will emit a notification when the save state of this object // chenges. We use directly the base notification class, with a // notification type that indicates the new state at which the // object has arrived. // // All these notification types will have the same prefix, which is // 'com.sun.jmx.examples.scandir.config'. // private final static String NOTIFICATION_PREFIX = ScanManagerConfig.class.getPackage().getName(); /** * The <i>com.sun.jmx.examples.scandir.config.saved</i> notification * indicates that the configuration data was saved. **/ public final static String NOTIFICATION_SAVED = NOTIFICATION_PREFIX+".saved"; /** * The <i>com.sun.jmx.examples.scandir.config.loaded</i> notification * indicates that the configuration data was loaded. **/ public final static String NOTIFICATION_LOADED = NOTIFICATION_PREFIX+".loaded"; /** * The <i>com.sun.jmx.examples.scandir.config.modified</i> notification * indicates that the configuration data was modified. **/ public final static String NOTIFICATION_MODIFIED = NOTIFICATION_PREFIX+".modified"; // The array of MBeanNotificationInfo that will be exposed in the // ScanDirConfigMXBean MBeanInfo. // We will pass this array to the NotificationBroadcasterSupport // constructor. // private static MBeanNotificationInfo[] NOTIFICATION_INFO = { new MBeanNotificationInfo( new String[] {NOTIFICATION_SAVED}, Notification.class.getName(), "Emitted when the configuration is saved"), new MBeanNotificationInfo( new String[] {NOTIFICATION_LOADED}, Notification.class.getName(), "Emitted when the configuration is loaded"), new MBeanNotificationInfo( new String[] {NOTIFICATION_MODIFIED}, Notification.class.getName(), "Emitted when the configuration is modified"), }; // The ScanDirConfigMXBean configuration data. private volatile ScanManagerConfig config; // The name of the configuration file private String filename = null; // The name of this configuration. This is usually both equal to // config.getName() and objectName.getKeyProperty(name). private volatile String configname = null; // This object save state. CREATED is the initial state. // private volatile SaveState status = CREATED; /** * Creates a new {@link ScanDirConfigMXBean}. * <p>{@code ScanDirConfigMXBean} can be created by the {@link * ScanManagerMXBean}, or directly by a remote client, using * {@code createMBean} or {@code registerMBean}. * </p> * <p>{@code ScanDirConfigMXBean} created by the {@link * ScanManagerMXBean} will be unregistered by the * {@code ScanManagerMXBean}. {@code ScanDirConfigMXBean} created * directly by a remote client will not be unregistered by the * {@code ScanManagerMXBean} - this will remain to the responsibility of * the code/client that created them. * </p> * <p>This object is created empty, you should call load() if you want it * to load its data from the configuration file. * </p> * @param filename The configuration file used by this MBean. * Can be null (in which case load() and save() will fail). * Can point to a file that does not exists yet (in which case * load() will fail if called before save(), and save() will * attempt to create that file). Can point to an existing file, * in which case load() will load that file and save() will save * to that file. * **/ public ScanDirConfig(String filename) { this(filename,null); } /** * Create a new ScanDirConfig MBean with an initial configuration. * @param filename The name of the configuration file. * @param initialConfig an initial configuration. **/ public ScanDirConfig(String filename, ScanManagerConfig initialConfig) { super(NOTIFICATION_INFO); this.filename = filename; this.config = initialConfig; } // see ScanDirConfigMXBean public void load() throws IOException { if (filename == null) throw new UnsupportedOperationException("load");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -