threshdconfigfactory.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 479 行 · 第 1/2 页
JAVA
479 行
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Copyright (C) 1999-2001 Oculan Corp. All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details. //// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// // For more information contact: // OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///// Tab Size = 8//package org.opennms.netmgt.config;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringWriter;import java.io.FileWriter;import java.util.Enumeration;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.log4j.Category;import org.apache.log4j.Priority;import org.exolab.castor.xml.Marshaller;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.Unmarshaller;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.ConfigFileConstants;import org.opennms.netmgt.config.threshd.ExcludeRange;import org.opennms.netmgt.config.threshd.IncludeRange;import org.opennms.netmgt.config.threshd.Service;import org.opennms.netmgt.config.threshd.ThreshdConfiguration;import org.opennms.netmgt.filter.Filter;import org.opennms.netmgt.utils.IPSorter;import org.opennms.netmgt.utils.IpListFromUrl;/** * This is the singleton class used to load the configuration for the OpenNMS * Thresholding Daemon from the threshd-configuration xml file. * * A mapping of the configured URLs to the iplist they contain is built at * init() time so as to avoid numerous file reads. * * <strong>Note: </strong>Users of this class should make sure the * <em>init()</em> is called before calling any other method to ensure the * config is loaded before accessing other convenience methods. * * @author <a href="mailto:jamesz@opennms.com>James Zuo </a> * @author <a href="mailto:mike@opennms.org">Mike Davidson </a> * @author <a href="mailto:sowmya@opennms.org">Sowmya Nataraj </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> */public final class ThreshdConfigFactory { /** * The singleton instance of this factory */ private static ThreshdConfigFactory m_singleton = null; /** * This member is set to true if the configuration file has been loaded. */ private static boolean m_loaded = false; /** * The config class loaded from the config file */ private ThreshdConfiguration m_config; /** * A mapping of the configured URLs to a list of the specific IPs configured * in each - so as to avoid file reads */ private Map m_urlIPMap; /** * A mapping of the configured package to a list of IPs selected via filter * rules, so as to avoid repetetive database access. */ private Map m_pkgIpMap; /** * A boolean flag to indicate If a filter rule agaist the local OpenNMS * server has to be used. */ private static boolean m_verifyServer; /** * The name of the local OpenNMS server */ private static String m_localServer; /** * Go through the configuration and build a mapping of each configured URL * to a list of IPs configured in that URL - done at init() time so that * repeated file reads can be avoided */ private void createUrlIpMap() { m_urlIPMap = new HashMap(); Enumeration pkgEnum = m_config.enumeratePackage(); while (pkgEnum.hasMoreElements()) { org.opennms.netmgt.config.threshd.Package pkg = (org.opennms.netmgt.config.threshd.Package) pkgEnum.nextElement(); Enumeration urlEnum = pkg.enumerateIncludeUrl(); while (urlEnum.hasMoreElements()) { String urlname = (String) urlEnum.nextElement(); java.util.List iplist = IpListFromUrl.parse(urlname); if (iplist.size() > 0) { m_urlIPMap.put(urlname, iplist); } } } } /** * This method is used to establish package agaist an iplist iplist mapping, * with which, the iplist is selected per package via the configured filter * rules from the database. */ private void createPackageIpListMap() { Category log = ThreadCategory.getInstance(this.getClass()); m_pkgIpMap = new HashMap(); Enumeration pkgEnum = m_config.enumeratePackage(); while (pkgEnum.hasMoreElements()) { org.opennms.netmgt.config.threshd.Package pkg = (org.opennms.netmgt.config.threshd.Package) pkgEnum.nextElement(); // // Get a list of ipaddress per package agaist the filter rules from // database and populate the package, IP list map. // Filter filter = new Filter(); StringBuffer filterRules = new StringBuffer(pkg.getFilter().getContent()); try { if (m_verifyServer) { filterRules.append(" & (serverName == "); filterRules.append('\"'); filterRules.append(m_localServer); filterRules.append('\"'); filterRules.append(")"); } if (log.isDebugEnabled()) log.debug("createPackageIpMap: package is " + pkg.getName() + ". filer rules are " + filterRules.toString()); List ipList = filter.getIPList(filterRules.toString()); if (ipList.size() > 0) { m_pkgIpMap.put(pkg, ipList); } } catch (Throwable t) { if (log.isEnabledFor(Priority.ERROR)) { log.error("createPackageIpMap: failed to map package: " + pkg.getName() + " to an IP List", t); } } } } /** * This nethod is used to rebuild the package agaist iplist mapping when * needed. When a node gained service event occurs, threshd has to determine * which package the ip/service combination is in, but if the interface is a * newly added one, the package iplist should be rebuilt so that threshd * could know which package this ip/service pair is in. */ public synchronized void rebuildPackageIpListMap() { createPackageIpListMap(); } /** * Private constructor * * @exception java.io.IOException * Thrown if the specified config file cannot be read * @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. */ private ThreshdConfigFactory(String configFile) throws IOException, MarshalException, ValidationException { InputStream cfgIn = new FileInputStream(configFile); m_config = (ThreshdConfiguration) Unmarshaller.unmarshal(ThreshdConfiguration.class, new InputStreamReader(cfgIn)); cfgIn.close(); createUrlIpMap(); OpennmsServerConfigFactory.init(); m_verifyServer = OpennmsServerConfigFactory.getInstance().verifyServer(); m_localServer = OpennmsServerConfigFactory.getInstance().getServerName(); createPackageIpListMap(); } /** * Load the config from the default config file and create the singleton * instance of this factory. * * @exception java.io.IOException * Thrown if the specified config file cannot be read * @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 init() throws IOException, MarshalException, ValidationException { if (m_loaded) { // init already called - return // to reload, reload() will need to be called
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?