⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manageddirectorypoller.java

📁 一个简易的轮训小程序 哈哈哈哈哈哈后 嘿嘿嘿嘿黑诶
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void addPollManager(PollManager pm) {
        // Allow only one JMXPollmanager
        if (pm instanceof JMXNotificationsPollManager)
            for(Iterator i=pollManagersList.iterator();i.hasNext();) {
                if (i.next() instanceof JMXNotificationsPollManager) {
                    throw new RuntimeException("JMXNotificationsPollManager is an internal-use only PollManager");
                }
            }
        super.addPollManager(pm);
        if (isVerbose()) 
            System.out.println("Added PollManager: "+pm);

    }
    

	/**
	 * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#getPollManagerFactory()
	 */
	public String getPollManagerFactoryClass() {
		if (pollManagerFactoryClsName==null) return "";
		return pollManagerFactoryClsName;
	}

	/**
	 * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#setPollManagerFactoryClass(java.lang.String)
	 */
	public void setPollManagerFactoryClass(String newFactoryClsName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		
		if (isRunning()) throw new RuntimeException("The directory poller is already running");
		if (this.pollManagerFactoryClsName!=null) {
			if (this.pollManagerFactoryClsName.equals(newFactoryClsName)) return;
			// Clear the pollManager list
			super.pollManagersList.clear();
			// Clear both existing factory name and instance
			this.pollManagerFactory=null;
			this.pollManagerFactoryClsName=null;
		}
		
		if (newFactoryClsName==null) return;
		if ("".equals("newFactoryClsName")) {
			this.pollManagerFactory=null;
			this.pollManagerFactoryClsName=null;
			return;
		}
		
		// Attempt to install the poll managers
		this.pollManagerFactory=(PollManagerFactory)Class.forName(newFactoryClsName).newInstance();
		this.pollManagerFactoryClsName=newFactoryClsName;
		
		if (pollerMBeanName==null) throw new RuntimeException("PreRegister hasn't been called on this bean - poller bean name is null");
		
		PollManager[] set = pollManagerFactory.createPollManagers(pollerMBeanName.getCanonicalName());
		for(int i=0;i<set.length;i++)
			addPollManager(set[i]);
	}

	/**
	 * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#getPollManagerFactory()
	 */
	public String getPollManagerFactory() {
		if (pollManagerFactory==null) return "(No PollManagerFactory set)";
		return pollManagerFactory.getDescription();
	}
	
	/**
	 * Returns the usingJMXTimer.
	 * @return boolean
	 */
	public boolean isUsingJMXTimer() {
		return usingJMXTimer;
	}

	/**
	 * Sets the usingJMXTimer.
	 * @param usingJMXTimer The usingJMXTimer to set
	 */
	public void setUsingJMXTimer(boolean usingJMXTimer_NewValue) {
		if (usingJMXTimer) { // Using timer previously
			if (usingJMXTimer_NewValue) {
				// Nothing to do
			} else {
				// deregister the hook
			}
		} else { // Not using timer previously
			if (usingJMXTimer_NewValue) {
			} else {
				// Is the poller running already? 
				if (isAlive()) 
					throw new IllegalStateException("Can't set the use of JMX timer while the poller is running. Please shut it down first.");
			}
		}
		this.usingJMXTimer = usingJMXTimer_NewValue;
	}

	/**
	 * Returns the jMXTimerObjectName.
	 * @return String
	 */
	public String getJMXTimerObjectName() {
		if (JMXTimerObjectName==null) return "";
		return JMXTimerObjectName;
	}

	/**
	 * Sets the jMXTimerObjectName.
	 * @param jMXTimerObjectName The jMXTimerObjectName to set
	 */
	public void setJMXTimerObjectName(String jMXTimerObjectName) {
		if ("".equals(jMXTimerObjectName)) jMXTimerObjectName=null;
		JMXTimerObjectName = jMXTimerObjectName;
	}

	/**
	 * Handles timer notifications, if a JMXTimer object is used
	 * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, java.lang.Object)
	 */
	public void handleNotification(Notification arg0, Object arg1) {
		
	}
	
	/**
	 * @param filenameFilterClsName The filenameFilterClsName to set.
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public void setFilenameFilterFactoryClass(String newFilenameFilterFactoryClsName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		if (isRunning()) throw new RuntimeException("The directory poller is already running");
		if (newFilenameFilterFactoryClsName==null) return;
		
		if (newFilenameFilterFactoryClsName==null || "".equals(newFilenameFilterFactoryClsName)) {
			setFilter(new NullFilenameFilter());
			this.filenameFilterFactoryClsName=null;
			return;
		}
		
		this.filenameFilterFactory=(FilenameFilterFactory) Class.forName(newFilenameFilterFactoryClsName).newInstance();
		this.filenameFilterFactoryClsName=newFilenameFilterFactoryClsName;
	
		if (pollerMBeanName==null) throw new RuntimeException("PreRegister hasn't been called on this bean - poller bean name is null");
		setFilter(filenameFilterFactory.createFilenameFilter(pollerMBeanName.getCanonicalName()));
	}
	
	public String getFilenameFilterFactory() {
		if (filenameFilterFactory==null) return "(No FilenameFilterFactory set)";
		return filenameFilterFactory.getDescription();
	}
	
	public String getFilenameFilterFactoryClass() {
		if (filenameFilterFactoryClsName==null) return "";
		return filenameFilterFactoryClsName;
	}
	
	
	public void setAcceptedFilenamePattern(String filenamePattern) {
		if (filenameFilterFactoryClsName!=null && !"".equals(filenamePattern)) {
			try {
				setFilenameFilterFactoryClass("");
			} catch (InstantiationException e) {
				// Ignore - can't happen
				throw new RuntimeException("This shouldn't happen ("+e.getClass().getName()+":"+e.getMessage()+") - please report", e);
			} catch (IllegalAccessException e) {
				// Ignore - can't happen
				throw new RuntimeException("This shouldn't happen ("+e.getClass().getName()+":"+e.getMessage()+") - please report", e);
			} catch (ClassNotFoundException e) {
				// Ignore - can't happen
				throw new RuntimeException("This shouldn't happen ("+e.getClass().getName()+":"+e.getMessage()+") - please report", e);
			}
		}
		
		if ("".equals(filenamePattern)) setFilter(new NullFilenameFilter());
		else setFilter(new RegexpFilenameFilter(filenamePattern));
	}
	
	/* (non-Javadoc)
	 * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#setFilter(java.io.FilenameFilter)
	 */
	public void setFilter(FilenameFilter filter) {
		
		if (filter instanceof RegexpFilenameFilter) 
			currentPattern=((RegexpFilenameFilter)filter).getPatternString();
		else
			currentPattern=null;
		
		super.setFilter(filter);
		
	}
		
    public String getAcceptedFilenamePattern() {
    	if (currentPattern==null) return "";
    	else return currentPattern;
    }
    
    public void setAutoMoveDirectoryPath(String directory, String automoveDirectory) {
        setAutoMoveDirectory(new File(directory), new File(automoveDirectory));
    }
    
    /* (non-Javadoc)
     * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#getFileComparatorClass()
     */
    public String getFilesSortComparatorClass() {
        if (getFilesSortComparator()!=null) return getFilesSortComparator().getClass().getName();
        else return "";
    }
    
    /* (non-Javadoc)
     * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#setFileComparatorClass(java.lang.String)
     */
    public void setFilesSortComparatorClass(String fileComparatorClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        if ("".equals(fileComparatorClassName)) {
            //System.out.println("Setting comparator class name to null");
            setFilesSortComparator(null);
        } else if (fileComparatorClassName.equals(GenericFileComparator.class.getName())) {
            // Ignore
            //System.out.println("Ignoring comparator class name "+fileComparatorClassName);
        } else {
            //System.out.println("Setting comparator class name "+fileComparatorClassName);
            Class cls = Class.forName(fileComparatorClassName);
            try {
                Constructor ctor = cls.getConstructor(new Class[0]);
                if (!Modifier.isPublic(ctor.getModifiers()))
                    throw new RuntimeException(
                            "The specified file comparator class "
                                    + fileComparatorClassName
                                    + " has a zero-parameters constructor, but it is not public");
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(
                        "The specified file comparator class "
                                + fileComparatorClassName
                                + " does not have a default constructor");
            }
            setFilesSortComparator((Comparator)cls.newInstance());
        }
    }
    
    /* (non-Javadoc)
     * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#setEventsOrdering(java.lang.String)
     */
    public void setEventsOrdering(String expr) {
        if ("by comparator class".equals(expr.trim().toLowerCase())) {
            // Ignore
            return;
        }
        if ("-".equals(expr.trim()) || "none".equals(expr.trim().toLowerCase()) || "".equals(expr.trim())) {
            setFilesSortComparator(null);
            //System.out.println("Setting comparator to null");
            return;
        }
        Comparator c = new GenericFileComparator(expr);
        setFilesSortComparator(c);
        //System.out.println("Setting event ordering to "+expr+" (GenericFileComparator "+c+")");
    }
    
    /* (non-Javadoc)
     * @see org.sadun.util.polling.ManagedDirectoryPollerMBean#getEventsOrdering()
     */
    public String getEventsOrdering() {
        Comparator c = getFilesSortComparator();
        if (c==null) return "none";
        if (c instanceof GenericFileComparator)
            return ((GenericFileComparator)c).getSpecification();
        else return "by comparator class";
    }
    
    public String getJMXSequenceNumberGeneratorClass() {
        return JMXsequenceNumberGeneratorClass;
    }

    public void setJMXSequenceNumberGeneratorClass(String sequenceNumberGeneratorClass) throws MBeanException {
        try {
            sqg = (SequenceNumberGenerator)Class.forName(sequenceNumberGeneratorClass).newInstance();
            this.JMXsequenceNumberGeneratorClass = sequenceNumberGeneratorClass;
        } catch(Exception e) {
            throw new MBeanException(e);
        }
    }
    
    public MBeanNotificationInfo[] getNotificationInfo() {
        MBeanNotificationInfo[] mbv = new MBeanNotificationInfo[1];
        mbv[0]=new MBeanNotificationInfo(new String [] {
          CycleStartJMXNotification.NOTIFICATION_TYPE,
          CycleEndJMXNotification.NOTIFICATION_TYPE,
          DirectoryLookupStartJMXNotification.NOTIFICATION_TYPE,
          DirectoryLookupEndJMXNotification.NOTIFICATION_TYPE,
          FileSetFoundJMXNotification.NOTIFICATION_TYPE,
          FileMovedJMXNotification.NOTIFICATION_TYPE,
          FileFoundJMXNotification.NOTIFICATION_TYPE, 
          ExceptionMovingFileJMXNotification.NOTIFICATION_TYPE,
          ExceptionDeletingTargetFileJMXNotification.NOTIFICATION_TYPE
        }, BaseJMXNotification.class.getName(), "Poller notification events"
        );
        return mbv;
    }
    
    /**
     * Implement the NotificationBroadcaster interface, by registering the listener at the
     * internal {@link JMXNotificationsPollManager}.  
     */
    public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException {
        jmxNotificationsPollManager.addListener(listener,filter,handback);
    }
    
    /**
     * Implement the NotificationBroadcaster interface, by removing the listener from the
     * internal {@link JMXNotificationsPollManager}.  
     */
    public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
        jmxNotificationsPollManager.removeListener(listener);
        
    }
    
    /**
     * Register a listening MBean (found in the only existing MBean server) via its ObjectName. Only one MBean server must
     * be present.
     *  
     * @param listenerMBeanName the object name of the mbean which listens to to the poller's JMX notifications  
     */
    public void addNotificationListener(ObjectName listenerMBeanName) throws InstanceNotFoundException {
        addNotificationListener(null, listenerMBeanName);
    }
        
    /**
     * Register a listening MBean via its ObjectName.
     * 
     * @param the name of the MBean server containing the listener MBean
     * @param listenerMBeanName the object name of the mbean which listens to to the poller's JMX notifications  
     */
    public void addNotificationListener(String mbeanServerName, ObjectName listenerMBeanName) throws InstanceNotFoundException {
        MBeanServer srv = getMBeanServer(mbeanServerName);
        if (isVerbose())
            System.out.println("Adding notification listener "+listenerMBeanName+" to "+pollerMBeanName+" on server "+srv);
        srv.addNotificationListener(this.pollerMBeanName, listenerMBeanName, null, null);
        if (isVerbose())
            System.out.println("Added notification listener "+listenerMBeanName+" to "+pollerMBeanName+" on server "+srv);
    }

    /**
     * Remove a listening MBean (found in the only existing MBean server) via its ObjectName. Only one MBean server must
     * be present.
     * @param listenerMBeanName the object name of the mbean which is not to listen anymore to the poller's JMX notifications  
     */
    public void removeNotificationListener(ObjectName listenerMBeanName) throws InstanceNotFoundException, ListenerNotFoundException {
        removeNotificationListener(null, listenerMBeanName);
    }
    
    /**
     * Remove a listening MBean via its ObjectName.
     * 
     * @param the name of the MBean server containing the listener MBean
     * @param listenerMBeanName the object name of the mbean which is not to listen anymore to the poller's JMX notifications  
     */
    public void removeNotificationListener(String mbeanServerName, ObjectName listenerMBeanName) throws InstanceNotFoundException, ListenerNotFoundException {    
        MBeanServer srv = getMBeanServer(mbeanServerName);
        if (isVerbose())
            System.out.println("Removing notification listener "+listenerMBeanName+" to "+pollerMBeanName+" on server "+srv);
        srv.removeNotificationListener(this.pollerMBeanName, listenerMBeanName);
        if (isVerbose())
            System.out.println("Removed notification listener "+listenerMBeanName+" to "+pollerMBeanName+" on server "+srv);
    }
    
    /**
     * @return
     */
    private MBeanServer getMBeanServer(String mbeanServerName) {
        MBeanServer srv=null;
        ArrayList al = MBeanServerFactory.findMBeanServer(mbeanServerName);
        if (al.isEmpty()) throw new RuntimeException("Could not find any MBean server"+mbeanServerName!=null ? " named \""+mbeanServerName+"\"" : "");
        else if (al.size()!=1) throw new RuntimeException("More than one MBean server "+(mbeanServerName!=null ? "named \""+mbeanServerName+"\" " : "")+"registered?");
        else srv=(MBeanServer)al.get(0);
        return srv;
    }
	
}

⌨️ 快捷键说明

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