1 21 package org.objectweb.fractal.jmx.agent; 22 23 import org.objectweb.fractal.api.Component; 24 import org.objectweb.fractal.api.NoSuchInterfaceException; 25 import org.objectweb.fractal.api.type.ComponentType; 26 import org.objectweb.fractal.api.type.InterfaceType; 27 import org.objectweb.fractal.api.control.ContentController; 28 import org.objectweb.fractal.api.control.LifeCycleController; 29 import org.objectweb.fractal.api.control.BindingController; 30 import org.objectweb.fractal.api.control.SuperController; 31 import org.objectweb.fractal.api.control.NameController; 32 import org.objectweb.fractal.util.Fractal; 33 34 import javax.management.ObjectName ; 35 import javax.management.MBeanServer ; 36 import javax.management.MBeanServerFactory ; 37 import javax.management.NotificationListener ; 38 import javax.management.NotificationFilter ; 39 import javax.management.Notification ; 40 import javax.management.JMException ; 41 import javax.management.AttributeNotFoundException ; 42 import javax.management.JMRuntimeException ; 43 import javax.management.modelmbean.ModelMBean ; 44 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 45 import javax.management.monitor.MonitorNotification ; 46 47 import java.util.*; 48 49 83 public class Agent implements BindingController, LifeCycleController, AdminAttributes, Admin { 84 private MBeanServer _server = MBeanServerFactory.newMBeanServer("Fractal"); 85 private int _depth = -1; 86 private String _itfPatterns = "*:*"; 87 private String _stringMonitors = ""; 88 private String _counterMonitors = ""; 89 private String _gaugeMonitors = ""; 90 private String _periodicMonitors = ""; 91 private Component _myId = null; 92 private NotificationListener _listener = null; 93 private AgentContext _ctx = new AgentContext(); 94 95 public String [] listFc() { 99 return new String [] { "listener" }; 100 } 101 102 public Object lookupFc(final String itfName) { 103 if (itfName.equals("listener")) { 104 return _listener; 105 } else 106 return null; 107 } 108 109 public void bindFc(final String itfName, final Object itfValue) { 110 if (itfName.equals("component")) { 111 _myId = (Component) itfValue; 112 } else if (itfName.equals("listener")) { 113 _listener = (NotificationListener ) itfValue; 114 } 115 } 116 117 public void unbindFc(final String itfName) { 118 if (itfName.equals("listener")) { 119 _listener = null; 120 } 121 } 122 123 public String getFcState() { 127 return null; 128 } 129 130 public void startFc() { 131 try { 132 ObjectName oName = new ObjectName (CONST.ADMIN + ":itf=admin"); 133 exposeFcItf(_myId, oName, "admin"); 134 oName = new ObjectName (CONST.ADMIN + ":itf=attribute-controller"); 135 exposeFcItf(_myId, oName, "attribute-controller"); 136 } catch (JMException e) { 137 throw new JMRuntimeException (e.toString()); 138 } 139 } 140 141 public void stopFc() {} 142 143 153 154 public String getItfPatterns() { 155 return _itfPatterns; 156 } 157 158 public void setItfPatterns(final String str) { 159 this._itfPatterns = (str == null) ? "" : str; 160 } 161 162 public String getMonitorStringPatterns() { 163 return _stringMonitors; 164 } 165 166 public void setMonitorStringPatterns(final String str) { 167 this._stringMonitors = (str == null) ? "" : str; 168 } 169 170 public String getMonitorCounterPatterns() { 171 return _counterMonitors; 172 } 173 174 public void setMonitorCounterPatterns(final String counters) { 175 this._counterMonitors = (counters == null) ? "" : counters; 176 } 177 178 public String getMonitorGaugePatterns() { 179 return _gaugeMonitors; 180 } 181 182 public void setMonitorGaugePatterns(final String gauges) { 183 this._gaugeMonitors = (gauges == null) ? "" : gauges; 184 } 185 186 public String getMonitorPeriodicPatterns() { 187 return _periodicMonitors; 188 } 189 190 public void setMonitorPeriodicPatterns(final String periodic) { 191 this._periodicMonitors = (periodic == null) ? "" : periodic; 192 } 193 194 public MBeanServer getRawMBeanServer() { 195 return _server; 196 } 197 198 public void setRawMBeanServer(final MBeanServer srv) { 199 throw new UnsupportedOperationException (); 200 } 201 202 208 209 public void expose() throws JMException { 210 try { 211 unregisterMBean(CONST.FC + "*:*"); 213 unregisterMBean(CONST.MONITOR + "*:*"); 214 _ctx.reset(_itfPatterns, _stringMonitors, _counterMonitors, _gaugeMonitors, _periodicMonitors); 216 Component ids[] = (Component[]) exposeSuper(_myId, new HashSet()).toArray(new Component[0]); 218 exposeSub(ids, 0); 219 startMonitors(_ctx.getMonitors()); 221 } catch (RuntimeException e) { 222 throw new JMException (e.toString()); 224 } 225 } 226 227 private Set exposeSuper(Component id, Set set) { 231 try { 232 SuperController supCtrl = Fractal.getSuperController(id); 233 Component ids[] = supCtrl.getFcSuperComponents(); 234 if (ids.length == 0){ 235 set.add(id); 236 } 237 else for (int j = 0; j < ids.length; j++) { 238 try{ 239 ids[j].getFcInterface("admin"); 240 exposeSuper(ids[j], set); 241 } catch (NoSuchInterfaceException e) { set.add(ids[j]);} 242 } 243 } catch (NoSuchInterfaceException e) { set.add(id);} 244 return set; 245 } 246 247 private void exposeSub(Component ids[], int crtDepth) throws JMException { 248 if (_depth >= 0 && crtDepth > _depth) 249 return; 250 for (int i = 0; i < ids.length; i++) { 251 try { 253 ContentController cCtrl = Fractal.getContentController(ids[i]); 254 exposeSub(cCtrl.getFcSubComponents(), crtDepth + 1); 255 } catch (NoSuchInterfaceException ignored) {} 256 String [] names = getFullFCName(ids[i]); 258 for (int j = 0; j < names.length; j++) { 259 exposeFCinfos(ids[i], "/" + names[j] + '@' + Integer.toHexString(ids[i].hashCode())); 260 } 261 } 262 } 263 264 private void exposeFCinfos(Component id, String name) throws JMException { 265 InterfaceType[] itfts = ((ComponentType) id.getFcType()).getFcInterfaceTypes(); 266 String domain = CONST.FC + name + (isShared(id) ? "-shared" : ""); 267 for (int j = 0; j < itfts.length; j++) { 268 ObjectName oName = new ObjectName (domain + ":itf=" + itfts[j].getFcItfName()); 269 if (!itfts[j].isFcClientItf() && (_ctx.matchItfPatterns(oName) || _ctx.matchMonitorPatterns(oName))) { 270 exposeFcItf(id, oName, itfts[j].getFcItfName()); 271 boolean ret = setObservers(oName, _ctx.getMonitors()); 272 if (!(_ctx.matchItfPatterns(oName) || ret)) 273 _server.unregisterMBean(oName); 274 } 275 } 276 } 277 278 private boolean exposeFcItf(Component cid, ObjectName oname, String itfName) throws JMException { 279 boolean ret = false; 280 try { 281 if (_server.isRegistered(oname)) 282 return true; 283 InterfaceType itfType = ((ComponentType) cid.getFcType()).getFcInterfaceType(itfName); 285 String signature = itfType.getFcItfSignature(); 286 ModelMBean rmBean = Introspector.createMBean(cid.getFcInterface(itfName), Class.forName(signature)); 288 _server.registerMBean(rmBean, oname); 289 ret = true; 290 } catch (NoSuchInterfaceException ignored) {} catch (InvalidTargetObjectTypeException e) { 291 throw new IllegalStateException (e.toString()); 292 } catch (ClassNotFoundException e) { 293 throw new IllegalStateException (e.toString()); 294 } 295 return ret; 296 } 297 298 private boolean setObservers(ObjectName oName, AgentMonitorMBean[] monitors) throws JMException { 299 boolean ret = false; 300 for (int i = 0; i < monitors.length; i++) { 301 try { 302 Class c = _server.getAttribute(oName, monitors[i].getObservedAttribute()).getClass(); 303 if (monitors[i].getPattern().apply(oName) && monitors[i].getAttributeType().isAssignableFrom(c)) { 305 monitors[i].addObservedObject(oName); 306 ret = true; 307 } 308 } catch (AttributeNotFoundException ignored) {} 309 } 310 return ret; 311 } 312 313 private void startMonitors(AgentMonitorMBean[] monitors) throws JMException { 314 if (_listener == null) 315 return; 316 for (int i = 0; i < monitors.length; i++) { 317 ObjectName [] oNames = monitors[i].getObservedObjects(); 318 if (oNames.length == 0) 319 continue; 320 ObjectName monitorName = _server.registerMBean(monitors[i], null).getObjectName(); 321 for (int j = 0; j < oNames.length; j++) 322 _server.addNotificationListener(monitorName, _listener, new Filter (oNames[j]), oNames[j]); 323 monitors[i].start(); 324 } 325 } 326 327 class Filter implements NotificationFilter { 328 ObjectName observedObject; 329 Filter(ObjectName observedObject) { 330 this.observedObject = observedObject; 331 } 332 333 public boolean isNotificationEnabled(Notification n) { 334 MonitorNotification notif = (MonitorNotification ) n; 335 if (observedObject.equals(notif.getObservedObject())) 336 return true; 337 return false; 338 } 339 } 340 341 private void unregisterMBean(String pattern) throws JMException { 345 ObjectName o = new ObjectName (pattern); 346 Set s = _server.queryNames(o, null); 347 for (Iterator i = s.iterator(); i.hasNext();) 348 _server.unregisterMBean((ObjectName ) i.next()); 349 } 350 351 private boolean isShared(Component id) { 355 boolean shared = false; 356 try { 357 Component ids[] = Fractal.getSuperController(id).getFcSuperComponents(); 358 if (ids.length > 1) 359 shared = true; 360 else if (ids.length > 1) 361 shared = isShared(ids[0]); 362 } catch (NoSuchInterfaceException ignored) {} 363 return shared; 364 } 365 366 private String [] getFullFCName(Component id) { 367 String [] names = null; 368 try { 369 Component ids[] = Fractal.getSuperController(id).getFcSuperComponents(); 370 List l = new ArrayList(); 371 for (int i = 0; i < ids.length; i++) { 372 String [] tmp = getFullFCName(ids[i]); 373 for (int j = 0; j < tmp.length; j++) 374 tmp[j] += "/" + getFCName(id); 375 l.addAll(Arrays.asList(tmp)); 376 } 377 names = (String []) l.toArray(new String [l.size()]); 378 } catch (NoSuchInterfaceException ignored) {} 379 if (names == null || names.length == 0) { 380 names = new String [1]; 381 names[0] = getFCName(id); 382 } 383 return names; 384 } 385 386 private String getFCName(Component id) { 387 try { 388 NameController nc = Fractal.getNameController(id); 389 return nc.getFcName(); 390 } catch (NoSuchInterfaceException e) { 391 throw new IllegalStateException ("NameController required"); 392 } 393 } 394 } 395 | Popular Tags |