discovery.java

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

JAVA
529
字号
//// 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.//// Modifications://// 2003 Oct 21: Fixed typo in variable name.// 2003 Jan 31: Cleaned up some unused imports.//// Original code base 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.discovery;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.net.MalformedURLException;import java.net.URL;import java.net.UnknownHostException;import java.sql.SQLException;import java.util.Enumeration;import java.util.LinkedList;import java.util.List;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.fiber.PausableFiber;import org.opennms.core.queue.FifoQueue;import org.opennms.core.queue.FifoQueueImpl;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.config.DiscoveryConfigFactory;import org.opennms.netmgt.config.discovery.DiscoveryConfiguration;import org.opennms.netmgt.config.discovery.IncludeRange;import org.opennms.netmgt.config.discovery.IncludeUrl;import org.opennms.netmgt.config.discovery.Specific;import org.opennms.netmgt.eventd.EventIpcManagerFactory;/** * This class is the main interface to the OpenNMS discovery service. The class * implements the <em>singleton</em> design pattern, in that there is only one * instance in any given virtual machine. The service delays the reading of * configuration information until the service is started. *  * @author <a href="mailto:weave@oculan.com">Brian Weaver </a> * @author <a href="http://www.opennms.org/">OpenNMS.org </a> *  */public final class Discovery implements PausableFiber {    /**     * The log4j category used to log messages.     */    private static final String LOG4J_CATEGORY = "OpenNMS.Discovery";    /**     * The string indicating the start of the comments in a line containing the     * IP address in a file URL     */    private final static String COMMENT_STR = " #";    /**     * This character at the start of a line indicates a comment line in a URL     * file     */    private final static char COMMENT_CHAR = '#';    /**     * The singular instance of the discovery service.     */    private static final Discovery m_singleton = new Discovery();    /**     * The IP Generator queue     */    private IPGenerator m_generator;    /**     * The fiber that generates and sends suspect events     */    private SuspectEventGenerator m_eventWriter;    /**     * The class instance used to recieve new events from for the system.     */    private BroadcastEventProcessor m_eventReader;    /**     * The ICMP Poller Manager class. This manager iterates over the address     * range, using the IPGenerator, checking for available systems.     */    private PingManager m_manager;    /**     * The current status of this fiber     */    private int m_status;    /**     * Constructs a new discovery instance.     */    private Discovery() {        m_generator = null;        m_eventWriter = null;        m_eventReader = null;        m_manager = null;        m_status = START_PENDING;    }    /**     * <pre>     * The file URL is read and a 'specific IP' is added for each entry     *  in this file. 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 specifics     *            the list to add to     * @param url     *            the URL file     * @param timeout     *            the timeout for all entries in this URL     * @param retries     *            the retries for all entries in this URL     */    private boolean addToSpecificsFromURL(List specifics, String url, long timeout, int retries) {        boolean bRet = true;        try {            // open the file indicated by the url            URL fileURL = new URL(url);            File file = new File(fileURL.getFile());            // check to see if the file exists            if (file.exists()) {                BufferedReader buffer = new BufferedReader(new FileReader(file));                String ipLine = null;                String specIP = null;                // get each line of the file and turn it into a specific range                while ((ipLine = buffer.readLine()) != null) {                    ipLine = ipLine.trim();                    if (ipLine.length() == 0 || ipLine.charAt(0) == COMMENT_CHAR) {                        // blank line or skip comment                        continue;                    }                    // check for comments after IP                    int comIndex = ipLine.indexOf(COMMENT_STR);                    if (comIndex == -1) {                        specIP = ipLine;                    } else {                        specIP = ipLine.substring(0, comIndex);                        ipLine = ipLine.trim();                    }                    try {                        specifics.add(new IPPollAddress(specIP, timeout, retries));                    } catch (UnknownHostException e) {                        ThreadCategory.getInstance().warn("Unknown host \'" + specIP + "\' read from URL \'" + url.toString() + "\': address ignored");                    }                    specIP = null;                }                buffer.close();            } else {                // log something                ThreadCategory.getInstance().warn("URL does not exist: " + url.toString());                bRet = true;            }        } catch (MalformedURLException e) {            ThreadCategory.getInstance().error("Error reading URL: " + url.toString() + ": " + e.getLocalizedMessage());            bRet = false;        } catch (FileNotFoundException e) {            ThreadCategory.getInstance().error("Error reading URL: " + url.toString() + ": " + e.getLocalizedMessage());            bRet = false;        } catch (IOException e) {            ThreadCategory.getInstance().error("Error reading URL: " + url.toString() + ": " + e.getLocalizedMessage());            bRet = false;        }        return bRet;    }    public synchronized void init() {        if (m_manager != null)            throw new IllegalStateException("The discovery service is already running");        ThreadCategory.setPrefix(LOG4J_CATEGORY);        Category log = ThreadCategory.getInstance();        // Initialize the Database configuration factory and verify        // that we can get a database connection.        //        java.sql.Connection ctest = null;        try {            DatabaseConnectionFactory.init();            ctest = DatabaseConnectionFactory.getInstance().getConnection();        } catch (IOException ie) {            log.fatal("IOException getting database connection", ie);            throw new UndeclaredThrowableException(ie);        } catch (MarshalException me) {            log.fatal("Marshall Exception getting database connection", me);            throw new UndeclaredThrowableException(me);        } catch (ValidationException ve) {            log.fatal("Validation Exception getting database connection", ve);            throw new UndeclaredThrowableException(ve);        } catch (SQLException sqlE) {            log.fatal("SQL Exception getting database connection", sqlE);            throw new UndeclaredThrowableException(sqlE);        } catch (ClassNotFoundException cnfE) {            log.fatal("Class Not Found Exception getting database connection", cnfE);            throw new UndeclaredThrowableException(cnfE);        } finally {            try {                if (ctest != null)                    ctest.close();            } catch (Exception e) {            }        }        // Initialize discovery configuration factory        DiscoveryConfigFactory dFactory = null;        try {

⌨️ 快捷键说明

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