KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jmx > agent > AgentContext


1 /***
2  * Fractal JMX
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  */

21 package org.objectweb.fractal.jmx.agent;
22
23 // JMX
24
import javax.management.ObjectName JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.JMException JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.monitor.MonitorMBean JavaDoc;
29 import javax.management.monitor.StringMonitor JavaDoc;
30 import javax.management.monitor.CounterMonitor JavaDoc;
31 import javax.management.monitor.GaugeMonitor JavaDoc;
32
33 import javax.management.monitor.PeriodicMonitor JavaDoc;
34
35 import java.util.*;
36
37 /**
38  * The context of the agent component (handles patterns and monitors).
39  *
40  * @version 0.1
41  */

42 public class AgentContext {
43     private ObjectName JavaDoc[] _itfPatterns = new ObjectName JavaDoc[0];
44     private ObjectName JavaDoc[] _monitorPatterns = new ObjectName JavaDoc[0];
45     private AgentMonitorMBean[] _monitors = new AgentMonitorMBean[0];
46
47     // -------------------------------------------------------------------------
48
// public
49
// -------------------------------------------------------------------------
50
/**
51      * returns the current {@link AgentMonitorMBean monitors}.
52      *
53      * @return the current {@link AgentMonitorMBean monitors}.
54      */

55     public AgentMonitorMBean[] getMonitors() {
56         return _monitors;
57     }
58
59     /**
60      * Checks if an {@link ObjectName} matches the current patterns for filtering component interface MBeans.
61      *
62      * @param oName the {@link ObjectName} to check.
63      * @return true if it matches, false otherwhise.
64      */

65     public boolean matchItfPatterns(ObjectName JavaDoc oName) {
66         if (matchPatterns(_itfPatterns, oName))
67             return true;
68         return false;
69     }
70
71     /**
72      * Checks if an {@link ObjectName} matches the current patterns for filtering monitor MBeans.
73      *
74      * @param oName the {@link ObjectName} to check.
75      * @return true if it matches, false otherwhise.
76      */

77     public boolean matchMonitorPatterns(ObjectName JavaDoc oName) {
78         if (matchPatterns(_monitorPatterns, oName))
79             return true;
80         return false;
81     }
82
83     /**
84      * Resets this {@link Agent agent} context.
85      *
86      * <P>This method translates the {@link org.objectweb.fractal.jmx.agent.AdminAttributes agent attributes},
87      * in their corresponding {@link ObjectName} patterns and {@link AgentMonitorMBean monitors}.
88      *
89      * @param fc the agent attribute for filtering component interface MBeans.
90      * @param str the agent attribute for filtering {@link javax.management.monitor.StringMonitorMBean string monitor} MBeans.
91      * @param counter the agent attribute for filtering {@link javax.management.monitor.CounterMonitorMBean counter monitor} MBeans.
92      * @param gauge the agent attribute for filtering {@link javax.management.monitor.GaugeMonitorMBean gauge monitor} MBeans.
93      * @param periodic the agent attribute for filtering {@link javax.management.monitor.PeriodicMonitorMBean gauge monitor} MBeans.
94      *
95      * @throws JMException if a problem occurs during the translation.
96      */

97     public void reset(String JavaDoc fc, String JavaDoc str, String JavaDoc counter, String JavaDoc gauge, String JavaDoc periodic) throws JMException JavaDoc {
98         cleanMonitors(_monitors);
99         set(fc, str, counter, gauge, periodic);
100     }
101
102     // -------------------------------------------------------------------------
103
// reset
104
// -------------------------------------------------------------------------
105
private void cleanMonitors(MonitorMBean JavaDoc[] monitors) {
106         for (int i = 0; i < monitors.length; i++) {
107             monitors[i].stop();
108             ObjectName JavaDoc[] onames = monitors[i].getObservedObjects();
109             for (int j = 0; j < onames.length; j++) {
110                 monitors[i].removeObservedObject(onames[j]);
111             }
112         }
113     }
114
115     private void set(String JavaDoc fc, String JavaDoc str, String JavaDoc counter, String JavaDoc gauge, String JavaDoc periodic) throws JMException JavaDoc {
116         // creates monitors
117
AgentMonitorMBean[] stringMonitors = createStringMonitors(str);
118         AgentMonitorMBean[] counterMonitors = createCounterMonitors(counter);
119         AgentMonitorMBean[] gaugeMonitors = createGaugeMonitors(gauge);
120         AgentMonitorMBean[] periodicMonitors = createPeriodicMonitors(periodic); // !!!
121
// merge monitors
122
List l = new ArrayList(Arrays.asList(stringMonitors));
123         l.addAll(Arrays.asList(counterMonitors));
124         l.addAll(Arrays.asList(gaugeMonitors));
125         l.addAll(Arrays.asList(periodicMonitors));
126         _monitors = (AgentMonitorMBean[]) l.toArray(new AgentMonitorMBean[0]);
127         // creates patterns
128
_itfPatterns = createItfPatterns(fc);
129         l = new ArrayList(Arrays.asList(getMonitorPatterns(_monitors)));
130         _monitorPatterns = (ObjectName JavaDoc[]) l.toArray(new ObjectName JavaDoc[0]);
131     }
132
133     // -------------------------------------------------------------------------
134
// patterns
135
// -------------------------------------------------------------------------
136
private ObjectName JavaDoc[] createItfPatterns(String JavaDoc str) throws MalformedObjectNameException JavaDoc {
137         String JavaDoc[] strTab = split(str, ";");
138         ObjectName JavaDoc[] oNames = new ObjectName JavaDoc[strTab.length];
139         for (int i = 0; i < strTab.length; i++) {
140             try {
141                 oNames[i] = new ObjectName JavaDoc(strTab[i]);
142             } catch (MalformedObjectNameException JavaDoc e) {
143                 throw new MalformedObjectNameException JavaDoc(
144                     "pattern \"" + strTab[i] + "\" is an invalid string representation of JMX object name");
145             }
146         }
147         return oNames;
148     }
149
150     private boolean matchPatterns(ObjectName JavaDoc[] patterns, ObjectName JavaDoc oName) {
151         for (int i = 0; i < patterns.length; i++) {
152             if (patterns[i].apply(oName))
153                 return true;
154         }
155         return false;
156     }
157
158     private ObjectName JavaDoc[] getMonitorPatterns(AgentMonitorMBean[] monitors) {
159         ObjectName JavaDoc[] oNames = new ObjectName JavaDoc[monitors.length];
160         for (int i = 0; i < oNames.length; i++)
161             oNames[i] = monitors[i].getPattern();
162         return oNames;
163     }
164
165     // -------------------------------------------------------------------------
166
// Periodic monitor
167
// -------------------------------------------------------------------------
168
private AgentMonitorMBean[] createPeriodicMonitors(String JavaDoc str) throws MalformedObjectNameException JavaDoc {
169         String JavaDoc[] strTab = split(str, ";");
170         //System.out.println("- " + Arrays.asList(strTab));
171
MyPeriodicMonitor[] periodics = new MyPeriodicMonitor[strTab.length];
172         for (int i = 0; i < strTab.length; i++) {
173             try {
174                 ObjectName JavaDoc oName = new ObjectName JavaDoc(strTab[i]);
175                 Hashtable h = oName.getKeyPropertyList();
176                 periodics[i] = new MyPeriodicMonitor((i + 1));
177                 // set String monitor params
178
periodics[i].setObservedAttribute((String JavaDoc) remove(h, "observedAttribute"));
179                 periodics[i].setGranularityPeriod(Long.parseLong((String JavaDoc) remove(h, "granularityPeriod")));
180                 if (oName.isPropertyPattern())
181                     periodics[i].setPattern(new ObjectName JavaDoc(oName.getDomain() + ":" + asString(h) + ",*"));
182                 else
183                     periodics[i].setPattern(new ObjectName JavaDoc(oName.getDomain(), h));
184             } catch (Exception JavaDoc e) {
185                 throw new IllegalStateException JavaDoc(
186                     "Periodic monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.PREFIX_SYNTAX);
187             }
188         }
189         return periodics;
190     }
191
192     class MyPeriodicMonitor extends PeriodicMonitor JavaDoc implements AgentMonitorMBean {
193         private ObjectName JavaDoc _pattern;
194         private int _i;
195
196         MyPeriodicMonitor(int i) throws JMException JavaDoc {
197             this._pattern = new ObjectName JavaDoc("*:*");
198             this._i = i;
199         }
200
201         public ObjectName JavaDoc getPattern() {
202             return _pattern;
203         }
204
205         public void setPattern(ObjectName JavaDoc pattern) {
206             _pattern = pattern;
207         }
208         public Class JavaDoc getAttributeType() {
209             return Object JavaDoc.class;
210         }
211
212         public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc {
213             super.preRegister(server, name);
214             return new ObjectName JavaDoc(CONST.MONITOR + ":type=periodicMonitor" + ",observer=" + _i);
215         }
216     }
217
218
219     // -------------------------------------------------------------------------
220
// String monitor
221
// -------------------------------------------------------------------------
222
private AgentMonitorMBean[] createStringMonitors(String JavaDoc str) throws MalformedObjectNameException JavaDoc {
223         String JavaDoc[] strTab = split(str, ";");
224         //System.out.println("- " + Arrays.asList(strTab));
225
MyStringMonitor[] gauges = new MyStringMonitor[strTab.length];
226         for (int i = 0; i < strTab.length; i++) {
227             try {
228                 ObjectName JavaDoc oName = new ObjectName JavaDoc(strTab[i]);
229                 Hashtable h = oName.getKeyPropertyList();
230                 gauges[i] = new MyStringMonitor((i + 1));
231                 // set String monitor params
232
gauges[i].setObservedAttribute((String JavaDoc) remove(h, "observedAttribute"));
233                 gauges[i].setGranularityPeriod(Long.parseLong((String JavaDoc) remove(h, "granularityPeriod")));
234                 gauges[i].setStringToCompare((String JavaDoc) remove(h, "stringToCompare"));
235                 gauges[i].setNotifyDiffer(Boolean.valueOf((String JavaDoc) remove(h, "notifyDiffer")).booleanValue());
236                 gauges[i].setNotifyMatch(Boolean.valueOf((String JavaDoc) remove(h, "notifyMatch")).booleanValue());
237                 if (oName.isPropertyPattern())
238                     gauges[i].setPattern(new ObjectName JavaDoc(oName.getDomain() + ":" + asString(h) + ",*"));
239                 else
240                     gauges[i].setPattern(new ObjectName JavaDoc(oName.getDomain(), h));
241             } catch (Exception JavaDoc e) {
242                 throw new IllegalStateException JavaDoc(
243                     "String monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.STRING_SYNTAX);
244             }
245         }
246         return gauges;
247     }
248
249     class MyStringMonitor extends StringMonitor JavaDoc implements AgentMonitorMBean {
250         private ObjectName JavaDoc _pattern;
251         private int _i;
252
253         MyStringMonitor(int i) throws JMException JavaDoc {
254             this._pattern = new ObjectName JavaDoc("*:*");
255             this._i = i;
256         }
257
258         public ObjectName JavaDoc getPattern() {
259             return _pattern;
260         }
261
262         public void setPattern(ObjectName JavaDoc pattern) {
263             _pattern = pattern;
264         }
265         public Class JavaDoc getAttributeType() {
266             return String JavaDoc.class;
267         }
268
269         public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc {
270             super.preRegister(server, name);
271             return new ObjectName JavaDoc(CONST.MONITOR + ":type=stringMonitor" + ",observer=" + _i);
272         }
273     }
274
275     // -------------------------------------------------------------------------
276
// Counter monitor
277
// -------------------------------------------------------------------------
278
private AgentMonitorMBean[] createCounterMonitors(String JavaDoc str) throws MalformedObjectNameException JavaDoc {
279         String JavaDoc[] strTab = split(str, ";");
280         MyCounterMonitor[] counters = new MyCounterMonitor[strTab.length * 4];
281         for (int i = 0; i < strTab.length; i++) {
282             try {
283                 ObjectName JavaDoc oName = new ObjectName JavaDoc(strTab[i]);
284                 Hashtable h = oName.getKeyPropertyList();
285                 int j = i * 4;
286                 counters[j] = new MyCounterMonitor((i + 1), Long JavaDoc.class);
287                 // set Counter monitor params
288
counters[j].setObservedAttribute((String JavaDoc) remove(h, "observedAttribute"));
289                 counters[j].setGranularityPeriod(Long.parseLong((String JavaDoc) remove(h, "granularityPeriod")));
290                 counters[j].setInitThreshold(Long.valueOf((String JavaDoc) remove(h, "initThreshold")));
291                 counters[j].setNotify(true);
292                 counters[j].setModulus(Long.valueOf((String JavaDoc) remove(h, "modulus")));
293                 counters[j].setOffset(Long.valueOf((String JavaDoc) remove(h, "offset")));
294                 counters[j].setDifferenceMode(Boolean.valueOf((String JavaDoc) remove(h, "differenceMode")).booleanValue());
295                 if (oName.isPropertyPattern())
296                     counters[j].setPattern(new ObjectName JavaDoc(oName.getDomain() + ":" + asString(h) + ",*"));
297                 else
298                     counters[j].setPattern(new ObjectName JavaDoc(oName.getDomain(), h));
299                 counters[j + 1] = new MyCounterMonitor(counters[j], Integer JavaDoc.class);
300                 counters[j + 2] = new MyCounterMonitor(counters[j], Short JavaDoc.class);
301                 counters[j + 3] = new MyCounterMonitor(counters[j], Byte JavaDoc.class);
302             } catch (Exception JavaDoc e) {
303                 throw new IllegalStateException JavaDoc(
304                     "Counter monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.COUNTER_SYNTAX);
305             }
306         }
307         return counters;
308     }
309
310     class MyCounterMonitor extends CounterMonitor JavaDoc implements AgentMonitorMBean {
311         private ObjectName JavaDoc _pattern;
312         private int _i;
313         private Class JavaDoc _attType;
314
315         MyCounterMonitor(int i, Class JavaDoc attType) throws JMException JavaDoc {
316             this._pattern = new ObjectName JavaDoc("*:*");
317             this._i = i;
318             this._attType = attType;
319         }
320
321         MyCounterMonitor(MyCounterMonitor g, Class JavaDoc attType) throws MalformedObjectNameException JavaDoc {
322             this._pattern = g.getPattern();
323             this._i = g._i;
324             this._attType = attType;
325             setObservedAttribute(g.getObservedAttribute());
326             setGranularityPeriod(g.getGranularityPeriod());
327             setDifferenceMode(g.getDifferenceMode());
328             setNotify(g.getNotify());
329             if (attType.equals(Long JavaDoc.class)) {
330                 setInitThreshold(new Long JavaDoc(g.getInitThreshold().longValue()));
331                 setModulus(new Long JavaDoc(g.getModulus().longValue()));
332                 setOffset(new Long JavaDoc(g.getOffset().longValue()));
333             } else if (attType.equals(Integer JavaDoc.class)) {
334                 setInitThreshold(new Integer JavaDoc(g.getInitThreshold().intValue()));
335                 setModulus(new Integer JavaDoc(g.getModulus().intValue()));
336                 setOffset(new Integer JavaDoc(g.getOffset().intValue()));
337             } else if (attType.equals(Short JavaDoc.class)) {
338                 setInitThreshold(new Short JavaDoc(g.getInitThreshold().shortValue()));
339                 setModulus(new Short JavaDoc(g.getModulus().shortValue()));
340                 setOffset(new Short JavaDoc(g.getOffset().shortValue()));
341             } else if (attType.equals(Byte JavaDoc.class)) {
342                 setInitThreshold(new Byte JavaDoc(g.getInitThreshold().byteValue()));
343                 setModulus(new Byte JavaDoc(g.getModulus().byteValue()));
344                 setOffset(new Byte JavaDoc(g.getOffset().byteValue()));
345             } else
346                 throw new IllegalStateException JavaDoc(attType + " is an invalid counter threshold type");
347         }
348
349         public ObjectName JavaDoc getPattern() {
350             return _pattern;
351         }
352
353         public void setPattern(ObjectName JavaDoc pattern) {
354             _pattern = pattern;
355         }
356
357         public Class JavaDoc getAttributeType() {
358             return _attType;
359         }
360
361         public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc {
362             super.preRegister(server, name);
363             return new ObjectName JavaDoc(
364                 CONST.MONITOR + ":type=counterMonitor" + ",observer=" + _i + ",attributeType=" + _attType.getName());
365         }
366     }
367
368     // -------------------------------------------------------------------------
369
// Gauge monitor
370
// -------------------------------------------------------------------------
371
private AgentMonitorMBean[] createGaugeMonitors(String JavaDoc str) throws MalformedObjectNameException JavaDoc {
372         String JavaDoc[] strTab = split(str, ";");
373         MyGaugeMonitor[] gauges = new MyGaugeMonitor[strTab.length * 6];
374         for (int i = 0; i < strTab.length; i++) {
375             try {
376                 ObjectName JavaDoc oName = new ObjectName JavaDoc(strTab[i]);
377                 Hashtable h = oName.getKeyPropertyList();
378                 int j = i * 4;
379                 gauges[j] = new MyGaugeMonitor((i + 1), Double JavaDoc.class);
380                 // set Gauge monitor params
381
gauges[j].setObservedAttribute((String JavaDoc) remove(h, "observedAttribute"));
382                 gauges[j].setGranularityPeriod(Long.parseLong((String JavaDoc) remove(h, "granularityPeriod")));
383                 gauges[j].setNotifyLow(true);
384                 gauges[j].setNotifyHigh(true);
385                 gauges[j].setThresholds(
386                     Double.valueOf((String JavaDoc) remove(h, "highThreshold")),
387                     Double.valueOf((String JavaDoc) remove(h, "lowThreshold")));
388                 gauges[j].setDifferenceMode(Boolean.valueOf((String JavaDoc) remove(h, "differenceMode")).booleanValue());
389                 if (oName.isPropertyPattern())
390                     gauges[j].setPattern(new ObjectName JavaDoc(oName.getDomain() + ":" + asString(h) + ",*"));
391                 else
392                     gauges[j].setPattern(new ObjectName JavaDoc(oName.getDomain(), h));
393                 gauges[j + 1] = new MyGaugeMonitor(gauges[j], Float JavaDoc.class);
394                 gauges[j + 2] = new MyGaugeMonitor(gauges[j], Long JavaDoc.class);
395                 gauges[j + 3] = new MyGaugeMonitor(gauges[j], Integer JavaDoc.class);
396                 gauges[j + 4] = new MyGaugeMonitor(gauges[j], Short JavaDoc.class);
397                 gauges[j + 5] = new MyGaugeMonitor(gauges[j], Byte JavaDoc.class);
398             } catch (Exception JavaDoc e) {
399                 throw new IllegalStateException JavaDoc(
400                     "Gauge monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.GAUGE_SYNTAX);
401             }
402         }
403         return gauges;
404     }
405
406     class MyGaugeMonitor extends GaugeMonitor JavaDoc implements AgentMonitorMBean {
407         private ObjectName JavaDoc _pattern;
408         private int _i;
409         private Class JavaDoc _attType;
410
411         MyGaugeMonitor(int i, Class JavaDoc attType) throws JMException JavaDoc {
412             this._pattern = new ObjectName JavaDoc("*:*");
413             this._i = i;
414             this._attType = attType;
415         }
416
417         MyGaugeMonitor(MyGaugeMonitor g, Class JavaDoc attType) {
418             this._pattern = g.getPattern();
419             this._i = g._i;
420             this._attType = attType;
421             setObservedAttribute(g.getObservedAttribute());
422             setGranularityPeriod(g.getGranularityPeriod());
423             setDifferenceMode(g.getDifferenceMode());
424             setNotifyLow(g.getNotifyLow());
425             setNotifyHigh(g.getNotifyHigh());
426             Number JavaDoc high = g.getHighThreshold();
427             Number JavaDoc low = g.getLowThreshold();
428             if (attType.equals(Double JavaDoc.class))
429                 setThresholds(new Double JavaDoc(high.doubleValue()), new Double JavaDoc(low.doubleValue()));
430             else if (attType.equals(Float JavaDoc.class))
431                 setThresholds(new Float JavaDoc(high.floatValue()), new Float JavaDoc(low.floatValue()));
432             else if (attType.equals(Long JavaDoc.class))
433                 setThresholds(new Long JavaDoc(high.longValue()), new Long JavaDoc(low.longValue()));
434             else if (attType.equals(Integer JavaDoc.class))
435                 setThresholds(new Integer JavaDoc(high.intValue()), new Integer JavaDoc(low.intValue()));
436             else if (attType.equals(Short JavaDoc.class))
437                 setThresholds(new Short JavaDoc(high.shortValue()), new Short JavaDoc(low.shortValue()));
438             else if (attType.equals(Byte JavaDoc.class))
439                 setThresholds(new Byte JavaDoc(high.byteValue()), new Byte JavaDoc(low.byteValue()));
440             else
441                 throw new IllegalStateException JavaDoc(attType + " is an invalid gauge threshold type");
442         }
443
444         public ObjectName JavaDoc getPattern() {
445             return _pattern;
446         }
447
448         public void setPattern(ObjectName JavaDoc pattern) {
449             _pattern = pattern;
450         }
451
452         public Class JavaDoc getAttributeType() {
453             return _attType;
454         }
455
456         public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc {
457             super.preRegister(server, name);
458             return new ObjectName JavaDoc(
459                 CONST.MONITOR + ":type=gaugeMonitor" + ",observer=" + _i + ",attributeType=" + _attType.getName());
460         }
461     }
462
463     // -------------------------------------------------------------------------
464
// Java utils
465
// -------------------------------------------------------------------------
466
private String JavaDoc[] split(String JavaDoc str, String JavaDoc regex) { // JDK1.4 !!!
467
if (str == null || str.matches(" *"))
468             return new String JavaDoc[0];
469         return str.split(regex);
470     }
471
472     private Object JavaDoc remove(Hashtable h, String JavaDoc key) throws Exception JavaDoc {
473         if (h.containsKey(key))
474             return h.remove(key);
475         throw new Exception JavaDoc("property \"" + key + "\" is missing");
476     }
477
478     private String JavaDoc asString(Hashtable h) {
479         String JavaDoc str = "";
480         for (Iterator i = h.entrySet().iterator(); i.hasNext();) {
481             Map.Entry e = (Map.Entry) i.next();
482             str += e.getKey() + "=" + e.getValue();
483             if (i.hasNext())
484                 str += ",";
485         }
486         return str;
487     }
488 }
489
Popular Tags