📄 notificationmanager.java
字号:
} // notifications interfaceID field String ipaddr = (String) params.get(NotificationManager.PARAM_INTERFACE); if (ipaddr != null && !ipaddr.trim().equals("") && !ipaddr.toLowerCase().equals("null") && !ipaddr.toLowerCase().equals("%interface%")) { statement.setString(6, ipaddr); } else { statement.setString(6, null); } // notifications serviceID field String service = (String) params.get(NotificationManager.PARAM_SERVICE); if (service != null && !service.trim().equals("") && !service.toLowerCase().equals("null") && !service.toLowerCase().equals("%service%")) { statement.setInt(7, getServiceId(service)); } else { statement.setNull(7, Types.INTEGER); } // eventID field String eventID = (String) params.get("eventID"); statement.setInt(8, Integer.parseInt(eventID)); statement.setString(9, (String) params.get("eventUEI")); // notifications subject field statement.setString(10, (String) params.get(NotificationManager.PARAM_SUBJECT)); // the queue this will be sent on statement.setString(11, queueID); statement.executeUpdate(); statement.close(); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } } /** * This method queries the database in search of a service id for a given * serivice name * * @param service * the name of the service * @return the serviceID of the service */ private int getServiceId(String service) throws SQLException { int serviceID = 0; Connection connection = null; try { connection = getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT serviceID from service where serviceName = ?"); statement.setString(1, service); ResultSet results = statement.executeQuery(); results.next(); serviceID = results.getInt(1); results.close(); statement.close(); return serviceID; } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } } /** * */ public Map getNotifications() throws IOException, MarshalException, ValidationException { update(); Map newMap = new HashMap(); Notification notices[] = m_notifications.getNotification(); for (int i = 0; i < notices.length; i++) { newMap.put(notices[i].getName(), notices[i]); } return newMap; } /** * */ public List getServiceNames() throws SQLException { Connection connection = null; List services = new ArrayList(); try { connection = getConnection(); Statement stmt = connection.createStatement(); ResultSet rset = stmt.executeQuery("SELECT servicename FROM service"); if (rset != null) { // Iterate through the result and build the array list while (rset.next()) { services.add(rset.getString(1)); } } } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } return services; } /** */ public Notification getNotification(String name) throws IOException, MarshalException, ValidationException { update(); return (Notification) getNotifications().get(name); } /** */ public List getNotificationNames() throws IOException, MarshalException, ValidationException { update(); List notificationNames = new ArrayList(); for (Enumeration e = m_notifications.enumerateNotification(); e.hasMoreElements();) { Notification curNotif = (Notification) e.nextElement(); notificationNames.add(curNotif.getName()); } return notificationNames; } /** * */ public synchronized void removeNotification(String name) throws MarshalException, ValidationException, IOException, ClassNotFoundException { m_notifications.removeNotification(getNotification(name)); saveCurrent(); } /** * Handles adding a new Notification. * * @param notice * The Notification to add. */ public synchronized void addNotification(Notification notice) throws MarshalException, ValidationException, IOException, ClassNotFoundException { // remove any existing notice with the same name m_notifications.removeNotification(getNotification(notice.getName())); m_notifications.addNotification(notice); saveCurrent(); } /** * */ public synchronized void replaceNotification(String oldName, Notification newNotice) throws MarshalException, ValidationException, IOException, ClassNotFoundException { // In order to preserver the order of the notices, we have to replace "in place". Notification notice = getNotification(oldName); if (notice != null) { notice.setWriteable(newNotice.getWriteable()); notice.setDescription(newNotice.getDescription()); notice.setUei(newNotice.getUei()); notice.setRule(newNotice.getRule()); notice.setDestinationPath(newNotice.getDestinationPath()); notice.setNoticeQueue(newNotice.getNoticeQueue()); notice.setTextMessage(newNotice.getTextMessage()); notice.setSubject(newNotice.getSubject()); notice.setNumericMessage(newNotice.getNumericMessage()); notice.setStatus(newNotice.getStatus()); notice.setVarbind(newNotice.getVarbind()); Parameter parameters[] = newNotice.getParameter(); for (int i = 0; i < parameters.length; i++) { Parameter newParam = new Parameter(); newParam.setName(parameters[i].getName()); newParam.setValue(parameters[i].getValue()); notice.addParameter(newParam); } saveCurrent(); } else addNotification(newNotice); } /** * Sets the status on an individual notification configuration and saves to * xml. * * @param name * The name of the notification. * @param status * The status (either "on" or "off"). */ public synchronized void updateStatus(String name, String status) throws MarshalException, ValidationException, IOException, ClassNotFoundException { if ("on".equals(status) || "off".equals(status)) { Notification notice = getNotification(name); notice.setStatus(status); saveCurrent(); } else throw new IllegalArgumentException("Status must be on|off, not " + status); } /** * */ public synchronized void saveCurrent() throws MarshalException, ValidationException, IOException, ClassNotFoundException { m_notifications.setHeader(rebuildHeader()); // marshall to a string first, then write the string to the file. This // way the original config // isn't lost if the xml from the marshall is hosed. StringWriter stringWriter = new StringWriter(); Marshaller.marshal(m_notifications, stringWriter); String xmlString = stringWriter.toString(); saveXML(xmlString); update(); } /** * @param xmlString * @throws IOException */ protected abstract void saveXML(String xmlString) throws IOException; /** * */ private Header rebuildHeader() { Header header = oldHeader; header.setCreated(EventConstants.formatToString(new Date())); return header; } /** * */ protected abstract void update() throws IOException, MarshalException, ValidationException; /** * @param notifId */ public Map rebuildParamterMap(int notifId, final String resolutionPrefix) throws Exception { final Map parmMap = new HashMap(); Querier querier = new Querier(m_dbConnectionFactory, "select notifications.*, service.* from notifications left outer join service on notifications.serviceID = service.serviceID where notifyId = ?") { public void processRow(ResultSet rs) throws SQLException { parmMap.put(NotificationManager.PARAM_TEXT_MSG, resolutionPrefix+rs.getObject("textMsg")); parmMap.put(NotificationManager.PARAM_NUM_MSG, rs.getObject("numericMsg")); parmMap.put(NotificationManager.PARAM_SUBJECT, resolutionPrefix+rs.getObject("subject")); parmMap.put(NotificationManager.PARAM_NODE, rs.getObject("nodeID").toString()); parmMap.put(NotificationManager.PARAM_INTERFACE, rs.getObject("interfaceID")); parmMap.put(NotificationManager.PARAM_SERVICE, rs.getObject("serviceName")); parmMap.put("noticeid", rs.getObject("notifyID").toString()); parmMap.put("eventID", rs.getObject("eventID").toString()); parmMap.put("eventUEI", rs.getObject("eventUEI")); } }; querier.execute(new Integer(notifId)); return parmMap; } /** * @param notifId * @return */ public void forEachUserNotification(int notifId, RowProcessor rp) { Querier querier = new Querier(m_dbConnectionFactory, "select * from usersNotified where notifyId = ?", rp); querier.execute(new Integer(notifId)); } /** * @param notifId * @return */ public String getQueueForNotification(int notifId) { SingleResultQuerier querier = new SingleResultQuerier(m_dbConnectionFactory, "select queueID from notifications where notifyId = ?"); querier.execute(new Integer(notifId)); return (String)querier.getResult(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -