threshdconfigfactory.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 479 行 · 第 1/2 页

JAVA
479
字号
            return;        }        File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.THRESHD_CONFIG_FILE_NAME);        ThreadCategory.getInstance(ThreshdConfigFactory.class).debug("init: config file path: " + cfgFile.getPath());        m_singleton = new ThreshdConfigFactory(cfgFile.getPath());        m_loaded = true;    }    /**     * Reload the config from the default config file     *      * @exception java.io.IOException     *                Thrown if the specified config file cannot be read/loaded     * @exception org.exolab.castor.xml.MarshalException     *                Thrown if the file does not conform to the schema.     * @exception org.exolab.castor.xml.ValidationException     *                Thrown if the contents do not match the required schema.     */    public static synchronized void reload() throws IOException, MarshalException, ValidationException {        m_singleton = null;        m_loaded = false;        init();    }         /**          * Saves the current in-memory configuration to disk and reloads          */         public synchronized void saveCurrent()                 throws MarshalException, IOException, ValidationException         {                 File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.THRESHD_CONFIG_FILE_NAME);                 //marshall to a string first, then write the string to the file. This way the original config                 //isn't lost if the xml from the marshall is hosed.                 StringWriter stringWriter = new StringWriter();                 Marshaller.marshal(m_config, stringWriter);                 if (stringWriter.toString()!=null)                 {                         FileWriter fileWriter = new FileWriter(cfgFile);                         fileWriter.write(stringWriter.toString());                         fileWriter.flush();                         fileWriter.close();                 }                 reload();         }    /**     * Return the singleton instance of this factory.     *      * @return The current factory instance.     *      * @throws java.lang.IllegalStateException     *             Thrown if the factory has not yet been initialized.     */    public static synchronized ThreshdConfigFactory getInstance() {        if (!m_loaded)            throw new IllegalStateException("The factory has not been initialized");        return m_singleton;    }    /**     * Return the threshd configuration object.     */    public synchronized ThreshdConfiguration getConfiguration() {        return m_config;    }         public synchronized  org.opennms.netmgt.config.threshd.Package getPackage(String name) {                 Enumeration packageEnum=m_config.enumeratePackage();                 while(packageEnum.hasMoreElements()) {                         org.opennms.netmgt.config.threshd.Package thisPackage=( org.opennms.netmgt.config.threshd.Package)packageEnum.nextElement();                         if(thisPackage.getName().equals(name)) {                                 return thisPackage;                         }                 }                 return null;         }    /**     * This method is used to determine if the named interface is included in     * the passed package's url includes. If the interface is found in any of     * the URL files, then a value of true is returned, else a false value is     * returned.     *      * <pre>     * The file URL is read and each entry in this file checked. Each line     *  in the URL file can be one of -     *  &lt;IP&gt;&lt;space&gt;#&lt;comments&gt;     *  or     *  &lt;IP&gt;     *  or     *  #&lt;comments&gt;     *      *  Lines starting with a '#' are ignored and so are characters after     *  a '&lt;space&gt;#' in a line.     * </pre>     *      * @param addr     *            The interface to test against the package's URL     * @param url     *            The url file to read     *      * @return True if the interface is included in the url, false otherwise.     */    private boolean interfaceInUrl(String addr, String url) {        boolean bRet = false;        // get list of IPs in this URL        java.util.List iplist = (java.util.List) m_urlIPMap.get(url);        if (iplist != null && iplist.size() > 0) {            bRet = iplist.contains(addr);        }        return bRet;    }    /**     * This method is used to determine if the named interface is included in     * the passed package definition. If the interface belongs to the package     * then a value of true is returned. If the interface does not belong to the     * package a false value is returned.     *      * <strong>Note: </strong>Evaluation of the interface against a package     * filter will only work if the IP is already in the database.     *      * @param iface     *            The interface to test against the package.     * @param pkg     *            The package to check for the inclusion of the interface.     *      * @return True if the interface is included in the package, false     *         otherwise.     */    public synchronized boolean interfaceInPackage(String iface, org.opennms.netmgt.config.threshd.Package pkg) {        Category log = ThreadCategory.getInstance(this.getClass());        boolean filterPassed = false;        // get list of IPs in this package        java.util.List ipList = (java.util.List) m_pkgIpMap.get(pkg);        if (ipList != null && ipList.size() > 0) {            filterPassed = ipList.contains(iface);        }        if (log.isDebugEnabled())            log.debug("interfaceInPackage: Interface " + iface + " passed filter for package " + pkg.getName() + "?: " + filterPassed);        if (!filterPassed)            return false;        //        // Ensure that the interface is in the specific list or        // that it is in the include range and is not excluded        //        boolean has_specific = false;        boolean has_range_include = false;        boolean has_range_exclude = false;        long addr = IPSorter.convertToLong(iface);        Enumeration eincs = pkg.enumerateIncludeRange();        while (!has_range_include && eincs.hasMoreElements()) {            IncludeRange rng = (IncludeRange) eincs.nextElement();            long start = IPSorter.convertToLong(rng.getBegin());            if (addr > start) {                long end = IPSorter.convertToLong(rng.getEnd());                if (addr <= end) {                    has_range_include = true;                }            } else if (addr == start) {                has_range_include = true;            }        }        Enumeration espec = pkg.enumerateSpecific();        while (!has_specific && espec.hasMoreElements()) {            long speca = IPSorter.convertToLong(espec.nextElement().toString());            if (speca == addr)                has_specific = true;        }        Enumeration eurl = pkg.enumerateIncludeUrl();        while (!has_specific && eurl.hasMoreElements()) {            has_specific = interfaceInUrl(iface, (String) eurl.nextElement());        }        Enumeration eex = pkg.enumerateExcludeRange();        while (!has_range_exclude && !has_specific && eex.hasMoreElements()) {            ExcludeRange rng = (ExcludeRange) eex.nextElement();            long start = IPSorter.convertToLong(rng.getBegin());            if (addr > start) {                long end = IPSorter.convertToLong(rng.getEnd());                if (addr <= end) {                    has_range_exclude = true;                }            } else if (addr == start) {                has_range_exclude = true;            }        }        return has_specific || (has_range_include && !has_range_exclude);    }    /**     * Returns true if the service is part of the package and the status of the     * service is set to "on". Returns false if the service is not in the     * package or it is but the status of the service is set to "off".     *      * @param svcName     *            The service name to lookup.     * @param pkg     *            The package to lookup up service.     */    public synchronized boolean serviceInPackageAndEnabled(String svcName, org.opennms.netmgt.config.threshd.Package pkg) {        boolean result = false;        Enumeration esvcs = pkg.enumerateService();        while (result == false && esvcs.hasMoreElements()) {            Service tsvc = (Service) esvcs.nextElement();            if (tsvc.getName().equalsIgnoreCase(svcName)) {                // Ok its in the package. Now check the                // status of the service                String status = tsvc.getStatus();                if (status.equals("on"))                    result = true;            }        }        return result;    }}

⌨️ 快捷键说明

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