1 21 package org.objectweb.fractal.jmx.agent; 22 23 import javax.management.ObjectName ; 25 import javax.management.MBeanServer ; 26 import javax.management.JMException ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.monitor.MonitorMBean ; 29 import javax.management.monitor.StringMonitor ; 30 import javax.management.monitor.CounterMonitor ; 31 import javax.management.monitor.GaugeMonitor ; 32 33 import javax.management.monitor.PeriodicMonitor ; 34 35 import java.util.*; 36 37 42 public class AgentContext { 43 private ObjectName [] _itfPatterns = new ObjectName [0]; 44 private ObjectName [] _monitorPatterns = new ObjectName [0]; 45 private AgentMonitorMBean[] _monitors = new AgentMonitorMBean[0]; 46 47 55 public AgentMonitorMBean[] getMonitors() { 56 return _monitors; 57 } 58 59 65 public boolean matchItfPatterns(ObjectName oName) { 66 if (matchPatterns(_itfPatterns, oName)) 67 return true; 68 return false; 69 } 70 71 77 public boolean matchMonitorPatterns(ObjectName oName) { 78 if (matchPatterns(_monitorPatterns, oName)) 79 return true; 80 return false; 81 } 82 83 97 public void reset(String fc, String str, String counter, String gauge, String periodic) throws JMException { 98 cleanMonitors(_monitors); 99 set(fc, str, counter, gauge, periodic); 100 } 101 102 private void cleanMonitors(MonitorMBean [] monitors) { 106 for (int i = 0; i < monitors.length; i++) { 107 monitors[i].stop(); 108 ObjectName [] 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 fc, String str, String counter, String gauge, String periodic) throws JMException { 116 AgentMonitorMBean[] stringMonitors = createStringMonitors(str); 118 AgentMonitorMBean[] counterMonitors = createCounterMonitors(counter); 119 AgentMonitorMBean[] gaugeMonitors = createGaugeMonitors(gauge); 120 AgentMonitorMBean[] periodicMonitors = createPeriodicMonitors(periodic); 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 _itfPatterns = createItfPatterns(fc); 129 l = new ArrayList(Arrays.asList(getMonitorPatterns(_monitors))); 130 _monitorPatterns = (ObjectName []) l.toArray(new ObjectName [0]); 131 } 132 133 private ObjectName [] createItfPatterns(String str) throws MalformedObjectNameException { 137 String [] strTab = split(str, ";"); 138 ObjectName [] oNames = new ObjectName [strTab.length]; 139 for (int i = 0; i < strTab.length; i++) { 140 try { 141 oNames[i] = new ObjectName (strTab[i]); 142 } catch (MalformedObjectNameException e) { 143 throw new MalformedObjectNameException ( 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 [] patterns, ObjectName 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 [] getMonitorPatterns(AgentMonitorMBean[] monitors) { 159 ObjectName [] oNames = new ObjectName [monitors.length]; 160 for (int i = 0; i < oNames.length; i++) 161 oNames[i] = monitors[i].getPattern(); 162 return oNames; 163 } 164 165 private AgentMonitorMBean[] createPeriodicMonitors(String str) throws MalformedObjectNameException { 169 String [] strTab = split(str, ";"); 170 MyPeriodicMonitor[] periodics = new MyPeriodicMonitor[strTab.length]; 172 for (int i = 0; i < strTab.length; i++) { 173 try { 174 ObjectName oName = new ObjectName (strTab[i]); 175 Hashtable h = oName.getKeyPropertyList(); 176 periodics[i] = new MyPeriodicMonitor((i + 1)); 177 periodics[i].setObservedAttribute((String ) remove(h, "observedAttribute")); 179 periodics[i].setGranularityPeriod(Long.parseLong((String ) remove(h, "granularityPeriod"))); 180 if (oName.isPropertyPattern()) 181 periodics[i].setPattern(new ObjectName (oName.getDomain() + ":" + asString(h) + ",*")); 182 else 183 periodics[i].setPattern(new ObjectName (oName.getDomain(), h)); 184 } catch (Exception e) { 185 throw new IllegalStateException ( 186 "Periodic monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.PREFIX_SYNTAX); 187 } 188 } 189 return periodics; 190 } 191 192 class MyPeriodicMonitor extends PeriodicMonitor implements AgentMonitorMBean { 193 private ObjectName _pattern; 194 private int _i; 195 196 MyPeriodicMonitor(int i) throws JMException { 197 this._pattern = new ObjectName ("*:*"); 198 this._i = i; 199 } 200 201 public ObjectName getPattern() { 202 return _pattern; 203 } 204 205 public void setPattern(ObjectName pattern) { 206 _pattern = pattern; 207 } 208 public Class getAttributeType() { 209 return Object .class; 210 } 211 212 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { 213 super.preRegister(server, name); 214 return new ObjectName (CONST.MONITOR + ":type=periodicMonitor" + ",observer=" + _i); 215 } 216 } 217 218 219 private AgentMonitorMBean[] createStringMonitors(String str) throws MalformedObjectNameException { 223 String [] strTab = split(str, ";"); 224 MyStringMonitor[] gauges = new MyStringMonitor[strTab.length]; 226 for (int i = 0; i < strTab.length; i++) { 227 try { 228 ObjectName oName = new ObjectName (strTab[i]); 229 Hashtable h = oName.getKeyPropertyList(); 230 gauges[i] = new MyStringMonitor((i + 1)); 231 gauges[i].setObservedAttribute((String ) remove(h, "observedAttribute")); 233 gauges[i].setGranularityPeriod(Long.parseLong((String ) remove(h, "granularityPeriod"))); 234 gauges[i].setStringToCompare((String ) remove(h, "stringToCompare")); 235 gauges[i].setNotifyDiffer(Boolean.valueOf((String ) remove(h, "notifyDiffer")).booleanValue()); 236 gauges[i].setNotifyMatch(Boolean.valueOf((String ) remove(h, "notifyMatch")).booleanValue()); 237 if (oName.isPropertyPattern()) 238 gauges[i].setPattern(new ObjectName (oName.getDomain() + ":" + asString(h) + ",*")); 239 else 240 gauges[i].setPattern(new ObjectName (oName.getDomain(), h)); 241 } catch (Exception e) { 242 throw new IllegalStateException ( 243 "String monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.STRING_SYNTAX); 244 } 245 } 246 return gauges; 247 } 248 249 class MyStringMonitor extends StringMonitor implements AgentMonitorMBean { 250 private ObjectName _pattern; 251 private int _i; 252 253 MyStringMonitor(int i) throws JMException { 254 this._pattern = new ObjectName ("*:*"); 255 this._i = i; 256 } 257 258 public ObjectName getPattern() { 259 return _pattern; 260 } 261 262 public void setPattern(ObjectName pattern) { 263 _pattern = pattern; 264 } 265 public Class getAttributeType() { 266 return String .class; 267 } 268 269 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { 270 super.preRegister(server, name); 271 return new ObjectName (CONST.MONITOR + ":type=stringMonitor" + ",observer=" + _i); 272 } 273 } 274 275 private AgentMonitorMBean[] createCounterMonitors(String str) throws MalformedObjectNameException { 279 String [] strTab = split(str, ";"); 280 MyCounterMonitor[] counters = new MyCounterMonitor[strTab.length * 4]; 281 for (int i = 0; i < strTab.length; i++) { 282 try { 283 ObjectName oName = new ObjectName (strTab[i]); 284 Hashtable h = oName.getKeyPropertyList(); 285 int j = i * 4; 286 counters[j] = new MyCounterMonitor((i + 1), Long .class); 287 counters[j].setObservedAttribute((String ) remove(h, "observedAttribute")); 289 counters[j].setGranularityPeriod(Long.parseLong((String ) remove(h, "granularityPeriod"))); 290 counters[j].setInitThreshold(Long.valueOf((String ) remove(h, "initThreshold"))); 291 counters[j].setNotify(true); 292 counters[j].setModulus(Long.valueOf((String ) remove(h, "modulus"))); 293 counters[j].setOffset(Long.valueOf((String ) remove(h, "offset"))); 294 counters[j].setDifferenceMode(Boolean.valueOf((String ) remove(h, "differenceMode")).booleanValue()); 295 if (oName.isPropertyPattern()) 296 counters[j].setPattern(new ObjectName (oName.getDomain() + ":" + asString(h) + ",*")); 297 else 298 counters[j].setPattern(new ObjectName (oName.getDomain(), h)); 299 counters[j + 1] = new MyCounterMonitor(counters[j], Integer .class); 300 counters[j + 2] = new MyCounterMonitor(counters[j], Short .class); 301 counters[j + 3] = new MyCounterMonitor(counters[j], Byte .class); 302 } catch (Exception e) { 303 throw new IllegalStateException ( 304 "Counter monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.COUNTER_SYNTAX); 305 } 306 } 307 return counters; 308 } 309 310 class MyCounterMonitor extends CounterMonitor implements AgentMonitorMBean { 311 private ObjectName _pattern; 312 private int _i; 313 private Class _attType; 314 315 MyCounterMonitor(int i, Class attType) throws JMException { 316 this._pattern = new ObjectName ("*:*"); 317 this._i = i; 318 this._attType = attType; 319 } 320 321 MyCounterMonitor(MyCounterMonitor g, Class attType) throws MalformedObjectNameException { 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 .class)) { 330 setInitThreshold(new Long (g.getInitThreshold().longValue())); 331 setModulus(new Long (g.getModulus().longValue())); 332 setOffset(new Long (g.getOffset().longValue())); 333 } else if (attType.equals(Integer .class)) { 334 setInitThreshold(new Integer (g.getInitThreshold().intValue())); 335 setModulus(new Integer (g.getModulus().intValue())); 336 setOffset(new Integer (g.getOffset().intValue())); 337 } else if (attType.equals(Short .class)) { 338 setInitThreshold(new Short (g.getInitThreshold().shortValue())); 339 setModulus(new Short (g.getModulus().shortValue())); 340 setOffset(new Short (g.getOffset().shortValue())); 341 } else if (attType.equals(Byte .class)) { 342 setInitThreshold(new Byte (g.getInitThreshold().byteValue())); 343 setModulus(new Byte (g.getModulus().byteValue())); 344 setOffset(new Byte (g.getOffset().byteValue())); 345 } else 346 throw new IllegalStateException (attType + " is an invalid counter threshold type"); 347 } 348 349 public ObjectName getPattern() { 350 return _pattern; 351 } 352 353 public void setPattern(ObjectName pattern) { 354 _pattern = pattern; 355 } 356 357 public Class getAttributeType() { 358 return _attType; 359 } 360 361 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { 362 super.preRegister(server, name); 363 return new ObjectName ( 364 CONST.MONITOR + ":type=counterMonitor" + ",observer=" + _i + ",attributeType=" + _attType.getName()); 365 } 366 } 367 368 private AgentMonitorMBean[] createGaugeMonitors(String str) throws MalformedObjectNameException { 372 String [] strTab = split(str, ";"); 373 MyGaugeMonitor[] gauges = new MyGaugeMonitor[strTab.length * 6]; 374 for (int i = 0; i < strTab.length; i++) { 375 try { 376 ObjectName oName = new ObjectName (strTab[i]); 377 Hashtable h = oName.getKeyPropertyList(); 378 int j = i * 4; 379 gauges[j] = new MyGaugeMonitor((i + 1), Double .class); 380 gauges[j].setObservedAttribute((String ) remove(h, "observedAttribute")); 382 gauges[j].setGranularityPeriod(Long.parseLong((String ) remove(h, "granularityPeriod"))); 383 gauges[j].setNotifyLow(true); 384 gauges[j].setNotifyHigh(true); 385 gauges[j].setThresholds( 386 Double.valueOf((String ) remove(h, "highThreshold")), 387 Double.valueOf((String ) remove(h, "lowThreshold"))); 388 gauges[j].setDifferenceMode(Boolean.valueOf((String ) remove(h, "differenceMode")).booleanValue()); 389 if (oName.isPropertyPattern()) 390 gauges[j].setPattern(new ObjectName (oName.getDomain() + ":" + asString(h) + ",*")); 391 else 392 gauges[j].setPattern(new ObjectName (oName.getDomain(), h)); 393 gauges[j + 1] = new MyGaugeMonitor(gauges[j], Float .class); 394 gauges[j + 2] = new MyGaugeMonitor(gauges[j], Long .class); 395 gauges[j + 3] = new MyGaugeMonitor(gauges[j], Integer .class); 396 gauges[j + 4] = new MyGaugeMonitor(gauges[j], Short .class); 397 gauges[j + 5] = new MyGaugeMonitor(gauges[j], Byte .class); 398 } catch (Exception e) { 399 throw new IllegalStateException ( 400 "Gauge monitor \"" + strTab[i] + "\" is invalid. " + e.toString() + ". " + CONST.GAUGE_SYNTAX); 401 } 402 } 403 return gauges; 404 } 405 406 class MyGaugeMonitor extends GaugeMonitor implements AgentMonitorMBean { 407 private ObjectName _pattern; 408 private int _i; 409 private Class _attType; 410 411 MyGaugeMonitor(int i, Class attType) throws JMException { 412 this._pattern = new ObjectName ("*:*"); 413 this._i = i; 414 this._attType = attType; 415 } 416 417 MyGaugeMonitor(MyGaugeMonitor g, Class 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 high = g.getHighThreshold(); 427 Number low = g.getLowThreshold(); 428 if (attType.equals(Double .class)) 429 setThresholds(new Double (high.doubleValue()), new Double (low.doubleValue())); 430 else if (attType.equals(Float .class)) 431 setThresholds(new Float (high.floatValue()), new Float (low.floatValue())); 432 else if (attType.equals(Long .class)) 433 setThresholds(new Long (high.longValue()), new Long (low.longValue())); 434 else if (attType.equals(Integer .class)) 435 setThresholds(new Integer (high.intValue()), new Integer (low.intValue())); 436 else if (attType.equals(Short .class)) 437 setThresholds(new Short (high.shortValue()), new Short (low.shortValue())); 438 else if (attType.equals(Byte .class)) 439 setThresholds(new Byte (high.byteValue()), new Byte (low.byteValue())); 440 else 441 throw new IllegalStateException (attType + " is an invalid gauge threshold type"); 442 } 443 444 public ObjectName getPattern() { 445 return _pattern; 446 } 447 448 public void setPattern(ObjectName pattern) { 449 _pattern = pattern; 450 } 451 452 public Class getAttributeType() { 453 return _attType; 454 } 455 456 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { 457 super.preRegister(server, name); 458 return new ObjectName ( 459 CONST.MONITOR + ":type=gaugeMonitor" + ",observer=" + _i + ",attributeType=" + _attType.getName()); 460 } 461 } 462 463 private String [] split(String str, String regex) { if (str == null || str.matches(" *")) 468 return new String [0]; 469 return str.split(regex); 470 } 471 472 private Object remove(Hashtable h, String key) throws Exception { 473 if (h.containsKey(key)) 474 return h.remove(key); 475 throw new Exception ("property \"" + key + "\" is missing"); 476 } 477 478 private String asString(Hashtable h) { 479 String 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 |