dbipinterfaceentry.java

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

JAVA
1,120
字号
//// 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://// 2005 Jan 03: Minor change to support ifindex for lame snmp hosts// 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.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> * 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:jamesz@opennms.com">James Zuo </a> * @author <a href="mailto:weave@oculan.com">Weave </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> *  */public final class DbIpInterfaceEntry {    public final static char SNMP_PRIMARY = 'P';    public final static char SNMP_SECONDARY = 'S';    public final static char SNMP_COLLECT = 'C';    public final static char SNMP_NOT_ELIGIBLE = 'N';    public final static char SNMP_UNKNOWN = ' ';    public final static char STATE_MANAGED = 'M';    public final static char STATE_UNMANAGED = 'U';    public final static char STATE_ALIAS = 'A';    public final static char STATE_DELETED = 'D';    public final static char STATE_FORCED = 'F';    public final static char STATE_NOT_POLLED = 'N';    public final static char STATE_UNKNOWN = ' ';    /**     * 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 ifIndex, ipHostname, isManaged, ipStatus, ipLastCapsdPoll, isSnmpPrimary FROM ipInterface WHERE nodeID = ? AND ipAddr = ? AND isManaged != 'D'";    /**     * This is the SQL statement used to load the list of service identifiers     * associated with this interface. Once the identifiers are known then a     * list of DbIfServiceEntry(s) can be returned to the caller.     */    private static final String SQL_LOAD_IFSVC_LIST = "SELECT serviceID FROM ifServices WHERE nodeID = ? AND ipAddr = ? AND status != 'D'";    /**     * This is the SQL statement used to load a record when the ifIndex is     * involved as part of the key lookup. This is mainly used when a node has     * multiple <em>unnamed</em> interfaces (i.e. 0.0.0.0) but each has a     * different ifIndex.     */    private static final String SQL_LOAD_REC_IFINDEX = "SELECT ifIndex, ipHostname, isManaged, ipStatus, ipLastCapsdPoll, isSnmpPrimary FROM ipInterface WHERE nodeID = ? AND ipAddr = ? AND ifIndex = ? AND isManaged != 'D'";    /**     * 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 IP address.     */    private InetAddress m_ipAddr;    /**     * The SNMP ifIndex     */    private int m_ifIndex;    /**     * The hostname string, if any     */    private String m_hostname;    /**     * The status of the interface     */    private int m_status;    /**     * The managed status, if any     */    private char m_managedState;    /**     * The last time the interface was checked.     */    private Timestamp m_lastPoll;    /**     * The SNMP primary status.     */    private char m_primaryState;    /**     * 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_IFINDEX = 1 << 0;    private static final int CHANGED_HOSTNAME = 1 << 1;    private static final int CHANGED_MANAGED = 1 << 2;    private static final int CHANGED_STATUS = 1 << 3;    private static final int CHANGED_POLLTIME = 1 << 4;    private static final int CHANGED_PRIMARY = 1 << 5;    // Indicates that the ifIndex is to be used as database key    // for this entry.    //    private boolean m_useIfIndexAsKey;    /**     * Inserts the new interface into the ipInterface 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());        // first extract the next node identifier        //        StringBuffer names = new StringBuffer("INSERT INTO ipInterface (nodeID,ipAddr");        StringBuffer values = new StringBuffer("?,?");        if ((m_changed & CHANGED_IFINDEX) == CHANGED_IFINDEX) {            values.append(",?");            names.append(",ifIndex");        }        if ((m_changed & CHANGED_HOSTNAME) == CHANGED_HOSTNAME) {            values.append(",?");            names.append(",ipHostname");        }        if ((m_changed & CHANGED_MANAGED) == CHANGED_MANAGED) {            values.append(",?");            names.append(",isManaged");        }        if ((m_changed & CHANGED_STATUS) == CHANGED_STATUS) {            values.append(",?");            names.append(",ipStatus");        }        if ((m_changed & CHANGED_POLLTIME) == CHANGED_POLLTIME) {            values.append(",?");            names.append(",ipLastCapsdPoll");        }        if ((m_changed & CHANGED_PRIMARY) == CHANGED_PRIMARY) {            values.append(",?");            names.append(",isSnmpPrimary");        }        names.append(") VALUES (").append(values).append(')');        log.debug("DbIpInterfaceEntry.insert: SQL insert statment = " + names.toString());        // create the Prepared statment and then        // start setting the result values        //        PreparedStatement stmt = null;                try {            stmt = c.prepareStatement(names.toString());            names = null;            int ndx = 1;            stmt.setInt(ndx++, m_nodeId);            stmt.setString(ndx++, m_ipAddr.getHostAddress());            if ((m_changed & CHANGED_IFINDEX) == CHANGED_IFINDEX)                stmt.setInt(ndx++, m_ifIndex);            if ((m_changed & CHANGED_HOSTNAME) == CHANGED_HOSTNAME)                stmt.setString(ndx++, m_hostname);            if ((m_changed & CHANGED_MANAGED) == CHANGED_MANAGED)                stmt.setString(ndx++, new String(new char[] { m_managedState }));            if ((m_changed & CHANGED_STATUS) == CHANGED_STATUS)                stmt.setInt(ndx++, m_status);            if ((m_changed & CHANGED_POLLTIME) == CHANGED_POLLTIME) {                stmt.setTimestamp(ndx++, m_lastPoll);            }            if ((m_changed & CHANGED_PRIMARY) == CHANGED_PRIMARY)                stmt.setString(ndx++, new String(new char[] { m_primaryState }));            // Run the insert            //            int rc = stmt.executeUpdate();            log.debug("DbIpInterfaceEntry.insert: SQL update result = " + rc);        } finally {            if (stmt !=null) stmt.close();        }        // clear the mask and mark as backed        // by the database        //        m_fromDb = true;        m_changed = 0;    }    /**     * Updates an existing record in the OpenNMS ipInterface table.     *      * @param c     *            The connection used for the update.     *      * @throws java.sql.SQLException     *             Thrown if an error occurs with the connection     */    private void update(Connection c) throws SQLException {        if (!m_fromDb)            throw new IllegalStateException("The record does not exists in the database");        Category log = ThreadCategory.getInstance(getClass());        // first extract the next node identifier        //        StringBuffer sqlText = new StringBuffer("UPDATE ipInterface SET ");        char comma = ' ';        if ((m_changed & CHANGED_IFINDEX) == CHANGED_IFINDEX) {            sqlText.append(comma).append("ifIndex = ?");            comma = ',';        }        if ((m_changed & CHANGED_HOSTNAME) == CHANGED_HOSTNAME) {            sqlText.append(comma).append("ipHostname = ?");            comma = ',';        }        if ((m_changed & CHANGED_MANAGED) == CHANGED_MANAGED) {            sqlText.append(comma).append("isManaged = ?");            comma = ',';        }        if ((m_changed & CHANGED_STATUS) == CHANGED_STATUS) {            sqlText.append(comma).append("ipStatus = ?");            comma = ',';        }        if ((m_changed & CHANGED_POLLTIME) == CHANGED_POLLTIME) {            sqlText.append(comma).append("ipLastCapsdPoll = ?");            comma = ',';        }        if ((m_changed & CHANGED_PRIMARY) == CHANGED_PRIMARY) {            sqlText.append(comma).append("isSnmpPrimary = ?");            comma = ',';        }        if (m_useIfIndexAsKey) {            sqlText.append(" WHERE nodeID = ? AND ipAddr = ? AND ifIndex = ?");        } else {            sqlText.append(" WHERE nodeID = ? AND ipAddr = ?");        }        sqlText.append(" AND isManaged <> 'D'");        log.debug("DbIpInterfaceEntry.update: SQL update statment = " + sqlText.toString());        // create the Prepared statment and then        // start setting the result values        //        PreparedStatement stmt = null;        try {            stmt = c.prepareStatement(sqlText.toString());            sqlText = null;            int ndx = 1;            if ((m_changed & CHANGED_IFINDEX) == CHANGED_IFINDEX) {                if (m_ifIndex == -1)                    stmt.setNull(ndx++, Types.INTEGER);                else                    stmt.setInt(ndx++, m_ifIndex);            }            if ((m_changed & CHANGED_HOSTNAME) == CHANGED_HOSTNAME) {                if (m_hostname != null)                    stmt.setString(ndx++, m_hostname);                else

⌨️ 快捷键说明

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