View Javadoc

1   package org.sourceforge.mbeanmonitoring.report;
2   
3   /**
4    **      Author:
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.io.NotSerializableException;
24  import java.util.ArrayList;
25  import java.util.Properties;
26  
27  import javax.management.InstanceNotFoundException;
28  import javax.management.ObjectName;
29  import javax.management.j2ee.statistics.CountStatistic;
30  import javax.management.j2ee.statistics.RangeStatistic;
31  import javax.management.j2ee.statistics.Statistic;
32  import javax.management.j2ee.statistics.Stats;
33  import javax.management.j2ee.statistics.TimeStatistic;
34  
35  import org.apache.log4j.Category;
36  import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
37  import org.jboss.management.j2ee.statistics.EntityBeanStatsImpl;
38  import org.sourceforge.mbeanmonitoring.report.castor.Stat;
39  import org.sourceforge.mbeanmonitoring.report.castor.types.MethodStatNameType;
40  
41  public class MServer implements Runnable {
42  
43  	private RMIAdaptor adaptor;
44  
45  	private Properties[] infos;
46  	private ObjectName[] mbeans;
47  	private String separator;
48  
49  	public MServer(RMIAdaptor adaptor, Properties[] infos, String separator) {
50  		this.adaptor = adaptor;
51  		this.infos = infos;
52  		this.separator = separator;
53  	}
54  
55  	private String[] getAttributs(int index) {
56  		return ((String[]) this.infos[index].get(Capture.KEY_MBEAN_ATTRS));
57  	}
58  
59  	private void getData() {
60  		int i = -1;
61  		if (this.mbeans == null)
62  			this.getMBeans();
63  		try {
64  			for (i = 0; i < this.infos.length; i++) {
65  				if (this.mbeans[i] != null) {
66  					String appender = this.infos[i].getProperty(Capture.KEY_APPENDER);//,this.infos[i].getProperty("name"));
67  					StringBuffer datas = new StringBuffer();
68  					for (int j = 0; j < getAttributs(i).length; j++) {
69  						if (datas.length() > 0)
70  							datas.append(this.separator);
71  
72  						Object result = null;
73  						try {
74  							result = this.adaptor.getAttribute(this.mbeans[i], getAttributs(i)[j]);
75  						} catch (NotSerializableException e) {
76  				            result = this.adaptor.invoke(this.mbeans[i], "list" + getAttributs(i)[j], null, null) ;
77  							System.out.println("Invocation de " + getAttributs(i)[j] + " : " + result.toString()) ;
78  							result = "0";
79  						} catch (InstanceNotFoundException e) {
80  							result = "0";
81  						}
82  						if (result instanceof Stats) {
83  							Stats st = (Stats) result;
84  							Statistic[] stats = st.getStatistics();
85  							if (result instanceof EntityBeanStatsImpl) {
86  								EntityBeanStatsImpl entityStats = (EntityBeanStatsImpl) result;
87  								stats = entityStats.getStatistics();
88  							}
89  
90  							String[] wantedStatsNames = getEntete(i);
91  
92  							for (int k = 0; k < wantedStatsNames.length; k++) {
93  								int ii = -1;
94  								do {
95  									ii++;
96  								} while (ii < stats.length && stats[ii] != null
97  										&& !wantedStatsNames[k].startsWith(stats[ii].getName()));
98  
99  								Statistic stat = null;
100 								if (ii < stats.length)
101 									stat = stats[ii];
102 
103 								if (datas.length() > 0)
104 									datas.append(this.separator);
105 								if (stat != null) {
106 									String value = stat.getDescription();
107 									if (stat instanceof RangeStatistic) {
108 										value = new Long(((RangeStatistic) stat).getCurrent()).toString();
109 									} else if (stat instanceof CountStatistic) {
110 										value = new Long(((CountStatistic) stat).getCount()).toString();
111 									} else if (stat instanceof TimeStatistic) {
112 										TimeStatistic time = (TimeStatistic) stat;
113 										if (wantedStatsNames[k].endsWith(MethodStatNameType.COUNT.toString()))
114 											value = new Long(time.getCount()).toString();
115 										else if (wantedStatsNames[k].endsWith(MethodStatNameType.MAX_TIME.toString()))
116 											value = new Long(time.getMaxTime()).toString();
117 										else if (wantedStatsNames[k].endsWith(MethodStatNameType.MIN_TIME.toString()))
118 											value = new Long(time.getMinTime()).toString();
119 										else if (wantedStatsNames[k].endsWith(MethodStatNameType.TOTAL_TIME.toString()))
120 											value = new Long(time.getTotalTime()).toString();
121 										else if (wantedStatsNames[k].endsWith(MethodStatNameType.MOY_TIME.toString())) {
122 											long totalTime = time.getTotalTime();
123 											long count = time.getCount();
124 											if (count != 0) {
125 												value = new Long(totalTime / count).toString();
126 											} else {
127 												value = "-1";
128 											}
129 										}
130 									}
131 									datas.append(value);
132 
133 								} else
134 									datas.append("0");
135 							}
136 						} else {
137 							datas.append(result);
138 						}
139 					}
140 					Category.getInstance(appender).debug(datas.toString());
141 				}
142 			}
143 			//        } catch (InstanceNotFoundException e) {
144 			//            e.printStackTrace();
145 			//this.mbeans[i] = null;
146 		} catch (Exception e) {
147 			e.printStackTrace();
148 		}
149 	}
150 
151 	private String[] getEntete(int index) {
152 		String[] attributs = getAttributs(index);
153 		ArrayList<String> result = new ArrayList<String>(attributs.length);
154 		for (int i = 0; i < attributs.length; i++) {
155 			Stat[] stats = (Stat[]) this.infos[index].get(attributs[i]);
156 
157 			if (stats.length != 0) {
158 				for (int j = 0; j < stats.length; j++) {
159 					Stat st = stats[j];
160 					if (st.getMethodStatCount() == 0)
161 						result.add(st.getName());
162 					else
163 						for (int k = 0; k < st.getMethodStat().length; k++) {
164 							result.add(st.getName() + "." + st.getMethodStat(k).getName());
165 						}
166 				}
167 			} else {
168 				result.add(attributs[i]);
169 			}
170 		}
171 		attributs = new String[result.size()];
172 		result.toArray(attributs);
173 		return attributs;
174 	}
175 
176 	private void getMBeans() {
177 		this.mbeans = new ObjectName[infos.length];
178 
179 		for (int i = 0; i < infos.length; i++) {
180 			try {
181 				this.mbeans[i] = new ObjectName(this.infos[i].getProperty(Capture.KEY_MBEAN_NAME));
182 			} catch (Exception e) {
183 				System.out.println("Failed to get MBean = " + this.infos[i].getProperty(Capture.KEY_MBEAN_NAME));
184 				//e.printStackTrace();
185 			}
186 		}
187 	}
188 
189 	/**
190 	 *
191 	 */
192 	public void logColumnNames() {
193 		for (int i = 0; i < this.infos.length; i++) {
194 			String appender = this.infos[i].getProperty(Capture.KEY_APPENDER);
195 
196 			String[] attributs = getEntete(i);
197 			StringBuffer entete = new StringBuffer();
198 			entete.append(attributs[0]);
199 
200 			for (int j = 1; j < attributs.length; j++) {
201 				entete.append(this.separator);
202 				entete.append(attributs[j]);
203 			}
204 
205 			Category.getInstance(appender).debug(entete.toString());
206 		}
207 	}
208 
209 	public void run() {
210 		if (this.adaptor != null) {
211 			this.getData();
212 			/*
213 			 * //////////////////////////////////////////////////////////////// //
214 			 * Pause for tests: // "NO TASKS TO RUN... INCREASE THE TASKS POOL
215 			 * SIZE !" // long duration = 1500 + Math.round(5000 *
216 			 * Math.random()); try { Thread.sleep(duration); } catch
217 			 * (InterruptedException e) { e.printStackTrace(); }
218 			 */
219 		}
220 	}
221 
222 }