View Javadoc

1   package org.sourceforge.mbeanmonitoring.report;
2   
3   /**
4    **      Authors:
5    **              Laurent Le Grandois <Laurent.Le.Grandois@gmail.com>
6    **              Gilles Bardouillet  <Gilles.Bardouillet@gmail.com>
7    **
8    **  This program is free software; you can redistribute it and/or modify
9    ** it under the terms of the GNU General Public License as published by
10   **  the Free Software Foundation; either version 2 of the License, or
11   **  (at your option) any later version.
12   **
13   ** This program is distributed in the hope that it will be useful,
14   **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15   **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   **  GNU General Public License for more details.
17   **
18   **  You should have received a copy of the GNU General Public License
19   ** along with this program; if not, write to the Free Software
20   **
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Color;
25  import java.awt.Dimension;
26  import java.awt.Font;
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileNotFoundException;
31  import java.io.IOException;
32  import java.io.InputStreamReader;
33  import java.text.ParseException;
34  import java.text.SimpleDateFormat;
35  import java.util.ArrayList;
36  import java.util.Date;
37  import java.util.NoSuchElementException;
38  import java.util.StringTokenizer;
39  
40  import javax.swing.JFrame;
41  import javax.swing.JPanel;
42  
43  import org.jfree.chart.ChartFactory;
44  import org.jfree.chart.ChartPanel;
45  import org.jfree.chart.JFreeChart;
46  import org.jfree.data.time.Millisecond;
47  import org.jfree.data.time.TimeSeries;
48  import org.jfree.data.time.TimeSeriesCollection;
49  
50  public class BatchGraph extends JFrame {
51  
52  	/**
53  	 *
54  	 */
55  	static final long serialVersionUID = 1L;
56  
57  	/**
58  	 * Was a Vector
59  	 */
60  	protected ArrayList<TimeSeries> _series;
61  
62  	/**
63  	 *
64  	 */
65  	protected String _file;
66  
67  	/**
68  	 *
69  	 */
70  	protected String _title;
71  
72  	/**
73  	 * @param file
74  	 * @throws StringIndexOutOfBoundsException
75  	 */
76  	public BatchGraph(String file) throws StringIndexOutOfBoundsException {
77  
78  		this._file = file;
79  		this.setForeground(Color.black);
80  		this.setBackground(Color.lightGray);
81  		this.setFont(new Font("Arial", Font.BOLD, 12));
82  		this.setTitle(this._file);
83  		this.setResizable(true);
84  		int deb = file.lastIndexOf(File.separator) + 1;
85  		int fin = file.indexOf('.');
86  		this._title = file.substring(deb, fin);
87  		this._series = new ArrayList<TimeSeries>();
88  	}
89  
90  	/**
91  	 * @throws NoSuchElementException
92  	 * @throws FileNotFoundException
93  	 * @throws IOException
94  	 * @throws ParseException
95  	 */
96  	public void fill() throws NoSuchElementException, FileNotFoundException, IOException, ParseException {
97  
98  		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
99  		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
100 
101 		BufferedReader dFile = new BufferedReader(new InputStreamReader(new FileInputStream(this._file)));
102 		boolean eof = false;
103 		String line = dFile.readLine();
104 		if (line != null) {
105 			StringTokenizer st = new StringTokenizer(line, ";");
106 			st.nextToken();
107 			st.nextToken();
108 			while (st.hasMoreElements()) {
109 				String column = st.nextToken();
110 				this._series.add(new TimeSeries(column, Millisecond.class));
111 			}
112 		}
113 		while (eof == false) {
114 			try {
115 				line = dFile.readLine();
116 
117 				if (line != null) {
118 					StringTokenizer st = new StringTokenizer(line, ";");
119 					st.nextToken();
120 					String toparse = st.nextToken();
121 
122 					Date measure = formatter.parse(toparse);
123 
124 					int c = 0;
125 					while (st.hasMoreElements()) {
126 						String column = st.nextToken();
127 						TimeSeries serie = this._series.get(c);
128 						serie.addOrUpdate(new Millisecond(measure), new Integer(column));
129 						c++;
130 					}
131 
132 				} else {
133 					eof = true;
134 
135 				}
136 
137 			} catch (StringIndexOutOfBoundsException e) {
138 				e.printStackTrace();
139 				eof = true;
140 			} catch (IOException e) {
141 				eof = true;
142 			}
143 		}
144 		for (int i = 0; i < this._series.size(); i++) {
145 			TimeSeries serie = this._series.get(i);
146 			timeseriescollection.addSeries(serie);
147 		}
148 		JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(this._title, "Time", "Value", timeseriescollection,
149 				true, true, false);
150 
151 		JPanel jpanel = new JPanel(new BorderLayout());
152 		ChartPanel chartpanel = new ChartPanel(jfreechart);
153 
154 		jpanel.add(chartpanel);
155 
156 		chartpanel.setPreferredSize(new Dimension(500, 270));
157 		setContentPane(jpanel);
158 	}
159 
160 	/**
161 	 * @param args
162 	 */
163 	public static void main(String[] args) {
164 		if (args.length < 1) {
165 			System.err.println("Usage : java BatchGraph repertoire");
166 			System.exit(1);
167 		}
168 
169 		String dir = args[0];
170 		System.out.println(dir);
171 		File fdir = new File(dir);
172 		if (fdir.isDirectory()) {
173 			File[] files = fdir.listFiles();
174 			for (int f = 0; f < files.length; f++) {
175 				String file = files[f].getAbsolutePath();
176 				try {
177 					BatchGraph graph = new BatchGraph(file);
178 					graph.fill();
179 					graph.pack();
180                     graph.setVisible(true);
181 				} catch (Exception e) {
182 					continue;
183 				}
184 			}
185 		} else {
186 			System.err.println("ERROR : " + dir + " is not a directory");
187 		}
188 	}
189 }