1 23 24 232 233 package com.sun.enterprise.admin.config; 234 235 import java.lang.reflect.InvocationTargetException ; 236 import java.util.Iterator ; 237 import java.util.Hashtable ; 238 import java.util.ArrayList ; 239 240 import javax.management.ObjectName ; 241 242 244 import javax.management.modelmbean.DescriptorSupport ; 245 import javax.management.Descriptor ; 248 import javax.management.modelmbean.ModelMBeanInfo ; 249 import javax.management.modelmbean.ModelMBeanOperationInfo ; 250 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 251 import javax.management.*; 252 253 import com.sun.org.apache.commons.modeler.BaseModelMBean; 254 import com.sun.enterprise.admin.BaseAdminMBean; 256 import com.sun.enterprise.admin.MBeanHelper; 257 258 import com.sun.enterprise.config.ConfigException; 260 import com.sun.enterprise.config.serverbeans.ServerTags; 262 import com.sun.enterprise.config.ConfigContext; 263 import com.sun.enterprise.config.ConfigFactory; 264 import com.sun.enterprise.config.ConfigBean; 265 import com.sun.enterprise.config.ConfigBeansFactory; 266 import com.sun.enterprise.config.serverbeans.ElementProperty; 267 268 import java.util.logging.Level ; 269 270 import com.sun.enterprise.admin.meta.MBeanRegistryFactory; 272 import com.sun.enterprise.admin.meta.MBeanRegistry; 273 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 274 import com.sun.enterprise.admin.meta.MBeanMetaConstants; 275 import com.sun.enterprise.admin.meta.MBeanMetaHelper; 276 277 283 284 public class BaseConfigMBean extends BaseAdminMBean implements MBeanRegistration 285 { 286 287 295 public static final boolean OVERWRITE = true; 296 297 299 private ManagedConfigBean mcb = null; 300 protected MBeanRegistry m_registry = null; 301 302 311 public BaseConfigMBean() { 312 super(); 313 m_registry = MBeanRegistryFactory.getAdminMBeanRegistry(); 314 } 315 316 318 319 320 322 323 335 public Object getAttribute(String name) 336 throws AttributeNotFoundException, MBeanException, 337 ReflectionException { 338 name = MBeanMetaHelper.mapToMBeanAttributeName(name); 339 ModelMBeanAttributeInfo attrInfo = (ModelMBeanAttributeInfo )MBeanHelper.findMatchingAttributeInfo((MBeanInfo )info, name); 340 if(attrInfo==null) 341 throw new AttributeNotFoundException(name); 342 Object o = null; 344 try { 345 o = super.getAttribute(name); 346 } catch (Exception e) { 347 o=mcb.getAttribute(attrInfo, name); 350 } 352 return o; 353 } 354 355 public AttributeList getAttributes(String [] attributeNames) { 356 ArrayList names = mcb.getSimpleAttributeNames(attributeNames); 357 AttributeList attrs = new AttributeList(); 358 for(int i=0; i<names.size(); i++) { 359 try { 360 String name = MBeanMetaHelper.mapToMBeanAttributeName((String )names.get(i)); 361 Object value = getAttribute(name); 362 attrs.add(new Attribute(name, value)); 363 } catch (Exception e) { 364 } 366 } 367 return attrs; 368 } 369 370 371 389 public void setManagedResource(Object resource, String type) 390 throws InstanceNotFoundException, 391 MBeanException, RuntimeOperationsException { 392 393 if (resource == null) 394 { 395 String msg = _localStrings.getString( "admin.server.core.mbean.config.base.managed_resource_is_null", mbeanType); 396 throw new RuntimeOperationsException(new IllegalArgumentException (msg), msg); 397 } 398 if (MBeanMetaConstants.CONFIG_BEAN_REF.equalsIgnoreCase(type)) { 399 if(! (resource instanceof ConfigBean)) 400 { 401 String msg = _localStrings.getString( "admin.server.core.mbean.config.base.managed_resource_is_not_configbean", mbeanType); 402 throw new RuntimeOperationsException(new ClassCastException (msg), msg); 403 } 404 this.mcb = new ManagedConfigBean(this, (ConfigBean) resource, m_registry); 405 408 } else { 409 } 411 } 412 413 414 433 public Object invoke(String name, Object params[], String signature[]) 434 throws MBeanException, ReflectionException { 435 ModelMBeanOperationInfo opInfo = (ModelMBeanOperationInfo )MBeanHelper.findMatchingOperationInfo((MBeanInfo )info, name, signature); 436 if (opInfo == null) 437 { 438 String msg = _localStrings.getString( "admin.server.core.mbean.config.base.operation_is_not_found", mbeanType, name); 439 throw new MBeanException 440 (new ServiceNotFoundException(msg), msg); 441 } 442 Descriptor descr = opInfo.getDescriptor(); 443 444 Object ret; 445 try 447 { 448 ret = MBeanHelper.invokeOperationInBean(opInfo, this, params); 449 if(ret!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT) 450 { 451 return ret; 452 } 453 if(mcb!=null && 455 (ret=mcb.invokeOperation(opInfo, params, signature))!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT) 456 { 457 return ret; 458 } 459 return super.invoke(name, params, signature); 460 } 461 catch (MBeanException mbe) 462 { 463 _sLogger.log(Level.FINE, "mbean.baseconfig.invoke_exception",mbe); 464 throw mbe; 465 } 466 catch (Exception e) 467 { 468 _sLogger.log(Level.FINE, "mbean.baseconfig.invoke_exception",e); 469 String msg = _localStrings.getString( "admin.server.core.mbean.config.base.invoke_error", mbeanType, name); 470 throw MBeanHelper.extractAndWrapTargetException(e, msg); 471 } 472 } 473 474 protected ObjectName createChildElementByType(String childElementName, Attribute[] attrs) throws Exception 476 { 477 return createChildElementByType(childElementName, attrs, true, false); 478 } 479 protected ObjectName createChildElementByType(String childElementName, Attribute[] attrs, boolean bSkipNullValued) throws Exception 480 { 481 return createChildElementByType(childElementName, attrs, bSkipNullValued, false); 482 } 483 protected ObjectName createChildElementByType(String childElementName, Attribute[] attrs, boolean bSkipNullValued, boolean bOnlyOne) throws Exception 484 { 485 AttributeList list = new AttributeList(); 486 for(int i=0; i<attrs.length; i++) 487 { 488 if(!bSkipNullValued || attrs[i].getValue()!=null) 489 list.add(attrs[i]); 490 } 491 ConfigBean bean = mcb.createChildByType(childElementName, list, null, bOnlyOne); 492 return ConfigMBeanHelper.getChildObjectName(m_registry, info, bean); 493 } 494 495 496 501 502 public AttributeList setAttributes(AttributeList list) { 503 try { 504 return super.setAttributes(list); 505 } catch (Exception e) { 506 507 if(mcb!=null) 510 return mcb.setAttributes(list); 511 } 513 return null; 514 } 515 516 529 public void setAttribute(Attribute attribute) 530 throws AttributeNotFoundException, MBeanException, 531 ReflectionException 532 { 533 attribute = new Attribute(MBeanMetaHelper.mapToMBeanAttributeName(attribute.getName()), attribute.getValue()); 534 ModelMBeanAttributeInfo attrInfo = (ModelMBeanAttributeInfo )MBeanHelper.findMatchingAttributeInfo((MBeanInfo )info, attribute.getName()); 535 if(attrInfo==null) 536 throw new AttributeNotFoundException(); 537 539 try { 540 super.setAttribute(attribute); 541 } catch (Exception e) { 542 543 if(mcb!=null) 546 mcb.setAttribute(attrInfo, attribute); 547 } 549 550 } 551 552 553 557 public ObjectName preRegister(MBeanServer server, 558 ObjectName name) 559 throws Exception 560 { 561 569 return name; 570 } 571 572 public void postRegister(Boolean registrationDone) { 573 } 574 575 public void preDeregister() throws Exception { 576 } 577 578 public void postDeregister() { 579 590 } 591 592 public boolean destroyConfigElement() throws Exception { 594 if(mcb==null) 595 { return false; 597 } 598 mcb.deleteSelf(); 599 ObjectName objectName = ConfigMBeanHelper.getOwnObjectName(m_registry, info); 601 if(objectName!=null) 602 { 603 try{ 604 MBeanServer server = (MBeanServer)(MBeanServerFactory.findMBeanServer(null)).get(0); 605 server.unregisterMBean(objectName); 606 } 607 catch (Throwable t) 608 { 609 _sLogger.fine("!!!!!!!!!!!!!! Can not unregister MBean: "+objectName); 610 } 611 return true; 612 } 613 614 return false; 615 } 618 619 protected ConfigContext getConfigContext() 622 { 623 if(mcb!=null) { 624 return mcb.getConfigContext(); 625 } 626 return null; 627 } 628 632 public ConfigBean getBaseConfigBean() { 633 if(mcb!=null) { 634 return mcb.getBaseConfigBean(); 635 } 636 return null; 637 } 638 protected String getDomainName() throws MBeanException 640 { 641 final ModelMBeanInfo info = (ModelMBeanInfo )getMBeanInfo(); 642 if (info != null) { 643 String [] location = MBeanHelper.getLocation(info); 644 if(location!=null && location.length>0) { 645 return location[0]; 646 } 647 } 648 return null; 649 } 650 651 protected ManagedConfigBean getManagedConfigBean(ConfigBean cb) throws Exception 653 { 654 return new ManagedConfigBean(cb, m_registry, getDomainName()); 655 } 656 657 public ObjectName getConfigBeanObjectName(ConfigBean configBean) throws Exception 659 { 660 return ConfigMBeanHelper.getConfigBeanObjectName( 661 m_registry, getDomainName(), configBean); 662 } 663 664 protected MBeanServer getMBeanServer() throws MBeanException 666 { 667 return (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0); 668 } 669 protected ObjectName getChildObjectName(String childMBeanType, String name ) throws MBeanException 673 { 674 if(m_registry==null) 675 return null; 676 String [] parentLocation = MBeanHelper.getLocation((ModelMBeanInfo )getMBeanInfo()); 677 if(parentLocation==null || parentLocation.length==0) 678 return null; 679 if(name==null) 680 return m_registry.getMbeanObjectName(childMBeanType, parentLocation); 681 String [] childLocation = new String [parentLocation.length+1]; 682 for(int i=0; i<parentLocation.length; i++) 683 childLocation[i] = parentLocation[i]; 684 childLocation[parentLocation.length] = name; 685 return m_registry.getMbeanObjectName(childMBeanType, childLocation); 686 } 687 688 protected ObjectName getServerObjectName(String server) 689 throws MBeanException 690 { 691 return m_registry.getMbeanObjectName("server", new String []{ 692 getDomainName(), server}); 693 } 694 695 protected ObjectName [] toServerONArray(String [] ca) throws MBeanException 696 { 697 int num = ca.length; 698 final ObjectName [] result = new ObjectName [num]; 699 700 for (int i = 0; i < num; i++) 701 { 702 result[i] = getServerObjectName(ca[i]); 703 } 704 return result; 705 } 706 707 protected ObjectName getClusterObjectName(String name) 708 throws MBeanException 709 { 710 return m_registry.getMbeanObjectName("cluster", new String []{ 711 getDomainName(), name}); 712 } 713 714 protected ObjectName [] toClusterONArray(String [] ca) throws MBeanException 715 { 716 int num = ca.length; 717 final ObjectName [] result = new ObjectName [num]; 718 for (int i = 0; i < num; i++) 719 { 720 result[i] = getClusterObjectName(ca[i]); 721 } 722 return result; 723 } 724 725 protected ObjectName getConfigurationObjectName(String name) 726 throws MBeanException 727 { 728 return m_registry.getMbeanObjectName("config", new String []{ 729 getDomainName(), name}); 730 } 731 732 protected ObjectName [] toConfigurationONArray(String [] ca) throws MBeanException 733 { 734 int num = ca.length; 735 final ObjectName [] result = new ObjectName [num]; 736 for (int i = 0; i < num; i++) 737 { 738 result[i] = getConfigurationObjectName(ca[i]); 739 } 740 return result; 741 } 742 743 protected ObjectName getNodeAgentObjectName(String name) 744 throws MBeanException 745 { 746 return m_registry.getMbeanObjectName("node-agent", new String []{ 747 getDomainName(), name}); 748 } 749 750 protected ObjectName [] toNodeAgentONArray(String [] names) throws MBeanException 751 { 752 int numNames = names.length; 753 final ObjectName [] result = new ObjectName [numNames]; 754 for (int i = 0; i < numNames; i++) { 755 result[i] = getNodeAgentObjectName(names[i]); 756 } 757 return result; 758 } 759 } 760 | Popular Tags |