eventfactory.java

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

JAVA
1,207
字号
//// 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.event;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.Date;import java.util.Vector;import org.opennms.core.resource.Vault;import org.opennms.web.event.filter.Filter;import org.opennms.web.event.filter.InterfaceFilter;import org.opennms.web.event.filter.NodeFilter;import org.opennms.web.event.filter.ServiceFilter;import org.opennms.web.event.filter.SeverityFilter;/** * Encapsulates all querying functionality for events. *  * @author <A HREF="mailto:larry@opennms.org">Lawrence Karnowski </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> */public class EventFactory extends Object {    /** Convenience class to determine sort style of a query. */    public static class SortStyle extends Object {        /* CORBA-style enumeration */        public static final int _SEVERITY = 1;        public static final int _TIME = 2;        public static final int _NODE = 3;        public static final int _INTERFACE = 4;        public static final int _SERVICE = 5;        public static final int _POLLER = 6;        public static final int _ID = 7;        public static final SortStyle SEVERITY = new SortStyle("SEVERITY", _SEVERITY);        public static final SortStyle TIME = new SortStyle("TIME", _TIME);        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 POLLER = new SortStyle("POLLER", _POLLER);        public static final SortStyle ID = new SortStyle("ID", _ID);        public static final int _REVERSE_SEVERITY = 101;        public static final int _REVERSE_TIME = 102;        public static final int _REVERSE_NODE = 103;        public static final int _REVERSE_INTERFACE = 104;        public static final int _REVERSE_SERVICE = 105;        public static final int _REVERSE_POLLER = 106;        public static final int _REVERSE_ID = 107;        public static final SortStyle REVERSE_SEVERITY = new SortStyle("REVERSE_SEVERITY", _REVERSE_SEVERITY);        public static final SortStyle REVERSE_TIME = new SortStyle("REVERSE_TIME", _REVERSE_TIME);        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_POLLER = new SortStyle("REVERSE_POLLER", _REVERSE_POLLER);        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 ("Event.SortStyle." + this.name);        }        public String getName() {            return (this.name);        }        public int getId() {            return (this.id);        }    }    /** Convenience class to determine what sort of events to include in a query. */    public static class AcknowledgeType extends Object {        /* CORBA-style enumeration */        public static final int _ACKNOWLEDGED = 1;        public static final int _UNACKNOWLEDGED = 2;        public static final int _BOTH = 3;        public static final AcknowledgeType ACKNOWLEDGED = new AcknowledgeType("ACKNOWLEDGED", _ACKNOWLEDGED);        public static final AcknowledgeType UNACKNOWLEDGED = new AcknowledgeType("UNACKNOWLEDGED", _UNACKNOWLEDGED);        public static final AcknowledgeType BOTH = new AcknowledgeType("BOTH", _BOTH);        protected String name;        protected int id;        private AcknowledgeType(String name, int id) {            this.name = name;            this.id = id;        }        public String toString() {            return ("Event.AcknowledgeType." + this.name);        }        public String getName() {            return (this.name);        }        public int getId() {            return (this.id);        }    }    /** Private constructor so this class cannot be instantiated. */    private EventFactory() {    }    /**     * Count all outstanding (unacknowledged) events.     */    public static int getEventCount() throws SQLException {        return getEventCount(AcknowledgeType.UNACKNOWLEDGED, new Filter[0]);    }    /**     * Count the number of events for a given acknowledgement type.     */    public static int getEventCount(AcknowledgeType ackType, Filter[] filters) throws SQLException {        if (ackType == null || filters == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        int eventCount = 0;        Connection conn = Vault.getDbConnection();        try {            StringBuffer select = new StringBuffer("SELECT COUNT(*) AS EVENTCOUNT FROM EVENTS LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE ");            select.append(getAcknowledgeTypeClause(ackType));            for (int i = 0; i < filters.length; i++) {                select.append(" AND");                select.append(filters[i].getSql());            }            select.append(" AND EVENTDISPLAY='Y' ");            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(select.toString());            if (rs.next()) {                eventCount = rs.getInt("EVENTCOUNT");            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return eventCount;    }    /**     * Count the number of events for a given acknowledgement type.     *      * @return An array of event counts. Each index of the array corresponds to     *         the event severity for the counts (indeterminate is 1, critical     *         is 7, etc).     */    public static int[] getEventCountBySeverity(AcknowledgeType ackType, Filter[] filters) throws SQLException {        if (ackType == null || filters == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        int[] eventCounts = new int[8];        Connection conn = Vault.getDbConnection();        try {            StringBuffer select = new StringBuffer("SELECT EVENTSEVERITY, COUNT(*) AS EVENTCOUNT FROM EVENTS LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE ");            select.append(getAcknowledgeTypeClause(ackType));            for (int i = 0; i < filters.length; i++) {                select.append(" AND");                select.append(filters[i].getSql());            }            select.append(" AND EVENTDISPLAY='Y'");            select.append(" GROUP BY EVENTSEVERITY");            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(select.toString());            while (rs.next()) {                int severity = rs.getInt("EVENTSEVERITY");                int eventCount = rs.getInt("EVENTCOUNT");                eventCounts[severity] = eventCount;            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return eventCounts;    }    /** Return a specific event. */    public static Event getEvent(int eventId) throws SQLException {        Event event = null;        Connection conn = Vault.getDbConnection();        try {            PreparedStatement stmt = conn.prepareStatement("SELECT EVENTS.*, NODE.NODELABEL, SERVICE.SERVICENAME FROM EVENTS LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE EVENTID=? ");            stmt.setInt(1, eventId);            ResultSet rs = stmt.executeQuery();            Event[] events = rs2Events(rs);            // what do I do if this actually returns more than one service?            if (events.length > 0) {                event = events[0];            }            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return event;    }    /** Return all unacknowledged events sorted by time. */    public static Event[] getEvents() throws SQLException {        return (EventFactory.getEvents(SortStyle.TIME, AcknowledgeType.UNACKNOWLEDGED));    }    /** Return all unacknowledged or acknowledged events sorted by time. */    public static Event[] getEvents(AcknowledgeType ackType) throws SQLException {        return (EventFactory.getEvents(SortStyle.TIME, ackType));    }    /** Return all unacknowledged events sorted by the given sort style. */    public static Event[] getEvents(SortStyle sortStyle) throws SQLException {        return (EventFactory.getEvents(sortStyle, AcknowledgeType.UNACKNOWLEDGED));    }    /**     * Return all events (optionally only unacknowledged events) sorted by the     * given sort style.     *      * @deprecated Replaced by     *             {@link " #getEvents(SortStyle,AcknowledgeType) getEvents(SortStyle, AcknowledgeType)"}     */    public static Event[] getEvents(SortStyle sortStyle, boolean includeAcknowledged) throws SQLException {        AcknowledgeType ackType = (includeAcknowledged) ? AcknowledgeType.BOTH : AcknowledgeType.UNACKNOWLEDGED;        return (EventFactory.getEvents(sortStyle, ackType));    }    /**     * Return all events (optionally only unacknowledged events) sorted by the     * given sort style.     */    public static Event[] getEvents(SortStyle sortStyle, AcknowledgeType ackType) throws SQLException {        return (EventFactory.getEvents(sortStyle, ackType, new Filter[0]));    }    /**     * Return all events (optionally only unacknowledged events) sorted by the     * given sort style.     */    public static Event[] getEvents(SortStyle sortStyle, AcknowledgeType ackType, Filter[] filters) throws SQLException {        return (EventFactory.getEvents(sortStyle, ackType, filters, -1, -1));    }    /**     * Return all events (optionally only unacknowledged events) 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 Event[] getEvents(SortStyle sortStyle, AcknowledgeType ackType, Filter[] filters, int limit, int offset) throws SQLException {        if (sortStyle == null || ackType == null || filters == null) {            throw new IllegalArgumentException("Cannot take null parameters.");        }        boolean useLimits = false;        if (limit > 0 && offset > -1) {            useLimits = true;        }        Event[] events = null;        Connection conn = Vault.getDbConnection();        try {            StringBuffer select = new StringBuffer("SELECT EVENTS.*, NODE.NODELABEL, SERVICE.SERVICENAME FROM EVENTS LEFT OUTER JOIN NODE USING(NODEID) LEFT OUTER JOIN SERVICE USING(SERVICEID) WHERE");            select.append(getAcknowledgeTypeClause(ackType));            for (int i = 0; i < filters.length; i++) {                select.append(" AND");                select.append(filters[i].getSql());            }            select.append(" AND EVENTDISPLAY='Y' ");            select.append(getOrderByClause(sortStyle));            if (useLimits) {                select.append(" LIMIT ");                select.append(limit);                select.append(" OFFSET ");                select.append(offset);            }            Statement stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(select.toString());            events = rs2Events(rs);            rs.close();            stmt.close();        } finally {            Vault.releaseDbConnection(conn);        }        return events;    }    /*

⌨️ 快捷键说明

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