1 23 24 29 30 package com.sun.enterprise.tools.admingui.handlers; 31 32 import java.util.StringTokenizer ; 33 import java.util.List ; 34 import java.util.Arrays ; 35 import java.util.ArrayList ; 36 import java.util.Map ; 37 import java.util.Vector ; 38 import java.util.Properties ; 39 import java.util.HashMap ; 40 import java.util.Enumeration ; 41 42 import javax.servlet.http.HttpServletRequest ; 43 44 import javax.management.ObjectName ; 45 import javax.management.Attribute ; 46 import javax.management.AttributeList ; 47 48 import com.iplanet.jato.RequestContext; 49 import com.iplanet.jato.RequestManager; 50 import com.iplanet.jato.model.DefaultModel; 51 import com.iplanet.jato.view.*; 52 import com.sun.web.ui.taglib.html.*; 53 import com.iplanet.jato.view.View; 54 import com.iplanet.jato.view.ViewBase; 55 import com.iplanet.jato.view.html.StaticTextField; 56 import com.iplanet.jato.view.html.HREF; 57 import com.iplanet.jato.RequestContext; 58 import com.iplanet.jato.view.html.OptionList; 59 import com.iplanet.jato.view.event.ChildContentDisplayEvent; 60 61 import com.sun.web.ui.model.CCAddRemoveModelInterface; 62 import com.sun.web.ui.view.addremove.CCAddRemove; 63 import com.sun.web.ui.model.CCPropertySheetModelInterface; 64 import com.sun.web.ui.model.CCActionTableModelInterface; 65 66 import com.sun.enterprise.tools.guiframework.view.HandlerContext; 67 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView; 68 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 69 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent; 70 import com.sun.enterprise.tools.guiframework.view.descriptors.CCAddRemoveDescriptor; 71 import com.sun.enterprise.tools.guiframework.view.descriptors.CCPropertySheetDescriptor; 72 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor; 73 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 74 import com.sun.enterprise.tools.guiframework.event.handlers.AttributeHandlers; 75 76 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 77 import com.sun.enterprise.tools.admingui.util.Util; 78 import com.sun.enterprise.tools.admingui.ConfigProperties; 79 80 81 import com.sun.enterprise.admin.servermgmt.RuntimeStatusList; 82 import com.sun.enterprise.admin.servermgmt.RuntimeStatus; 83 import com.sun.enterprise.admin.common.Status; 84 85 86 public class TargetHandlers { 87 88 public void setupTargetsSection(RequestContext ctx, HandlerContext handlerCtx) { 91 ViewDescriptor vd; 92 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 93 vd = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 94 } 95 else { 96 vd = handlerCtx.getViewDescriptor(); 97 } 98 99 if (vd instanceof CCPropertySheetDescriptor) { 100 CCPropertySheetModelInterface model = 101 ((CCPropertySheetDescriptor)vd).getModel(); 102 String targetsSectionName = (String )handlerCtx.getInputValue("targetsSectionName"); 103 Boolean hasTargets = ConfigProperties.getInstance().getTargetSupported(); 104 if (targetsSectionName != null) { 105 model.setVisible(targetsSectionName, hasTargets.booleanValue()); 106 handlerCtx.setOutputValue("hasTargets", hasTargets); 107 } 108 else { 109 handlerCtx.setOutputValue("hasTargets", new Boolean (false)); 110 } 111 } else { 112 throw new RuntimeException ("ViewDesc should be of type property sheet."); 113 } 114 } 115 116 public void setAvailableList(RequestContext ctx, HandlerContext handlerCtx) { 117 ViewDescriptor vd; 118 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 119 vd = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 120 } 121 else { 122 vd = handlerCtx.getViewDescriptor(); 123 } 124 if (vd instanceof CCAddRemoveDescriptor) { 125 ObjectName [] clusters = (ObjectName [])handlerCtx.getInputValue("clusters"); 126 ObjectName [] instances = (ObjectName [])handlerCtx.getInputValue("instances"); 127 String [] names = getAllTargets(clusters, instances); 128 CCAddRemoveModelInterface model = ((CCAddRemoveDescriptor)vd).getModel(); 129 CCAddRemoveDescriptor addRemoveDesc = (CCAddRemoveDescriptor)vd; 130 String defaultInstance = (String )handlerCtx.getInputValue("defaultInstance"); 131 String defaultCluster = (String )handlerCtx.getInputValue("defaultCluster"); 132 String defaultTarget = null; 133 Vector vv = new Vector (Arrays.asList(names)); 134 if (!Util.isEmpty(defaultInstance)){ 135 vv.remove(defaultInstance); 136 defaultTarget = defaultInstance; 137 } 138 if (!Util.isEmpty(defaultCluster)){ 139 vv.remove(defaultCluster); 140 defaultTarget = defaultCluster; 141 } 142 if (defaultTarget != null){ 143 names = new String [vv.size()]; 144 for(int i=0; i<vv.size(); i++){ 145 names[i] = (String ) vv.elementAt(i); 146 } 147 model.setSelectedOptionList(new OptionList(new String []{defaultTarget}, new String []{defaultTarget})); 148 } 149 model.setAvailableOptionList(new OptionList(names, names)); 150 } else { 151 throw new RuntimeException ("Not an AddRemoveComponent!"); 152 153 } 154 } 155 public void setAllTargets(RequestContext ctx, HandlerContext handlerCtx) { 156 ObjectName [] clusters = (ObjectName [])handlerCtx.getInputValue("clusters"); 157 ObjectName [] instances = (ObjectName [])handlerCtx.getInputValue("instances"); 158 159 String [] allTargets = getAllTargets(clusters, instances); 160 handlerCtx.setOutputValue("allTargets", allTargets); 161 } 162 163 private String [] getAllTargets(ObjectName [] clusters, ObjectName [] instances) { 164 int clusterCount = (clusters == null)?(0):(clusters.length); 165 int instanceCount = (instances == null)?(0):(instances.length); 166 167 String [] allTargets = new String [clusterCount + instanceCount]; 168 int i; 169 for (i = 0; i < clusterCount; i++) { 170 allTargets[i] = (String )MBeanUtil.getAttribute(clusters[i], "name"); 171 } 172 for (i = 0; i < instanceCount; i++) { 173 allTargets[i+clusterCount] = (String )MBeanUtil.getAttribute(instances[i], "name"); 174 } 175 return allTargets; 176 } 177 178 public void setSelectedTargets(RequestContext ctx, HandlerContext handlerCtx) { 179 ViewDescriptor vd; 180 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 181 vd = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 182 } 183 else { 184 vd = handlerCtx.getViewDescriptor(); 185 } 186 if (vd instanceof CCAddRemoveDescriptor) { 187 String [] targets = (String [])handlerCtx.getInputValue("targets"); 188 String objectName = (String )handlerCtx.getInputValue("objectName"); 189 Object refName = (Object )handlerCtx.getInputValue("referedTargets"); 190 String key = (String )handlerCtx.getInputValue("key"); 191 if(key == null) 192 key = "name"; 193 194 if (targets == null) { 195 targets = new String [0]; 196 } 197 String [] associatedTargets = null; 198 if(refName == null){ 199 refName = new String [0]; 200 } 201 if (refName instanceof ObjectName []) { 202 associatedTargets = getAssociatedTargets((ObjectName [])refName, key); 203 } else if (refName instanceof String []) { 204 associatedTargets = (String [])refName; 205 } 206 207 targets = getAvailableVirtualServers(targets, associatedTargets); 208 209 CCAddRemoveDescriptor addRemoveDesc = (CCAddRemoveDescriptor)vd; 210 CCAddRemoveModelInterface model = addRemoveDesc.getModel(); 211 212 model.setAvailableOptionList(new OptionList(targets, targets)); 213 model.setSelectedOptionList(new OptionList(associatedTargets, associatedTargets)); 214 } else { 215 throw new RuntimeException ("Not an AddRemoveComponent!"); 216 217 } 218 } 219 220 221 private String [] getAssociatedTargets(ObjectName [] refName, String key) { 222 String [] associatedTargets = (refName != null) ? new String [refName.length]:new String [0]; 223 224 for (int i=0; refName != null && i < refName.length; i++) { 225 associatedTargets[i] = refName[i].getKeyProperty(key); 226 } 227 return associatedTargets; 228 } 229 230 public void setAvailableVirtualServers(RequestContext ctx, HandlerContext handlerCtx) { 231 ViewDescriptor vd; 232 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 233 vd = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 234 } 235 else { 236 vd = handlerCtx.getViewDescriptor(); 237 } 238 if (vd instanceof CCAddRemoveDescriptor) { 239 ObjectName [] virtualServers = (ObjectName [])handlerCtx.getInputValue("virtualServers"); 240 String objectName = (String )handlerCtx.getInputValue("objectName"); 241 int vsCount = (virtualServers == null)?(0):(virtualServers.length); 242 243 String [] availableVS = new String [vsCount]; 244 for (int i = 0; i < vsCount; i++) { 245 availableVS[i] = (String )MBeanUtil.getAttribute(virtualServers[i], "id"); 246 } 247 248 CCAddRemoveDescriptor addRemoveDesc = (CCAddRemoveDescriptor)vd; 249 CCAddRemoveModelInterface model = addRemoveDesc.getModel(); 250 String [] selectedVS = getAssociatedVirtualServers(objectName); 251 252 if (selectedVS == null || selectedVS.length == 0) { 253 selectedVS = availableVS; 254 availableVS = new String [0]; 255 } 256 else { 257 availableVS = getAvailableVirtualServers(availableVS, selectedVS); 258 } 259 model.setAvailableOptionList(new OptionList(availableVS, availableVS)); 260 model.setSelectedOptionList(new OptionList(selectedVS, selectedVS)); 261 } else { 262 throw new RuntimeException ("Not an AddRemoveComponent!"); 263 264 } 265 } 266 private String [] getAvailableVirtualServers(String [] availableVS, String [] selectedVS) { 268 Vector availableVector = new Vector (); 269 boolean selected = false; 270 271 for(int i=0; i < availableVS.length; i++) { 272 for(int j=0; j < selectedVS.length; j++) { 273 if(availableVS[i].equals(selectedVS[j])) { 274 selected = true; 275 break; 276 } 277 } 278 if(!selected) { 279 availableVector.add(availableVS[i]); 280 } 281 selected = false; 282 } 283 availableVS = (String [])availableVector.toArray(new String [availableVector.size()]); 284 return availableVS; 285 } 286 287 private String [] getAssociatedVirtualServers(String objectName) { 288 String str = (String )MBeanUtil.getAttribute(objectName, "virtual-servers"); 289 String [] vs = Util.stringToArray(str, ","); 290 return vs; 291 } 292 293 public void setVirtualServerAttribute(RequestContext ctx, HandlerContext handlerCtx) { 294 String objectName = (String )handlerCtx.getInputValue("objectName"); 295 String [] targets = (String [])handlerCtx.getInputValue("targets"); 296 String virtualServers = Util.concatDelimiter(targets, ","); 297 Attribute attr = new Attribute ("virtual-servers", virtualServers); 298 MBeanUtil.setAttribute(objectName, attr); 299 } 300 301 public void getSelectedTargets(RequestContext ctx, HandlerContext handlerCtx) { 302 String childName = (String )handlerCtx.getInputValue("targetsChildName"); 303 if (childName == null || childName.length() == 0) { 304 return; 305 } 306 307 View view = handlerCtx.getView(); 308 DescriptorContainerView descView = (DescriptorContainerView) 309 (((ViewBase)view).getParentViewBean()); 310 ViewDescriptor vd = descView.getViewDescriptor(); 311 ViewDescriptor desc = vd.getChildDescriptor(childName); 312 if (!(desc instanceof CCAddRemoveDescriptor)) { 313 throw new FrameworkException("addRemoveDescriptor is of wrong type", desc, view); 314 } 315 CCAddRemoveDescriptor addRemoveDesc = ((CCAddRemoveDescriptor)desc); 316 CCAddRemoveModelInterface model = addRemoveDesc.getModel(); 317 CCAddRemove addRemoveChild = (CCAddRemove)addRemoveDesc.getView(ctx); 318 319 324 OptionList selectedOptions = model.getSelectedOptionList(addRemoveChild); 325 OptionList availableOptions = model.getAvailableOptionList(addRemoveChild); 326 if (selectedOptions == null) 327 selectedOptions = model.getSelectedOptionList(); 328 if (availableOptions == null) 329 availableOptions = model.getAvailableOptionList(); 330 331 String [] targets = null; 332 if (selectedOptions != null) { 333 targets = new String [selectedOptions.size()]; 334 for (int i = 0; i < targets.length; i++) { 335 targets[i] = selectedOptions.getValue(i); 336 } 337 } 338 String [] availableTargets = null; 340 if (availableOptions != null) { 341 availableTargets = new String [availableOptions.size()]; 342 for (int i = 0; i < availableTargets.length; i++) { 343 availableTargets[i] = availableOptions.getValue(i); 344 } 345 } 346 handlerCtx.setOutputValue("targets", targets); 347 handlerCtx.setOutputValue("availableTargets", availableTargets); 348 } 349 350 public void createReferences(RequestContext ctx, HandlerContext handlerCtx) { 351 String objectName = (String )handlerCtx.getInputValue("objectName"); 352 String methodName = (String )handlerCtx.getInputValue("methodName"); 353 String key = (String )handlerCtx.getInputValue("key"); 354 String [] targets = (String [])handlerCtx.getInputValue("targets"); 355 String [] types= new String []{"java.lang.String", "boolean", "java.lang.String"}; 356 if (targets != null) { 357 for (int i = 0; i < targets.length; i++) { 358 Object [] params = new Object []{targets[i], new Boolean (true), key}; 359 MBeanUtil.invoke(objectName, methodName, params, types); 360 } 361 } 362 } 363 public void createMBeanWithReferences(RequestContext ctx, HandlerContext handlerCtx) { 365 String objectName = (String )handlerCtx.getInputValue("objectName"); 366 String methodName = (String )handlerCtx.getInputValue("methodName"); 367 ArrayList typesList= (ArrayList )handlerCtx.getInputValue("types"); 368 ArrayList paramsList = (ArrayList )handlerCtx.getInputValue("params"); 369 String [] targets = (String [])handlerCtx.getInputValue("targets"); 370 String [] types = (String [])typesList.toArray(new String [typesList.size()]); 371 Boolean isTargetSupported = ConfigProperties.getInstance().getTargetSupported(); 372 373 if(targets == null || targets.length == 0) { 374 targets = new String []{"server"}; 376 } 377 378 Map attrsMap = (Map ) paramsList.get(0); 379 380 if(isTargetSupported){ 381 String enabled = (String )attrsMap.get("EEenabled"); 382 if (Util.isEmpty(enabled)) enabled="true"; 383 attrsMap.put("enabled", enabled); 384 }else{ 385 String enabled = (String )attrsMap.get("PEenabled"); 386 if (Util.isEmpty(enabled)) enabled="true"; 387 attrsMap.put("enabled", enabled); 388 } 389 attrsMap.remove("PEenabled"); 390 attrsMap.remove("EEenabled"); 391 for (int i = 0; i < targets.length; i++) { 392 paramsList.add(0, targets[i]); 393 Object [] params = paramsList.toArray(); 394 MBeanUtil.invoke(objectName, methodName, params, types); 395 paramsList.remove(0); 396 String objName="com.sun.appserv:type=application-ref,category=config,ref=" + attrsMap.get("name")+ ",server="+ targets[i]; 397 Attribute attr = new Attribute ("enabled", attrsMap.get("enabled")); 399 MBeanUtil.setAttribute(objName, attr); 400 } 401 402 if(! Util.isEmpty( (String ) attrsMap.get("description"))){ 404 Attribute desc = new Attribute ("description", attrsMap.get("description")); 405 MBeanUtil.setAttribute("com.sun.appserv:type=mbean,category=config,name="+attrsMap.get("name"), desc); 406 } 407 408 } 409 410 public void deleteReferences(RequestContext ctx, HandlerContext handlerCtx) { 411 String objectName = (String )handlerCtx.getInputValue("objectName"); 412 String methodName = (String )handlerCtx.getInputValue("methodName"); 413 String key = (String )handlerCtx.getInputValue("key"); 414 String [] targets = (String [])handlerCtx.getInputValue("availableTargets"); 415 String [] types= new String []{"java.lang.String", "java.lang.String"}; 416 if (targets != null) { 417 for (int i = 0; i < targets.length; i++) { 418 Object [] params = new Object []{targets[i],key}; 419 MBeanUtil.invoke(objectName, methodName, params, types); 420 } 421 } 422 } 423 424 public void deleteLBReferences(RequestContext ctx, HandlerContext handlerCtx) { 425 String objectName = (String )handlerCtx.getInputValue("objectName"); 426 String methodName = (String )handlerCtx.getInputValue("methodName"); 427 String key = (String )handlerCtx.getInputValue("key"); 428 String [] deleteTargets = (String [])handlerCtx.getInputValue("availableTargets"); 429 String [] types= new String []{"java.lang.String"}; 430 431 if(deleteTargets != null) { 432 for (int i = 0; i < deleteTargets.length; i++) { 433 Object params[] = {deleteTargets[i]}; 434 MBeanUtil.invoke(objectName, methodName, params, types); 435 } 436 } 437 } 438 439 public void createLBReferences(RequestContext ctx, HandlerContext handlerCtx) { 440 String objectName = (String )handlerCtx.getInputValue("objectName"); 441 String methodName = (String )handlerCtx.getInputValue("methodName"); 442 String key = (String )handlerCtx.getInputValue("key"); 443 String [] targets = (String [])handlerCtx.getInputValue("targets"); 444 Object objName = null; 445 if (targets != null ) { 446 for (int i = 0; i < targets.length; i++) { 447 String [] types2= new String []{"java.lang.String"}; 448 Object params2[] = {targets[i]}; 449 try { 450 objName = MBeanUtil.invoke(objectName, "getClusterRefByRef", params2, types2); 451 } catch (Exception ex) { 452 } 454 if(objName == null) { 455 String [] types= new String []{"javax.management.AttributeList"}; 456 AttributeList attrList = new AttributeList (); 457 attrList.add(new Attribute ("ref", targets[i])); 458 Object params[] = {attrList}; 459 MBeanUtil.invoke(objectName, methodName, params, types); 460 } 461 462 } 463 } 464 } 465 466 467 public void getRequiredTargets(RequestContext ctx, HandlerContext handlerCtx) { 506 Object objectName = (Object )handlerCtx.getInputValue("ObjectName"); 507 String [] targets = (String [])handlerCtx.getInputValue("availableTargets"); String [] allTargets = (String [])handlerCtx.getInputValue("targetList"); 509 String key = (String )handlerCtx.getInputValue("key"); 510 HashMap availableTargetsList = Util.stringArrayToHashMap(targets); 511 Vector deleteTargets = new Vector (); 512 Vector createTargets = new Vector (); 513 if(key == null) 514 key = "name"; 515 String [] instanceName = null; 516 if (objectName instanceof ObjectName []) { 517 instanceName = getAssociatedTargets((ObjectName [])objectName, key); 518 } else if (objectName instanceof String []) { 519 instanceName = (String [])objectName; 520 } 521 HashMap instancesList = Util.stringArrayToHashMap(instanceName); 522 523 525 for (int i=0; allTargets != null && i < allTargets.length; i++) { 526 if(availableTargetsList.containsKey(allTargets[i]) && instancesList.containsKey(allTargets[i])) { 528 deleteTargets.add(allTargets[i]); 529 } 530 if(!availableTargetsList.containsKey(allTargets[i]) && !instancesList.containsKey(allTargets[i])) { 532 createTargets.add(allTargets[i]); 533 } 534 } 535 handlerCtx.setOutputValue("deleteTargets", (String [])deleteTargets.toArray(new String [deleteTargets.size()])); 536 handlerCtx.setOutputValue("createTargets", (String [])createTargets.toArray(new String [createTargets.size()])); 537 } 538 539 public void beginDisplayTargetPlaceHolder(RequestContext ctx, HandlerContext handlerCtx) { 540 StaticTextField field = (StaticTextField)handlerCtx.getView(); 541 field.setValue(".TARGETVALUE."); 542 } 543 544 public void beginDisplayTargetsInTable(RequestContext ctx, HandlerContext handlerCtx) { 545 HREF href = (HREF)handlerCtx.getView(); 546 href.setValue(".TARGETVALUE."); 547 } 548 549 public String endDisplayTargetsInTable(RequestContext ctx, HandlerContext handlerCtx) { 550 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 551 return null; 552 } 553 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 554 String content = dispEvent.getContent(); 555 ObjectName [] targets = (ObjectName [])handlerCtx.getInputValue("targets"); 556 String key = (String )handlerCtx.getInputValue("key"); 557 if(key == null) 558 key = "name"; 559 560 String hrefs = ""; 561 if (targets != null) { 562 for (int i = 0; i < targets.length; i++) { 563 if (i > 0) 564 hrefs += "<br />"; 565 566 String objectType = targets[i].getKeyProperty("type"); 567 String isCluster = (objectType.equalsIgnoreCase("cluster"))?"true":"false"; 568 String statusIcon = getStatusIconHtml(targets[i]); 569 570 String tmp = content.replaceFirst(".TARGETVALUE.", 571 (String )MBeanUtil.getAttribute(targets[i], key)+"&isCluster="+isCluster); 572 hrefs += statusIcon + tmp.replaceAll(".TARGETVALUE.", 573 (String )MBeanUtil.getAttribute(targets[i], key)); 574 } 575 } 576 return hrefs; 577 } 578 579 public String endDisplayLBsInTable(RequestContext ctx, HandlerContext handlerCtx) { 580 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 581 return null; 582 } 583 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 584 String content = dispEvent.getContent(); 585 String [] targets = (String [])handlerCtx.getInputValue("targets"); 586 ObjectName [] lbs = (ObjectName [])handlerCtx.getInputValue("lbs"); 587 String key = (String )handlerCtx.getInputValue("key"); 588 if(key == null) 589 key = "name"; 590 String hrefs = ""; 591 592 if (targets != null && lbs != null) { 593 for (int i = 0; i < targets.length; i++) { 594 String lbConfig = ""; 595 String name = ""; 596 if (i > 0) 597 hrefs += "<br />"; 598 for(int j = 0; j < lbs.length; j++){ 599 lbConfig = (String )MBeanUtil.getAttribute(lbs[j].toString(), "lb-config-name"); 600 if(targets[i].equals(lbConfig)) { 601 name = lbs[j].getKeyProperty("name"); 602 } 603 } 604 605 String tmp = content.replaceFirst(".TARGETVALUE.", 606 name); 607 hrefs += tmp.replaceAll(".TARGETVALUE.", 608 name); 609 } 610 } 611 return hrefs; 612 } 613 614 public void getStatusCounts(RequestContext ctx, HandlerContext handlerCtx) { 615 ObjectName [] instances = (ObjectName [])handlerCtx.getInputValue("instances"); 616 int running = 0; 617 int restart = 0; 618 int stopped = 0; 619 620 if (instances != null) { 621 for (int i = 0; i < instances.length; i++) { 622 Object sts = null; 623 try { 624 sts = MBeanUtil.invoke(instances[i], "getRuntimeStatus", null, null); 625 } catch (Exception ex) { 626 } 628 if (sts != null && sts instanceof RuntimeStatus) { 629 boolean restartNeeded = ((RuntimeStatus)sts).isRestartNeeded(); 630 Status s = ((RuntimeStatus)sts).getStatus(); 631 switch (s.getStatusCode()){ 632 case Status.kInstanceStartingCode: 633 case Status.kInstanceRunningCode: 634 running++; 635 if (restartNeeded) 636 restart++; 637 break; 638 case Status.kInstanceStoppingCode: 639 case Status.kInstanceNotRunningCode: 640 stopped++; 641 break; 642 } 643 } 644 } 645 } 646 handlerCtx.setOutputValue("running", new Integer (running)); 647 handlerCtx.setOutputValue("restart", new Integer (restart)); 648 handlerCtx.setOutputValue("stopped", new Integer (stopped)); 649 } 650 651 public static String getStatusIconHtml(ObjectName obj) { 652 if (obj == null) 653 return ""; 654 Object sts = null; 655 try { 656 sts = MBeanUtil.invoke(obj, "getRuntimeStatus", null, null); 657 } catch (Exception ex) { 658 } 660 return getStatusIconHtml(sts); 661 } 662 663 public static String getStatusHtml(Object sts) { 664 if (sts == null) { 665 return getStatusIconHtml(-1, false, null) + 666 Util.getMessage("serverinst.unknown"); 667 } 668 else if (sts instanceof RuntimeStatus) { 669 boolean restartNeeded = ((RuntimeStatus)sts).isRestartNeeded(); 670 Status s = ((RuntimeStatus)sts).getStatus(); 671 int statusCode = s.getStatusCode(); 672 String statusString = ""; 673 switch (statusCode) { 674 case Status.kInstanceStartingCode: 675 case Status.kInstanceRunningCode: { 676 if (restartNeeded) { 677 statusString = Util.getMessage("serverinst.restart"); 678 } else { 679 statusString = Util.getMessage("serverinst.running"); 680 } 681 break; 682 } 683 case Status.kInstanceStoppingCode: 684 case Status.kInstanceNotRunningCode: { 685 statusString = Util.getMessage("serverinst.notRunning"); 686 break; 687 } 688 } 689 return getStatusIconHtml(statusCode, restartNeeded, null) + statusString; 690 } else if (sts instanceof RuntimeStatusList) { 691 RuntimeStatusList stsList = (RuntimeStatusList) sts; 692 if (stsList.anyRunning()) 693 return getStatusIconHtml(Status.kInstanceRunningCode, false, stsList.toString()) + stsList.toString(); 694 else 695 return getStatusIconHtml(Status.kInstanceNotRunningCode, false, stsList.toString()) + stsList.toString(); 696 } 697 return ""; 698 } 699 700 public static String getStatusIconHtml(Object sts) { 701 if (sts == null) { 702 return getStatusIconHtml(-1, false, null); 703 } 704 else if (sts instanceof RuntimeStatus) { 705 boolean restartNeeded = ((RuntimeStatus)sts).isRestartNeeded(); 706 Status s = ((RuntimeStatus)sts).getStatus(); 707 int statusCode = s.getStatusCode(); 708 return getStatusIconHtml(statusCode, restartNeeded, null); 709 } 710 else if (sts instanceof RuntimeStatusList) { 711 RuntimeStatusList stsList = (RuntimeStatusList) sts; 712 if (stsList.anyRunning()) 713 return getStatusIconHtml(Status.kInstanceRunningCode, false, stsList.toString()); 714 else 715 return getStatusIconHtml(Status.kInstanceNotRunningCode, false, stsList.toString()); 716 } 717 return "<img SRC=\"" + blankGif + "\" border=\"0\" alt=\"\"/> "; 718 } 720 721 private static final String blankGif = 722 "/com_sun_web_ui/images/other/dot.gif\" width=\"11\" height=\"11"; 723 724 public static String getStatusIconHtml(int statusCode, boolean restartNeeded, 725 String altMessage) { 726 if (altMessage != null && altMessage.length() == 0) 727 altMessage = null; 728 switch (statusCode) { 729 case Status.kInstanceStartingCode: 730 case Status.kInstanceRunningCode: { 731 if (restartNeeded) { 732 String restartGif = Util.getMessage("serverinst.restartGif"); 733 return "<img SRC=\"" + 734 ((restartGif==null || restartGif.length()==0)?(blankGif):(restartGif)) + 735 "\" border=\"0\" alt=\"" + 736 ((altMessage==null)?(Util.getMessage("serverinst.restart")):altMessage) + 737 "\" /> "; 738 } else { 739 String runningGif = Util.getMessage("serverinst.runningGif"); 740 return "<img SRC=\"" + 741 ((runningGif==null || runningGif.length()==0)?(blankGif):(runningGif)) + 742 "\" border=\"0\" alt=\"" + 743 ((altMessage==null)?Util.getMessage("serverinst.running"):altMessage) + 744 "\" /> "; 745 } 746 } 747 case Status.kInstanceStoppingCode: 748 case Status.kInstanceNotRunningCode: { 749 String stoppedGif = Util.getMessage("serverinst.stoppedGif"); 750 return "<img SRC=\"" + 751 ((stoppedGif==null || stoppedGif.length()==0)?(blankGif):(stoppedGif)) + 752 "\" border=\"0\" alt=\"" + 753 ((altMessage==null)?Util.getMessage("serverinst.notRunning"):altMessage) + 754 "\" /> "; 755 } 756 case -1: { 757 String unknownGif = Util.getMessage("serverinst.unknownGif"); 758 return "<img SRC=\"" + 759 ((unknownGif==null || unknownGif.length()==0)?(blankGif):(unknownGif)) + 760 "\" border=\"0\" alt=\"" + 761 ((altMessage==null)?Util.getMessage("serverinst.unknown"):altMessage) + 762 "\" /> "; 763 764 } 765 case Status.kEntityEnabledCode: 766 case Status.kEntityDisabledCode: 767 default: 768 return "<img SRC=\"" + blankGif + "\" border=\"0\" alt=\"\"/> "; 769 } 771 } 772 773 public void disableButtons(RequestContext ctx, HandlerContext handlerCtx) { 774 775 Integer rCount = (Integer )handlerCtx.getInputValue("RunningCount"); 776 Integer sCount = (Integer )handlerCtx.getInputValue("StoppedCount"); 777 if (rCount == null || sCount == null) 778 throw new IllegalArgumentException ( 779 "The parameter map did not contain 'StatusCount'!"); 780 int r = ((Integer )rCount).intValue(); 781 int s = ((Integer )sCount).intValue(); 782 783 if(r == 0) { 784 handlerCtx.setOutputValue("runngingStatus", "true"); 785 } else { 786 handlerCtx.setOutputValue("runngingStatus", "false"); 787 } 788 if(s == 0) { 789 handlerCtx.setOutputValue("stoppedStatus", "true"); 790 } else { 791 handlerCtx.setOutputValue("stoppedStatus", "false"); 792 } 793 } 794 795 796 public String formatStatusCount(RequestContext ctx, HandlerContext handlerCtx) { 797 Object obj = handlerCtx.getInputValue("count"); 798 if (obj == null) 799 throw new RuntimeException ("null count in formatStatusCount!"); 800 String count = obj.toString(); 801 if (count.trim().equals("0")) { 802 if (blankGif == null || blankGif.length() == 0) 803 return " --"; 804 else 805 return "<img SRC=\"" + blankGif + "\" border=\"0\" alt=\"\" />--"; 806 } 807 808 String icon = (String )handlerCtx.getInputValue("icon"); 809 if (icon == null) 810 throw new RuntimeException ("ICON is null in formatStatusCount!"); 811 String s = ""; 812 if (icon.equals("stopped")) 813 s = getStatusIconHtml(Status.kInstanceStoppingCode, false, null) + " " +count; 814 else if (icon.equals("restart")) 815 s = getStatusIconHtml(Status.kInstanceRunningCode, true, null) + " " + count; 816 else if (icon.equals("running")) 817 s = getStatusIconHtml(Status.kInstanceRunningCode, false, null) + " " + count; 818 return s; 819 } 820 821 public String getObjectStatus(RequestContext ctx, HandlerContext handlerCtx) { 822 Object sts = null; 823 String objectName = (String ) handlerCtx.getInputValue("objectName"); 824 825 try { 828 String objectType = new ObjectName (objectName).getKeyProperty("type"); 829 if (objectType.equals("node-agent")) { 830 String [] types = new String []{"java.lang.String"}; 831 Object [] params = new Object []{"rendezvousOccurred"}; 832 String result = (String )MBeanUtil.invoke( 833 objectName, "getPropertyValue", params, types); 834 if (result.equalsIgnoreCase("false")) { 835 String status = Util.getMessage("nodeAgent.awaitingInitialSync"); 836 String stoppedGif = Util.getMessage("serverinst.stoppedGif"); 837 if (stoppedGif != null && stoppedGif.length() != 0) { 838 status = "<img SRC=\"" + stoppedGif + 839 "\" border=\"0\" alt=\"" + status + "\" /> " + 840 status; 841 } 842 handlerCtx.setOutputValue("status", status); 843 return status; 844 } 845 } 846 } catch (Exception ex) { 847 } 849 850 try { 851 sts = MBeanUtil.invoke(objectName, "getRuntimeStatus", null, null); 852 } catch (Exception ex) { 853 } 855 String status = getStatusHtml(sts); 856 if (status.indexOf("dot.gif") > 0) { 858 int i = status.indexOf(";"); if (i > 0) 860 status = status.substring(i+1); 861 } 862 handlerCtx.setOutputValue("status", status); 863 return status; 864 } 865 866 public String isRunning(RequestContext ctx, HandlerContext handlerCtx) { 867 String status = "false"; 868 String objectName = (String ) handlerCtx.getInputValue("objectName"); 869 try { 870 Object sts = MBeanUtil.invoke(objectName, "getRuntimeStatus", null, null); 871 if (sts != null && sts instanceof RuntimeStatus) { 872 Status s = ((RuntimeStatus)sts).getStatus(); 873 int statusCode = s.getStatusCode(); 874 switch (statusCode) { 875 case Status.kInstanceStartingCode: 876 case Status.kInstanceRunningCode: 877 status = "true"; 878 break; 879 default: 880 status = "false"; 881 break; 882 } 883 } 884 } catch (Exception ex) { 885 } 887 handlerCtx.setOutputValue("status", status); 888 return status; 889 } 890 891 public String getClusterStatus(RequestContext ctx, HandlerContext handlerCtx) { 892 int restart = 0; 893 int running = 0; 894 int stopped = 0; 895 int unknown = 0; 896 897 String status = Util.getMessage("serverinst.noInstances"); String clusterObjectName = (String ) handlerCtx.getInputValue("clusterObjectName"); 899 Object objs = MBeanUtil.invoke(clusterObjectName, "listServerInstances", null, null); 900 if (objs != null && objs instanceof ObjectName []) { 901 ObjectName [] instances = (ObjectName [])objs; 902 for (int i=0; i<instances.length; i++) { 903 Object sts = null; 904 try { 905 sts = MBeanUtil.invoke(instances[i], "getRuntimeStatus", null, null); 906 } catch (Exception ex) { 907 } 909 if (sts != null && sts instanceof RuntimeStatus) { 910 boolean restartNeeded = ((RuntimeStatus)sts).isRestartNeeded(); 911 Status s = ((RuntimeStatus)sts).getStatus(); 912 int statusCode = s.getStatusCode(); 913 switch (statusCode) { 914 case Status.kInstanceStartingCode: 915 case Status.kInstanceRunningCode: 916 running++; 917 if (restartNeeded) 918 restart++; 919 break; 920 case Status.kInstanceStoppingCode: 921 case Status.kInstanceNotRunningCode: 922 stopped++; 923 break; 924 } 925 } else { 926 unknown ++; 927 } 928 } 929 if (instances.length > 0) { 930 status = "" + running + Util.getMessage("serverinst.runningInstances"); 931 if (stopped > 0) 932 status += "<br />" + stopped + Util.getMessage("serverinst.stoppedInstances"); 933 if (restart > 0) 934 status += "<br />" + restart + Util.getMessage("serverinst.restartInstances"); 935 if (unknown > 0) 936 status += "<br />" + unknown + Util.getMessage("serverinst.unknown"); 937 } 938 } 939 handlerCtx.setOutputValue("status", status); 940 return status; 941 } 942 943 public boolean isTargetSupported(RequestContext ctx, HandlerContext handlerCtx) { 944 Boolean isTargetSupported = 945 ConfigProperties.getInstance().getTargetSupported(); 946 handlerCtx.setOutputValue("isTargetSupported", isTargetSupported); 947 return isTargetSupported.booleanValue(); 948 } 949 950 public String endDisplayInstanceStatus(RequestContext ctx, HandlerContext handlerCtx) { 951 ObjectName [] instances = (ObjectName [])handlerCtx.getInputValue("instances"); 952 String method = (String ) handlerCtx.getInputValue("methodName"); 953 954 String result = ""; 955 if (instances != null) { 956 for (int i = 0; i < instances.length; i++) { 957 if (i > 0) { 958 result = result + "<br />"; 959 } 960 Object value = MBeanUtil.invoke(instances[i], method, null, null); 961 if (value != null) { 962 if (value instanceof RuntimeStatus) 963 result += ((RuntimeStatus)value).toShortString().replaceAll(" ", " "); 964 else 965 result += value.toString().replaceAll(" ", " "); 966 } 967 } 968 } 969 return result; 970 } 971 972 public void resourceRefAction(RequestContext ctx, HandlerContext handlerCtx) { 973 View view = handlerCtx.getView(); 974 DescriptorContainerView descView = (DescriptorContainerView) 975 (((ViewBase)view).getParentViewBean()); 976 ViewDescriptor vd = descView.getViewDescriptor(); 977 String tableChildName = (String )handlerCtx.getInputValue("tableChildName"); 979 ViewDescriptor tableDescriptor = vd.getChildDescriptor(tableChildName); 980 if (tableDescriptor == null) { 981 throw new FrameworkException("tableDescriptor is null", vd, view); 982 } 983 if (!(tableDescriptor instanceof CCActionTableDescriptor)) { 984 throw new FrameworkException("tableDescriptor is of wrong type", 985 tableDescriptor, view); 986 } 987 CCActionTableModelInterface model = (CCActionTableModelInterface)handlerCtx.getInputValue("propertiesModel"); 988 if (model == null) { 989 throw new FrameworkException("PropertiesHandler.getModel: Parameter 'propertiesModel' not specified"); 990 } 991 String editKeyValue = (String )handlerCtx.getInputValue("editKeyValue"); 992 String action = (String )handlerCtx.getInputValue("action"); 993 String methodName = (String )handlerCtx.getInputValue("methodName"); 994 996 model.setRowSelectionType("multiple"); 997 try { 998 model.beforeFirst(); 999 while(model.next()) { 1000 if (model.isRowSelected()) { 1001 String objectName = (String )model.getValue("objectName"); 1002 ObjectName resRefObjectName = (ObjectName )MBeanUtil.invoke(objectName, 1003 methodName, new Object []{editKeyValue}, new String []{"java.lang.String"}); 1004 changeEnableStatus(resRefObjectName, action); 1005 model.setRowSelected(false); 1006 } 1007 } 1008 } catch (Exception ex) { 1009 throw new FrameworkException("Error while enabling: '"+ 1010 vd.getName()+"'", ex, vd, null); 1011 } 1012 ContainerViewBase containerView = 1013 (ContainerViewBase)(tableDescriptor.getView(ctx).getParent()); 1014 containerView.removeChild(tableDescriptor.getName()); 1015 ((DefaultModel)model).clear(); 1016 } 1017 1018 public static void changeEnableStatus(ObjectName resRefObjectName, String action){ 1019 Attribute attr = new Attribute ("enabled", action); 1020 MBeanUtil.setAttribute(resRefObjectName, attr); 1021 if(action.equals("true")){ 1022 String editKeyValue = resRefObjectName.getKeyProperty("ref"); 1024 changeResourceAttribute(editKeyValue, attr); 1025 } 1026 } 1027 1028 static private void changeResourceAttribute(String resName, Attribute attr){ 1029 try{ 1030 String resType = (String ) MBeanUtil.invoke( 1031 "com.sun.com.sun.appserv:type=resources,category=config", 1032 "getResourceType", 1033 new String [] {resName}, 1034 new String [] {"java.lang.String"}); 1035 ObjectName obj = new ObjectName ("com.sun.com.sun.appserv:category=config,type="+resType+",jndi-name="+resName); 1036 MBeanUtil.setAttribute(obj, attr); 1037 }catch(javax.management.MalformedObjectNameException ex){ 1038 throw new FrameworkException("error in TargetHandlers changeEnableStatus :", ex); 1039 } 1040 } 1041 1042 public void changeResourceStatusAll(RequestContext ctx, HandlerContext handlerCtx) { 1043 View view = handlerCtx.getView(); 1044 DescriptorContainerView descView = (DescriptorContainerView) 1045 (((ViewBase)view).getParentViewBean()); 1046 ViewDescriptor vd = descView.getViewDescriptor(); 1047 String childName = (String )vd.getParameter("tableChildName"); 1048 1049 if (childName == null) { 1050 throw new FrameworkException("childName not specified", vd, view); 1051 } 1052 ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName); 1053 if (tableDescriptor == null) { 1054 throw new FrameworkException("tableDescriptor is null", vd, view); 1055 } 1056 if (!(tableDescriptor instanceof CCActionTableDescriptor)) { 1057 throw new FrameworkException("tableDescriptor is of wrong type", 1058 tableDescriptor, view); 1059 } 1060 String newStatus = (String )handlerCtx.getInputValue("new-status"); 1061 String key = (String )handlerCtx.getInputValue("key"); 1062 doAction(((CCActionTableDescriptor)tableDescriptor).getModel(), tableDescriptor, key, newStatus); 1063 } 1064 1065 static private void doAction(CCActionTableModelInterface model, ViewDescriptor vd, String key, String newStatus) { 1066 RequestContext ctx = RequestManager.getRequestContext(); 1067 Attribute attr = new Attribute ("enabled", newStatus); 1068 try{ 1069 model.setRowSelectionType("multiple"); 1070 model.beforeFirst(); 1071 while(model.next()) { 1073 if (model.isRowSelected()) { 1074 String resName = (String ) model.getValue(key); 1075 if (newStatus.equals("true")){ 1077 MBeanUtil.setAttribute((String )model.getValue("objectName"), attr); 1078 } 1080 String [] params = new String [] {resName}; 1081 String [] types = new String [] {"java.lang.String"}; 1082 boolean isTargetSupported = ConfigProperties.getInstance().getTargetSupported().booleanValue(); 1083 if (!isTargetSupported){ 1084 MBeanUtil.setAttribute("com.sun.appserv:type=resource-ref,server=server,category=config,ref="+resName, attr); 1085 }else{ 1086 ObjectName [] targets = (ObjectName []) MBeanUtil.invoke("com.sun.appserv:type=resources,category=config" , "listReferencees", params, types); 1087 if (targets == null) 1088 continue; 1089 for(int i=0; i<targets.length; i++){ 1090 ObjectName resRef = (ObjectName )MBeanUtil.invoke(targets[i], "getResourceRefByRef", params, types); 1091 MBeanUtil.setAttribute(resRef, attr); 1092 } 1093 } 1094 model.setRowSelected(false); 1095 } 1096 } 1097 }catch (Exception ex){ 1098 throw new FrameworkException("error in changeResourceStatus ", ex); 1099 } 1100 View view = vd.getView(ctx).getParent(); 1101 ContainerViewBase descView = (ContainerViewBase)view; 1102 descView.removeChild(vd.getName()); 1103 ((DefaultModel)model).clear(); 1104 } 1105 1106 1108 public void getDefaultTarget(RequestContext ctx, HandlerContext handlerCtx) { 1109 handlerCtx.setOutputValue("defaultTarget", 1110 ConfigProperties.getInstance().getDefaultTarget()); 1111 } 1112 1113 public void beginResourceEditTabsDisplay(RequestContext ctx, HandlerContext handlerCtx) { 1114 HREF href = (HREF)handlerCtx.getView(); 1115 href.setValue(href.getValue() + ".RESOURCENAME."); 1116 } 1117 1118 public String endResourceEditTabsDisplay(RequestContext ctx, HandlerContext handlerCtx) { 1119 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 1120 return null; 1121 } 1122 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 1123 String content = dispEvent.getContent(); 1124 content = content.replaceAll(".RESOURCENAME.", 1125 "&name="+ctx.getRequest().getAttribute("editKeyValue")); 1126 return content; 1127 } 1128 1129 public void beginEditTabsDisplay(RequestContext ctx, HandlerContext handlerCtx) { 1130 HREF href = (HREF)handlerCtx.getView(); 1131 href.setValue(href.getValue() + ".PLACEHOLDER."); 1132 } 1133 1134 public String endEditTabsDisplay(RequestContext ctx, HandlerContext handlerCtx) { 1135 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 1136 return null; 1137 } 1138 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 1139 String content = dispEvent.getContent(); 1140 String key = (String )handlerCtx.getInputValue("key"); 1141 if (key==null) 1142 key="name"; 1143 content = content.replaceAll(".PLACEHOLDER.", 1144 "&"+ key + "=" + ctx.getRequest().getAttribute(key)); 1145 return content; 1146 } 1147 1148 public String endClusteredInstanceTabsDisplay(RequestContext ctx, HandlerContext handlerCtx) { 1149 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 1150 return null; 1151 } 1152 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 1153 String content = dispEvent.getContent(); 1154 String key = (String )handlerCtx.getInputValue("key"); 1155 if (key==null) 1156 key="name"; 1157 content = content.replaceAll(".PLACEHOLDER.", 1158 "&"+ key + "=" + ctx.getRequest().getAttribute(key) + "&isCluster=true"); 1159 return content; 1160 } 1161} 1162 | Popular Tags |