📄 ksc_performancereportfactory.java
字号:
//// 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://// 2005 Jan 18: Changed the default report to "mib2.bits".// 2003 Apr 24: Changed the default report to "bits".//// 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.netmgt.config;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringReader;import java.io.StringWriter;import java.util.Calendar;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.Marshaller;import org.exolab.castor.xml.Unmarshaller;import org.exolab.castor.xml.ValidationException;import org.opennms.netmgt.ConfigFileConstants;import org.opennms.netmgt.config.kscReports.Graph;import org.opennms.netmgt.config.kscReports.Report;import org.opennms.netmgt.config.kscReports.ReportsList;public class KSC_PerformanceReportFactory { /** The static singleton instance object */ private static KSC_PerformanceReportFactory instance; /** File name of the KSC_PerformanceReport.xml */ private static File KSC_PerformanceReportFile; /** An instance of the ReportsList configuration */ private static ReportsList m_config; /** The input stream for the config file */ private static InputStream configIn; /** Boolean indicating if the init() method has been called */ private static boolean initialized = false; /** Last Modified timestamp */ private static long m_lastModified; /** * The array of values that may be used in the timespan declaration of a * graph */ public final String[] timespan_options = { "1_hour", "2_hour", "4_hour", "8_hour", "1_day", "7_day", "1_month", "6_month", "1_year", "Today", "Yesterday", "This Week", "Last Week", "This Month", "Last Month", "This Quarter", "Last Quarter", "This Year", "Last Year" }; /** * This is a working report that may be used to hold a report & its index * temporarily while moving between jsp's */ private Report working_report = null; private int working_index = -1; /** * This is a working graph that may be used to hold a report graph & its * index temporarily while moving between jsp's */ private Graph working_graph = null; private int graph_index = -1; /** * Empty Private Constructor. Cannot be instantiated outside itself. */ private KSC_PerformanceReportFactory() { } /** Init routine. Must be called before calling getInstance() to instantiate * */ public static synchronized void init() throws IOException, FileNotFoundException, MarshalException, ValidationException { if (instance == null) { instance = new KSC_PerformanceReportFactory(); KSC_PerformanceReportFactory.reload(); KSC_PerformanceReportFactory.initialized = true; } } /** * Singleton static call to get the only instance that should exist for the * KSC_PerformanceReportFactory * * @return the single KSC_PerformanceReportFactory instance */ public static synchronized KSC_PerformanceReportFactory getInstance() throws IllegalStateException { if (instance == null) { throw new IllegalStateException("KSC_PerformanceReportFactory.init() must be called before KSC_PerformanceReportFactory.getInstance()."); } return instance; } /** Parses the KSC_PerformanceReport.xml via the Castor classes */ public static synchronized void reload() throws IOException, FileNotFoundException, MarshalException, ValidationException { KSC_PerformanceReportFile = ConfigFileConstants.getFile(ConfigFileConstants.KSC_REPORT_FILE_NAME); InputStream configIn = new FileInputStream(KSC_PerformanceReportFile); m_lastModified = KSC_PerformanceReportFile.lastModified(); m_config = (ReportsList) Unmarshaller.unmarshal(ReportsList.class, new InputStreamReader(configIn)); } /** Saves the KSC_PerformanceReport.xml data */ public synchronized void saveCurrent() throws IOException, FileNotFoundException, MarshalException, ValidationException { if (instance == null) { throw new IllegalStateException("KSC_PerformanceReportFactory.init() must be called before KSC_PerformanceReportFactory.saveCurrent()."); } sortByTitle(); // Marshall to a string first, then to file. This way the original // config isn't lost if teh xml from the marshall is hosed. StringWriter stringWriter = new StringWriter(); Marshaller.marshal(m_config, stringWriter); if (stringWriter.toString() != null) { FileWriter fileWriter = new FileWriter(KSC_PerformanceReportFile); fileWriter.write(stringWriter.toString()); fileWriter.flush(); fileWriter.close(); } reload(); } /** Sorts the Reports List by their title. Simple bubble sort. */ public void sortByTitle() { Report[] report_array = m_config.getReport(); for (int j = 0; j < report_array.length; j++) { for (int i = j + 1; i < report_array.length; i++) { if (report_array[i].getTitle().compareTo(report_array[j].getTitle()) < 0) { Report temp_report = report_array[j]; report_array[j] = report_array[i]; report_array[i] = temp_report; } } } m_config.setReport(report_array); // write back the sorted list } /** Returns the KSC_PerformanceReport configuration object */ public static ReportsList getConfiguration() { return m_config; } /** Deletes the indexed report and updates file configuration */ public void deleteReportAndSave(int index) throws ArrayIndexOutOfBoundsException, IOException, FileNotFoundException, MarshalException, ValidationException { int total_reports = m_config.getReportCount(); if ((index < 0) || (index >= total_reports)) { // Out of range. Throw range error. throw new ArrayIndexOutOfBoundsException("Reports List index to be deleted is out of bounds."); } else { Report removee = m_config.getReport(index); m_config.removeReport(removee); saveCurrent(); reload(); // ensure consistent state with file } } /** Returns the working report object */ public Report getWorkingReport() { return working_report; } /** Loads the source report into the working report object */ public void loadWorkingReport(Report source_report) throws MarshalException, ValidationException { // Create a new and unique instance of the report for screwing around // with StringWriter stringWriter = new StringWriter(); Marshaller.marshal(source_report, stringWriter); StringReader stringReader = new StringReader(stringWriter.toString()); working_report = (Report) Unmarshaller.unmarshal(Report.class, stringReader); } /** * Loads the indexed report into the working report object or creates a new * one if the object does not exist */ public void loadWorkingReport(int index) throws MarshalException, ValidationException { int total_reports = m_config.getReportCount(); working_index = index; if ((working_index < 0) || (working_index >= total_reports)) { // Out of range. Assume new report needs to be created. working_report = KSC_PerformanceReportFactory.getNewReport(); working_index = -1; } else { loadWorkingReport(m_config.getReport(working_index)); } } /** * Unloads the working report into the indexed report list at the point
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -