1 23 24 31 32 package com.sun.enterprise.admin.server.core.mbean.config; 33 34 import com.sun.enterprise.admin.util.HostAndPort; 35 import com.sun.enterprise.admin.util.Debug; 36 import com.sun.enterprise.admin.util.ArgChecker; 37 import com.sun.enterprise.admin.common.OperationProgress; 38 import com.sun.enterprise.admin.common.RequestID; 39 import com.sun.enterprise.admin.common.ObjectNames; 40 import com.sun.enterprise.admin.common.*; 41 import com.sun.enterprise.admin.common.MBeanServerFactory; 42 import com.sun.enterprise.admin.common.constant.ConfigAttributeName; 43 import com.sun.enterprise.admin.common.constant.AdminConstants; 44 import com.sun.enterprise.admin.server.core.mbean.meta.MBeanEasyConfig; 45 import com.sun.enterprise.admin.common.exception.MBeanConfigException; 46 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo; 47 import com.sun.enterprise.admin.monitor.CommandMapper; 48 import com.sun.enterprise.admin.monitor.MonitorCommand; 49 import com.sun.enterprise.admin.monitor.MonitorGetCommand; 50 import com.sun.enterprise.admin.monitor.MonitorSetCommand; 51 import com.sun.enterprise.admin.monitor.MonitorListCommand; 52 import com.sun.enterprise.admin.event.MonitoringEvent; 53 import com.sun.enterprise.admin.event.AdminEventMulticaster; 54 import com.sun.enterprise.admin.event.AdminEventResult; 55 56 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeansNaming; 57 58 import javax.management.ObjectName ; 60 import javax.management.MBeanServer ; 61 import javax.management.DynamicMBean ; 62 import javax.management.AttributeList ; 63 import javax.management.MBeanInfo ; 64 import javax.management.MBeanAttributeInfo ; 65 import javax.management.Attribute ; 66 import javax.management.AttributeNotFoundException ; 67 import javax.management.MBeanException ; 68 import javax.management.ReflectionException ; 69 import javax.management.InvalidAttributeValueException ; 70 import javax.management.MalformedObjectNameException ; 71 import javax.management.InstanceNotFoundException ; 72 import javax.management.IntrospectionException ; 73 import javax.management.JMRuntimeException ; 74 import javax.management.ServiceNotFoundException ; 75 76 import java.util.Set ; 77 import java.util.Hashtable ; 78 import java.util.Iterator ; 79 import java.util.ArrayList ; 80 import java.util.logging.Level ; 81 import java.util.logging.Logger ; 82 83 import com.sun.enterprise.util.i18n.StringManager; 85 86 94 95 public class GenericConfigurator extends AdminBase 96 { 97 public static final Logger sLogger = Logger.getLogger(AdminConstants.kLoggerName); 98 private final static String MSG_ATTR_NOT_FOUND = "mbean.config.attribute_not_found"; 99 private final static String MSG_GETMBEANINFO_FAILED = "mbean.config.getmbeaninfo_failed"; 100 private final static String MSG_GET_ATTRIBUTE = "mbean.config.get_attribute"; 101 private final static String MSG_GET_ATTRIBUTE_DEFAULT = "mbean.config.get_attribute_default"; 102 private final static String MSG_SET_ATTRIBUTE = "mbean.config.set_attribute"; 103 private final static String MSG_LIST_NAMES_CONTS = "mbean.config.list_names_continuations"; 104 private static StringManager localStrings = 106 StringManager.getManager( GenericConfigurator.class ); 107 108 MBeanServer mServer = null; 109 public GenericConfigurator() { 110 } 111 112 120 public AttributeList getGenericAttribute(String dottedName) throws 121 InstanceNotFoundException , AttributeNotFoundException , MBeanException , ReflectionException , 122 MalformedNameException, MalformedObjectNameException , IntrospectionException ,MBeanConfigException 123 { 124 sLogger.log(Level.FINEST, MSG_GET_ATTRIBUTE, dottedName); 125 if(mServer==null) 126 mServer=MBeanServerFactory.getMBeanServer(); 127 dottedName = dottedName.trim(); 128 String attrName = extractAttrNameFromDotted(dottedName); 129 int prefixLen = dottedName.length() - attrName.length(); 130 String namePrefix = null; 131 if(prefixLen>0) 132 namePrefix = dottedName.substring(0, prefixLen); 133 134 ArrayList objectNames = getMBeanNamesFromDottedPattern(dottedName); 135 136 AttributeList attrList = new AttributeList (); 137 for(int i=0; i<objectNames.size(); i++) 138 { 139 140 addAttributeToList(attrList, (ObjectName )objectNames.get(i), attrName); 141 } 142 if(namePrefix!=null && attrList!=null) 143 { 144 for(int i=0; i<attrList.size(); i++) 145 { 146 Attribute attr = (Attribute )attrList.get(i); 147 attrList.set(i, new Attribute (namePrefix+attr.getName(),attr.getValue())); 148 } 149 } 150 return attrList; 151 } 152 153 161 public AttributeList getGenericAttributeDefaultValues(String instanceName, String mbeanType, String [] attrNames) throws MBeanConfigException 162 { 163 sLogger.log(Level.FINEST, MSG_GET_ATTRIBUTE_DEFAULT, new String []{instanceName, mbeanType}); 164 ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(mbeanType, new String [] {instanceName}, false); 165 ConfigMBeanBase mbean = info.constructConfigMBean(); 166 AttributeList attrList = new AttributeList (); 167 for(int i=0; i<attrNames.length; i++) 168 { 169 Object defaultValue; 170 try 171 { 172 defaultValue = mbean.getDefaultAttributeValue(attrNames[i]); 173 } 174 catch (AttributeNotFoundException e) 175 { 176 defaultValue = null; 177 } 178 catch (MBeanConfigException e) 179 { 180 defaultValue = null; 181 } 182 if(defaultValue!=null) 183 attrList.add(new Attribute (attrNames[i], defaultValue)); 184 } 185 return attrList; 186 } 187 188 189 191 public String [] listGenericDottedNameContinuiations(String dottedName) throws MalformedNameException 192 { 193 sLogger.log(Level.FINEST, MSG_LIST_NAMES_CONTS, dottedName); 194 dottedName = dottedName.trim(); 195 String instanceName = extractInstanceNameFromDotted(dottedName); 196 return ConfigMBeansNaming.findNameContinuation(instanceName, dottedName); 197 } 198 199 207 public AttributeList getGenericAttributes(String [] dottedNames) throws 208 InstanceNotFoundException ,AttributeNotFoundException , MBeanException , ReflectionException , 209 MalformedNameException, MalformedObjectNameException , IntrospectionException ,MBeanConfigException 210 { 211 AttributeList list = new AttributeList (); 212 for(int i=0; i<dottedNames.length; i++) 213 { 214 list.addAll(getGenericAttribute(dottedNames[i])); 215 } 216 return list; 217 } 218 219 227 public AttributeList setGenericAttribute(String dottedName, Object value) throws 228 MBeanConfigException,InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , 229 MBeanException , ReflectionException , MalformedNameException, MalformedObjectNameException , IntrospectionException 230 { 231 sLogger.log(Level.FINEST, MSG_SET_ATTRIBUTE, new Object []{dottedName, value}); 232 if(mServer==null) 233 mServer=MBeanServerFactory.getMBeanServer(); 234 String attrName = extractAttrNameFromDotted(dottedName); 235 236 ArrayList objectNames = getMBeanNamesFromDottedPattern(dottedName); 237 238 AttributeList attrList = new AttributeList (); 239 for(int i=0; i<objectNames.size(); i++) 240 setMatchedAttributesToValue(attrList, (ObjectName )objectNames.get(i), attrName, value); 241 return attrList; 242 } 243 244 245 251 public AttributeList setGenericAttributes(AttributeList al) 252 throws InvalidAttributeValueException ,InstanceNotFoundException ,InvalidAttributeValueException , MBeanConfigException, 253 MBeanException , ReflectionException , MalformedNameException, MalformedObjectNameException , IntrospectionException 254 { 255 AttributeList list = new AttributeList (); 256 Iterator it = al.iterator(); 257 while (it.hasNext()) 258 { 259 Attribute attribute = (Attribute ) it.next(); 260 String name = attribute.getName(); 261 Object value = attribute.getValue(); 262 try 263 { 264 list.addAll(setGenericAttribute(name, value)); 265 } 266 catch (AttributeNotFoundException anfe) 267 { 268 sLogger.log(Level.FINE, MSG_ATTR_NOT_FOUND, name); 269 } 270 } 271 return list; 272 } 273 274 294 295 public AttributeList getMonitor(String dottedName) throws Exception 296 { 297 Name name = new Name(dottedName); 298 String instanceName = name.getNamePart(0).toString(); 299 300 CommandMapper cm = CommandMapper.getInstance(instanceName); 302 MonitorGetCommand command = cm.mapGetCommand(dottedName); MonitoringEvent event = new MonitoringEvent(instanceName, dottedName, MonitoringEvent.GET_MONITOR_DATA, command); 305 AdminEventResult result = AdminEventMulticaster.multicastEvent(event); 307 if(!result.getResultCode().equals(result.SUCCESS)) 309 { 310 handleMonitoringError(result, instanceName, dottedName); 311 } 312 return (AttributeList )result.getAttribute(event.getEffectiveDestination(), MonitorCommand.MONITOR_RESULT); 314 } 315 316 public AttributeList getMonitor(String [] dottedNames) throws Exception 317 { 318 AttributeList list = new AttributeList (); 319 for(int i=0; i<dottedNames.length; i++) 320 { 321 list.addAll(getMonitor(dottedNames[i])); 322 } 323 return list; 324 } 325 326 339 340 public String [] listMonitor(String dottedName) throws Exception 341 { 342 Name name = new Name(dottedName); 343 String instanceName = name.getNamePart(0).toString(); 344 345 CommandMapper cm = CommandMapper.getInstance(instanceName); 347 MonitorListCommand command = cm.mapListCommand(dottedName); MonitoringEvent event = new MonitoringEvent(instanceName, dottedName, MonitoringEvent.LIST_MONITORABLE, command); 350 AdminEventResult result = AdminEventMulticaster.multicastEvent(event); 352 if(!result.getResultCode().equals(result.SUCCESS)) 354 { 355 handleMonitoringError(result, instanceName, dottedName); 356 } 357 return (String [])result.getAttribute(event.getEffectiveDestination(), MonitorCommand.MONITOR_RESULT); 359 } 360 361 public AttributeList setMonitor(AttributeList al) throws Exception 362 { 363 AttributeList list = new AttributeList (); 364 Iterator it = al.iterator(); 365 while (it.hasNext()) 366 { 367 Attribute attribute = (Attribute ) it.next(); 368 String name = attribute.getName(); 369 Object value = attribute.getValue(); 370 try 371 { 372 list.addAll(setMonitor(name, value)); 373 } 374 catch (AttributeNotFoundException anfe) 375 { 376 sLogger.log(Level.FINE, MSG_ATTR_NOT_FOUND, name); 377 } 378 } 379 380 return list; 381 } 382 383 public AttributeList setMonitor(String dottedName, Object value) throws Exception 384 { 385 Name name = new Name(dottedName); 386 String instanceName = name.getNamePart(0).toString(); 387 388 CommandMapper cm = CommandMapper.getInstance(instanceName); 390 MonitorSetCommand command = cm.mapSetCommand(dottedName, value); MonitoringEvent event = new MonitoringEvent(instanceName, dottedName, MonitoringEvent.SET_MONITOR_DATA, command); 393 AdminEventResult result = AdminEventMulticaster.multicastEvent(event); 395 if(!result.getResultCode().equals(result.SUCCESS)) 397 { 398 handleMonitoringError(result, instanceName, dottedName); 399 } 400 AttributeList resultList = null; 402 AttributeList tmp = (AttributeList )result.getAttribute(event.getEffectiveDestination(), MonitorCommand.MONITOR_RESULT); 403 Iterator it = tmp.iterator(); 404 while (it.hasNext()) 405 { 406 Attribute attribute = (Attribute ) it.next(); 407 resultList = (AttributeList )attribute.getValue(); 408 } 409 return resultList; 410 } 411 412 private int setMatchedAttributesToValue(AttributeList attrList, ObjectName objName, String attrPattern, Object value ) throws 414 InstanceNotFoundException ,AttributeNotFoundException , InvalidAttributeValueException , 415 MBeanException , ReflectionException , MalformedNameException, IntrospectionException 416 { 417 ArrayList attrNames = getAttrNames(objName, attrPattern); 418 if(attrNames.size()==0) 419 { 420 String msg = localStrings.getString( "admin.server.core.mbean.config.no_attributes_matched_to_pattern", attrPattern ); 421 throw new AttributeNotFoundException ( msg ); 422 } 423 for(int i=0; i<attrNames.size(); i++) 424 { 425 String name = (String )attrNames.get(i); 426 Object obj = convertValueToAttributeType(objName, name, value); 427 mServer.setAttribute(objName, new Attribute (name, obj)); 428 attrList.add(new Attribute (name, obj)); 429 } 430 return attrNames.size(); } 432 433 private void addAttributeToList(AttributeList attrList, ObjectName objName, String attrPattern ) throws 435 InstanceNotFoundException ,AttributeNotFoundException , MBeanException , 436 ReflectionException , MalformedNameException, IntrospectionException 437 { 438 AttributeList list = null; 441 if(attrPattern.equals(""+Tokens.kWildCardChar)) 442 { 443 list = mServer.getAttributes(objName, new String []{""}); 444 if(list!=null) 445 attrList.addAll(list); 446 } 447 else 448 if(attrPattern.equals(ConfigAttributeName.PROPERTY_NAME_PREFIX+Tokens.kWildCardChar)) 449 { 450 list = mServer.getAttributes(objName, new String []{ConfigAttributeName.PROPERTY_NAME_PREFIX}); 451 if(list!=null) 452 attrList.addAll(list); 453 } 454 else 455 { 456 Object attValue = mServer.getAttribute(objName, attrPattern); 457 attrList.add(new Attribute (attrPattern, attValue)); 458 } 459 460 488 } 489 490 503 private void handleMonitoringError(AdminEventResult result, String instance, 504 String compName) 505 throws AttributeNotFoundException , InstanceNotFoundException , 506 ServiceNotFoundException { 507 String resultCode = result.getResultCode(); 508 if (AdminEventResult.TRANSMISSION_ERROR.equals(resultCode)) { 509 String msg = localStrings.getString( "admin.server.core.mbean.config.failed_to_connect_instance", instance ); 510 throw new ServiceNotFoundException ( msg ); 511 } else if (AdminEventResult.MBEAN_NOT_FOUND.equals(resultCode)) { 512 String msg = localStrings.getString( "admin.server.core.mbean.config.unmatched_mbean", compName ); 513 throw new InstanceNotFoundException ( msg ); 514 } else if (AdminEventResult.MBEAN_ATTR_NOT_FOUND.equals(resultCode)) { 515 String msg = localStrings.getString( "admin.server.core.mbean.config.unmatched_attribute", compName ); 516 throw new AttributeNotFoundException ( msg ); 517 } else if (!AdminEventResult.SUCCESS.equals(resultCode)) { 518 String msg = localStrings.getString( "admin.server.core.mbean.config.other_monitoring_error", resultCode ); 519 throw new JMRuntimeException ( msg ); 520 } 521 } 522 523 private ArrayList getAttrNames(ObjectName objName, String attrPattern ) throws 525 InstanceNotFoundException , IntrospectionException , ReflectionException 526 { 527 ArrayList list = new ArrayList (); 528 if(attrPattern==null) 529 return list; 530 531 if(attrPattern.indexOf(Tokens.kWildCardChar)>=0 || attrPattern.indexOf(ObjectNames.kSingleMatchChar)>=0) 532 { 533 MBeanInfo mi = mServer.getMBeanInfo(objName); 534 if(mi!=null) 535 { 536 MBeanAttributeInfo [] ai = mi.getAttributes(); 537 if(ai!=null) 538 for(int i=0; i<ai.length; i++) 539 { 540 String name = ai[i].getName(); 541 try 542 { 543 if((new CombinedPatternMatcher(attrPattern, name)).matches()) 544 list.add(ai[i].getName()); 545 } catch (Throwable e) 546 { 547 } 548 } 549 } 550 } 551 else 552 list.add(attrPattern); return list; 554 } 555 556 private String getAttrType(ObjectName objName, String attrName) throws 558 InstanceNotFoundException , IntrospectionException , ReflectionException 559 { 560 return getAttrType(attrName, mServer.getMBeanInfo(objName)); 561 } 562 563 private String getAttrType(String attrName, MBeanInfo mi) 565 { 566 if(attrName==null && mi==null) 567 return null; 568 569 MBeanAttributeInfo [] ai = mi.getAttributes(); 570 if(ai!=null) 571 for(int i=0; i<ai.length; i++) 572 { 573 String name = ai[i].getName(); 574 if(attrName.equals(ai[i].getName())) 575 return ai[i].getType(); 576 } 577 return null; 578 } 579 580 private ArrayList getTargetObjectNames(ObjectName objectNamePattern ) throws MalformedObjectNameException 582 { 583 ArrayList list = new ArrayList (); 584 585 if(objectNamePattern==null) 586 return list; 587 588 Set mNames = mServer.queryMBeans(objectNamePattern, null); 590 Iterator it = mNames.iterator(); 591 while (it.hasNext()) 592 { 593 list.add(it.next()); 594 } 595 596 if(list.size()==0) 600 { 601 list.add(objectNamePattern); 602 } 603 return list; 605 } 606 607 String extractAttrNameFromDotted(String dottedStringName) throws MalformedNameException 609 { 610 int idx = dottedStringName.lastIndexOf(Tokens.kDelimiterChar); 611 if(idx<=0 || idx==(dottedStringName.length()-1)) 612 { 613 String msg = localStrings.getString( "admin.server.core.mbean.config.cannot_extract_attribute_name_from_dotted_notation", dottedStringName ); 614 throw new MalformedNameException( msg ); 615 } 616 if(dottedStringName.substring(0, idx+1).endsWith(ConfigAttributeName.PROPERTY_NAME_PREFIX)) 618 { 619 int idx2 = idx-ConfigAttributeName.PROPERTY_NAME_PREFIX.length(); 620 if(dottedStringName.charAt(idx2)==Tokens.kDelimiterChar) 621 { 622 idx = idx2; 623 if(idx<1) { 624 String msg = localStrings.getString( "admin.server.core.mbean.config.cannot_extract_attribute_name_from_dotted_notation", dottedStringName ); 625 throw new MalformedNameException( msg ); 626 } 627 } 628 } 629 return dottedStringName.substring(idx+1); 630 } 631 632 String extractInstanceNameFromDotted(String dottedName) throws MalformedNameException 634 { 635 Name name = new Name(dottedName); 636 return name.getNamePart(0).toString(); 637 } 638 639 ArrayList getMBeanNamesFromDottedPattern(String dottedStringName) throws MBeanConfigException,MalformedNameException, MalformedObjectNameException 641 { 642 String attrName = extractAttrNameFromDotted(dottedStringName); 643 int idx = dottedStringName.length() - attrName.length() - 1; 644 if(idx<1) { 645 String msg = localStrings.getString( "admin.server.core.mbean.config.genericconfigurator_cannot_extract_attribute_name_from_dotted_notation", dottedStringName ); 646 throw new MalformedNameException( msg ); 647 } 648 649 ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(dottedStringName.substring(0,idx)); 650 ArrayList list = new ArrayList (); 651 list.add(info.getObjectName()); 652 return list; 653 } 654 655 private Object convertValueToAttributeType(ObjectName objName, String attrName, Object value ) 657 { 658 try 659 { 660 if(value instanceof String ) { 662 String type = getAttrType(objName, attrName); 663 return MBeanEasyConfig.convertStringValueToProperType((String )value, type); 664 } 665 } 666 catch (Throwable t) 667 { 668 } 669 return value; } 671 672 673 679 680 private static final String [] mAttrs = new String [0]; 681 private static final String [] mOpers = 682 { 683 "getGenericAttribute(String name), INFO", 684 "getGenericAttributes(String[] attributeNames), INFO", 685 "setGenericAttribute(String name, Object objValue), ACTION_INFO", 686 "setGenericAttributes(javax.management.AttributeList al), ACTION_INFO", 687 "getMonitor(String[] name), INFO", 688 "setMonitor(javax.management.AttributeList al), ACTION", 689 "listMonitor(String name), INFO", 690 "listGenericDottedNameContinuiations(String dottedName), INFO", 691 }; 692 693 697 public MBeanInfo getMBeanInfo() 698 { 699 700 try 701 { 702 return (new MBeanEasyConfig(getClass(), mAttrs, mOpers, null)).getMBeanInfo(); 703 } 704 catch(Throwable e) 705 { 706 sLogger.log(Level.FINE, MSG_GETMBEANINFO_FAILED, e); 707 return null; 708 } 709 } 710 711 719 protected Class getImplementingClass() { 720 return ( this.getClass() ); 721 } 722 723 724 protected Object getImplementingMBean() { 725 return ( this ); 726 } 727 728 } 729 | Popular Tags |