1 package org.sourceforge.mbeanmonitoring.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
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
144
145
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
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
215
216
217
218
219 }
220 }
221
222 }