1 10 package org.mule.management.agents; 11 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import javax.management.InstanceAlreadyExistsException ; 18 import javax.management.MBeanRegistrationException ; 19 import javax.management.MBeanServer ; 20 import javax.management.MBeanServerFactory ; 21 import javax.management.MalformedObjectNameException ; 22 import javax.management.NotCompliantMBeanException ; 23 import javax.management.ObjectName ; 24 import javax.management.remote.JMXConnectorServer ; 25 import javax.management.remote.JMXConnectorServerFactory ; 26 import javax.management.remote.JMXServiceURL ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.mule.MuleManager; 31 import org.mule.MuleRuntimeException; 32 import org.mule.config.i18n.Message; 33 import org.mule.config.i18n.Messages; 34 import org.mule.impl.internal.notifications.ModelNotification; 35 import org.mule.impl.internal.notifications.ModelNotificationListener; 36 import org.mule.impl.internal.notifications.NotificationException; 37 import org.mule.management.mbeans.ComponentService; 38 import org.mule.management.mbeans.ComponentServiceMBean; 39 import org.mule.management.mbeans.ConnectorService; 40 import org.mule.management.mbeans.ConnectorServiceMBean; 41 import org.mule.management.mbeans.EndpointService; 42 import org.mule.management.mbeans.EndpointServiceMBean; 43 import org.mule.management.mbeans.ModelService; 44 import org.mule.management.mbeans.ModelServiceMBean; 45 import org.mule.management.mbeans.MuleConfigurationService; 46 import org.mule.management.mbeans.MuleConfigurationServiceMBean; 47 import org.mule.management.mbeans.MuleService; 48 import org.mule.management.mbeans.MuleServiceMBean; 49 import org.mule.management.mbeans.StatisticsService; 50 import org.mule.management.support.AutoDiscoveryJmxSupportFactory; 51 import org.mule.management.support.JmxSupport; 52 import org.mule.management.support.JmxSupportFactory; 53 import org.mule.providers.AbstractConnector; 54 import org.mule.umo.UMOException; 55 import org.mule.umo.lifecycle.InitialisationException; 56 import org.mule.umo.manager.UMOAgent; 57 import org.mule.umo.manager.UMOManager; 58 import org.mule.umo.manager.UMOServerNotification; 59 import org.mule.umo.provider.UMOConnector; 60 import org.mule.umo.provider.UMOMessageReceiver; 61 import org.mule.util.StringUtils; 62 63 69 public class JmxAgent implements UMOAgent 70 { 71 72 75 protected static Log logger = LogFactory.getLog(JmxAgent.class); 76 77 80 protected boolean locateServer = true; 81 82 private String name = "JMX Agent"; 83 private boolean createServer = true; 84 private String connectorServerUrl; 85 private MBeanServer mBeanServer; 86 private JMXConnectorServer connectorServer; 87 private Map connectorServerProperties = null; 88 private boolean enableStatistics = true; 89 private List registeredMBeans = new ArrayList (); 90 private boolean serverCreated = false; 91 private boolean initialized = false; 92 93 private JmxSupportFactory jmxSupportFactory = new AutoDiscoveryJmxSupportFactory(); 94 private JmxSupport jmxSupport; 95 96 100 public String getName() 101 { 102 return this.name; 103 } 104 105 109 public void setName(String name) 110 { 111 this.name = name; 112 } 113 114 118 public String getDescription() 119 { 120 if (connectorServerUrl != null) 121 { 122 return "JMX Agent: " + connectorServerUrl; 123 } 124 else 125 { 126 return "JMX Agent"; 127 } 128 } 129 130 135 public void initialise() throws InitialisationException 136 { 137 if (initialized) { 138 return; 139 } 140 if (!locateServer && !createServer) { 141 throw new InitialisationException(new Message(Messages.JMX_CREATE_OR_LOCATE_SHOULD_BE_SET), this); 142 } 143 if (mBeanServer == null && locateServer) { 144 List l = MBeanServerFactory.findMBeanServer(null); 145 if (l != null && l.size() > 0) { 146 mBeanServer = (MBeanServer ) l.get(0); 147 } 148 } 149 if (mBeanServer == null && createServer) { 150 mBeanServer = MBeanServerFactory.createMBeanServer(); 151 serverCreated = true; 152 } 153 if (mBeanServer == null) { 154 throw new InitialisationException(new Message(Messages.JMX_CANT_LOCATE_CREATE_SERVER), this); 155 } 156 if (connectorServerUrl != null) { 157 try { 158 JMXServiceURL url = new JMXServiceURL (connectorServerUrl); 159 connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, connectorServerProperties, mBeanServer); 160 } catch (Exception e) { 161 throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X, "Jmx Connector"), e, this); 162 } 163 } 164 165 jmxSupport = jmxSupportFactory.newJmxSupport(); 166 167 ModelNotificationListener l = new ModelNotificationListener() { 169 public void onNotification(UMOServerNotification notification) 170 { 171 if (notification.getAction() == ModelNotification.MODEL_STARTED) { 172 try { 173 registerStatisticsService(); 174 registerMuleService(); 175 registerConfigurationService(); 176 registerModelService(); 177 registerComponentServices(); 178 registerEndpointServices(); 179 registerConnectorServices(); 180 } catch (Exception e) { 181 throw new MuleRuntimeException(new Message(Messages.X_FAILED_TO_INITIALISE, "MBeans"), e); 182 } 183 } 184 } 185 }; 186 187 try 188 { 189 UMOManager manager = MuleManager.getInstance(); 190 if (StringUtils.isBlank(manager.getId())) 191 { 192 throw new IllegalArgumentException ( 194 "Manager ID is mandatory when running with JmxAgent. Give your Mule configuration a valid ID."); 195 } 196 MuleManager.getInstance().registerListener(l); 197 } catch (NotificationException e) { 198 throw new InitialisationException(e, this); 199 } 200 initialized = true; 201 } 202 203 208 public void start() throws UMOException 209 { 210 if (connectorServer != null) { 211 try { 212 logger.info("Starting JMX agent connector Server"); 213 connectorServer.start(); 214 } catch (Exception e) { 215 throw new JmxManagementException(new Message(Messages.FAILED_TO_START_X, "Jmx Connector"), e); 216 } 217 } 218 } 219 220 225 public void stop() throws UMOException 226 { 227 if (connectorServer != null) { 228 try { 229 connectorServer.stop(); 230 } catch (Exception e) { 231 throw new JmxManagementException(new Message(Messages.FAILED_TO_STOP_X, "Jmx Connector"), e); 232 } 233 } 234 } 235 236 241 public void dispose() 242 { 243 if (mBeanServer != null) { 244 for (Iterator iterator = registeredMBeans.iterator(); iterator.hasNext();) { 245 ObjectName objectName = (ObjectName ) iterator.next(); 246 try { 247 mBeanServer.unregisterMBean(objectName); 248 } catch (Exception e) { 249 logger.warn("Failed to unregister MBean: " + objectName + ". Error is: " + e.getMessage()); 250 } 251 } 252 if (serverCreated) { 253 MBeanServerFactory.releaseMBeanServer(mBeanServer); 254 } 255 mBeanServer = null; 256 } 257 } 258 259 264 public void registered() 265 { 266 } 268 269 274 public void unregistered() 275 { 276 } 278 279 protected void registerStatisticsService() throws NotCompliantMBeanException , MBeanRegistrationException , 280 InstanceAlreadyExistsException , MalformedObjectNameException 281 { 282 ObjectName on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Statistics,name=AllStatistics"); 283 StatisticsService mBean = new StatisticsService(); 284 mBean.setManager(MuleManager.getInstance()); 285 mBean.setEnabled(isEnableStatistics()); 286 logger.debug("Registering statistics with name: " + on); 287 mBeanServer.registerMBean(mBean, on); 288 registeredMBeans.add(on); 289 } 290 291 protected void registerModelService() throws NotCompliantMBeanException , MBeanRegistrationException , 292 InstanceAlreadyExistsException , MalformedObjectNameException 293 { 294 ModelServiceMBean serviceMBean = new ModelService(); 295 String rawName = serviceMBean.getName() + "(" + serviceMBean.getType() + ")"; 296 String name = jmxSupport.escape(rawName); 297 ObjectName on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Model,name=" + name); 298 logger.debug("Registering model with name: " + on); 299 mBeanServer.registerMBean(serviceMBean, on); 300 registeredMBeans.add(on); 301 } 302 303 protected void registerMuleService() throws NotCompliantMBeanException , MBeanRegistrationException , 304 InstanceAlreadyExistsException , MalformedObjectNameException 305 { 306 ObjectName on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.ManagementContext,name=MuleServerInfo"); 307 MuleServiceMBean serviceMBean = new MuleService(); 308 logger.debug("Registering mule with name: " + on); 309 mBeanServer.registerMBean(serviceMBean, on); 310 registeredMBeans.add(on); 311 } 312 313 protected void registerConfigurationService() throws NotCompliantMBeanException , MBeanRegistrationException , 314 InstanceAlreadyExistsException , MalformedObjectNameException 315 { 316 ObjectName on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Configuration,name=GlobalConfiguration"); 317 MuleConfigurationServiceMBean serviceMBean = new MuleConfigurationService(); 318 logger.debug("Registering configuration with name: " + on); 319 mBeanServer.registerMBean(serviceMBean, on); 320 registeredMBeans.add(on); 321 } 322 323 protected void registerComponentServices() throws NotCompliantMBeanException , MBeanRegistrationException , 324 InstanceAlreadyExistsException , MalformedObjectNameException 325 { 326 Iterator iter = MuleManager.getInstance().getModel().getComponentNames(); 327 String rawName; 328 while (iter.hasNext()) { 329 rawName = iter.next().toString(); 330 final String name = jmxSupport.escape(rawName); 331 ObjectName on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Component,name=" + name); 332 ComponentServiceMBean serviceMBean = new ComponentService(rawName); 333 logger.debug("Registering component with name: " + on); 334 mBeanServer.registerMBean(serviceMBean, on); 335 registeredMBeans.add(on); 336 } 337 } 338 339 protected void registerEndpointServices() throws NotCompliantMBeanException , MBeanRegistrationException , 340 InstanceAlreadyExistsException , MalformedObjectNameException 341 { 342 Iterator iter = MuleManager.getInstance().getConnectors().values().iterator(); 343 UMOConnector connector; 344 while (iter.hasNext()) { 345 connector = (UMOConnector) iter.next(); 346 if (connector instanceof AbstractConnector) { 347 for (Iterator iterator = ((AbstractConnector) connector).getReceivers().values().iterator(); iterator.hasNext();) { 348 EndpointServiceMBean mBean = new EndpointService((UMOMessageReceiver) iterator.next()); 349 final String rawName = mBean.getName(); 350 final String name = jmxSupport.escape(rawName); 351 if (logger.isInfoEnabled()) { 352 logger.info("Attempting to register service with name: " + jmxSupport.getDomainName() 353 + ":type=org.mule.umo.UMOEndpoint,name=" + name); 354 } 355 ObjectName on = jmxSupport.getObjectName( 356 jmxSupport.getDomainName() + 357 ":type=org.mule.Endpoint,component=" + 358 jmxSupport.escape(mBean.getComponentName()) + 359 ",name=" + name); 360 mBeanServer.registerMBean(mBean, on); 361 registeredMBeans.add(on); 362 logger.info("Registered Endpoint Service with name: " + on); 363 } 364 } else { 365 logger.warn("Connector: " + connector 366 + " is not an istance of AbstractConnector, cannot obtain Endpoint MBeans from it"); 367 } 368 369 } 370 } 371 372 protected void registerConnectorServices() throws 373 MalformedObjectNameException , 374 NotCompliantMBeanException , 375 MBeanRegistrationException , 376 InstanceAlreadyExistsException 377 { 378 Iterator iter = MuleManager.getInstance().getConnectors().values().iterator(); 379 while (iter.hasNext()) { 380 UMOConnector connector = (UMOConnector) iter.next(); 381 ConnectorServiceMBean mBean = new ConnectorService(connector); 382 final String rawName = mBean.getName(); 383 final String name = jmxSupport.escape(rawName); 384 final String stringName = jmxSupport.getDomainName() + ":type=org.mule.Connector,name=" + name; 385 if (logger.isDebugEnabled()) { 386 logger.debug("Attempting to register service with name: " + stringName); 387 } 388 ObjectName oName = jmxSupport.getObjectName(stringName); 389 mBeanServer.registerMBean(mBean, oName); 390 registeredMBeans.add(oName); 391 logger.info("Registered Connector Service with name " + oName); 392 } 393 } 394 395 398 public boolean isCreateServer() 399 { 400 return createServer; 401 } 402 403 406 public void setCreateServer(boolean createServer) 407 { 408 this.createServer = createServer; 409 } 410 411 414 public boolean isLocateServer() 415 { 416 return locateServer; 417 } 418 419 422 public void setLocateServer(boolean locateServer) 423 { 424 this.locateServer = locateServer; 425 } 426 427 430 public String getConnectorServerUrl() 431 { 432 return connectorServerUrl; 433 } 434 435 438 public void setConnectorServerUrl(String connectorServerUrl) 439 { 440 this.connectorServerUrl = connectorServerUrl; 441 } 442 443 446 public boolean isEnableStatistics() 447 { 448 return enableStatistics; 449 } 450 451 454 public void setEnableStatistics(boolean enableStatistics) 455 { 456 this.enableStatistics = enableStatistics; 457 } 458 459 462 public MBeanServer getMBeanServer() 463 { 464 return mBeanServer; 465 } 466 467 470 public void setMBeanServer(MBeanServer mBeanServer) 471 { 472 this.mBeanServer = mBeanServer; 473 } 474 475 480 public Map getConnectorServerProperties() { 481 return connectorServerProperties; 482 } 483 484 489 public void setConnectorServerProperties(Map connectorServerProperties) { 490 this.connectorServerProperties = connectorServerProperties; 491 } 492 493 494 499 public JmxSupportFactory getJmxSupportFactory() 500 { 501 return jmxSupportFactory; 502 } 503 504 509 public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory) 510 { 511 this.jmxSupportFactory = jmxSupportFactory; 512 } 513 } 514 | Popular Tags |