001: /*
002: * MCS Media Computer Software Copyright (c) 2006 by MCS
003: * -------------------------------------- Created on 20.03.2006 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.jmeasurement;
018:
019: import java.io.IOException;
020: import java.io.Writer;
021: import java.text.SimpleDateFormat;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Date;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.Properties;
029:
030: import org.xml.sax.SAXException;
031: import org.xml.sax.helpers.AttributesImpl;
032:
033: import com.megginson.sax.DataWriter;
034:
035: import de.mcs.jmeasurement.renderer.MeasureDataRenderer;
036: import de.mcs.jmeasurement.renderer.MeasureDataRendererColumnHeader;
037: import de.mcs.jmeasurement.renderer.MeasureDataRendererPage;
038: import de.mcs.jmeasurement.renderer.MeasureDataRendererSnapshot;
039:
040: /**
041: * This class contains all data for a snapshot.
042: *
043: * @author w.klaas
044: */
045: public class SnapShot {
046:
047: /** id of this snapshot. */
048: private static int count = 0;
049:
050: /** default size of buffers. */
051: private static final int BUFFER_SIZE = 1024;
052:
053: /** free memory of this snapshot. */
054: private static final String KEY_FREEMEMORY = "freememory";
055:
056: /** max memory of this snapshot. */
057: private static final String KEY_MAXMEMORY = "maxmemory";
058:
059: /** total memory of this snapshot. */
060: private static final String KEY_TOTALMEMORY = "totalmemory";
061:
062: /** name of this snapshot. */
063: private String name;
064:
065: /** id of this snapshot. */
066: private int id;
067:
068: /** date of this snapshot. */
069: private Date date;
070:
071: /** all measure points of this snapshot. */
072: private HashMap<String, MeasurePoint> measurePoints;
073:
074: /** properties for this snapshot. */
075: private Properties properties;
076:
077: /**
078: * Constructor to create a snapshot with the desired name.
079: *
080: * @param snapshotname
081: * name of this snapshot.
082: */
083: public SnapShot(final String snapshotname) {
084: this .name = snapshotname;
085: properties = new Properties();
086: measurePoints = new HashMap<String, MeasurePoint>();
087: initFields();
088: id = count++;
089: }
090:
091: /**
092: * init fields with values.
093: */
094: private void initFields() {
095: this .date = new Date();
096: properties.setProperty(KEY_FREEMEMORY, Long.toString(Runtime
097: .getRuntime().freeMemory()));
098: properties.setProperty(KEY_MAXMEMORY, Long.toString(Runtime
099: .getRuntime().maxMemory()));
100: properties.setProperty(KEY_TOTALMEMORY, Long.toString(Runtime
101: .getRuntime().totalMemory()));
102: }
103:
104: /**
105: * @return name of the snapshot.
106: */
107: public final String getName() {
108: return name;
109: }
110:
111: /**
112: * @return date of the snapshot.
113: */
114: public final Date getDate() {
115: return (Date) date.clone();
116: }
117:
118: /**
119: * @return free memory of this snapshot.
120: */
121: public final long getFreeMemory() {
122: String mem = properties.getProperty(KEY_FREEMEMORY);
123: if (mem == null) {
124: return 0L;
125: } else {
126: return Long.parseLong(mem);
127: }
128: }
129:
130: /**
131: * @return max memory of this snapshot.
132: */
133: public final long getMaxMemory() {
134: String mem = properties.getProperty(KEY_MAXMEMORY);
135: if (mem == null) {
136: return 0L;
137: } else {
138: return Long.parseLong(mem);
139: }
140: }
141:
142: /**
143: * @return total memory of this snapshot.
144: */
145: public final long getTotalMemory() {
146: String mem = properties.getProperty(KEY_TOTALMEMORY);
147: if (mem == null) {
148: return 0L;
149: } else {
150: return Long.parseLong(mem);
151: }
152: }
153:
154: /**
155: * Now cloning all measurepoints for this snapshot.
156: *
157: * @param orgMeasurePoints
158: * map with all measure points
159: */
160: @SuppressWarnings("unchecked")
161: public final void cloneMeasurePoints(
162: final Map<String, MeasurePoint> orgMeasurePoints) {
163: this .measurePoints = new HashMap<String, MeasurePoint>(
164: orgMeasurePoints.size());
165: for (Iterator iter = orgMeasurePoints.entrySet().iterator(); iter
166: .hasNext();) {
167: Map.Entry<String, MeasurePoint> pointEntry = (Map.Entry<String, MeasurePoint>) iter
168: .next();
169: MeasurePoint addPoint = pointEntry.getValue();
170: measurePoints.put(pointEntry.getKey(), addPoint);
171: }
172: // for (Iterator iter = orgMeasurePoints.keySet().iterator(); iter
173: // .hasNext();) {
174: // String key = (String) iter.next();
175: // MeasurePoint point = (MeasurePoint) orgMeasurePoints.get(key);
176: // MeasurePoint addPoint = (MeasurePoint) point.clone();
177: // measurePoints.put(key, addPoint);
178: // }
179: }
180:
181: /**
182: * getting a report for this snapshot.
183: *
184: * @param pointname
185: * regular expression to match the point names (or null for all
186: * points)
187: * @param renderer
188: * the renderer to use.
189: * @param priority
190: * the priority of points, which has to be added to the report.
191: * @return String the report
192: * @throws RendererMustNotBeNullException
193: * if something goes wrong
194: */
195: public final String getReport(final String pointname,
196: final MeasureDataRenderer renderer, final int priority)
197: throws RendererMustNotBeNullException {
198: String regexPointName = pointname;
199: if (renderer == null) {
200: throw new RendererMustNotBeNullException(
201: "renderer must not be null");
202: }
203: if (null == regexPointName) {
204: regexPointName = ".*";
205: }
206: MeasurePoint[] points = getMeasurePoints(pointname);
207:
208: StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
209: if (renderer instanceof MeasureDataRendererSnapshot) {
210: stringBuffer
211: .append(((MeasureDataRendererSnapshot) renderer)
212: .startSnapShot(this ));
213: }
214: if (renderer instanceof MeasureDataRendererPage) {
215: stringBuffer.append(((MeasureDataRendererPage) renderer)
216: .beginPage());
217: }
218:
219: if (renderer instanceof MeasureDataRendererColumnHeader) {
220: stringBuffer
221: .append(((MeasureDataRendererColumnHeader) renderer)
222: .getColumnHeaderAsString(new DefaultMeasurePoint(
223: "empty", null)));
224: }
225:
226: for (int i = 0; i < points.length; i++) {
227: if (points[i].getPriority() >= priority) {
228: stringBuffer.append(renderer.getDataAsString(points[i],
229: "s" + Integer.toString(id)));
230: }
231: }
232:
233: if (renderer instanceof MeasureDataRendererPage) {
234: stringBuffer.append(((MeasureDataRendererPage) renderer)
235: .endPage());
236: }
237: if (renderer instanceof MeasureDataRendererSnapshot) {
238: stringBuffer
239: .append(((MeasureDataRendererSnapshot) renderer)
240: .endSnapShot(this ));
241: }
242: return stringBuffer.toString();
243: }
244:
245: /**
246: * getting a report for this snapshot.
247: *
248: * @param pointname
249: * regular expression to match the point names (or null for all
250: * points)
251: * @param renderer
252: * the renderer to use.
253: * @param priority
254: * the priority of points, which has to be added to the report.
255: * @param output
256: * the writer to report to.
257: * @throws RendererMustNotBeNullException
258: * if something goes wrong
259: * @throws IOException
260: * if something goes wrong with the writer IO operation.
261: */
262: public final void getReport(final String pointname,
263: final MeasureDataRenderer renderer, final int priority,
264: final Writer output) throws RendererMustNotBeNullException,
265: IOException {
266: String regexPointName = pointname;
267: if (renderer == null) {
268: throw new RendererMustNotBeNullException(
269: "renderer must not be null");
270: }
271: if (null == regexPointName) {
272: regexPointName = ".*";
273: }
274: MeasurePoint[] points = getMeasurePoints(pointname);
275:
276: if (renderer instanceof MeasureDataRendererSnapshot) {
277: output.write(((MeasureDataRendererSnapshot) renderer)
278: .startSnapShot(this ));
279: }
280: if (renderer instanceof MeasureDataRendererPage) {
281: output.write(((MeasureDataRendererPage) renderer)
282: .beginPage());
283: }
284:
285: if (renderer instanceof MeasureDataRendererColumnHeader) {
286: output.write(((MeasureDataRendererColumnHeader) renderer)
287: .getColumnHeaderAsString(new DefaultMeasurePoint(
288: "empty", null)));
289: }
290:
291: for (int i = 0; i < points.length; i++) {
292: if (points[i].getPriority() >= priority) {
293: output.write(renderer.getDataAsString(points[i], "s"
294: + Integer.toString(id)));
295: }
296: }
297:
298: if (renderer instanceof MeasureDataRendererPage) {
299: output
300: .write(((MeasureDataRendererPage) renderer)
301: .endPage());
302: }
303: if (renderer instanceof MeasureDataRendererSnapshot) {
304: output.write(((MeasureDataRendererSnapshot) renderer)
305: .endSnapShot(this ));
306: }
307: }
308:
309: /**
310: * getting an array of measurement points. The pointname is a regular
311: * expression of the desired points.
312: *
313: * @param pointName
314: * regular expression of the desired point names (or null for all
315: * points)
316: * @return MeasurePoint[] the desired MeasurePoints
317: */
318: public final MeasurePoint[] getMeasurePoints(final String pointName) {
319: ArrayList<MeasurePoint> points = new ArrayList<MeasurePoint>();
320: String regexPointName = pointName;
321: if (null == regexPointName) {
322: regexPointName = ".*";
323: }
324: String[] names = (String[]) measurePoints.keySet().toArray(
325: new String[0]);
326: Arrays.sort(names);
327: for (int i = 0; i < names.length; i++) {
328: String string = names[i];
329: if (string.matches(regexPointName)) {
330: MeasurePoint point = (MeasurePoint) measurePoints
331: .get(string);
332: points.add(point);
333: }
334: }
335: return (MeasurePoint[]) points.toArray(new MeasurePoint[0]);
336: }
337:
338: /**
339: * getting the properties ofthis snapshot.
340: *
341: * @return Properties
342: */
343: public final Properties getProperties() {
344: return properties;
345: }
346:
347: /**
348: * Adding a new measurepoint to the point registration.
349: *
350: * @param measurePoint
351: * measure point to add
352: */
353: public final void register(final MeasurePoint measurePoint) {
354: // measurePoint.setMeasureDataCallback(measureDataCallback);
355: measurePoints.put(measurePoint.getName(), measurePoint);
356: }
357:
358: /**
359: * saving this snapshot to an datawriter.
360: *
361: * @param writer
362: * the writer uto use.
363: * @throws SAXException
364: * if something goes wrong.
365: * @throws IOException
366: * if something goes wrong.
367: */
368: public final void saveToXML(final DataWriter writer)
369: throws SAXException, IOException {
370: AttributesImpl atts;
371: MeasurePoint[] points;
372: atts = new AttributesImpl();
373: atts.addAttribute("", MeasureFactory.XMLATT_NAME, "", "String",
374: getName());
375: atts.addAttribute("", MeasureFactory.XMLATT_CREATED, "",
376: "String", new SimpleDateFormat().format(getDate()));
377: writer.startElement("", MeasureFactory.XMLNODE_SNAPSHOT, "",
378: atts);
379: for (Iterator iterator = properties.keySet().iterator(); iterator
380: .hasNext();) {
381: String key = (String) iterator.next();
382: atts = new AttributesImpl();
383: atts.addAttribute("", MeasureFactory.XMLATT_NAME, "",
384: "String", key);
385: atts.addAttribute("", MeasureFactory.XMLATT_VALUE, "",
386: "String", properties.getProperty(key));
387: writer.emptyElement("", MeasureFactory.XMLNODE_PROPERTY,
388: "", atts);
389: }
390: writer.startElement(MeasureFactory.XMLNODE_MEASUREPOINTS);
391: points = getMeasurePoints(".*");
392: for (int i = 0; i < points.length; i++) {
393: if (points[i] instanceof DefaultMeasurePoint) {
394: ((DefaultMeasurePoint) points[i]).toXML(writer);
395: }
396: }
397: writer.endElement(MeasureFactory.XMLNODE_MEASUREPOINTS);
398: writer.endElement(MeasureFactory.XMLNODE_SNAPSHOT);
399: }
400: }
|