[jboss-cvs] jboss-profiler/java/src/expansion/org/jboss/profiler/agent/persistor ...

Takuro Okada t2-okada at nri.co.jp
Wed Apr 11 07:13:10 EDT 2007


  User: tokada  
  Date: 07/04/11 07:13:10

  Added:       java/src/expansion/org/jboss/profiler/agent/persistor   Tag:
                        JBossProfiler_Expansion XmlMetricPersistor.java
                        MetricsPersistor.java
  Log:
  Moved to another directory
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +314 -0    jboss-profiler/java/src/expansion/org/jboss/profiler/agent/persistor/Attic/XmlMetricPersistor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: XmlMetricPersistor.java
  ===================================================================
  RCS file: XmlMetricPersistor.java
  diff -N XmlMetricPersistor.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ XmlMetricPersistor.java	11 Apr 2007 11:13:10 -0000	1.1.2.1
  @@ -0,0 +1,314 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + * Copyright 2006, JBoss Inc., and individual contributors as indicated
  + * by the @authors tag. See the copyright.txt in the distribution for a
  + * full listing of individual contributors.
  + *
  + * This is free software; you can redistribute it and/or modify it
  + * under the terms of the GNU Lesser General Public License as
  + * published by the Free Software Foundation; either version 2.1 of
  + * the License, or (at your option) any later version.
  + *
  + * This software is distributed in the hope that it will be useful,
  + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  + * Lesser General Public License for more details.
  + *
  + * You should have received a copy of the GNU Lesser General Public
  + * License along with this software; if not, write to the Free
  + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  + */
  +
  +package org.jboss.profiler.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.agent.collector.model.Metric;
  +import org.jboss.profiler.agent.collector.model.MetricName;
  +import org.jboss.profiler.agent.collector.model.MetricsMap;
  +import org.jboss.profiler.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.
  + * Copyright(c) Information-technology Promotion Agency, Japan. All rights reserved 2006.
  + * Result of Open Source Software Development Activities of Information-technology Promotion Agency, Japan.
  + */
  +public class XmlMetricPersistor implements MetricsPersistor {
  +
  +    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 void setPersistentPath(String persistentPath) {
  +        this.persistentPath = persistentPath;
  +        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) {
  +            logger.error("failed to save the metrics data.");
  +        }finally {
  +            try {
  +                if(writer!=null) writer.close();
  +                if(bos!=null) bos.close();
  +            } catch (Exception e) {
  +                logger.error("failed to close a stream.");
  +            }
  +        }
  +        
  +        // 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) {
  +            logger.error("failed to save the metrics data.");
  +        }finally {
  +            try {
  +                if(bis!=null) bis.close();
  +                if(zos!=null) zos.close();
  +                if(fos!=null) fos.close();
  +            } catch (IOException e) {
  +                logger.error("failed to close a stream.");
  +            }
  +        }
  +        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[] metricNames, Date fromDate, Date toDate) {
  +        MetricsMap metricsMap = new MetricsMap();
  +        File dir = new File(persistentPath);
  +        SimpleDateFormat dfDate = new SimpleDateFormat("yyyyMMdd");
  +        SimpleDateFormat dfTime = new SimpleDateFormat("yyyyMMdd HHmmss");
  +        
  +        // 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 = dfDate.parse(zipFileName);
  +                if((fromDate!=null && d.compareTo(roundDate(dfDate, fromDate))<0) ||
  +                   (toDate!=null && d.compareTo(roundDate(dfDate, toDate))>0)) {
  +                    continue;
  +                }
  +            } catch (Exception e) {
  +                logger.error("Log file name is illegal.");
  +            }
  +            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));
  +                    // File datetime is rounded off because input values do not have millisecond precision.
  +                    ed = roundDate(dfTime, ed);
  +                    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, metricNames));
  +                    zis.closeEntry();
  +                }
  +            } catch (Exception e) {
  +                logger.error("failed to load the metrics data.");
  +            }finally {
  +                try {
  +                    if(zis!=null) zis.close();
  +                    if(fis!=null) fis.close();
  +                } catch (IOException e) {
  +                    logger.error("failed to close a stream.");
  +                }
  +            }
  +        }
  +        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[] metricNames) 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)) {
  +                    boolean target = false;
  +                    for(MetricName metricName : metricNames) {
  +                        if(metricName==MetricName.valueOf(metricNameString)) target = true;
  +                    }
  +                    if(!target) 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));
  +    }
  +
  +}
  
  
  
  1.1.2.1   +62 -0     jboss-profiler/java/src/expansion/org/jboss/profiler/agent/persistor/Attic/MetricsPersistor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: MetricsPersistor.java
  ===================================================================
  RCS file: MetricsPersistor.java
  diff -N MetricsPersistor.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ MetricsPersistor.java	11 Apr 2007 11:13:10 -0000	1.1.2.1
  @@ -0,0 +1,62 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + * Copyright 2006, JBoss Inc., and individual contributors as indicated
  + * by the @authors tag. See the copyright.txt in the distribution for a
  + * full listing of individual contributors.
  + *
  + * This is free software; you can redistribute it and/or modify it
  + * under the terms of the GNU Lesser General Public License as
  + * published by the Free Software Foundation; either version 2.1 of
  + * the License, or (at your option) any later version.
  + *
  + * This software is distributed in the hope that it will be useful,
  + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  + * Lesser General Public License for more details.
  + *
  + * You should have received a copy of the GNU Lesser General Public
  + * License along with this software; if not, write to the Free
  + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  + * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  + */
  +
  +package org.jboss.profiler.agent.persistor;
  +
  +import java.util.Date;
  +
  +import org.jboss.profiler.agent.collector.model.MetricName;
  +import org.jboss.profiler.agent.collector.model.MetricsMap;
  +
  +/**
  + * This interface store metric data.
  + * 
  + * @author Takuro Okada (Nomura Research Institute, Ltd.)
  + * Copyright 2006 Nomura Research Institute, Ltd. All Rights Reserved.
  + * Copyright(c) Information-technology Promotion Agency, Japan. All rights reserved 2006.
  + * Result of Open Source Software Development Activities of Information-technology Promotion Agency, Japan.
  + */
  +public interface MetricsPersistor {
  +
  +    /**
  +     * Stores metric data.
  +     * @param metricsMap - metrics data
  +     */
  +    void store(MetricsMap metricsMap);
  +    
  +    /**
  +     * Loads metric data.
  +     * @param metricClass - The searching expression of class that extends Metric.
  +     * @param metricNames - searching array of MetricName
  +     * @param fromDate - searching  date (from)
  +     * @param toDate - searching  date (to)
  +     * @return metrics data
  +     */
  +    MetricsMap load(Class metricClass, MetricName[] metricNames, Date fromDate, Date toDate);
  +    
  +    /**
  +     * Sets the path of persistent directory.
  +     * @param persistentPath the path of persistent directory
  +     */
  +    void setPersistentPath(String persistentPath);
  +    
  +}
  
  
  



More information about the jboss-cvs-commits mailing list