1 23 24 package com.sun.enterprise.tools.admingui.tree; 25 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.io.Serializable ; 33 import java.io.IOException ; 34 import java.lang.reflect.Constructor ; 35 36 import javax.servlet.*; 37 import javax.servlet.http.*; 38 import javax.management.ObjectName ; 39 import javax.management.MBeanServer ; 40 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 44 import com.iplanet.jato.RequestContext; 45 import com.iplanet.jato.RequestManager; 46 import com.iplanet.jato.ViewBeanManager; 47 import com.iplanet.jato.view.ViewBean; 48 import com.iplanet.jato.view.TreeViewStateData; 49 import com.iplanet.jato.util.NonSyncStringBuffer; 50 51 import com.sun.enterprise.tools.admingui.util.Util; 52 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 53 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 54 import com.sun.enterprise.tools.guiframework.view.DescriptorViewBeanBase; 55 56 public class IndexTreeNode extends Object implements Serializable { 57 58 private static HashMap idsMap = new HashMap (); 61 private static int nodeCount = 3; private static String computeID(IndexTreeNode parent, IndexTreeNode child, String name) { 63 String key = child.getNamePath(); 64 String id = (String )idsMap.get(key); 65 if (id == null) { 66 nodeCount++; 67 id = (String )(""+nodeCount); 68 idsMap.put(key, id); 69 } 70 return id; 71 } 72 73 private String qualifyType(String type) { 74 if (type.equalsIgnoreCase(IndexTreeModel.CONTAINER)) { 75 return IndexTreeModel.CONTAINER; 76 } else if (type.equalsIgnoreCase(IndexTreeModel.LEAF)) { 77 return IndexTreeModel.LEAF; 78 } else if (type.equalsIgnoreCase(IndexTreeModel.ROOT)) { 79 return IndexTreeModel.ROOT; 80 } 81 throw new RuntimeException ("Unknown node type specified. Error likely in treeXML."); 82 } 83 84 private String qualifyName(String name) { 85 if (name != null && name.length() > 0) { 86 return name; 87 } 88 throw new RuntimeException ("Node name must be non-null and non-blank. " 89 + "Error likely in treeXML."); 90 } 91 92 public IndexTreeNode(IndexTreeNode parent, String type, String name, IndexTreeModel model) { 93 super(); 94 this.type = qualifyType(type); 95 this.name = qualifyName(name); 96 this.id = model.getNextNodeID(); 97 this.model = model; 98 this.link = "underConstruction"; setParent(parent); 100 101 setAttribute(IndexTreeModel.FIELD_NAME, name); 102 setAttribute(IndexTreeModel.FIELD_ID, getPath()); 103 104 105 setRefresh(true); 109 } 110 111 public void setParent(IndexTreeNode parent) { 112 if (parent == null) { 113 parent = model.getRoot(); 114 } 115 this.parent = parent; 116 117 if (parent != null) { 118 parent.addChild(this); 119 120 if (parent.getType().equals(IndexTreeModel.LEAF)) 121 parent.type = IndexTreeModel.CONTAINER; 122 } 123 this.hid=computeID(parent, this, name); } 125 126 public void removeChild(String childName) { 127 Iterator it = children.iterator(); 128 while (it.hasNext()) { 129 IndexTreeNode childNode = (IndexTreeNode)it.next(); 130 if (childNode.getName().equals(childName)) { 131 IndexTreeNode n = childNode.getParent(); 132 if (n != null) { 133 n.children.remove(childNode); 134 break; 135 } 136 } 137 } 138 } 139 140 public IndexTreeNode getChild(String childName) { 141 Iterator it = children.iterator(); 142 while (it.hasNext()) { 143 IndexTreeNode childNode = (IndexTreeNode)it.next(); 144 if (childNode.getName().equals(childName)) { 145 return childNode; 146 } 147 } 148 return null; 149 } 150 151 public IndexTreeNode getChildByUniqueID(String uniqueID) { 152 Iterator it = children.iterator(); 153 while (it.hasNext()) { 154 IndexTreeNode childNode = (IndexTreeNode)it.next(); 155 if (childNode.getUniqueID() != null) { 156 if (childNode.getUniqueID().equals(uniqueID)) { 157 return childNode; 158 } 159 } 160 } 161 return null; 162 } 163 164 public void addChild(IndexTreeNode child) { 165 String isCluster = null; 166 if(child.getName().equalsIgnoreCase("Group Management Service")){ 172 ObjectName [] targets = null; 173 try { 174 targets = (ObjectName [])MBeanUtil.invoke("com.sun.appserv:type=config,name="+child.getParent().getName()+",category=config", "listReferencees", null, null); 175 if (targets != null) { 176 for (int j = 0; j < targets.length; j++) { 177 178 String objectType = targets[j].getKeyProperty("type"); 179 isCluster = (objectType.equalsIgnoreCase("cluster"))?"true":"false"; 180 } 181 } 182 if(isCluster.equals("true")) 183 children.add(child); 184 }catch (Exception ex) { 185 186 return; 188 } 189 } else { 190 children.add(child); 191 } 192 } 193 194 public void setDynamicChild(Element node) { 195 dynamicChild = node; 196 } 197 198 protected void loadParams(Element node, HashMap params) { 199 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 200 String nodeName = child.getNodeName(); 201 if (nodeName.equalsIgnoreCase("parameter")) { 202 String name = ((Element )child).getAttribute("name"); 203 String value = ((Element )child).getAttribute("value"); 204 if (name != null && value != null) { 205 value = replaceTokens(value); 206 params.put(name, value); 207 } 208 } 209 } 210 } 211 212 protected IndexTreeNode createNode(Element element, String name, Object objectName) { 213 if (getChild(name) != null) { 214 return null; 215 } 216 217 IndexTreeNode treeNode = createNode(element, this, 218 IndexTreeModelImpl.LEAF, name, model); 219 220 221 if (MBeanUtil.isValidMBean(objectName.toString())) { 222 treeNode.setAttribute(OBJECT_NAME, objectName.toString()); 223 } 224 225 boolean hasChildren = TreeReader.process(element, model, treeNode); 226 if (hasChildren) { 227 treeNode.type = IndexTreeModel.CONTAINER; 228 treeNode.getChildren(); 229 } 230 return treeNode; 231 } 232 233 public static IndexTreeNode createNode(Element element, IndexTreeNode parent, 234 String nodeType, String name, IndexTreeModel model) { 235 236 IndexTreeNode treeNode = null; 237 String classType = element.getAttribute("classType"); 238 if (classType == null || classType.length() == 0) { 239 treeNode = new IndexTreeNode(parent, nodeType, name, model); 240 } else { 241 String clazz = TreeReader.getClass(classType); 242 if (clazz == null) 243 throw new RuntimeException ("NodeClassType not specified for: "+classType); 244 try { 245 Class treeNodeClass = Class.forName(clazz); 246 Constructor constructor = treeNodeClass.getConstructor( 247 new Class [] {IndexTreeNode.class, String .class, 248 String .class, IndexTreeModel.class}); 249 250 treeNode = (IndexTreeNode)constructor.newInstance( 251 new Object [] {parent, nodeType, name, model}); 252 } catch (Exception ex) { 253 throw new RuntimeException (ex); 254 } 255 } 256 257 treeNode.setIcon(element.getAttribute("icon")); 258 treeNode.setIconExpanded(element.getAttribute("expandIcon")); 259 treeNode.setLink(element.getAttribute("link")); 260 261 String id = element.getAttribute("id"); 262 if (id != null && id.length() > 0) 263 treeNode.setUniqueID(id); 264 265 if (nodeType.equalsIgnoreCase(model.ROOT)) 266 model.setRoot(treeNode); 267 processAttributes(element, treeNode); 268 return treeNode; 269 } 270 271 private static void processAttributes(Element node, IndexTreeNode treeNode) { 272 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 273 String nodeName = child.getNodeName(); 274 if (nodeName.equalsIgnoreCase("attribute")) { 275 String name = ((Element )child).getAttribute("name"); 276 String value = ((Element )child).getAttribute("value"); 277 if (value == null || value.length() == 0) { 278 ArrayList values = new ArrayList (); 279 for (Node n = child.getFirstChild(); n != null; n = n.getNextSibling()) { 280 if (n.getNodeName().equalsIgnoreCase("values")) { 281 String v = ((Element )n).getAttribute("value"); 282 if (v != null && v.length() > 0) { 283 v = treeNode.replaceTokens(v); 284 values.add(v); 285 } 286 } 287 } 288 if (values.size() > 0) 289 treeNode.setAttribute(name, values); 290 } 291 else if (name != null && name.length() > 0 && 292 value != null && value.length() > 0) { 293 value = treeNode.replaceTokens(value); 294 treeNode.setAttribute(name, value); 295 } 296 } 297 } 298 } 299 300 312 313 protected String replaceTokens(String str) { 314 if (str == null) return str; 315 return replaceVariableWithAttribute(str, "$", "(", ")", ""); 316 } 317 public String replaceVariableWithAttribute(String string, String startToken, 319 String typeDelim, String endToken, String valueForNull) { 320 int startIndex = string.lastIndexOf(startToken); 321 int delimIndex; 322 int endIndex; 323 int startTokenLen = startToken.length(); 324 int delimLen = typeDelim.length(); 325 int endTokenLen = endToken.length(); 326 boolean expressionIsWholeString = false; 327 328 for (; startIndex != -1; startIndex = string.lastIndexOf(startToken)) { 329 if ((startIndex == 0) && (string.endsWith(endToken))) { 334 expressionIsWholeString = true; 336 } 337 338 delimIndex = string.indexOf(typeDelim, startIndex+startTokenLen); 340 if (delimIndex == -1) { 341 continue; 342 } 343 344 endIndex = string.indexOf(endToken, delimIndex+delimLen); 346 if (endIndex == -1) { 347 continue; 348 } 349 if (startIndex+startTokenLen != delimIndex) 352 throw new RuntimeException ("types not support in tree expressions."); 353 354 String variable = string.substring(delimIndex+delimLen, endIndex); 356 357 Object value = getAttribute(variable); 359 if (value == null) { 360 variable = valueForNull; 361 } else if ((value instanceof String ) == false) { 362 variable = value.toString(); 363 } else { 364 variable = (String ) value; 365 } 366 if (expressionIsWholeString) { 367 return variable; 368 } 369 string = string.substring(0, startIndex) + variable + string.substring(endIndex+endTokenLen); } 374 return string; 375 } 376 377 protected void removeDeletedNodes(HashSet newNames) { 378 if (newNames.size() != this.children.size()) { 380 boolean doAgain = true; 382 while (doAgain) { 383 doAgain = false; 384 for (int i=0; i<this.children.size(); i++) { 385 if (newNames.contains(((IndexTreeNode)this.children.get(i)).name) == false) { 386 this.children.remove(i); 387 doAgain = true; 388 break; 389 } 390 } 391 } 392 } 393 if (this.children.size() == 0) { 394 closeNode(RequestManager.getRequestContext()); 395 } 396 } 397 398 400 protected boolean isChildValid(ObjectName objectName) { 401 try { 402 String category = objectName.getKeyProperty("category"); 403 if (category.equalsIgnoreCase("monitor")) 404 return true; 405 String objectType = objectName.getKeyProperty("type"); 406 if (objectType.equals("j2ee-application") || 407 objectType.equals("web-module") || 408 objectType.equals("ejb-module") || 409 objectType.equals("connector-module")) { 410 String type = (String )MBeanUtil.getAttribute(objectName, "object_type"); 411 if (!type.equalsIgnoreCase("user")) { 412 return false; 413 } 414 } 415 } catch (Exception ex) { 416 ex.printStackTrace(); } 418 return true; 419 } 420 421 protected void ensureChildren() { 422 } 424 425 public List getChildren() { 426 ensureChildren(); 427 return children; 428 } 429 430 public String toString() { 431 return "[name=" + getNamePath() + " path=" + getPath() + " hid=" + getHighlightID() + "]"; 432 } 433 434 public String getNamePath() { 435 if (parent!=null) 436 return parent.getNamePath()+"."+name; 437 else 438 return ""+name; 439 } 440 441 public String getPath() { 442 if (parent!=null) 443 return parent.getPath()+"."+id; 444 else 445 return ""+id; 446 } 447 448 public IndexTreeNode getNextSibling() { 449 IndexTreeNode result = null; 450 if (parent!=null) { 451 int index=parent.getChildren().indexOf(this); 452 index++; 453 if (index<parent.getChildren().size()) 454 result=(IndexTreeNode)parent.getChildren().get(index); 455 } 456 return result; 457 } 458 459 public void setIsExpanded(boolean expanded) { 460 if (isExpanded == true && expanded == false) { 462 setRefresh(true); 463 } 464 isExpanded = expanded; 465 } 466 467 public IndexTreeNode getParent() { 468 return parent; 469 } 470 471 public String getHighlightID() { 472 return hid; 473 } 474 475 public String getHighlightIDPath() { 476 String path = hid; 477 IndexTreeNode p = parent; 478 while (p != null) { 479 path += "."+p.getHighlightID(); 480 p = p.parent; 481 } 482 return path; 483 } 484 485 public String getName() { 486 return Util.getMessage(name); 487 } 488 489 public String getType() { 490 return type; 491 } 492 493 public Object getAttribute(String name) { 494 return this.getAttribute(name, true); 495 } 496 497 public Object getAttribute(String name, boolean lookUpHiearchy) { 498 Object obj = getAttributes().get(name); 499 if (lookUpHiearchy == true && obj == null && parent != null) 500 return parent.getAttribute(name); 501 return obj; 502 } 503 504 public void setAttribute(String name, Object value) { 505 attributes.put(name,value); 506 } 507 508 public void setRefresh(boolean refresh) { 509 setAttribute(IndexTreeModel.FIELD_REFRESH, new Boolean (refresh)); 510 } 511 512 public boolean getRefresh() { 513 return ((Boolean )getAttributes().get(IndexTreeModel.FIELD_REFRESH)).booleanValue(); 514 } 515 516 public void setUniqueID(String uniqueID) { 517 setAttribute(IndexTreeModel.FIELD_UNIQUEID, uniqueID); 518 } 519 520 public String getUniqueID() { 521 return (String )getAttributes().get(IndexTreeModel.FIELD_UNIQUEID); 522 } 523 524 public String getDisplayName() { 525 String displayName = (String )getAttributes().get(IndexTreeModel.FIELD_DISPLAYNAME); 528 return (displayName == null)?(getName()):(displayName); 529 } 530 531 public String getLink() { 532 return link; 533 } 534 535 public void setLink(String link) { 536 this.link = link; 537 } 538 539 public void removeAllChildren() { 540 children.clear(); 541 } 542 543 protected Map getAttributes() { 544 return attributes; 545 } 546 547 public String getIconName(boolean expanded) { 548 if (expanded && iconExpanded != null && iconExpanded.equals("")==false) 549 return iconExpanded; 550 if (icon != null) 551 return icon; 552 return "object.gif"; 554 } 555 556 public void setIcon(String icon) { 557 this.icon = icon; 558 } 559 560 public void setIconExpanded(String iconExpanded) { 561 this.iconExpanded = iconExpanded; 562 } 563 564 public void setName(String name) { 565 this.name = name; 566 } 567 568 public int getID() { 569 return id; 570 } 571 572 public IndexTreeModel getModel() { 573 return model; 574 } 575 576 public boolean closeNode(RequestContext rc) { 577 boolean closed = false; 578 TreeViewStateData data = 579 (TreeViewStateData)rc.getRequest().getSession().getAttribute( 580 model.getStateDataName()); 581 if (data == null) 582 return closed; 583 584 if (data.isNodeExpanded(getPath())) { 585 closed = true; 586 data.setNodeExpanded(getPath(), false); 587 } 588 return closed; 589 } 590 591 public boolean openNode(RequestContext rc) { 592 boolean opened = false; 593 TreeViewStateData data = 594 (TreeViewStateData)rc.getRequest().getSession().getAttribute( 595 model.getStateDataName()); 596 if (data == null) 597 return opened; 598 599 IndexTreeNode node = this; 600 while (node != null && node.getParent() != null) { 601 if (data.isNodeExpanded(node.getPath()) == false) { 602 opened = true; 603 data.setNodeExpanded(node.getPath(), true); 604 } 605 node = node.getParent(); 606 } 607 return opened; 608 } 609 610 private void setRequestAttribute(HttpServletRequest req, String attrName) { 611 String attr = (String ) this.getAttribute(attrName); 612 if (attr == null) 613 req.removeAttribute(attrName); 614 else 615 req.setAttribute(attrName, attr); 616 } 617 618 private void setRequestAttributes(HttpServletRequest req) { 619 req.setAttribute(EDIT_KEY_VALUE, name); 620 req.setAttribute(OBJECT_NAME, this.getAttribute(OBJECT_NAME, false)); 621 req.getSession().setAttribute(EDIT_KEY_VALUE, name); 622 623 req.setAttribute("numberOfChildNodes", new Integer (this.getChildren().size())); 625 setRequestAttribute(req, INSTANCE_NAME); 626 setRequestAttribute(req, CONFIG_NAME); 627 setRequestAttribute(req, CLUSTER_NAME); 628 setRequestAttribute(req, NODEAGENT_NAME); 629 setRequestAttribute(req, APPLICATION_TYPE); 630 } 631 632 public static IndexTreeNode validateNode(RequestContext rc, IndexTreeNode node) { 633 String objectName = (String )node.getAttribute(IndexTreeNode.OBJECT_NAME, false); 637 if (objectName == null) return node; 639 boolean valid = MBeanUtil.isValidMBean(objectName); 640 if (valid) 641 return node; 642 IndexTreeNode parent = node.getParent(); 643 parent.setRefresh(true); rc.getRequest().setAttribute("refreshTree", new Boolean ("true")); 645 return validateNode(rc, parent); 647 } 648 649 652 public void handleSelection(RequestContext rc) 653 throws ClassNotFoundException , ServletException, IOException { 654 if (Util.isLoggableFINER()) { 655 Util.logFINER("ENTERING IndexTreeNode.handleSelection('"+name+"')"); 656 } 657 658 IndexTreeNode selectedNode = validateNode(rc, this); 659 model.setCurrentNode(selectedNode); Util.setSelectedNode(selectedNode); 661 selectedNode.setRequestAttributes(rc.getRequest()); 662 663 String linkPage = selectedNode.getLink(); 664 665 if (linkPage != null) { 666 if (linkPage.toLowerCase().endsWith(".html") || 667 linkPage.toLowerCase().endsWith(".jsp")) { 668 rc.getServletContext().getRequestDispatcher( 669 Util.getLocalizedURL(rc, linkPage)). 670 forward( rc.getRequest(), rc.getResponse()); 671 } else { 672 ViewBeanManager mgr = rc.getViewBeanManager(); 673 ViewBean targetView = (ViewBean)mgr.getViewBean(linkPage); 674 if ((targetView instanceof DescriptorViewBeanBase)) { 675 DescriptorViewBeanBase viewBean = ((DescriptorViewBeanBase)targetView); 676 viewBean.setAttributes(getAttributes()); } 678 targetView.forwardTo(rc); 679 } 680 } else { 681 throw new FrameworkException( 682 "The link in the Tree node must not be null!"); 683 } 684 if (Util.isLoggableFINER()) { 685 Util.logFINER("LEAVING IndexTreeNode.handleSelection('"+name+"')"); 686 } 687 } 688 689 public static final String INSTANCE_NAME = "instanceName"; 690 public static final String CONFIG_NAME = "configName"; 691 public static final String CLUSTER_NAME = "clusterName"; 692 public static final String NODEAGENT_NAME = "nodeAgentName"; 693 public static final String EDIT_KEY_VALUE = "editKeyValue"; 694 public static final String OBJECT_NAME = "objectName"; 695 public static final String APPLICATION_TYPE = "ApplicationType"; 696 697 private int id = 0; 698 private IndexTreeNode parent; 699 private String name; 700 private String type; 701 private String hid; 702 private String icon = null; 703 private String iconExpanded = null; 704 private String link = "underConstruction"; 705 protected Element dynamicChild = null; 706 707 protected List children=new ArrayList (); 708 private Map attributes=new HashMap (); 709 protected IndexTreeModel model; 710 protected boolean isExpanded = false; 711 } 712 | Popular Tags |