📄 scandirconfig.java
字号:
synchronized(this) { config = new XmlConfigUtils(filename).readFromFile(); if (configname != null) config = config.copy(configname); else configname = config.getName(); status=LOADED; } sendNotification(NOTIFICATION_LOADED); } // see ScanDirConfigMXBean public void save() throws IOException { if (filename == null) throw new UnsupportedOperationException("load"); synchronized (this) { new XmlConfigUtils(filename).writeToFile(config); status = SAVED; } sendNotification(NOTIFICATION_SAVED); } // see ScanDirConfigMXBean public ScanManagerConfig getConfiguration() { synchronized (this) { return XmlConfigUtils.xmlClone(config); } } // sends a notification indicating the new save state. private void sendNotification(String type) { final Object source = (objectName==null)?this:objectName; final Notification n = new Notification(type,source, getNextSeqNumber(), "The configuration is "+ type.substring(type.lastIndexOf('.')+1)); sendNotification(n); } /** * Allows the MBean to perform any operations it needs before being * registered in the MBean server. 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. * @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 will * try to guess its MBean name by examining its configuration data. * If its configuration data is null (nothing was provided in the * constructor) or doesn't contain a name, this method returns {@code null}, * and registration will fail. * <p> * Otherwise, if {@code name} wasn't {@code null} or if a default name could * be constructed, the name of the configuration will be set to * the value of the ObjectName's {@code name=} key, and the configuration * data will always be renamed to reflect this change. * </p> * * @return The name under which the MBean is to be registered. * @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) { if (config == null) return null; if (config.getName() == null) return null; name = ScanManager. makeMBeanName(ScanDirConfigMXBean.class,config.getName()); } objectName = name; mbeanServer = server; synchronized (this) { configname = name.getKeyProperty("name"); if (config == null) config = new ScanManagerConfig(configname); else config = config.copy(configname); } 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) { // Nothing to do 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 { // Nothing to do here. } /** * Allows the MBean to perform any operations needed after having been * unregistered in the MBean server. * <p>This implementation does nothing</p> */ public void postDeregister() { // Nothing to do here. } // see ScanDirConfigMXBean public String getConfigFilename() { return filename; } // see ScanDirConfigMXBean public void setConfiguration(ScanManagerConfig config) { synchronized (this) { if (config == null) { this.config = null; return; } if (configname == null) configname = config.getName(); this.config = config.copy(configname); status = MODIFIED; } sendNotification(NOTIFICATION_MODIFIED); } // see ScanDirConfigMXBean public DirectoryScannerConfig addDirectoryScanner(String name, String dir, String filePattern, long sizeExceedsMaxBytes, long sinceLastModified) { final DirectoryScannerConfig scanner = new DirectoryScannerConfig(name); scanner.setRootDirectory(dir); if (filePattern!=null||sizeExceedsMaxBytes>0||sinceLastModified>0) { final FileMatch filter = new FileMatch(); filter.setFilePattern(filePattern); filter.setSizeExceedsMaxBytes(sizeExceedsMaxBytes); if (sinceLastModified > 0) filter.setLastModifiedBefore(new Date(new Date().getTime() -sinceLastModified)); scanner.addIncludeFiles(filter); } synchronized (this) { config.putScan(scanner); status = MODIFIED; } LOG.fine("config: "+config); sendNotification(NOTIFICATION_MODIFIED); return scanner; } // see ScanDirConfigMXBean public DirectoryScannerConfig removeDirectoryScanner(String name) throws IOException, InstanceNotFoundException { final DirectoryScannerConfig scanner; synchronized (this) { scanner = config.removeScan(name); if (scanner == null) throw new IllegalArgumentException(name+": scanner not found"); status = MODIFIED; } sendNotification(NOTIFICATION_MODIFIED); return scanner; } // see ScanDirConfigMXBean public SaveState getSaveState() { return status; } // These methods are used by ScanManager to guess a configuration name from // a configuration filename. // static String DEFAULT = "DEFAULT"; private static String getBasename(String name) { final int dot = name.indexOf('.'); if (dot<0) return name; if (dot==0) return getBasename(name.substring(1)); return name.substring(0,dot); } static String guessConfigName(String configFileName,String defaultFile) { try { if (configFileName == null) return DEFAULT; final File f = new File(configFileName); if (f.canRead()) { final String confname = XmlConfigUtils.read(f).getName(); if (confname != null && confname.length()>0) return confname; } final File f2 = new File(defaultFile); if (f.equals(f2)) return DEFAULT; final String guess = getBasename(f.getName()); if (guess == null) return DEFAULT; if (guess.length()==0) return DEFAULT; return guess; } catch (Exception x) { return DEFAULT; } } // Set by preRegister() private volatile MBeanServer mbeanServer; private volatile ObjectName objectName; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -