outagefactory.java

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

JAVA
698
字号
//// 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.//// 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.outage;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;import java.util.ArrayList;import org.apache.log4j.Category;import org.opennms.core.resource.Vault;import org.opennms.core.utils.ThreadCategory;import org.opennms.web.outage.filter.Filter;import org.opennms.web.outage.filter.InterfaceFilter;import org.opennms.web.outage.filter.NodeFilter;import org.opennms.web.outage.filter.ServiceFilter;/** * Encapsulates all querying functionality for outages. *  * @author <A HREF="mailto:larry@opennms.org">Lawrence Karnowski </A> * @author <A HREF="mailto:jason@opennms.org">Jason Johns </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> */public class OutageFactory extends Object {    /** Convenience class to determine sort style of a query. */    public static class SortStyle extends Object {        /* CORBA-style enumeration */        public static final int _NODE = 1;        public static final int _INTERFACE = 2;        public static final int _SERVICE = 3;        public static final int _IFLOSTSERVICE = 4;        public static final int _IFREGAINEDSERVICE = 5;        public static final int _ID = 6;        public static final SortStyle NODE = new SortStyle("NODE", _NODE);        public static final SortStyle INTERFACE = new SortStyle("INTERFACE", _INTERFACE);        public static final SortStyle SERVICE = new SortStyle("SERVICE", _SERVICE);        public static final SortStyle IFLOSTSERVICE = new SortStyle("IFLOSTSERVICE", _IFLOSTSERVICE);        public static final SortStyle IFREGAINEDSERVICE = new SortStyle("IFREGAINEDSERVICE", _IFREGAINEDSERVICE);        public static final SortStyle ID = new SortStyle("ID", _ID);        public static final int _REVERSE_NODE = 101;        public static final int _REVERSE_INTERFACE = 102;        public static final int _REVERSE_SERVICE = 103;        public static final int _REVERSE_IFLOSTSERVICE = 104;        public static final int _REVERSE_IFREGAINEDSERVICE = 105;        public static final int _REVERSE_ID = 106;        public static final SortStyle REVERSE_NODE = new SortStyle("REVERSE_NODE", _REVERSE_NODE);        public static final SortStyle REVERSE_INTERFACE = new SortStyle("REVERSE_INTERFACE", _REVERSE_INTERFACE);        public static final SortStyle REVERSE_SERVICE = new SortStyle("REVERSE_SERVICE", _REVERSE_SERVICE);        public static final SortStyle REVERSE_IFLOSTSERVICE = new SortStyle("REVERSE_IFLOSTSERVICE", _REVERSE_IFLOSTSERVICE);        public static final SortStyle REVERSE_IFREGAINEDSERVICE = new SortStyle("REVERSE_IFREGAINEDSERVICE", _REVERSE_IFREGAINEDSERVICE);        public static final SortStyle REVERSE_ID = new SortStyle("REVERSE_ID", _REVERSE_ID);        protected String name;        protected int id;        private SortStyle(String name, int id) {            this.name = name;            this.id = id;        }        public String toString() {            return ("Outage.SortStyle." + this.name);        }        public String getName() {            return (this.name);        }        public int getId() {            return (this.id);        }    }    /**     * Convenience class to determine what sort of notices to include in a     * query.     */    public static class OutageType extends Object {        /* CORBA-style enumeration */        public static final int _CURRENT = 1;        public static final int _RESOLVED = 2;        public static final int _BOTH = 3;        public static final OutageType CURRENT = new OutageType("CURRENT", _CURRENT);        public static final OutageType RESOLVED = new OutageType("RESOLVED", _RESOLVED);        public static final OutageType BOTH = new OutageType("BOTH", _BOTH);        protected String name;        protected int id;        private OutageType(String name, int id) {            this.name = name;            this.id = id;        }        public String toString() {            return ("Outage.OutageType." + this.name);        }        public String getName() {            return (this.name);        }        public int getId() {            return (this.id);        }    }    public static final SortStyle DEFAULT_SORT_STYLE = SortStyle.ID;    protected static final Category log = ThreadCategory.getInstance("OutageFactory");    /** Private constructor so this class cannot be instantiated. */    private OutageFactory() {    }    /**     * Return the count of current outages.     *      * <p>     * Note: This method has been optimized for the simplest query.     * </p>     */    public static int getOutageCount() throws SQLException {        int outageCount = 0;        Connection conn = Vault.getDbConnection();        try {            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery("SELECT COUNT(OUTAGEID) AS OUTAGECOUNT FROM OUTAGES " + "JOIN NODE USING(NODEID) " + "JOIN IPINTERFACE ON OUTAGES.NODEID=IPINTERFACE.NODEID AND OUTAGES.IPADDR=IPINTERFACE.IPADDR " + "JOIN IFSERVICES ON OUTAGES.NODEID=IFSERVICES.NODEID AND OUTAGES.IPADDR=IFSERVICES.IPADDR AND OUTAGES.SERVICEID=IFSERVICES.SERVICEID " + "WHERE IFREGAINEDSERVICE IS NULL " + "AND (NODE.NODETYPE != 'D' AND IPINTERFACE.ISMANAGED != 'D' AND IFSERVICES.STATUS != 'D') ");            if (rs.next()) {                outageCount = rs.getInt("OUTAGECOUNT");            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return outageCount;    }    /**     * Count the number of outages for a given acknowledgement type.     */    public static int getOutageCount(OutageType ackType, Filter[] filters) throws SQLException {        if (ackType == null || filters == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        int outageCount = 0;        Connection conn = Vault.getDbConnection();        try {            StringBuffer select = new StringBuffer("SELECT COUNT(OUTAGEID) AS OUTAGECOUNT FROM OUTAGES " + "JOIN NODE USING(NODEID) " + "JOIN IPINTERFACE ON OUTAGES.NODEID=IPINTERFACE.NODEID AND OUTAGES.IPADDR=IPINTERFACE.IPADDR " + "JOIN IFSERVICES ON OUTAGES.NODEID=IFSERVICES.NODEID AND OUTAGES.IPADDR=IFSERVICES.IPADDR AND OUTAGES.SERVICEID=IFSERVICES.SERVICEID " + "LEFT OUTER JOIN SERVICE ON OUTAGES.SERVICEID=SERVICE.SERVICEID " + "LEFT OUTER JOIN NOTIFICATIONS ON SVCLOSTEVENTID=NOTIFICATIONS.NOTIFYID " + "WHERE (NODE.NODETYPE != 'D' AND IPINTERFACE.ISMANAGED != 'D' AND IFSERVICES.STATUS != 'D') " + "AND ");            select.append(getOutageTypeClause(ackType));            for (int i = 0; i < filters.length; i++) {                select.append(" AND ");                select.append(filters[i].getSql());            }            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(select.toString());            if (rs.next()) {                outageCount = rs.getInt("OUTAGECOUNT");            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return outageCount;    }    public static Outage getOutage(int outageId) throws SQLException {        Outage outage = null;        Connection conn = Vault.getDbConnection();        try {            PreparedStatement stmt = conn.prepareStatement("SELECT OUTAGES.*, NODE.NODELABEL, IPINTERFACE.IPHOSTNAME, SERVICE.SERVICENAME, NOTIFICATIONS.NOTIFYID, NOTIFICATIONS.ANSWEREDBY FROM OUTAGES " + "JOIN NODE USING(NODEID) " + "JOIN IPINTERFACE ON OUTAGES.NODEID=IPINTERFACE.NODEID AND OUTAGES.IPADDR=IPINTERFACE.IPADDR " + "LEFT OUTER JOIN SERVICE USING(SERVICEID) " + "LEFT OUTER JOIN NOTIFICATIONS ON SVCLOSTEVENTID=NOTIFICATIONS.EVENTID " + "WHERE OUTAGEID=?");            stmt.setInt(1, outageId);            ResultSet rs = stmt.executeQuery();            Outage[] outages = rs2Outages(rs);            if (outages != null && outages.length > 0) {                outage = outages[0];            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return outage;    }    /**     * Return all unresolved outages sorted by the default sort style, outage     * identifier.     */    public static Outage[] getOutages() throws SQLException {        return OutageFactory.getOutages(DEFAULT_SORT_STYLE, OutageType.CURRENT, new Filter[0], -1, -1);    }    /** Return all unresolved outages sorted by the given sort style. */    public static Outage[] getOutages(SortStyle sortStyle) throws SQLException {        return OutageFactory.getOutages(sortStyle, OutageType.CURRENT, new Filter[0], -1, -1);    }    /**     * Return all outages (optionally only unresolved outages) sorted by the     * given sort style.     */    public static Outage[] getOutages(SortStyle sortStyle, OutageType outType) throws SQLException {        return OutageFactory.getOutages(sortStyle, outType, new Filter[0], -1, -1);    }    /**     * Return all outages (optionally only unresolved outages) sorted by the     * given sort style.     */    public static Outage[] getOutages(SortStyle sortStyle, OutageType outType, Filter[] filters) throws SQLException {        return OutageFactory.getOutages(sortStyle, outType, filters, -1, -1);    }    /**     * Return all notices (optionally only unacknowledged notices) sorted by the     * given sort style.     *      * <p>     * <strong>Note: </strong> This limit/offset code is <em>Postgres      * specific!</em>     * Per <a href="mailto:shaneo@opennms.org">Shane </a>, this is okay for now     * until we can come up with an Oracle alternative too.     * </p>     *      * @param limit     *            if -1 or zero, no limit or offset is used     * @param offset     *            if -1, no limit or offset if used     */    public static Outage[] getOutages(SortStyle sortStyle, OutageType outType, Filter[] filters, int limit, int offset) throws SQLException {        if (sortStyle == null || outType == null || filters == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        boolean useLimits = false;        if (limit > 0 && offset > -1) {            useLimits = true;        }        Outage[] outages = null;        Connection conn = Vault.getDbConnection();        try {            StringBuffer select = new StringBuffer("SELECT OUTAGES.*, NODE.NODELABEL, IPINTERFACE.IPHOSTNAME, SERVICE.SERVICENAME, NOTIFICATIONS.NOTIFYID, NOTIFICATIONS.ANSWEREDBY FROM OUTAGES " + "JOIN NODE USING(NODEID) " + "JOIN IPINTERFACE ON OUTAGES.NODEID=IPINTERFACE.NODEID AND OUTAGES.IPADDR=IPINTERFACE.IPADDR " + "JOIN IFSERVICES ON OUTAGES.NODEID=IFSERVICES.NODEID AND OUTAGES.IPADDR=IFSERVICES.IPADDR AND OUTAGES.SERVICEID=IFSERVICES.SERVICEID " + "LEFT OUTER JOIN SERVICE ON OUTAGES.SERVICEID=SERVICE.SERVICEID " + "LEFT OUTER JOIN NOTIFICATIONS ON SVCLOSTEVENTID=NOTIFICATIONS.EVENTID " + "WHERE (NODE.NODETYPE != 'D' AND IPINTERFACE.ISMANAGED != 'D' AND IFSERVICES.STATUS != 'D') " + "AND ");            select.append(getOutageTypeClause(outType));            for (int i = 0; i < filters.length; i++) {                select.append(" AND ");                select.append(filters[i].getSql());            }            select.append(getOrderByClause(sortStyle));            if (useLimits) {                select.append(" LIMIT ");                select.append(limit);                select.append(" OFFSET ");                select.append(offset);            }            log.debug(select.toString());            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(select.toString());

⌨️ 快捷键说明

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