dbnodeentry.java

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

JAVA
1,908
字号
//// 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 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.capsd;import java.net.InetAddress;import java.net.UnknownHostException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import java.sql.Types;import java.text.ParseException;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.log4j.Category;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.DatabaseConnectionFactory;/** * <p> * This class is used to model a row of the <em>node</em> table from the * OpenNMS database. The node table is indexed by the elements <em>dpNode</em> * and <em>nodeID</em>. When a new element is created using the * <code>create</code> call a node id will be automatically defined. If the * name of the distribute poller is not passed to the create method, the it also * is assigned a default value. * </p> *  * <p> * Once loaded or create, the class tracks any changes and will write those * changes to the database whenever the <code>store</code> method is invoked. * If a database conneciton is not passed to the store method, then a temporary * one is allocated to write the results. * </p> *  * <p> * NOTE: if the connection is passed in and is not in auto commit mode, then the * caller must call <code>commit</code> to inform the database that the * transaction is complete. *  * @author <a href="mailto:weave@oculan.com">Weave </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> *  */final class DbNodeEntry {    /**     * The character returned if the node is active     */    static final char NODE_TYPE_ACTIVE = 'A';    /**     * The character returned if the node is deleted     */    static final char NODE_TYPE_DELETED = 'D';    /**     * The character returned if the node type is unset/unknown.     */    static final char NODE_TYPE_UNKNOWN = ' ';    /**     * Label source set by user     */    static final char LABEL_SOURCE_USER = 'U';    /**     * Label source set by netbios     */    static final char LABEL_SOURCE_NETBIOS = 'N';    /**     * Label source set by hostname     */    static final char LABEL_SOURCE_HOSTNAME = 'H';    /**     * Label source set by SNMP sysname     */    static final char LABEL_SOURCE_SYSNAME = 'S';    /**     * Label source set by IP Address     */    static final char LABEL_SOURCE_ADDRESS = 'A';    /**     * Label source unset/unknown     */    static final char LABEL_SOURCE_UNKNOWN = ' ';    /**     * The default distributed poller name to use if one is not supplied     */    private static final String DEFAULT_DP_NAME = "localhost";    /**     * The SQL text used to extract the next sequence id for the node table.     */    private static final String SQL_NEXT_NID = "SELECT NEXTVAL('nodeNxtId')";    /**     * The SQL statement used to read a node from the database. This record is     * keyed by the node identifier and distributed poller name.     */    private static final String SQL_LOAD_REC = "SELECT nodeCreateTime, nodeParentID, nodeType, nodeSysOID, nodeSysName, nodeSysDescription, nodeSysLocation, nodeSysContact, nodeLabel, nodeLabelSource, nodeNetBIOSName, nodeDomainName, operatingSystem, lastCapsdPoll FROM node WHERE nodeID = ? AND dpName = ? AND nodeType != 'D'";    /**     * The SQL statement used to read the list of IP Addresses associated with     * this node.     */    private static final String SQL_LOAD_IF_LIST = "SELECT ipAddr, ifIndex FROM ipInterface WHERE nodeID = ? AND isManaged != 'D'";    /**     * The SQL statement used to read the list of managed IP Addresses     * associated with this node.     */    private static final String SQL_LOAD_MANAGED_IF_LIST = "SELECT ipAddr, ifIndex FROM ipInterface WHERE nodeID = ? AND isManaged = 'M'";    /**     * The SQL statement used to read the list of SNMP interface entries for     * this particular node.     */    private static final String SQL_LOAD_SNMP_LIST = "SELECT ipAddr, snmpIfIndex FROM snmpInterface WHERE nodeID = ?";    /**     * True if this recored was loaded from the database. False if it's new.     */    private boolean m_fromDb;    /**     * The node identifier     */    private int m_nodeId;    /**     * The name of the distributed poller     */    private String m_dpName;    /**     * The date the record was created, if any     */    private Timestamp m_createTime;    /**     * The parent identifier, if any     */    private int m_parentId;    /**     * The type of node, active or deleted.     */    private char m_type;    /**     * SNMP system object identifier     */    private String m_sysoid;    /**     * SNMP system name     */    private String m_sysname;    /**     * SNMP system description     */    private String m_sysdescr;    /**     * SNMP system location     */    private String m_syslocation;    /**     * SNMP system contact     */    private String m_syscontact;    /**     * The node's label     */    private String m_label;    /**     * Source of the label     */    private char m_labelSource;    /**     * The netbios name     */    private String m_nbName;    /**     * The netbios domain name     */    private String m_nbDomainName;    /**     * The operating system     */    private String m_os;    /**     * The last time the node was scanned.     */    private Timestamp m_lastPoll;    /**     * The bit map used to determine which elements have changed since the     * record was created.     */    private int m_changed;    // Mask fields    //    private static final int CHANGED_CREATE_TIME = 1 << 0;    private static final int CHANGED_PARENT_ID = 1 << 1;    private static final int CHANGED_TYPE = 1 << 2;    private static final int CHANGED_SYSOID = 1 << 3;    private static final int CHANGED_SYSNAME = 1 << 4;    private static final int CHANGED_SYSLOC = 1 << 5;    private static final int CHANGED_SYSCONTACT = 1 << 6;    private static final int CHANGED_LABEL = 1 << 7;    private static final int CHANGED_LABEL_SOURCE = 1 << 8;    private static final int CHANGED_NETBIOS_NAME = 1 << 9;    private static final int CHANGED_DOMAIN_NAME = 1 << 10;    private static final int CHANGED_OS = 1 << 11;    private static final int CHANGED_DPNAME = 1 << 12;    private static final int CHANGED_SYSDESCR = 1 << 13;    private static final int CHANGED_POLLTIME = 1 << 14;    /**     * Inserts the new node into the node table of the OpenNMS databasee.     *      * @param c     *            The connection to the database.     *      * @throws java.sql.SQLException     *             Thrown if an error occurs with the connection     */    private void insert(Connection c) throws SQLException {        if (m_fromDb)            throw new IllegalStateException("The record already exists in the database");        Category log = ThreadCategory.getInstance(getClass());        // Get the next node identifier        //        PreparedStatement stmt = null;        ResultSet rset = null;        try {            stmt = c.prepareStatement(SQL_NEXT_NID);            rset = stmt.executeQuery();            rset.next();            m_nodeId = rset.getInt(1);            rset.close();            rset = null;            stmt.close();            stmt = null;            // first extract the next node identifier            //            StringBuffer names = new StringBuffer("INSERT INTO node (nodeID,dpName");            StringBuffer values = new StringBuffer("?");            if ((m_changed & CHANGED_DPNAME) == CHANGED_DPNAME) {                values.append(",?");            } else {                values.append(",'").append(DEFAULT_DP_NAME).append("'");            }            if ((m_changed & CHANGED_PARENT_ID) == CHANGED_PARENT_ID) {                values.append(",?");                names.append(",nodeParentID");            }            if ((m_changed & CHANGED_TYPE) == CHANGED_TYPE) {                values.append(",?");                names.append(",nodeType");            }            if ((m_changed & CHANGED_CREATE_TIME) == CHANGED_CREATE_TIME) {                values.append(",?");                names.append(",nodeCreateTime");            } else {                values.append(",?");                names.append(",nodeCreateTime");                m_createTime = new Timestamp((new Date()).getTime());                m_changed |= CHANGED_CREATE_TIME;            }            if ((m_changed & CHANGED_SYSOID) == CHANGED_SYSOID) {                values.append(",?");                names.append(",nodeSysOID");            }            if ((m_changed & CHANGED_SYSNAME) == CHANGED_SYSNAME) {                values.append(",?");                names.append(",nodeSysName");            }            if ((m_changed & CHANGED_SYSDESCR) == CHANGED_SYSDESCR) {                values.append(",?");                names.append(",nodeSysDescription");            }            if ((m_changed & CHANGED_SYSLOC) == CHANGED_SYSLOC) {                values.append(",?");                names.append(",nodeSysLocation");            }            if ((m_changed & CHANGED_SYSCONTACT) == CHANGED_SYSCONTACT) {                values.append(",?");                names.append(",nodeSysContact");            }            if ((m_changed & CHANGED_LABEL) == CHANGED_LABEL) {                values.append(",?");                names.append(",nodeLabel");            }            if ((m_changed & CHANGED_LABEL_SOURCE) == CHANGED_LABEL_SOURCE) {                values.append(",?");                names.append(",nodeLabelSource");            }            if ((m_changed & CHANGED_NETBIOS_NAME) == CHANGED_NETBIOS_NAME) {                values.append(",?");                names.append(",nodeNetBIOSName");            }            if ((m_changed & CHANGED_DOMAIN_NAME) == CHANGED_DOMAIN_NAME) {                values.append(",?");                names.append(",nodeDomainName");            }            if ((m_changed & CHANGED_OS) == CHANGED_OS) {                values.append(",?");                names.append(",operatingSystem");            }            if ((m_changed & CHANGED_POLLTIME) == CHANGED_POLLTIME) {                values.append(",?");                names.append(",lastCapsdPoll");            }            names.append(") VALUES (").append(values).append(')');            log.debug("DbNodeEntry.insert: SQL insert statment = " + names.toString());            // create the Prepared statment and then            // start setting the result values            //            stmt = c.prepareStatement(names.toString());            names = null;            int ndx = 1;            stmt.setInt(ndx++, m_nodeId);            if ((m_changed & CHANGED_DPNAME) == CHANGED_DPNAME)                stmt.setString(ndx++, m_dpName);            if ((m_changed & CHANGED_PARENT_ID) == CHANGED_PARENT_ID)                if (m_parentId == -1)                    stmt.setNull(ndx++, Types.INTEGER);                else                    stmt.setInt(ndx++, m_parentId);            if ((m_changed & CHANGED_TYPE) == CHANGED_TYPE)                stmt.setString(ndx++, new String(new char[] { m_type }));            if ((m_changed & CHANGED_CREATE_TIME) == CHANGED_CREATE_TIME) {                stmt.setTimestamp(ndx++, m_createTime);            }            if ((m_changed & CHANGED_SYSOID) == CHANGED_SYSOID)                stmt.setString(ndx++, m_sysoid);            if ((m_changed & CHANGED_SYSNAME) == CHANGED_SYSNAME)                stmt.setString(ndx++, m_sysname);            if ((m_changed & CHANGED_SYSDESCR) == CHANGED_SYSDESCR)                stmt.setString(ndx++, m_sysdescr);            if ((m_changed & CHANGED_SYSLOC) == CHANGED_SYSLOC)                stmt.setString(ndx++, m_syslocation);            if ((m_changed & CHANGED_SYSCONTACT) == CHANGED_SYSCONTACT)                stmt.setString(ndx++, m_syscontact);            if ((m_changed & CHANGED_LABEL) == CHANGED_LABEL)                stmt.setString(ndx++, m_label);            if ((m_changed & CHANGED_LABEL_SOURCE) == CHANGED_LABEL_SOURCE)                stmt.setString(ndx++, new String(new char[] { m_labelSource }));            if ((m_changed & CHANGED_NETBIOS_NAME) == CHANGED_NETBIOS_NAME)                stmt.setString(ndx++, m_nbName);            if ((m_changed & CHANGED_DOMAIN_NAME) == CHANGED_DOMAIN_NAME)                stmt.setString(ndx++, m_nbDomainName);            if ((m_changed & CHANGED_OS) == CHANGED_OS)                stmt.setString(ndx++, m_os);            if ((m_changed & CHANGED_POLLTIME) == CHANGED_POLLTIME) {                stmt.setTimestamp(ndx++, m_lastPoll);            }            if (log.isDebugEnabled()) {                log.debug("nodeid='" + m_nodeId + "'" + " nodetype='" + new String(new char[] { m_type }) + "'" + " createTime='" + m_createTime + "'" + " lastPoll='" + m_lastPoll + "'" + " dpName='" + m_dpName + "'" + " sysname='" + m_sysname + "'" + " sysoid='" + m_sysoid + "'" + " sysdescr='" + m_sysdescr + "'" + " syslocation='" + m_syslocation + "'" + " syscontact='" + m_syscontact + "'" + " label='" + m_label + "'" + " labelsource='" + new String(new char[] { m_labelSource }) + "'" + " netbios='" + m_nbName + "'" + " domain='" + m_nbDomainName + "'" + " os='" + m_os + "'");            }            // Run the insert            //            int rc = stmt.executeUpdate();            log.debug("DbNodeEntry.insert: SQL update result = " + rc);            // Insert a null entry into the asset table            createAssetNodeEntry(c, m_nodeId);            // clear the mask and mark as backed

⌨️ 快捷键说明

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