[jboss-cvs] jboss-profiler/java/src/expansion/org/jboss/profiler/exp/agent/persistor ...
Takuro Okada
t2-okada at nri.co.jp
Tue Oct 31 06:30:19 EST 2006
User: tokada
Date: 06/10/31 06:30:19
Modified: java/src/expansion/org/jboss/profiler/exp/agent/persistor
Tag: JBossProfiler_Expansion
XmlMetricPersistor.java
Log:
Revision Changes Path
No revision
No revision
1.1.2.2 +275 -2 jboss-profiler/java/src/expansion/org/jboss/profiler/exp/agent/persistor/Attic/XmlMetricPersistor.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: XmlMetricPersistor.java
===================================================================
RCS file: /cvsroot/jboss/jboss-profiler/java/src/expansion/org/jboss/profiler/exp/agent/persistor/Attic/XmlMetricPersistor.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- XmlMetricPersistor.java 26 Oct 2006 08:58:57 -0000 1.1.2.1
+++ XmlMetricPersistor.java 31 Oct 2006 11:30:18 -0000 1.1.2.2
@@ -22,9 +22,43 @@
package org.jboss.profiler.exp.agent.persistor;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.EnumMap;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
+import javolution.xml.stream.XMLInputFactory;
+import javolution.xml.stream.XMLOutputFactory;
+import javolution.xml.stream.XMLStreamConstants;
+import javolution.xml.stream.XMLStreamException;
+import javolution.xml.stream.XMLStreamReader;
+import javolution.xml.stream.XMLStreamWriter;
+
+import org.apache.log4j.Logger;
+import org.jboss.profiler.exp.adaptor.ServiceManager;
+import org.jboss.profiler.exp.agent.collector.model.Metric;
+import org.jboss.profiler.exp.agent.collector.model.MetricName;
+import org.jboss.profiler.exp.agent.collector.model.MetricsMap;
+import org.jboss.profiler.exp.util.HierarchicalMap;
/**
+ * Writes log file to /log directory of the JBoss AS.
*
* @author Takuro Okada (Nomura Research Institute, Ltd.)
* Copyright 2006 Nomura Research Institute, Ltd. All Rights Reserved.
@@ -33,8 +67,247 @@
*/
public class XmlMetricPersistor implements MetricsPersistor {
- public void store(Map metricsMap) {
- // TODO implement
+ private static final String FOLDER_NAME = "jboss-profiler";
+
+ private final String XML_ROOT = "metrics";
+ private final String XML_RECORD_SET = "recordSet";
+ private final String XML_RECORD = "record";
+
+ private String persistentPath = null;
+
+ private static Logger logger = Logger.getLogger(XmlMetricPersistor.class);
+
+ public XmlMetricPersistor() {
+ Object path = ServiceManager.getAttributeLocal("jboss.system:type=ServerConfig", "ServerLogDir");
+ if(path!=null) {
+ persistentPath = path.toString() + File.separator + FOLDER_NAME + File.separator;
+ File dir = new File(persistentPath);
+ if(!dir.exists()) dir.mkdir();
+ }
+ }
+
+ public void store(MetricsMap metricsMap) {
+ String entryFileName = String.valueOf(System.currentTimeMillis()) + ".xml";
+
+ // Write the metrics data to byte stream
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ XMLStreamWriter writer = null;
+ XMLOutputFactory factory = XMLOutputFactory.newInstance();
+ factory.setProperty(XMLOutputFactory.INDENTATION, " ");
+ try {
+ writer = factory.createXMLStreamWriter(bos);
+ writer.writeStartDocument();
+ writer.writeStartElement(XML_ROOT);
+ for(Map.Entry<String, Map<String, Map<MetricName, Metric>>> cEntry : metricsMap.entrySet()) {
+ Map<String, Map<MetricName, Metric>> omap = cEntry.getValue();
+ for(Map.Entry<String, Map<MetricName, Metric>> oEntry : omap.entrySet()) {
+ Map<MetricName, Metric> mmap = oEntry.getValue();
+ for(Map.Entry<MetricName, Metric> mEntry : mmap.entrySet()) {
+ writer.writeStartElement(XML_RECORD_SET);
+ writer.writeAttribute("category", String.valueOf(cEntry.getKey()));
+ writer.writeAttribute("operationName", String.valueOf(oEntry.getKey()));
+ writer.writeAttribute("metricName", mEntry.getKey().name());
+ Object obj = mEntry.getValue().toProperties();
+ if(obj instanceof Map) {
+ writeRecord(writer, (Map)obj);
+ }else if(obj instanceof List) {
+ List propsList = (List)obj;
+ for(Object props : propsList) {
+ writeRecord(writer, (Map)props);
+ }
+ }
+ writer.writeEndElement();
+ }
+ }
+ }
+ writer.writeEndElement();
+ writer.writeEndDocument();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }finally {
+ try {
+ if(writer!=null) writer.close();
+ if(bos!=null) bos.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ // Write byte stream to the compressed file
+ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+ ZipOutputStream zos = null;
+ FileOutputStream fos = null;
+ String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date()) + ".zip";
+ File file = new File(persistentPath+fileName);
+ File fileTemp = null;
+ if(file.exists()) {
+ fileTemp = new File(persistentPath+"_"+fileName);
+ }
+ try {
+ if(fileTemp!=null) {
+ fos = new FileOutputStream(fileTemp);
+ zos = new ZipOutputStream(fos);
+ ZipFile zipFile = new ZipFile(file);
+ Enumeration e = zipFile.entries();
+ while(e.hasMoreElements()) {
+ ZipEntry entry = (ZipEntry)e.nextElement();
+ writeToFile(zipFile.getInputStream(entry), zos, entry);
+ }
+ writeToFile(bis, zos, new ZipEntry(entryFileName));
+ zipFile.close();
+ file.delete();
+ }else {
+ fos = new FileOutputStream(file);
+ zos = new ZipOutputStream(fos);
+ writeToFile(bis, zos, new ZipEntry(entryFileName));
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }finally {
+ try {
+ if(bis!=null) bis.close();
+ if(zos!=null) zos.close();
+ if(fos!=null) fos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if(fileTemp!=null) fileTemp.renameTo(new File(persistentPath+fileName));
+ if(logger.isDebugEnabled()) logger.debug("Writing the log file succeeded.");
+ }
+
+ public MetricsMap load(Class metricClass, MetricName metricName, Date fromDate, Date toDate) {
+ MetricsMap metricsMap = new MetricsMap();
+ File dir = new File(persistentPath);
+ SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
+
+ // Read the compressed file to byte stream
+ for(File file : dir.listFiles()) {
+ // Check the file name
+ String zipFileName = file.getName();
+ zipFileName = zipFileName.substring(0, zipFileName.lastIndexOf('.'));
+ try {
+ Date d = df.parse(zipFileName);
+ if((fromDate!=null && d.compareTo(roundDate(df, fromDate))<0) ||
+ (toDate!=null && d.compareTo(roundDate(df, toDate))>0)) {
+ continue;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ FileInputStream fis = null;
+ ZipInputStream zis = null;
+ try {
+ fis = new FileInputStream(file);
+ zis = new ZipInputStream(new FileInputStream(file));
+
+ ZipEntry entry = null;
+ while((entry = zis.getNextEntry()) != null) {
+ // Check the entry file name
+ String fileName = entry.getName();
+ fileName = fileName.substring(0, fileName.lastIndexOf('.'));
+ Date ed = new Date(Long.valueOf(fileName));
+ if((fromDate!=null && ed.compareTo(fromDate)<0) ||
+ (toDate!=null && ed.compareTo(toDate)>0)) {
+ continue;
+ }
+ byte[] buffer = new byte[1024];
+ int byteLength = -1;
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ while ((byteLength = zis.read(buffer, 0, buffer.length)) != -1) {
+ bos.write(buffer, 0, byteLength);
+ }
+ bos.close();
+ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+ metricsMap.merge(readFromFile(bis, metricClass, metricName));
+ zis.closeEntry();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }finally {
+ try {
+ if(zis!=null) zis.close();
+ if(fis!=null) fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ if(logger.isDebugEnabled()) logger.debug("Reading the log file succeeded.");
+ return metricsMap;
+ }
+
+ private void writeRecord(XMLStreamWriter writer, Map properties) throws XMLStreamException {
+ writer.writeEmptyElement(XML_RECORD);
+ for(Object pe : properties.entrySet()) {
+ Map.Entry pEntry = (Map.Entry)pe;
+ writer.writeAttribute(String.valueOf(pEntry.getKey()), String.valueOf(pEntry.getValue()));
+ }
+ }
+
+ private void writeToFile(InputStream inputStream, ZipOutputStream outputStream, ZipEntry entry) throws IOException {
+ outputStream.putNextEntry(entry);
+ byte[] buffer = new byte[1024];
+ int byteLength = -1;
+ while ((byteLength = inputStream.read(buffer, 0, buffer.length)) != -1) {
+ outputStream.write(buffer, 0, byteLength);
+ }
+ outputStream.closeEntry();
+ }
+
+ @SuppressWarnings("unchecked")
+ private MetricsMap readFromFile(InputStream inputStream, Class metricClass, MetricName metricName) throws XMLStreamException, InstantiationException, IllegalAccessException {
+ MetricsMap metricsMap = new MetricsMap();
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
+ String category = null;
+ String operationName = null;
+ String metricNameString = null;
+ while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
+ switch(reader.next()) {
+ case XMLStreamConstants.START_ELEMENT:
+ if(reader.getLocalName().equals(XML_RECORD_SET)) {
+ category = reader.getAttributeValue(0).toString();
+ operationName = reader.getAttributeValue(1).toString();
+ metricNameString = reader.getAttributeValue(2).toString();
+ }else if(reader.getLocalName().equals(XML_RECORD)) {
+ if(metricName!=MetricName.valueOf(metricNameString)) continue;
+ Map<String, String> properties = new HashMap<String, String>(reader.getAttributeCount());
+ for(int i=0; i<reader.getAttributeCount(); i++) {
+ properties.put(reader.getAttributeLocalName(i).toString(), reader.getAttributeValue(i).toString());
+ }
+ Map<MetricName, Metric> mmap = null;
+ if(metricsMap.containsKey(category)) {
+ Map<String, Map<MetricName, Metric>> omap = metricsMap.get(category);
+ mmap = omap.get(operationName);
+ if(mmap==null) {
+ mmap = new EnumMap<MetricName, Metric>(MetricName.class);
+ omap.put(operationName, mmap);
+ }
+ }else {
+ mmap = new EnumMap<MetricName, Metric>(MetricName.class);
+ Map<String, Map<MetricName, Metric>> omap = new HierarchicalMap<String, Map<MetricName, Metric>>();
+ omap.put(operationName, mmap);
+ metricsMap.put(category, omap);
+ }
+ Metric metric = (Metric)metricClass.newInstance();
+ metric.fromProperties(properties);
+ Metric existMetric = mmap.get(MetricName.valueOf(metricNameString));
+ if(existMetric!=null) {
+ metric.merge(existMetric);
+ }
+ mmap.put(MetricName.valueOf(metricNameString), metric);
+ }
+ break;
+ }
+ }
+ reader.close();
+ return metricsMap;
+ }
+
+ private Date roundDate(DateFormat df, Date date) throws ParseException {
+ return df.parse(df.format(date));
}
}
More information about the jboss-cvs-commits
mailing list