1 17 package org.apache.servicemix.jbi.management; 18 19 import java.io.IOException ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashMap ; 23 import java.util.Map ; 24 import javax.jbi.JBIException; 25 import javax.management.JMException ; 26 import javax.management.MBeanAttributeInfo ; 27 import javax.management.MBeanOperationInfo ; 28 import javax.management.MBeanServer ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.servicemix.jbi.container.EnvironmentContext; 34 import org.apache.servicemix.jbi.container.JBIContainer; 35 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 36 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 37 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService; 38 import edu.emory.mathcs.backport.java.util.concurrent.Executors; 39 40 45 public class ManagementContext extends BaseSystemService implements ManagementContextMBean { 46 49 public static final String DEFAULT_DOMAIN = "org.apache.servicemix"; 50 public static final String DEFAULT_CONNECTOR_PATH = "/jmxrmi"; 51 public static final int DEFAULT_CONNECTOR_PORT = 1099; 52 private final static Log log = LogFactory.getLog(ManagementContext.class); 53 private Map beanMap = new ConcurrentHashMap(); 54 protected Map systemServices = new ConcurrentHashMap(); 55 private MBeanServerContext mbeanServerContext = new MBeanServerContext(); 56 private ExecutorService executors; 57 58 61 public ManagementContext() { 62 mbeanServerContext.setJmxDomainName(DEFAULT_DOMAIN); 63 } 64 65 70 public String getDescription() { 71 return "JMX Management"; 72 } 73 74 75 76 81 public MBeanServer getMBeanServer() { 82 return mbeanServerContext.getMBeanServer(); 83 } 84 85 88 public String getJmxDomainName(){ 89 return mbeanServerContext.getJmxDomainName(); 90 } 91 92 93 94 97 public boolean isUseMBeanServer() { 98 return mbeanServerContext.isUseMBeanServer(); 99 } 100 101 104 public void setUseMBeanServer(boolean useMBeanServer) { 105 mbeanServerContext.setUseMBeanServer(useMBeanServer); 106 } 107 108 111 public boolean isCreateMBeanServer() { 112 return mbeanServerContext.isCreateMBeanServer(); 113 } 114 115 118 public void setCreateMBeanServer(boolean enableJMX) { 119 mbeanServerContext.setCreateMBeanServer(enableJMX); 120 } 121 122 public void setNamingPort(int portNum) { 123 mbeanServerContext.setConnectorPort(portNum); 124 } 125 126 public int getNamingPort() { 127 return mbeanServerContext.getConnectorPort(); 128 } 129 130 public boolean isCreateJmxConnector() { 131 return mbeanServerContext.isCreateConnector(); 132 } 133 134 public void setCreateJmxConnector(boolean createJmxConnector) { 135 mbeanServerContext.setCreateConnector(createJmxConnector); 136 } 137 138 146 public void init(JBIContainer container, MBeanServer server) throws JBIException { 147 if (container.isEmbedded() && server == null) { 148 mbeanServerContext.setUseMBeanServer(false); 149 mbeanServerContext.setCreateMBeanServer(false); 150 } 151 mbeanServerContext.setMBeanServer(server); 152 try { 153 mbeanServerContext.start(); 154 } catch (IOException e) { 155 log.error("Failed to start mbeanServerContext", e); 156 } 157 this.executors = Executors.newCachedThreadPool(); 158 super.init(container); 159 } 160 161 protected Class getServiceMBean() { 162 return ManagementContextMBean.class; 163 } 164 165 170 public void start() throws JBIException { 171 super.start(); 172 } 173 174 179 public void stop() throws JBIException { 180 super.stop(); 181 } 182 183 188 public void shutDown() throws JBIException { 189 super.shutDown(); 190 Object [] beans = beanMap.keySet().toArray(); 192 for (int i = 0; i < beans.length; i++) { 193 try { 194 unregisterMBean(beans[i]); 195 } catch (Exception e) { 196 log.debug("Could not unregister mbean", e); 197 } 198 } 199 try{ 200 mbeanServerContext.stop(); 201 }catch(IOException e){ 202 log.debug("Failed to shutdown mbeanServerContext cleanly",e); 203 } 204 executors.shutdown(); 205 } 206 207 212 public ObjectName [] getBindingComponents() { 213 return container.getRegistry().getBindingComponents(); 214 } 215 216 222 public ObjectName getComponentByName(String componentName) { 223 ComponentMBeanImpl component = container.getRegistry().getComponent(componentName); 224 return component != null ? component.getMBeanName() : null; 225 } 226 227 232 public ObjectName [] getEngineComponents() { 233 return container.getRegistry().getEngineComponents(); 234 } 235 236 239 public ObjectName [] getPojoComponents(){ 240 return container.getRegistry().getPojoComponents(); 241 } 242 243 248 public String getSystemInfo() { 249 return "ServiceMix JBI Container: version: " + EnvironmentContext.getVersion(); 250 } 251 252 258 public ObjectName getSystemService(String serviceName) { 259 return (ObjectName ) systemServices.get(serviceName); 260 } 261 262 267 public ObjectName [] getSystemServices() { 268 ObjectName [] result = null; 269 Collection col = systemServices.values(); 270 result = new ObjectName [col.size()]; 271 col.toArray(result); 272 return result; 273 } 274 275 281 public boolean isBinding(String componentName) { 282 ComponentMBeanImpl component = container.getRegistry().getComponent(componentName); 283 return component != null ? component.isBinding() : false; 284 } 285 286 292 public boolean isEngine(String componentName) { 293 ComponentMBeanImpl component = container.getRegistry().getComponent(componentName); 294 return component != null ? component.isEngine() : false; 295 } 296 297 304 public String startComponent(String componentName) throws JBIException { 305 String result = "NOT FOUND: " + componentName; 306 ObjectName objName = getComponentByName(componentName); 307 if (objName != null) { 308 ComponentMBeanImpl mbean = (ComponentMBeanImpl) beanMap.get(objName); 309 if (mbean != null) { 310 mbean.start(); 311 result = mbean.getCurrentState(); 312 } 313 } 314 return result; 315 } 316 317 324 public String stopComponent(String componentName) throws JBIException { 325 String result = "NOT FOUND: " + componentName; 326 ObjectName objName = getComponentByName(componentName); 327 if (objName != null) { 328 ComponentMBeanImpl mbean = (ComponentMBeanImpl) beanMap.get(objName); 329 if (mbean != null) { 330 mbean.stop(); 331 result = mbean.getCurrentState(); 332 } 333 } 334 return result; 335 } 336 337 344 public String shutDownComponent(String componentName) throws JBIException { 345 String result = "NOT FOUND: " + componentName; 346 ObjectName objName = getComponentByName(componentName); 347 if (objName != null) { 348 ComponentMBeanImpl mbean = (ComponentMBeanImpl) beanMap.get(objName); 349 if (mbean != null) { 350 mbean.shutDown(); 351 result = mbean.getCurrentState(); 352 } 353 } 354 return result; 355 } 356 357 364 public ObjectName createCustomComponentMBeanName(String type, String name) { 365 Map result = new LinkedHashMap (); 366 result.put("ContainerName", container.getName()); 367 result.put("Type", "Component"); 368 result.put("Name", sanitizeString(name)); 369 result.put("SubType", sanitizeString(type)); 370 return createObjectName(result); 371 } 372 373 374 380 public ObjectName createObjectName(MBeanInfoProvider provider) { 381 Map props = createObjectNameProps(provider); 382 return createObjectName(props); 383 } 384 385 391 public ObjectName createObjectName(String name) { 392 ObjectName result = null; 393 try { 394 result = new ObjectName (name); 395 } 396 catch (MalformedObjectNameException e) { 397 String error = "Could not create ObjectName for " + name; 399 log.error(error, e); 400 throw new RuntimeException (error); 401 } 402 return result; 403 } 404 405 411 public ObjectName createObjectName(String domain, Map props) { 412 StringBuffer sb = new StringBuffer (); 413 sb.append(domain).append(':'); 414 int i = 0; 415 for (Iterator it = props.entrySet().iterator(); it.hasNext();) { 416 Map.Entry entry = (Map.Entry ) it.next(); 417 if (i++ > 0) { 418 sb.append(","); 419 } 420 sb.append(entry.getKey()).append("=").append(entry.getValue()); 421 } 422 ObjectName result = null; 423 try { 424 result = new ObjectName (sb.toString()); 425 } 426 catch (MalformedObjectNameException e) { 427 String error = "Could not create ObjectName for " + props; 429 log.error(error, e); 430 throw new RuntimeException (error); 431 } 432 return result; 433 } 434 435 440 public ObjectName createObjectName(Map props) { 441 return createObjectName(getJmxDomainName(), props); 442 } 443 444 450 public Map createObjectNameProps(MBeanInfoProvider provider){ 451 Map result = new LinkedHashMap (); 452 result.put("ContainerName", container.getName()); 453 result.put("Type", sanitizeString(provider.getType())); 454 result.put("Name", sanitizeString(provider.getName())); 455 if (provider.getSubType() != null) { 456 result.put("SubType", sanitizeString(provider.getSubType())); 457 } 458 return result; 459 } 460 461 467 private static String sanitizeString(String in) { 468 String result = null; 469 if (in != null) { 470 result = in.replace(':', '_'); 471 result = result.replace('/', '_'); 472 result = result.replace('\\', '_'); 473 result = result.replace('?', '_'); 474 result = result.replace('=', '_'); 475 result = result.replace(',', '_'); 476 } 477 return result; 478 } 479 480 488 public void registerMBean(ObjectName name, MBeanInfoProvider resource, Class interfaceMBean) throws JMException { 489 registerMBean(name, resource, interfaceMBean, resource.getDescription()); 490 } 491 492 501 public void registerMBean(ObjectName name, Object resource, Class interfaceMBean, String description) 502 throws JMException { 503 if (mbeanServerContext.getMBeanServer() != null) { 504 Object mbean = MBeanBuilder.buildStandardMBean(resource, interfaceMBean, description, executors); 505 if (mbeanServerContext.getMBeanServer().isRegistered(name)) { 506 mbeanServerContext.getMBeanServer().unregisterMBean(name); 507 } 508 mbeanServerContext.getMBeanServer().registerMBean(mbean, name); 509 beanMap.put(name, resource); 510 } 511 } 512 513 514 522 public static ObjectName getSystemObjectName(String domainName, String containerName, Class interfaceType) { 523 String tmp = domainName + ":ContainerName=" + containerName + ",Type=SystemService,Name=" + getSystemServiceName(interfaceType); 524 ObjectName result = null; 525 try { 526 result = new ObjectName (tmp); 527 } 528 catch (MalformedObjectNameException e) { 529 log.error("Failed to build ObjectName:",e); 530 } 531 catch (NullPointerException e) { 532 log.error("Failed to build ObjectName:",e); 533 } 534 return result; 535 } 536 537 public static String getSystemServiceName(Class interfaceType) { 538 String name = interfaceType.getName(); 539 name = name.substring(name.lastIndexOf('.') + 1); 540 if (name.endsWith("MBean")) { 541 name = name.substring(0, name.length() - 5); 542 } 543 return name; 544 } 545 546 public static ObjectName getContainerObjectName(String domainName, String containerName) { 547 String tmp = domainName + ":ContainerName=" + containerName + ",Type=JBIContainer"; 548 ObjectName result = null; 549 try { 550 result = new ObjectName (tmp); 551 } 552 catch (MalformedObjectNameException e) { 553 e.printStackTrace(); 555 } 556 catch (NullPointerException e) { 557 e.printStackTrace(); 559 } 560 return result; 561 } 562 563 570 public void registerSystemService(BaseSystemService service, Class interfaceType) throws JBIException { 571 try { 572 573 String name = service.getName(); 574 if (systemServices.containsKey(name)) { 575 throw new JBIException("A system service for the name " + name + " is already registered"); 576 } 577 ObjectName objName = createObjectName(service); 578 if (log.isDebugEnabled()) { 579 log.debug("Registering system service: " + objName); 580 } 581 registerMBean(objName, service, interfaceType, service.getDescription()); 582 systemServices.put(name, objName); 583 } 584 catch (MalformedObjectNameException e) { 585 throw new JBIException(e); 586 } 587 catch (JMException e) { 588 throw new JBIException(e); 589 } 590 } 591 592 598 public void unregisterSystemService(BaseSystemService service) throws JBIException { 599 String name = service.getName(); 600 if (!systemServices.containsKey(name)) { 601 throw new JBIException("A system service for the name " + name + " is not registered"); 602 } 603 ObjectName objName = (ObjectName )systemServices.remove(name); 604 if (log.isDebugEnabled()) { 605 log.debug("Unregistering system service: " + objName); 606 } 607 unregisterMBean(objName); 608 } 609 610 616 public void unregisterMBean(ObjectName name) throws JBIException { 617 try{ 618 mbeanServerContext.unregisterMBean(name); 619 beanMap.remove(name); 620 }catch(JMException e){ 621 log.error("Failed to unregister mbean: " + name,e); 622 throw new JBIException(e); 623 } 624 } 625 626 632 public void unregisterMBean(Object bean) throws JBIException { 633 for (Iterator i = beanMap.entrySet().iterator();i.hasNext();) { 634 Map.Entry entry = (Map.Entry ) i.next(); 635 if (entry.getValue() == bean) { 636 ObjectName name = (ObjectName ) entry.getKey(); 637 unregisterMBean(name); 638 break; 639 } 640 } 641 } 642 643 644 645 651 public MBeanAttributeInfo [] getAttributeInfos() throws JMException { 652 AttributeInfoHelper helper = new AttributeInfoHelper(); 653 helper.addAttribute(getObjectToManage(), "bindingComponents", "Get list of all binding components"); 654 helper.addAttribute(getObjectToManage(), "engineComponents", "Get list of all engine components"); 655 helper.addAttribute(getObjectToManage(), "pojoComponents", "Get list of all pojo components"); 656 helper.addAttribute(getObjectToManage(), "systemInfo", "Return current version"); 657 helper.addAttribute(getObjectToManage(), "systemServices", "Get list of system services"); 658 return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos()); 659 } 660 661 public MBeanOperationInfo [] getOperationInfos() throws JMException { 662 OperationInfoHelper helper = new OperationInfoHelper(); 663 ParameterHelper ph = helper.addOperation(getObjectToManage(), "getComponentByName", 1, "look up Component by name"); 664 ph.setDescription(0, "name", "Component name"); 665 ph = helper.addOperation(getObjectToManage(), "getSystemService", 1, "look up System service by name"); 666 ph.setDescription(0, "name", "System name"); 667 ph = helper.addOperation(getObjectToManage(), "isBinding", 1, "Is Component a binding Component?"); 668 ph.setDescription(0, "name", "Component name"); 669 ph = helper.addOperation(getObjectToManage(), "isEngine", 1, "Is Component a service engine?"); 670 ph.setDescription(0, "name", "Component name"); 671 return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos()); 672 } 673 674 } | Popular Tags |