networkelementfactory.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 854 行 · 第 1/2 页
JAVA
854 行
//// 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 Feb 05: Added ORDER BY to SQL statement.//// Orignal 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///package org.opennms.web.element;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;import java.util.HashMap;import java.util.Map;import java.util.Vector;import org.opennms.core.resource.Vault;import org.opennms.netmgt.EventConstants;/** * The source for all network element business objects (nodes, interfaces, * services). Encapsulates all lookup functionality for the network element * business objects in one place. * * To use this factory to lookup network elements, you must first initialize the * Vault with the database connection manager * and JDBC URL it will use. Call * the init method to initialize the factory. After that, you can call any * lookup methods. * * @author <A HREF="larry@opennms.org">Larry Karnowski </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> */public class NetworkElementFactory extends Object { /** * A mapping of service names (strings) to service identifiers (integers). */ protected static Map serviceName2IdMap; /** * A mapping of service identifiers (integers) to service names (strings). */ protected static Map serviceId2NameMap; /** * Private, empty constructor so that this class cannot be instantiated. All * of its methods should static and accessed through the class name. */ private NetworkElementFactory() { } /** * Translate a node id into a human-readable node label. Note these values * are not cached. * * @return A human-readable node name or null if the node id given does not * specify a real node. */ public static String getNodeLabel(int nodeId) throws SQLException { String label = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT NODELABEL FROM NODE WHERE NODEID = ?"); stmt.setInt(1, nodeId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { label = rs.getString("NODELABEL"); } rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return (label); } public static Node getNode(int nodeId) throws SQLException { Node node = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM NODE WHERE NODEID = ?"); stmt.setInt(1, nodeId); ResultSet rs = stmt.executeQuery(); Node[] nodes = rs2Nodes(rs); // what do I do if this actually returns more than one node? if (nodes.length > 0) { node = nodes[0]; } rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return node; } /** * Returns all non-deleted nodes. */ public static Node[] getAllNodes() throws SQLException { Node[] nodes = null; Connection conn = Vault.getDbConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM NODE WHERE NODETYPE != 'D' ORDER BY NODELABEL"); nodes = rs2Nodes(rs); rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return nodes; } /** * Returns all non-deleted nodes that have the given nodeLabel substring * somewhere in their nodeLabel. */ public static Node[] getNodesLike(String nodeLabel) throws SQLException { if (nodeLabel == null) { throw new IllegalArgumentException("Cannot take null parameters."); } Node[] nodes = null; nodeLabel = nodeLabel.toLowerCase(); Connection conn = Vault.getDbConnection(); try { StringBuffer buffer = new StringBuffer("%"); buffer.append(nodeLabel); buffer.append("%"); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM NODE WHERE LOWER(NODELABEL) LIKE ? AND NODETYPE != 'D' ORDER BY NODELABEL"); stmt.setString(1, buffer.toString()); ResultSet rs = stmt.executeQuery(); nodes = rs2Nodes(rs); rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return nodes; } /** * Returns all non-deleted nodes with an IP address like the rule given. */ public static Node[] getNodesWithIpLike(String iplike) throws SQLException { if (iplike == null) { throw new IllegalArgumentException("Cannot take null parameters."); } Node[] nodes = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT DISTINCT * FROM NODE, IPINTERFACE WHERE NODE.NODEID=IPINTERFACE.NODEID AND IPLIKE(IPINTERFACE.IPADDR,?) AND NODETYPE != 'D' ORDER BY NODELABEL"); stmt.setString(1, iplike); ResultSet rs = stmt.executeQuery(); nodes = rs2Nodes(rs); rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return nodes; } /** * Returns all non-deleted nodes that have the given service. */ public static Node[] getNodesWithService(int serviceId) throws SQLException { Node[] nodes = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM NODE WHERE NODEID IN (SELECT NODEID FROM IFSERVICES WHERE SERVICEID=?) AND NODETYPE != 'D' ORDER BY NODELABEL"); stmt.setInt(1, serviceId); ResultSet rs = stmt.executeQuery(); nodes = rs2Nodes(rs); rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return nodes; } /** * Resolve an IP address to a DNS hostname via the database. If no hostname * can be found, the given IP address is returned. */ public static String getHostname(String ipAddress) throws SQLException { if (ipAddress == null) { throw new IllegalArgumentException("Cannot take null parameters."); } String hostname = ipAddress; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT DISTINCT IPADDR, IPHOSTNAME FROM IPINTERFACE WHERE IPADDR=? AND IPHOSTNAME IS NOT NULL"); stmt.setString(1, ipAddress); ResultSet rs = stmt.executeQuery(); if (rs.next()) { hostname = (String) rs.getString("IPHOSTNAME"); } rs.close(); stmt.close(); } finally { Vault.releaseDbConnection(conn); } return hostname; } public static Interface getInterface(int nodeId, String ipAddress) throws SQLException { if (ipAddress == null) { throw new IllegalArgumentException("Cannot take null parameters."); } Interface intf = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM IPINTERFACE WHERE NODEID = ? AND IPADDR=?"); stmt.setInt(1, nodeId); stmt.setString(2, ipAddress); ResultSet rs = stmt.executeQuery(); Interface[] intfs = rs2Interfaces(rs); rs.close(); stmt.close(); augmentInterfacesWithSnmpData(intfs, conn); // what do I do if this actually returns more than one node? if (intfs.length > 0) { intf = intfs[0]; } } finally { Vault.releaseDbConnection(conn); } return intf; } public static Interface getInterface(int nodeId, String ipAddress, int ifindex) throws SQLException { if (ipAddress == null) { throw new IllegalArgumentException("Cannot take null parameters."); } Interface intf = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM IPINTERFACE WHERE NODEID = ? AND IPADDR=? AND IFINDEX=?"); stmt.setInt(1, nodeId); stmt.setString(2, ipAddress); stmt.setInt(3, ifindex); ResultSet rs = stmt.executeQuery(); Interface[] intfs = rs2Interfaces(rs); rs.close(); stmt.close(); augmentInterfacesWithSnmpData(intfs, conn); // what do I do if this actually returns more than one node? if (intfs.length > 0) { intf = intfs[0]; } } finally { Vault.releaseDbConnection(conn); } return intf; } public static Interface[] getInterfacesWithIpAddress(String ipAddress) throws SQLException { if (ipAddress == null) { throw new IllegalArgumentException("Cannot take null parameters."); } Interface[] intfs = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM IPINTERFACE WHERE IPADDR=?"); stmt.setString(1, ipAddress); ResultSet rs = stmt.executeQuery(); intfs = rs2Interfaces(rs); rs.close(); stmt.close(); augmentInterfacesWithSnmpData(intfs, conn); } finally { Vault.releaseDbConnection(conn); } return intfs; } public static Interface[] getAllInterfacesOnNode(int nodeId) throws SQLException { Interface[] intfs = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM IPINTERFACE WHERE NODEID = ?"); stmt.setInt(1, nodeId); ResultSet rs = stmt.executeQuery(); intfs = rs2Interfaces(rs); rs.close(); stmt.close(); augmentInterfacesWithSnmpData(intfs, conn); } finally { Vault.releaseDbConnection(conn); } return intfs; } public static Interface[] getActiveInterfacesOnNode(int nodeId) throws SQLException { Interface[] intfs = null; Connection conn = Vault.getDbConnection(); try { PreparedStatement stmt = conn.prepareStatement("SELECT * FROM IPINTERFACE WHERE NODEID = ? AND ISMANAGED != 'D'"); stmt.setInt(1, nodeId); ResultSet rs = stmt.executeQuery(); intfs = rs2Interfaces(rs); rs.close(); stmt.close(); augmentInterfacesWithSnmpData(intfs, conn); } finally { Vault.releaseDbConnection(conn); } return intfs; } /* * Returns all interfaces, including their SNMP information */ public static Interface[] getAllInterfaces() throws SQLException { return getAllInterfaces(true); } /* * Returns all interfaces, but only includes snmp data if includeSNMP is true * This may be useful for pages that don't need snmp data and don't want to execute * a sub-query per interface! */ public static Interface[] getAllInterfaces(boolean includeSNMP) throws SQLException { Interface[] intfs = null; Connection conn = Vault.getDbConnection(); try {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?