View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.statistics.detailed.jmx;
18  
19  import java.util.List;
20  
21  import javax.management.MBeanServer;
22  import javax.management.ObjectName;
23  
24  import org.springframework.beans.factory.DisposableBean;
25  import org.springframework.beans.factory.InitializingBean;
26  
27  import ch.elca.el4j.services.monitoring.jmx.JmxHtmlFormatter;
28  import ch.elca.el4j.services.statistics.detailed.MeasureItem;
29  import ch.elca.el4j.services.statistics.detailed.processing.DataRepository;
30  import ch.elca.el4j.services.statistics.detailed.processing.StatisticsOutputter;
31  import ch.elca.el4j.util.codingsupport.Reject;
32  
33  /**
34   * This class publishes the detailed statistics through JMX.
35   *
36   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/detailed_statistics/jmx/src/main/java/ch/elca/el4j/services/statistics/detailed/jmx/DetailedStatisticsReporter.java $
37   *
38   * @author David Stefan (DST)
39   */
40  public class DetailedStatisticsReporter implements
41  	DetailedStatisticsReporterMBean, InitializingBean, DisposableBean {
42  
43  	/** The address that points to this class. */
44  	public static final String NAME
45  		= "Performance:key=detailedStatisticsReporter";
46  
47  	/** The MBean server where this class is registered in. */
48  	private MBeanServer m_server;
49  
50  	/** The DataAssembler from which supplies the data for the statistics. */
51  	private DataRepository m_dataRepository;
52  
53  	/**
54  	 * @return Retruns the MBean server instance where this instance is
55  	 *         registered in.
56  	 */
57  	public MBeanServer getServer() {
58  		return m_server;
59  	}
60  
61  	/**
62  	 * Sets the MBean server where this instance has to register.
63  	 *
64  	 * @param beanServer
65  	 *            The MBean server to set.
66  	 */
67  	public void setServer(MBeanServer beanServer) {
68  		m_server = beanServer;
69  	}
70  
71  	/**
72  	 * @return Retruns the DataAssembler which supplies the data for the
73  	 *         statistics.
74  	 */
75  	public DataRepository getDataRepository() {
76  		return m_dataRepository;
77  	}
78  
79  	/**
80  	 * Sets the DataAssembler.
81  	 *
82  	 * @param dataRepository
83  	 *            The DataRepository to set
84  	 */
85  	public void setDataRepository(DataRepository dataRepository) {
86  		m_dataRepository = dataRepository;
87  	}
88  
89  	/**
90  	 * {@inheritDoc}
91  	 */
92  	public void afterPropertiesSet() throws Exception {
93  		if (m_server == null) {
94  			throw new IllegalStateException("m_beanServer has not been set!");
95  		}
96  
97  		m_server.registerMBean(this, new ObjectName(NAME));
98  	}
99  
100 	/**
101 	 * {@inheritDoc}
102 	 */
103 	public void destroy() throws Exception {
104 		if (m_server != null) {
105 			m_server.unregisterMBean(new ObjectName(NAME));
106 		}
107 	}
108 
109 	/**
110 	 * {@inheritDoc}
111 	 */
112 	public String showMeasureIDTable() {
113 		String result = "";
114 		String[][] table;
115 		// Due to unexpected method calls, display all measurements for now.
116 		List<MeasureItem> measureItems
117 			= m_dataRepository.getAllMeasureItems();
118 		// Add one row for labeling
119 		int noOfRowsInTable = measureItems.size() + 1;
120 		// Checkstyle: MagicNumber off
121 		table = new String[noOfRowsInTable][3];
122 		// Checkstyle: MagicNumber on
123 
124 		table[0][0] = "MeasureID";
125 		table[0][1] = "Duration in [ms]";
126 		table[0][2] = "Invocation Start";
127 		
128 		int i = 1;
129 		for (MeasureItem m : measureItems) {
130 			table[i][0] = m.getID().getFormattedString();
131 			table[i][1] = Long.toString(m.getDuration());
132 			table[i][2] = m.getEjbName() + "." + m.getMethodName();
133 			i++;
134 		}
135 		result = JmxHtmlFormatter.getHtmlTable(table);
136 		return result;
137 	}
138 
139 	/**
140 	 *
141 	 * {@inheritDoc}
142 	 */
143 	public void createCSVFile(String filename, String measureId) {
144 		Reject.ifNull(filename);
145 		Reject.ifNull(measureId);
146 		StatisticsOutputter sv = new StatisticsOutputter(m_dataRepository
147 			.getAllMeasureItems());
148 		sv.createCVSFile(filename, measureId);
149 	}
150 
151 	/**
152 	 * {@inheritDoc}
153 	 */
154 	public void createDiagramFile(String filename, String measureId) {
155 		createDiagramFile(filename, measureId, 0, 0);
156 		
157 	}
158 
159 	/**
160 	 * {@inheritDoc}
161 	 */
162 	public void createDiagramFile(String filename, String measureId,
163 		int width, int height) {
164 		Reject.ifNull(filename);
165 		Reject.ifNull(measureId);
166 		StatisticsOutputter sv = new StatisticsOutputter(m_dataRepository
167 			.getAllMeasureItems());
168 		sv.createDiagFile(filename, measureId, width, height);
169 		
170 	}
171 }