View Javadoc

1   /*
2    * Copyright 2006 Commsen International
3    * 
4    * Licensed under the Common Public License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.opensource.org/licenses/cpl1.0.txt
9    * 
10   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
11   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
12   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
13   *
14   */
15  package com.commsen.stopwatch.jmx;
16  
17  import javax.xml.parsers.DocumentBuilder;
18  import javax.xml.parsers.DocumentBuilderFactory;
19  import javax.xml.parsers.ParserConfigurationException;
20  
21  import org.apache.log4j.Logger;
22  import org.w3c.dom.Document;
23  import org.w3c.dom.Element;
24  
25  import com.commsen.stopwatch.Report;
26  import com.commsen.stopwatch.Stopwatch;
27  import com.commsen.stopwatch.reports.MemoryStopwatchReport;
28  
29  public class StopwatchManager implements StopwatchManagerMBean {
30  
31  	/***
32  	 * Logger for this class
33  	 */
34  	private static final Logger log = Logger.getLogger(StopwatchManager.class);
35  
36  	private boolean changed;
37  	private boolean debug;
38  	private String engine;
39  	private String storage;
40  
41  
42  	/***
43  	 * 
44  	 */
45  	public StopwatchManager() {
46  		reloadProperties();
47  	}
48  
49  
50  	public void start() {
51  		if (!isActive()) Stopwatch.setActive(true);
52  		reloadProperties();
53  	}
54  
55  
56  	public void stop() {
57  		if (isActive()) Stopwatch.setActive(false);
58  	}
59  
60  
61  	public void reset() {
62  		setProperties();
63  		Stopwatch.reset();
64  		reloadProperties();
65  	}
66  
67  
68  	public Report[] getReports(String group, String label) {
69  		if (group == null || group.trim().length() == 0) {
70  			if (label == null || label.trim().length() == 0) {
71  				// no group and no label
72  				return Stopwatch.getAllReports();
73  			} else {
74  				// no group but label
75  				return Stopwatch.getLabelReports(label);
76  			}
77  		} else if (label == null || label.trim().length() == 0) {
78  			// group but no label
79  			return Stopwatch.getGroupReports(group);
80  		} else {
81  			// both present
82  			return new Report[] { Stopwatch.getSingleReport(group, label) };
83  		}
84  	}
85  
86  
87  	public long[] getLoadReports(String group, String label, int periodField, int numberOfPeriods) {
88  		if (group == null || group.trim().length() == 0) {
89  			if (label == null || label.trim().length() == 0) {
90  				// no group and no label
91  				return Stopwatch.getLoad(periodField, numberOfPeriods);
92  			} else {
93  				// no group but label
94  				return Stopwatch.getLabelLoad(label, periodField, numberOfPeriods);
95  			}
96  		} else if (label == null || label.trim().length() == 0) {
97  			// group but no label
98  			return Stopwatch.getGroupLoad(group, periodField, numberOfPeriods);
99  		} else {
100 			// both present
101 			return Stopwatch.getLoad(group, label, periodField, numberOfPeriods);
102 		}
103 	}
104 
105 
106 	/***
107 	 * 
108 	 * @see com.commsen.stopwatch.jmx.StopwatchManagerMBean#getReportsAsString(java.lang.String,
109 	 *      java.lang.String)
110 	 */
111 	public String getReportsAsString(String group, String label) {
112 
113 		Report[] reports = getReports(group, label);
114 		if (reports == null || reports.length == 0) return null;
115 
116 		StringBuffer result = new StringBuffer();
117 		StringBuffer header = new StringBuffer("|");
118 		StringBuffer divider = new StringBuffer("+");
119 
120 		header.append(fixedString("group")).append("|");
121 		divider.append(fixedLine()).append("+");
122 		header.append(fixedString("label")).append("|");
123 		divider.append(fixedLine()).append("+");
124 		header.append(fixedString("count")).append("|");
125 		divider.append(fixedLine()).append("+");
126 		header.append(fixedString("min time")).append("|");
127 		divider.append(fixedLine()).append("+");
128 		header.append(fixedString("max time")).append("|");
129 		divider.append(fixedLine()).append("+");
130 		header.append(fixedString("average time")).append("|");
131 		divider.append(fixedLine()).append("+");
132 		header.append(fixedString("total time")).append("|");
133 		divider.append(fixedLine()).append("+");
134 
135 		if (reports[0] instanceof MemoryStopwatchReport) {
136 			header.append(fixedString("min memory")).append("|");
137 			divider.append(fixedLine()).append("+");
138 			header.append(fixedString("max memory")).append("|");
139 			divider.append(fixedLine()).append("+");
140 			header.append(fixedString("average memory")).append("|");
141 			divider.append(fixedLine()).append("+");
142 		}
143 
144 		String headerDivider = divider.toString().replaceAll("-", "=");
145 
146 		result.append(headerDivider).append("\n").append(header).append("\n").append(headerDivider).append("\n");
147 
148 		for (int i = 0; i < reports.length; i++) {
149 			result.append("|").append(fixedString(reports[i].getGroup())) // 
150 			        .append("|").append(fixedString(reports[i].getLabel())) //
151 			        .append("|").append(fixedString("" + reports[i].getCount())) // 
152 			        .append("|").append(fixedString("" + reports[i].getMinTime())) // 
153 			        .append("|").append(fixedString("" + reports[i].getMaxTime())) // 
154 			        .append("|").append(fixedString("" + reports[i].getAverageTime())) // 
155 			        .append("|").append(fixedString("" + reports[i].getTotalTime())) // 
156 			        .append("|");
157 
158 			if (reports[i] instanceof MemoryStopwatchReport) {
159 				MemoryStopwatchReport memoryUsageReport = (MemoryStopwatchReport) reports[i];
160 				result.append(fixedString("" + memoryUsageReport.getMinMemory())) //
161 				        .append("|").append(fixedString("" + memoryUsageReport.getMaxMemory())) // 
162 				        .append("|").append(fixedString("" + memoryUsageReport.getAverageMemory())) // 
163 				        .append("|");
164 			}
165 
166 			result.append("\n").append(divider).append("\n");
167 		}
168 
169 		return result.toString();
170 	}
171 
172 
173 	/***
174 	 * 
175 	 * @see com.commsen.stopwatch.jmx.StopwatchManagerMBean#getReportsAsXML(java.lang.String,
176 	 *      java.lang.String)
177 	 */
178 	public String getReportsAsXML(String group, String label) {
179 		log.info("getReportsAsXML caled");
180 		Report[] reports = getReports(group, label);
181 		if (reports == null || reports.length == 0) return null;
182 
183 		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
184 		DocumentBuilder documentBuilder = null;
185 		try {
186 			documentBuilder = documentBuilderFactory.newDocumentBuilder();
187 		} catch (ParserConfigurationException e) {
188 			log.error(e, e);
189 			return e.toString();
190 		}
191 
192 		Document document = documentBuilder.newDocument();
193 		log.info("created document");
194 
195 		Element rootElement = document.createElement("report");
196 		log.info("created root elemet");
197 
198 		for (int i = 0; i < reports.length; i++) {
199 			Element entry = document.createElement("report-entry");
200 			entry.setAttribute("group", reports[i].getGroup());
201 			entry.setAttribute("label", reports[i].getLabel());
202 
203 			rootElement.appendChild(entry);
204 			log.info("created child element " + group + ":" + label);
205 		}
206 
207 		document.appendChild(rootElement);
208 		return document.toString();
209 	}
210 
211 
212 	private void reloadProperties() {
213 		debug = Stopwatch.isDebugEnabled();
214 		engine = Stopwatch.getEngineClass();
215 		storage = Stopwatch.getStorageClass();
216 		changed = false;
217 	}
218 
219 
220 	private void setProperties() {
221 		System.setProperty(Stopwatch.SYSTEM_PROPERTIES_PREFIX + Stopwatch.PROPERTY_ENGINE, engine);
222 		System.setProperty(Stopwatch.SYSTEM_PROPERTIES_PREFIX + Stopwatch.PROPERTY_STORAGE, storage);
223 		System.setProperty(Stopwatch.SYSTEM_PROPERTIES_PREFIX + Stopwatch.PROPERTY_DEBUG, Boolean.toString(debug));
224 	}
225 
226 
227 	/***
228 	 * @return Returns the changed.
229 	 */
230 	public boolean isChanged() {
231 		return changed;
232 	}
233 
234 
235 	/***
236 	 * @return Returns the debug.
237 	 */
238 	public boolean isActive() {
239 		return Stopwatch.isActive();
240 	}
241 
242 
243 	/***
244 	 * @return Returns the debug.
245 	 */
246 	public boolean isDebug() {
247 		return debug;
248 	}
249 
250 
251 	/***
252 	 * @param debug The debug to set.
253 	 */
254 	public void setDebug(boolean debug) {
255 		this.debug = debug;
256 		this.changed = true;
257 	}
258 
259 
260 	/***
261 	 * @return Returns the engine.
262 	 */
263 	public String getEngine() {
264 		return engine;
265 	}
266 
267 
268 	/***
269 	 * @param engine The engine to set.
270 	 */
271 	public void setEngine(String engine) {
272 		this.engine = engine;
273 		this.changed = true;
274 	}
275 
276 
277 	/***
278 	 * @return Returns the storage.
279 	 */
280 	public String getStorage() {
281 		return storage;
282 	}
283 
284 
285 	/***
286 	 * @param storage The storage to set.
287 	 */
288 	public void setStorage(String storage) {
289 		this.storage = storage;
290 		this.changed = true;
291 	}
292 
293 
294 	private String fixedString(String s) {
295 		int l = s.length();
296 		if (l == fixed) {
297 			return s;
298 		} else if (l < fixed) {
299 			StringBuffer result = new StringBuffer(s);
300 			for (int i = s.length(); i < fixed; i++)
301 				result.append(" ");
302 			return result.toString();
303 		} else {
304 			return s.substring(0, fixed - 3) + "...";
305 		}
306 
307 	}
308 
309 
310 	private String fixedLine() {
311 		StringBuffer result = new StringBuffer();
312 		for (int i = 0; i < fixed; i++)
313 			result.append("-");
314 		return result.toString();
315 	}
316 
317 	int fixed = 15;
318 
319 }