All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mcs.jmeasurement.SnapShot Maven / Gradle / Ivy

There is a newer version: 1.1.226
Show newest version
/*
 * MCS Media Computer Software Copyright (c) 2006 by MCS
 * -------------------------------------- Created on 20.03.2006 by w.klaas
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.mcs.jmeasurement;

import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import com.megginson.sax.DataWriter;

import de.mcs.jmeasurement.MeasurePoint.PRIORITY;
import de.mcs.jmeasurement.renderer.MeasureDataRenderer;
import de.mcs.jmeasurement.renderer.MeasureDataRendererColumnHeader;
import de.mcs.jmeasurement.renderer.MeasureDataRendererPage;
import de.mcs.jmeasurement.renderer.MeasureDataRendererSnapshot;

/**
 * This class contains all data for a snapshot.
 * @author w.klaas
 */
public class SnapShot {

  /** id of this snapshot. */
  private static int count = 0;

  /** default size of buffers. */
  private static final int BUFFER_SIZE = 1024;

  /** free memory of this snapshot. */
  private static final String KEY_FREEMEMORY = "freememory";

  /** max memory of this snapshot. */
  private static final String KEY_MAXMEMORY = "maxmemory";

  /** total memory of this snapshot. */
  private static final String KEY_TOTALMEMORY = "totalmemory";

  /** name of this snapshot. */
  private String name;

  /** id of this snapshot. */
  private int id;

  /** date of this snapshot. */
  private Date date;

  /** all measure points of this snapshot. */
  private HashMap measurePoints;

  /** properties for this snapshot. */
  private Properties properties;

  /**
   * Constructor to create a snapshot with the desired name.
   * @param snapshotname
   *            name of this snapshot.
   */
  public SnapShot(final String snapshotname) {
    this.name = snapshotname;
    properties = new Properties();
    measurePoints = new HashMap();
    initFields();
    id = count++;
  }

  /**
   * init fields with values.
   */
  private void initFields() {
    this.date = new Date();
    properties.setProperty(KEY_FREEMEMORY, Long.toString(Runtime.getRuntime().freeMemory()));
    properties.setProperty(KEY_MAXMEMORY, Long.toString(Runtime.getRuntime().maxMemory()));
    properties.setProperty(KEY_TOTALMEMORY, Long.toString(Runtime.getRuntime().totalMemory()));
  }

  /**
   * @return name of the snapshot.
   */
  public final String getName() {
    return name;
  }

  /**
   * @return date of the snapshot.
   */
  public final Date getDate() {
    return (Date) date.clone();
  }

  /**
   * @return free memory of this snapshot.
   */
  public final long getFreeMemory() {
    String mem = properties.getProperty(KEY_FREEMEMORY);
    if (mem == null) {
      return 0L;
    } else {
      return Long.parseLong(mem);
    }
  }

  /**
   * @return max memory of this snapshot.
   */
  public final long getMaxMemory() {
    String mem = properties.getProperty(KEY_MAXMEMORY);
    if (mem == null) {
      return 0L;
    } else {
      return Long.parseLong(mem);
    }
  }

  /**
   * @return total memory of this snapshot.
   */
  public final long getTotalMemory() {
    String mem = properties.getProperty(KEY_TOTALMEMORY);
    if (mem == null) {
      return 0L;
    } else {
      return Long.parseLong(mem);
    }
  }

  /**
   * Now cloning all measurepoints for this snapshot.
   * @param orgMeasurePoints
   *            map with all measure points
   */
  public final void cloneMeasurePoints(final Map orgMeasurePoints) {
    this.measurePoints = new HashMap(orgMeasurePoints.size());
    // for (Iterator iter = orgMeasurePoints.entrySet().iterator();
    // iter.hasNext();) {
    // Map.Entry pointEntry = (Map.Entry) iter.next();
    // MeasurePoint addPoint = pointEntry.getValue();
    // measurePoints.put(pointEntry.getKey(), addPoint);
    // }
    for (Iterator iter = orgMeasurePoints.keySet().iterator(); iter.hasNext();) {
      String key = iter.next();
      MeasurePoint point = (MeasurePoint) orgMeasurePoints.get(key);
      MeasurePoint addPoint = (MeasurePoint) point.clone();
      measurePoints.put(key, addPoint);
    }
  }

  /**
   * getting a report for this snapshot.
   * @param pointname
   *            regular expression to match the point names (or null for all
   *            points)
   * @param renderer
   *            the renderer to use.
   * @param priority
   *            the priority of points, which has to be added to the report.
   * @return String the report
   * @throws RendererMustNotBeNullException
   *             if something goes wrong
   */
  public final String getReport(final String pointname, final MeasureDataRenderer renderer, final PRIORITY priority)
      throws RendererMustNotBeNullException {
    String regexPointName = pointname;
    if (renderer == null) {
      throw new RendererMustNotBeNullException("renderer must not be null");
    }
    if (null == regexPointName) {
      regexPointName = ".*";
    }
    MeasurePoint[] points = getMeasurePoints(pointname);

    StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
    if (renderer instanceof MeasureDataRendererSnapshot) {
      stringBuffer.append(((MeasureDataRendererSnapshot) renderer).startSnapShot(this));
    }
    if (renderer instanceof MeasureDataRendererPage) {
      stringBuffer.append(((MeasureDataRendererPage) renderer).beginPage());
    }

    if (renderer instanceof MeasureDataRendererColumnHeader) {
      stringBuffer.append(((MeasureDataRendererColumnHeader) renderer).getColumnHeaderAsString(new DefaultMeasurePoint(
          "empty", null)));
    }

    for (int i = 0; i < points.length; i++) {
      if (points[i].getPriority().compareTo(priority) >= 0) {
        stringBuffer.append(renderer.getDataAsString(points[i], "s" + Integer.toString(id)));
      }
    }

    if (renderer instanceof MeasureDataRendererPage) {
      stringBuffer.append(((MeasureDataRendererPage) renderer).endPage());
    }
    if (renderer instanceof MeasureDataRendererSnapshot) {
      stringBuffer.append(((MeasureDataRendererSnapshot) renderer).endSnapShot(this));
    }
    return stringBuffer.toString();
  }

  /**
   * getting a report for this snapshot.
   * @param pointname
   *            regular expression to match the point names (or null for all
   *            points)
   * @param renderer
   *            the renderer to use.
   * @param priority
   *            the priority of points, which has to be added to the report.
   * @param output
   *            the writer to report to.
   * @throws RendererMustNotBeNullException
   *             if something goes wrong
   * @throws IOException
   *             if something goes wrong with the writer IO operation.
   */
  public final void getReport(final String pointname, final MeasureDataRenderer renderer, final PRIORITY priority,
      final Writer output) throws RendererMustNotBeNullException, IOException {
    String regexPointName = pointname;
    if (renderer == null) {
      throw new RendererMustNotBeNullException("renderer must not be null");
    }
    if (null == regexPointName) {
      regexPointName = ".*";
    }
    MeasurePoint[] points = getMeasurePoints(pointname);

    if (renderer instanceof MeasureDataRendererSnapshot) {
      output.write(((MeasureDataRendererSnapshot) renderer).startSnapShot(this));
    }
    if (renderer instanceof MeasureDataRendererPage) {
      output.write(((MeasureDataRendererPage) renderer).beginPage());
    }

    if (renderer instanceof MeasureDataRendererColumnHeader) {
      output.write(((MeasureDataRendererColumnHeader) renderer).getColumnHeaderAsString(new DefaultMeasurePoint(
          "empty", null)));
    }

    for (int i = 0; i < points.length; i++) {
      if (points[i].getPriority().compareTo(priority) >= 0) {
        output.write(renderer.getDataAsString(points[i], "s" + Integer.toString(id)));
      }
    }

    if (renderer instanceof MeasureDataRendererPage) {
      output.write(((MeasureDataRendererPage) renderer).endPage());
    }
    if (renderer instanceof MeasureDataRendererSnapshot) {
      output.write(((MeasureDataRendererSnapshot) renderer).endSnapShot(this));
    }
  }

  /**
   * getting an array of measurement points. The pointname is a regular
   * expression of the desired points.
   * @param pointName
   *            regular expression of the desired point names (or null for all
   *            points)
   * @return MeasurePoint[] the desired MeasurePoints
   */
  public final MeasurePoint[] getMeasurePoints(final String pointName) {
    ArrayList points = new ArrayList();
    String regexPointName = pointName;
    if (null == regexPointName) {
      regexPointName = ".*";
    }
    String[] names = (String[]) measurePoints.keySet().toArray(new String[0]);
    Arrays.sort(names);
    for (int i = 0; i < names.length; i++) {
      String string = names[i];
      if (string.matches(regexPointName)) {
        MeasurePoint point = (MeasurePoint) measurePoints.get(string);
        points.add(point);
      }
    }
    return (MeasurePoint[]) points.toArray(new MeasurePoint[0]);
  }

  /**
   * getting the properties ofthis snapshot.
   * @return Properties
   */
  public final Properties getProperties() {
    return properties;
  }

  /**
   * Adding a new measurepoint to the point registration.
   * @param measurePoint
   *            measure point to add
   */
  public final void register(final MeasurePoint measurePoint) {
    // measurePoint.setMeasureDataCallback(measureDataCallback);
    measurePoints.put(measurePoint.getName(), measurePoint);
  }

  /**
   * saving this snapshot to an datawriter.
   * @param writer
   *            the writer uto use.
   * @throws SAXException
   *             if something goes wrong.
   * @throws IOException
   *             if something goes wrong.
   */
  public final void saveToXML(final DataWriter writer) throws SAXException, IOException {
    AttributesImpl atts;
    MeasurePoint[] points;
    atts = new AttributesImpl();
    atts.addAttribute("", MeasureFactory.XMLATT_NAME, "", "String", getName());
    atts.addAttribute("", MeasureFactory.XMLATT_CREATED, "", "String", new SimpleDateFormat().format(getDate()));
    writer.startElement("", MeasureFactory.XMLNODE_SNAPSHOT, "", atts);
    for (Iterator iterator = properties.keySet().iterator(); iterator.hasNext();) {
      String key = (String) iterator.next();
      atts = new AttributesImpl();
      atts.addAttribute("", MeasureFactory.XMLATT_NAME, "", "String", key);
      atts.addAttribute("", MeasureFactory.XMLATT_VALUE, "", "String", properties.getProperty(key));
      writer.emptyElement("", MeasureFactory.XMLNODE_PROPERTY, "", atts);
    }
    writer.startElement(MeasureFactory.XMLNODE_MEASUREPOINTS);
    points = getMeasurePoints(".*");
    for (int i = 0; i < points.length; i++) {
      if (points[i] instanceof DefaultMeasurePoint) {
        ((DefaultMeasurePoint) points[i]).toXML(writer);
      }
    }
    writer.endElement(MeasureFactory.XMLNODE_MEASUREPOINTS);
    writer.endElement(MeasureFactory.XMLNODE_SNAPSHOT);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy