/*
* $Id$
*
* Copyright 2006 Commsen International
*
* Licensed under the Common Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.txt
*
* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
* OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package com.commsen.stopwatch.jmx;
import org.apache.log4j.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.commsen.stopwatch.Report;
import com.commsen.stopwatch.Stopwatch;
import com.commsen.stopwatch.reports.LoadStopwatchReport;
import com.commsen.stopwatch.reports.MemoryStopwatchReport;
public class StopwatchManager implements StopwatchManagerMBean {
/**
* Logger for this class
*/
private static final Logger log = Logger.getLogger(StopwatchManager.class);
private boolean changed;
private boolean debug;
private String engine;
private String storage;
/**
*
*/
public StopwatchManager() {
reloadProperties();
}
public void start() {
if (!isActive()) Stopwatch.setActive(true);
reloadProperties();
}
public void stop() {
if (isActive()) Stopwatch.setActive(false);
}
public void reset() {
setProperties();
Stopwatch.reset();
reloadProperties();
}
public Report[] getReports(String group, String label) {
if (group == null || group.trim().length() == 0) {
if (label == null || label.trim().length() == 0) {
// no group and no label
return Stopwatch.getAllReports();
} else {
// no group but label
return Stopwatch.getLabelReports(label);
}
} else if (label == null || label.trim().length() == 0) {
// group but no label
return Stopwatch.getGroupReports(group);
} else {
// both present
return new Report[] {Stopwatch.getSingleReport(group, label)};
}
}
public long[] getLoadReports(String group, String label, int periodField, int numberOfPeriods) {
if (group == null || group.trim().length() == 0) {
if (label == null || label.trim().length() == 0) {
// no group and no label
return Stopwatch.getLoad(periodField, numberOfPeriods);
} else {
// no group but label
return Stopwatch.getLabelLoad(label, periodField, numberOfPeriods);
}
} else if (label == null || label.trim().length() == 0) {
// group but no label
return Stopwatch.getGroupLoad(group, periodField, numberOfPeriods);
} else {
// both present
return Stopwatch.getLoad(group, label, periodField, numberOfPeriods);
}
}
/**
*
* @see com.commsen.stopwatch.jmx.StopwatchManagerMBean#getReportsAsString(java.lang.String, java.lang.String)
*/
public String getReportsAsString(String group, String label) {
Report[] reports = getReports(group, label);
if (reports == null || reports.length == 0) return null;
StringBuffer result = new StringBuffer();
StringBuffer header = new StringBuffer("|");
StringBuffer divider = new StringBuffer("+");
header.append(fixedString("group")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("label")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("count")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("min time")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("max time")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("average time")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("total time")).append("|");
divider.append(fixedLine()).append("+");
if (reports[0] instanceof LoadStopwatchReport) {
header.append(fixedString("min load")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("max load")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("average load")).append("|");
divider.append(fixedLine()).append("+");
}
if (reports[0] instanceof MemoryStopwatchReport) {
header.append(fixedString("min memory")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("max memory")).append("|");
divider.append(fixedLine()).append("+");
header.append(fixedString("average memory")).append("|");
divider.append(fixedLine()).append("+");
}
String headerDivider = divider.toString().replaceAll("-", "=");
result.append(headerDivider).append("\n").append(header).append("\n").append(headerDivider).append("\n");
for (int i=0; i < reports.length; i++ ) {
result.append("|")
.append(fixedString(reports[i].getGroup())).append("|")
.append(fixedString(reports[i].getLabel())).append("|")
.append(fixedString(""+reports[i].getCount())).append("|")
.append(fixedString(""+reports[i].getMinTime())).append("|")
.append(fixedString(""+reports[i].getMaxTime())).append("|")
.append(fixedString(""+reports[i].getAverageTime())).append("|")
.append(fixedString(""+reports[i].getTotalTime())).append("|");
if (reports[i] instanceof LoadStopwatchReport) {
LoadStopwatchReport loadReport = (LoadStopwatchReport)reports[i];
result
.append(fixedString(""+loadReport.getMinLoad())).append("|")
.append(fixedString(""+loadReport.getMaxLoad())).append("|")
.append(fixedString(""+loadReport.getAverageLoad())).append("|");
}
if (reports[i] instanceof MemoryStopwatchReport) {
MemoryStopwatchReport memoryUsageReport = (MemoryStopwatchReport)reports[i];
result
.append(fixedString(""+memoryUsageReport.getMinMemory())).append("|")
.append(fixedString(""+memoryUsageReport.getMaxMemory())).append("|")
.append(fixedString(""+memoryUsageReport.getAverageMemory())).append("|");
}
result.append("\n").append(divider).append("\n");
}
return result.toString();
}
/**
*
* @see com.commsen.stopwatch.jmx.StopwatchManagerMBean#getReportsAsXML(java.lang.String, java.lang.String)
*/
public String getReportsAsXML(String group, String label) {
log.info("getReportsAsXML caled");
Report[] reports = getReports(group, label);
if (reports == null || reports.length == 0) return null;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = null;
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
log.error(e,e);
return e.toString();
}
Document document = documentBuilder.newDocument();
log.info("created document");
Element rootElement = document.createElement("report");
log.info("created root elemet");
for (int i=0; i < reports.length; i++ ) {
Element entry = document.createElement("report-entry");
entry.setAttribute("group", reports[i].getGroup());
entry.setAttribute("label", reports[i].getLabel());
rootElement.appendChild(entry);
log.info("created child element " + group + ":" + label);
}
document.appendChild(rootElement);
return document.toString();
}
private void reloadProperties() {
debug = Stopwatch.isDebugEnabled();
engine = Stopwatch.getEngineClass();
storage = Stopwatch.getStorageClass();
changed = false;
}
private void setProperties() {
System.setProperty(Stopwatch.SYSTEM_PROPERTIES_PREFIX + Stopwatch.PROPERTY_ENGINE, engine);
System.setProperty(Stopwatch.SYSTEM_PROPERTIES_PREFIX + Stopwatch.PROPERTY_STORAGE, storage);
System.setProperty(Stopwatch.SYSTEM_PROPERTIES_PREFIX + Stopwatch.PROPERTY_DEBUG, Boolean.toString(debug));
}
/**
* @return Returns the changed.
*/
public boolean isChanged() {
return changed;
}
/**
* @return Returns the debug.
*/
public boolean isActive() {
return Stopwatch.isActive();
}
/**
* @return Returns the debug.
*/
public boolean isDebug() {
return debug;
}
/**
* @param debug The debug to set.
*/
public void setDebug(boolean debug) {
this.debug = debug;
this.changed = true;
}
/**
* @return Returns the engine.
*/
public String getEngine() {
return engine;
}
/**
* @param engine The engine to set.
*/
public void setEngine(String engine) {
this.engine = engine;
this.changed = true;
}
/**
* @return Returns the storage.
*/
public String getStorage() {
return storage;
}
/**
* @param storage The storage to set.
*/
public void setStorage(String storage) {
this.storage = storage;
this.changed = true;
}
private String fixedString (String s) {
int l = s.length();
if (l == fixed) {
return s;
} else if (l < fixed) {
StringBuffer result = new StringBuffer (s);
for (int i = s.length(); i < fixed; i++) result.append(" ");
return result.toString();
} else {
return s.substring(0,fixed-3) + "...";
}
}
private String fixedLine () {
StringBuffer result = new StringBuffer();
for (int i = 0; i < fixed; i++) result.append("-");
return result.toString();
}
int fixed = 15;
}