1 23 24 package com.sun.enterprise.tools.admingui.handlers; 25 26 import com.iplanet.jato.RequestContext; 27 import com.iplanet.jato.view.ContainerView; 28 import com.iplanet.jato.view.View; 29 import com.iplanet.jato.view.ViewBase; 30 31 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 32 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 33 import com.sun.enterprise.tools.guiframework.view.HandlerContext; 34 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager; 35 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 36 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView; 37 38 import java.util.ArrayList ; 39 import java.util.Properties ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.HashMap ; 43 import java.util.Map ; 44 import java.util.Date ; 45 import java.util.StringTokenizer ; 46 47 import javax.management.Attribute ; 48 import javax.management.AttributeList ; 49 import javax.management.MBeanServer ; 50 51 import com.sun.enterprise.tools.admingui.util.Util; 52 53 56 public class MBeanHandlers { 57 58 67 public void getAttributeListFromMBean(RequestContext reqCtx, HandlerContext handlerCtx) { 68 ArrayList attributeNames = 69 (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_NAMES); 70 if (attributeNames == null) { 71 throw new IllegalArgumentException ( 72 "The parameter map did not contain " + ATTRIBUTE_NAMES); 73 } 74 Object objectName = handlerCtx.getInputValue(OBJECT_NAME); 75 if (objectName == null) { 76 throw new IllegalArgumentException ( 77 "The parameter map did not contain " + OBJECT_NAME); 78 } 79 AttributeList values = null; 80 try { 81 values = (AttributeList )MBeanUtil.getAttributes( 82 objectName.toString(), 83 (String [])attributeNames.toArray(new String [attributeNames.size()])); 84 } catch (Exception ex) { 86 if (ex.getCause() instanceof javax.management.InstanceNotFoundException ) { 87 if (Util.isLoggableFINE()) { 90 Util.logFINE("caught: InstanceNotFoundException"); 91 } 92 } else { 93 throw new FrameworkException( 94 "Unable to get MBean attributes for object name: '"+ 95 objectName+"'.", ex, 96 null, null); 97 } 98 } 99 handlerCtx.setOutputValue(VALUE, values); 100 } 101 102 111 public void getAttributeValueFromMBean(RequestContext reqCtx, HandlerContext handlerCtx) { 112 String attributeName = 113 (String )handlerCtx.getInputValue(ATTRIBUTE_NAME); 114 if (attributeName == null) { 115 throw new IllegalArgumentException ( 116 "The parameter map did not contain " + ATTRIBUTE_NAME); 117 } 118 Object objectName = handlerCtx.getInputValue(OBJECT_NAME); 119 if (objectName == null) { 120 throw new IllegalArgumentException ( 121 "The parameter map did not contain " + OBJECT_NAME); 122 } 123 try { 124 handlerCtx.setOutputValue(VALUE, MBeanUtil.getAttribute( 125 objectName.toString(), attributeName)); 126 } catch (Exception ex) { 128 if (ex.getCause() instanceof javax.management.InstanceNotFoundException ) { 129 if (Util.isLoggableFINE()) { 132 Util.logFINE("caught: InstanceNotFoundException"); 133 } 134 } else { 135 throw new FrameworkException( 136 "Unable to get MBean attribute for object name: '"+ 137 objectName+"'.", ex, 138 null, null); 139 } 140 } 141 } 142 143 150 public void setAttributeListToMBean(RequestContext reqCtx, HandlerContext handlerCtx) { 151 AttributeList value = (AttributeList )handlerCtx.getInputValue(VALUE); 152 Object objectName = handlerCtx.getInputValue(OBJECT_NAME); 153 if (objectName == null) { 154 throw new IllegalArgumentException ( 155 "The parameter map did not contain " + OBJECT_NAME); 156 } 157 try { 158 MBeanUtil.setAttributes(objectName.toString(), value); 159 } catch (Exception ex) { 160 throw new FrameworkException( 161 "Error while setting attributes on MBean '"+objectName+ 162 "'!", ex, handlerCtx.getViewDescriptor(), 163 handlerCtx.getView()); 164 } 165 } 166 167 176 public void setAttributeToMBean(RequestContext reqCtx, HandlerContext handlerCtx) { 177 Object value = handlerCtx.getInputValue(VALUE); 178 String name = (String )handlerCtx.getInputValue(KEY); 179 String val = null; 180 181 if (value instanceof Object []){ 182 val = convertToCommaSeperatedList((Object [] ) value); 183 }else{ 184 val = (String ) value; 185 } 186 187 Attribute attr = new Attribute (name, val); 188 189 Object objectName = handlerCtx.getInputValue(OBJECT_NAME); 190 if (objectName == null) { 191 throw new IllegalArgumentException ( 192 "The parameter map did not contain " + OBJECT_NAME); 193 } 194 if (name == null) { 195 throw new IllegalArgumentException ( 196 "The parameter map did not contain AttributeName"); 197 } 198 try { 199 MBeanUtil.setAttribute(objectName.toString(), attr); 200 } catch (Exception ex) { 201 throw new FrameworkException( 202 "Error while setting attributes on MBean '"+objectName+ 203 "'!", ex, handlerCtx.getViewDescriptor(), 204 handlerCtx.getView()); 205 } 206 } 207 208 private String convertToCommaSeperatedList(Object [] values){ 209 String allValues = null; 210 if (values != null) { 211 int len = ((Object [])values).length; 212 for (int count=0; count<len; count++) { 213 String val = (String ) (((Object [])values)[count]); 214 if ((val == null) || (val.toString().trim().length() == 0)) { 215 continue; 216 } 217 allValues = (allValues == null) ? val : allValues+ ","+ val; 218 } 219 } 220 return allValues; 221 } 222 223 232 private Attribute split(Attribute attr, String splitChar) { 233 if (attr == null || attr.getValue() == null) { 234 return attr; 235 } 236 String newline = System.getProperty("line.separator"); 237 String strValue = attr.getValue().toString(); 238 boolean changed = false; 239 if (strValue.indexOf(splitChar) > -1) { 240 strValue = strValue.replaceAll(splitChar, newline); 241 changed = true; 242 } 243 if (strValue.indexOf("${path.separator}") > -1) { 244 strValue = strValue.replaceAll("\\$\\{path.separator\\}", newline); 245 changed = true; 246 } 247 return (changed) ? new Attribute (attr.getName(), strValue) : attr; 248 } 249 250 251 254 private Attribute unsplit(Attribute attr, String splitChar) { 255 if (attr == null || attr.getValue() == null) { 256 return attr; 257 } 258 String strValue = attr.getValue().toString(); 259 260 String result = ""; 261 StringTokenizer st = new StringTokenizer (strValue, "\n\r"); 262 while (st.hasMoreTokens()) { 263 if (result.equals("") == false) { 264 result += "${path.separator}"; } 266 result += st.nextToken(); 267 } 268 return new Attribute (attr.getName(), result); 269 } 270 271 272 276 public void split(RequestContext reqCtx, HandlerContext handlerCtx) { 277 Object value = null; 278 279 Object inputList = handlerCtx.getInputValue(IN); 280 281 String splitChar = System.getProperty("path.separator"); 282 if (!splitChar.equals("") && (inputList instanceof AttributeList )) { 283 AttributeList list = (AttributeList )inputList; 284 AttributeList newList = new AttributeList (); 285 for (int i=0; i<list.size(); i++) { 286 newList.add(i, split((Attribute )list.get(i), splitChar)); 287 } 288 value = newList; 289 } 290 291 handlerCtx.setOutputValue(OUT, value); 293 } 294 295 296 302 public void unsplit(RequestContext reqCtx, HandlerContext handlerCtx) { 303 Object value = handlerCtx.getInputValue(IN); 304 String splitChar = System.getProperty("path.separator"); 305 if (!splitChar.equals("") && (value instanceof AttributeList )) { 306 AttributeList list = (AttributeList )value; 307 AttributeList newList = new AttributeList (); 308 for (int i=0; i<list.size(); i++) { 309 newList.add(i, unsplit((Attribute )list.get(i), splitChar)); 310 } 311 value = newList; 312 } 313 handlerCtx.setOutputValue(OUT, value); 314 } 315 316 325 public void getAttributeListFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) { 326 ArrayList attributeNames = (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_NAMES); 327 if (attributeNames == null) { 328 throw new IllegalArgumentException ( 329 "The parameter map did not contain " + ATTRIBUTE_NAMES); 330 } 331 332 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 333 if (displayNames == null) { 334 throw new IllegalArgumentException ( 335 "The parameter map did not contain " + DISPLAY_NAMES); 336 } 337 338 if (displayNames.size() != attributeNames.size()) { 339 throw new IllegalArgumentException ( 340 "displayNames and attributeNames should have same number of entries!"); 341 } 342 ContainerView view = getView(reqCtx, handlerCtx); 343 344 AttributeList attrList = new AttributeList (); 345 for (int i = 0; i < attributeNames.size(); i++) { 346 Object fieldValue = view.getDisplayFieldValue((String )displayNames.get(i)); 347 if (fieldValue != null) { 348 attrList.add(new Attribute ((String )attributeNames.get(i), fieldValue)); 349 } 350 } 351 handlerCtx.setOutputValue(VALUE, attrList); 353 } 354 355 364 public void getPropertiesListFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) { 365 ArrayList propertyNames = (ArrayList )handlerCtx.getInputValue(PROPERTY_NAMES); 366 if (propertyNames == null) { 367 throw new IllegalArgumentException ( 368 "The parameter map did not contain " + PROPERTY_NAMES); 369 } 370 371 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 372 if (displayNames == null) { 373 throw new IllegalArgumentException ( 374 "The parameter map did not contain " + DISPLAY_NAMES); 375 } 376 377 if (displayNames.size() != propertyNames.size()) { 378 throw new IllegalArgumentException ( 379 "displayNames and attributeNames should have same number of entries!"); 380 } 381 ContainerView view = getView(reqCtx, handlerCtx); 382 383 Properties props = new Properties (); 384 for (int i = 0; i < propertyNames.size(); i++) { 385 String fieldValue = (String )view.getDisplayFieldValue((String )displayNames.get(i)); 386 if (fieldValue != null && fieldValue != "") { 387 props.put((String )propertyNames.get(i), fieldValue); 388 } 389 } 390 handlerCtx.setOutputValue(VALUE, props); 392 } 393 394 395 public void getAttributeListFromNameValues(RequestContext reqCtx, HandlerContext handlerCtx) { 397 ArrayList attributeNames = (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_NAMES); 398 ArrayList attributeValues = (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_VALUES); 399 if (attributeNames == null || attributeValues == null) { 400 throw new IllegalArgumentException ( 401 "The parameter map must contain " + ATTRIBUTE_NAMES + " and " + ATTRIBUTE_VALUES); 402 } 403 404 if (attributeValues.size() != attributeNames.size()) { 405 throw new IllegalArgumentException ( 406 ATTRIBUTE_NAMES + "and " + ATTRIBUTE_VALUES + " should have same number of entries!"); 407 } 408 409 AttributeList attrList = new AttributeList (); 410 for (int i = 0; i < attributeNames.size(); i++) { 411 attrList.add(new Attribute ((String )attributeNames.get(i), attributeValues.get(i))); 412 } 413 handlerCtx.setOutputValue(VALUE, attrList); 415 } 416 417 426 public void getArrayListFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) { 427 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 428 if (displayNames == null) { 429 throw new IllegalArgumentException ( 430 "The parameter map did not contain " + DISPLAY_NAMES); 431 } 432 433 ContainerView view = getView(reqCtx, handlerCtx); 434 435 ArrayList arrayList = new ArrayList (); 436 for (int i = 0; i < displayNames.size(); i++) { 437 Object fieldValue = view.getDisplayFieldValue((String )displayNames.get(i)); 438 if (fieldValue instanceof String && ((String )(fieldValue)).length() == 0) 439 fieldValue = null; 440 arrayList.add(fieldValue); 441 } 442 handlerCtx.setOutputValue(VALUE, arrayList); 444 } 445 446 455 public void getMapFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) { 456 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 457 ArrayList keyNames = (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_NAMES); 458 if (displayNames == null) { 459 throw new IllegalArgumentException ( 460 "The parameter map did not contain " + DISPLAY_NAMES); 461 } 462 ContainerView view = getView(reqCtx, handlerCtx); 463 Map map = new HashMap (); 464 for (int i = 0; i < displayNames.size(); i++) { 465 Object fieldValue = view.getDisplayFieldValue((String )displayNames.get(i)); 466 if (fieldValue instanceof String && ((String )(fieldValue)).length() == 0) 467 fieldValue = null; 468 map.put((String )keyNames.get(i),fieldValue); 469 } 470 handlerCtx.setOutputValue(VALUE, map); 471 } 472 473 474 475 482 public void setDisplayFieldsFromAttributeList(RequestContext reqCtx, HandlerContext handlerCtx) { 483 Object value = handlerCtx.getInputValue(VALUE); 484 if (value == null) { 485 return; 486 } 487 488 ArrayList attributeNames = (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_NAMES); 490 if (attributeNames == null) { 491 throw new IllegalArgumentException ( 492 "The parameter map did not contain " + ATTRIBUTE_NAMES); 493 } 494 495 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 497 if (displayNames == null) { 498 throw new IllegalArgumentException ( 499 "The parameter map did not contain " + DISPLAY_NAMES); 500 } 501 502 if (displayNames.size() != attributeNames.size()) { 504 throw new IllegalArgumentException ( 505 "displayNames and attributeNames should have same number of entries!"); 506 } 507 508 ContainerView view = getView(reqCtx, handlerCtx); 510 AttributeList attrList = (AttributeList )value; 511 for (int i=0; i<displayNames.size(); i++) { 512 String attrName = (String )attributeNames.get(i); 513 int j=0; 514 while ((j<attrList.size()) && 515 !((Attribute )attrList.get(j)).getName().equals(attrName)) { 516 j++; 517 } 518 if (j < attrList.size()) { 519 Object attrValue = ((Attribute )attrList.get(j)).getValue(); 520 view.setDisplayFieldValue((String )displayNames.get(i), attrValue); 521 } 522 } 523 } 524 525 532 public void setDisplayFieldsFromPropertiesList(RequestContext reqCtx, HandlerContext handlerCtx) { 533 Object value = handlerCtx.getInputValue(VALUE); 534 if (value == null) { 535 return; 536 } 537 538 ArrayList propertyNames = (ArrayList )handlerCtx.getInputValue(PROPERTY_NAMES); 540 if (propertyNames == null) { 541 throw new IllegalArgumentException ( 542 "The parameter map did not contain " + PROPERTY_NAMES); 543 } 544 545 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 547 if (displayNames == null) { 548 throw new IllegalArgumentException ( 549 "The parameter map did not contain " + DISPLAY_NAMES); 550 } 551 552 if (displayNames.size() != propertyNames.size()) { 554 throw new IllegalArgumentException ( 555 "displayNames and attributeNames should have same number of entries!"); 556 } 557 558 ContainerView view = getView(reqCtx, handlerCtx); 560 Properties props = (Properties )value; 561 for (int i=0; i<displayNames.size(); i++) { 562 String propertyValue = props.getProperty(propertyNames.get(i).toString()); 563 view.setDisplayFieldValue((String )displayNames.get(i), propertyValue); 564 565 } 566 } 567 568 569 576 public void setDisplayFieldsFromList(RequestContext reqCtx, HandlerContext handlerCtx) { 577 578 ArrayList valueList = (ArrayList )handlerCtx.getInputValue("valueList"); 579 if (valueList == null) { 580 throw new IllegalArgumentException ( 581 "The parameter map did not contain valueList" ); 582 } 583 584 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 586 if (displayNames == null) { 587 throw new IllegalArgumentException ( 588 "The parameter map did not contain " + DISPLAY_NAMES); 589 } 590 591 if (displayNames.size() != valueList.size()) { 593 throw new IllegalArgumentException ( 594 "displayNames and valueList should have same number of entries!"); 595 } 596 597 ContainerView view = getView(reqCtx, handlerCtx); 599 for (int i=0; i < valueList.size(); i++) { 600 view.setDisplayFieldValue((String )displayNames.get(i), (String )valueList.get(i)); 601 } 602 } 603 604 605 622 626 public static ContainerView getView(RequestContext reqCtx, HandlerContext handlerCtx) { 627 ViewDescriptor viewDesc = null; 628 Object value = handlerCtx.getInputValue(VIEW_DESCRIPTOR_NAME); 629 if (value == null) { 630 viewDesc = handlerCtx.getViewDescriptor(); 631 } else { 632 ViewDescriptorManager mgr = ViewDescriptorManager.getInstance(); 634 635 if (value instanceof List ) { 637 Iterator it = ((List )value).iterator(); 639 String key = it.next().toString(); 640 viewDesc = mgr.getViewDescriptor(key); 641 if (viewDesc == null) { 642 throw new IllegalArgumentException ( 643 "Top-level ViewDescriptor '"+key+"' not found!"); 644 } 645 while (it.hasNext()) { 646 key = it.next().toString(); 647 viewDesc = viewDesc.getChildDescriptor(key); 648 if (viewDesc == null) { 649 throw new IllegalArgumentException ( 650 "Child ViewDescriptor '"+key+"' not found!"); 651 } 652 } 653 } else if (value instanceof String ) { 654 View view = handlerCtx.getView(); 655 DescriptorContainerView descView = (DescriptorContainerView) 656 (((ViewBase)view).getParentViewBean()); 657 return (ContainerView)descView.getChild((String )value); 658 659 } else { 687 throw new IllegalArgumentException ( 688 "Unknown type for "+VIEW_DESCRIPTOR_NAME+". ("+value.toString()+")"); 689 } 690 } 691 View view = viewDesc.getView(reqCtx); 692 while ((view instanceof ContainerView) == false) 693 view = view.getParent(); 694 return (ContainerView)view; 695 } 696 697 698 707 public void getJMXAttributeValue(RequestContext reqCtx, HandlerContext handlerCtx) { 708 Object val = handlerCtx.getInputValue("JMXAttribute"); 710 if (val == null) { 711 throw new FrameworkException( 712 "The input value for 'JMXAttribute' was not specified.", 713 null, null); 714 } 715 716 720 Attribute att = null; 721 if (val instanceof AttributeList ) { 722 AttributeList list = ((AttributeList )val); 723 if (list.size() > 0) { 725 att = (Attribute )list.get(0); 726 } 727 } else { 728 att = (Attribute )val; 730 } 731 732 handlerCtx.setOutputValue(VALUE, (att != null) ? att.getValue() : null); 734 } 735 736 public void restoreDefaultValues(RequestContext reqCtx, HandlerContext handlerCtx) { 737 ContainerView view = getView(reqCtx, handlerCtx); 738 739 ArrayList displayNames = (ArrayList )handlerCtx.getInputValue(DISPLAY_NAMES); 740 ArrayList modelNames = (ArrayList )handlerCtx.getInputValue(ATTRIBUTE_NAMES); 741 String objectName = (String )handlerCtx.getInputValue(OBJECT_NAME); 742 String [] params = new String [1]; 743 String [] types = {"java.lang.String"}; 744 for (int i = 0; i < modelNames.size(); i++) { 745 params[0] = (String )modelNames.get(i); 746 String displayName = (String )displayNames.get(i); 747 if (view.getDisplayFieldValue(displayName) == null) { 748 view.setDisplayFieldValue(displayName, 749 MBeanUtil.getAttribute(objectName, params[0])); 750 } else { 751 view.setDisplayFieldValue(displayName, 752 MBeanUtil.invoke(objectName, "getDefaultAttributeValue", params, types)); 753 } 754 } 755 } 756 757 758 public static final String IN = "in"; 759 public static final String OUT = "out"; 760 public static final String KEY = "key"; 761 public static final String VALUE = "value"; 762 public static final String OBJECT_NAME = "objectName"; 763 public static final String DISPLAY_NAMES = "displayNames"; 764 public static final String ATTRIBUTE_NAMES = "attributeNames"; 765 public static final String PROPERTY_NAMES = "propertyNames"; 766 public static final String ATTRIBUTE_NAME = "attributeName"; 767 public static final String ATTRIBUTE_VALUES = "attributeValues"; 768 public static final String DATA_CONVERSION_TYPE = "dataConversionType"; 769 public static final String VIEW_DESCRIPTOR_NAME = "viewDescriptorName"; 770 } 771 | Popular Tags |