dbsnmpinterfaceentry.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,065 行 · 第 1/3 页
JAVA
1,065 行
//// 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.Types;import org.apache.log4j.Category;import org.opennms.core.utils.ThreadCategory;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:weave@oculan.com">Weave </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> * */final class DbSnmpInterfaceEntry { /** * The SQL statement used to read a node from the database. This record is * keyed by the node identifier and the ifIndex. */ private static final String SQL_LOAD_REC = "SELECT ipAddr, snmpIpAdEntNetMask, snmpPhysAddr, snmpIfDescr, snmpIfType, snmpIfName, snmpIfSpeed, snmpIfAdminStatus, snmpIfOperStatus, snmpIfAlias FROM snmpInterface WHERE nodeID = ? AND snmpIfIndex = ?"; /** * 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; private InetAddress m_netmask; private String m_physAddr; private String m_ifDescription; private int m_ifType; private String m_ifName; private String m_ifAlias; private int m_ifSpeed; private int m_ifAdminStatus; private int m_ifOperStatus; /** * 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_IFADDRESS = 1 << 0; private static final int CHANGED_NETMASK = 1 << 1; private static final int CHANGED_PHYSADDR = 1 << 2; private static final int CHANGED_DESCRIPTION = 1 << 3; private static final int CHANGED_IFTYPE = 1 << 4; private static final int CHANGED_IFNAME = 1 << 5; private static final int CHANGED_IFSPEED = 1 << 6; private static final int CHANGED_IFADMINSTATUS = 1 << 7; private static final int CHANGED_IFOPERSTATUS = 1 << 8; private static final int CHANGED_IFALIAS = 1 << 9; /** * 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 snmpInterface (nodeID,snmpIfIndex,ipaddr"); StringBuffer values = new StringBuffer("?,?,?"); // We have to submit an ipaddr with an insert //if ((m_changed & CHANGED_IFADDRESS) == CHANGED_IFADDRESS) { // values.append(",?"); // names.append(",ipAddr"); //} if ((m_changed & CHANGED_NETMASK) == CHANGED_NETMASK) { values.append(",?"); names.append(",snmpIpAdEntNetMask"); } if ((m_changed & CHANGED_PHYSADDR) == CHANGED_PHYSADDR) { values.append(",?"); names.append(",snmpPhysAddr"); } if ((m_changed & CHANGED_DESCRIPTION) == CHANGED_DESCRIPTION) { values.append(",?"); names.append(",snmpIfDescr"); } if ((m_changed & CHANGED_IFTYPE) == CHANGED_IFTYPE) { values.append(",?"); names.append(",snmpIfType"); } if ((m_changed & CHANGED_IFNAME) == CHANGED_IFNAME) { values.append(",?"); names.append(",snmpIfName"); } if ((m_changed & CHANGED_IFSPEED) == CHANGED_IFSPEED) { values.append(",?"); names.append(",snmpIfSpeed"); } if ((m_changed & CHANGED_IFADMINSTATUS) == CHANGED_IFADMINSTATUS) { values.append(",?"); names.append(",snmpIfAdminStatus"); } if ((m_changed & CHANGED_IFOPERSTATUS) == CHANGED_IFOPERSTATUS) { values.append(",?"); names.append(",snmpIfOperStatus"); } if ((m_changed & CHANGED_IFALIAS) == CHANGED_IFALIAS) { values.append(",?"); names.append(",snmpIfAlias"); } names.append(") VALUES (").append(values).append(')'); log.debug("DbSnmpInterfaceEntry.insert: SQL insert statment = " + names.toString()); // create the Prepared statment and then // start setting the result values // PreparedStatement stmt = c.prepareStatement(names.toString()); names = null; int ndx = 1; stmt.setInt(ndx++, m_nodeId); stmt.setInt(ndx++, m_ifIndex); if ((m_changed & CHANGED_IFADDRESS) == CHANGED_IFADDRESS) stmt.setString(ndx++, m_ipAddr.getHostAddress()); else stmt.setString(ndx++, "0.0.0.0"); if ((m_changed & CHANGED_NETMASK) == CHANGED_NETMASK) stmt.setString(ndx++, m_netmask.getHostAddress()); if ((m_changed & CHANGED_PHYSADDR) == CHANGED_PHYSADDR) stmt.setString(ndx++, m_physAddr); if ((m_changed & CHANGED_DESCRIPTION) == CHANGED_DESCRIPTION) stmt.setString(ndx++, m_ifDescription); if ((m_changed & CHANGED_IFTYPE) == CHANGED_IFTYPE) stmt.setInt(ndx++, m_ifType); if ((m_changed & CHANGED_IFNAME) == CHANGED_IFNAME) stmt.setString(ndx++, m_ifName); if ((m_changed & CHANGED_IFSPEED) == CHANGED_IFSPEED) stmt.setInt(ndx++, m_ifSpeed); if ((m_changed & CHANGED_IFADMINSTATUS) == CHANGED_IFADMINSTATUS) stmt.setInt(ndx++, m_ifAdminStatus); if ((m_changed & CHANGED_IFOPERSTATUS) == CHANGED_IFOPERSTATUS) stmt.setInt(ndx++, m_ifOperStatus); if ((m_changed & CHANGED_IFALIAS) == CHANGED_IFALIAS) stmt.setString(ndx++, m_ifAlias); // Run the insert // int rc = stmt.executeUpdate(); log.debug("DbSnmpInterfaceEntry.insert: SQL update result = " + rc); 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 snmpInterface SET "); char comma = ' '; if ((m_changed & CHANGED_IFADDRESS) == CHANGED_IFADDRESS) { sqlText.append(comma).append("ipAddr = ?"); comma = ','; } if ((m_changed & CHANGED_NETMASK) == CHANGED_NETMASK) { sqlText.append(comma).append("snmpIpAdEntNetMask = ?"); comma = ','; } if ((m_changed & CHANGED_PHYSADDR) == CHANGED_PHYSADDR) { sqlText.append(comma).append("snmpPhysAddr = ?"); comma = ','; } if ((m_changed & CHANGED_DESCRIPTION) == CHANGED_DESCRIPTION) { sqlText.append(comma).append("snmpIfDescr = ?"); comma = ','; } if ((m_changed & CHANGED_IFTYPE) == CHANGED_IFTYPE) { sqlText.append(comma).append("snmpIfType = ?"); comma = ','; } if ((m_changed & CHANGED_IFNAME) == CHANGED_IFNAME) { sqlText.append(comma).append("snmpIfName = ?"); comma = ','; } if ((m_changed & CHANGED_IFSPEED) == CHANGED_IFSPEED) { sqlText.append(comma).append("snmpIfSpeed = ?"); comma = ','; } if ((m_changed & CHANGED_IFADMINSTATUS) == CHANGED_IFADMINSTATUS) { sqlText.append(comma).append("snmpIfAdminStatus = ?"); comma = ','; } if ((m_changed & CHANGED_IFOPERSTATUS) == CHANGED_IFOPERSTATUS) { sqlText.append(comma).append("snmpIfOperStatus = ?"); comma = ','; } if ((m_changed & CHANGED_IFALIAS) == CHANGED_IFALIAS) { sqlText.append(comma).append("snmpIfAlias = ?"); comma = ','; } sqlText.append(" WHERE nodeID = ? AND snmpIfIndex = ? "); log.debug("DbSnmpInterfaceEntry.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());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?