1 23 24 29 30 package com.sun.enterprise.tools.admingui.handlers; 31 32 import java.util.Properties ; 33 import java.util.ArrayList ; 34 import java.util.Map ; 35 import java.util.Vector ; 36 import java.util.Iterator ; 37 import java.util.Set ; 38 import javax.management.ObjectName ; 39 import javax.management.AttributeList ; 40 import javax.management.Attribute ; 41 import javax.management.j2ee.statistics.CountStatistic ; 42 import javax.management.j2ee.statistics.TimeStatistic ; 43 44 45 import com.iplanet.jato.RequestContext; 46 import com.iplanet.jato.RequestManager; 47 import com.iplanet.jato.view.View; 48 import com.iplanet.jato.view.ViewBase; 49 import com.sun.web.ui.model.CCActionTableModelInterface; 50 import com.iplanet.jato.view.ContainerView; 51 import com.iplanet.jato.view.ContainerViewBase; 52 import com.iplanet.jato.view.html.SelectableGroup; 53 import com.iplanet.jato.view.html.OptionList; 54 import com.iplanet.jato.model.DefaultModel; 55 import com.iplanet.jato.view.event.ChildContentDisplayEvent; 56 import com.sun.web.ui.model.CCActionTableModel; 57 import com.iplanet.jato.model.ModelControlException; 58 import com.iplanet.jato.view.html.*; 59 import com.iplanet.jato.util.NonSyncStringBuffer; 60 61 import com.sun.enterprise.tools.guiframework.view.event.BeforeCreateEvent; 62 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 63 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 64 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView; 65 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle; 66 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor; 67 import com.sun.enterprise.tools.guiframework.view.HandlerContext; 68 import com.sun.enterprise.tools.guiframework.view.DescriptorCCActionTable; 69 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 70 import com.sun.enterprise.tools.admingui.util.Util; 71 import com.sun.enterprise.tools.admingui.util.AMXUtil; 72 import com.sun.appserv.management.j2ee.statistics.StringStatistic; 73 import com.sun.appserv.management.helper.LBConfigHelper; 74 75 import com.sun.enterprise.deployment.phasing.ApplicationConfigHelper; 76 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils; 77 78 import com.sun.appserv.management.monitor.statistics.LoadBalancerServerStats; 79 import com.sun.appserv.management.monitor.statistics.LoadBalancerContextRootStats; 80 import com.sun.appserv.management.base.QueryMgr; 81 import com.sun.appserv.management.config.LBConfig; 82 83 84 87 public class LoadBalancerHandlers { 88 89 public void setLbProperties(RequestContext ctx, HandlerContext handlerCtx) { 90 91 String objectName = (String ) handlerCtx.getInputValue("objectName"); 92 String methodName = (String ) handlerCtx.getInputValue("methodName"); 93 AttributeList properties = (AttributeList )handlerCtx.getInputValue("properties"); 94 if(properties == null){ 95 properties = new AttributeList (); 96 } 97 Object [] params = null; 98 String [] types = {"javax.management.Attribute"}; 99 100 for (int i = 0; i < properties.size(); i++) { 101 Attribute attr = (Attribute )properties.get(i); 102 try { 103 MBeanUtil.invoke(objectName, methodName, new Object [] {attr}, types); 104 }catch (Exception ex) { 105 106 Util.logINFO("Could not set property for load balancer:"+ex.getMessage()); 107 108 } 109 } 110 } 111 112 public void getLbProperties(RequestContext ctx, HandlerContext handlerCtx) { 113 String objectName = (String )handlerCtx.getInputValue("objectName"); 114 String methodName = (String )handlerCtx.getInputValue("methodName"); 115 String propName = (String )handlerCtx.getInputValue("propName"); 116 String [] types = {"java.lang.String"}; 117 String propValue = ""; 118 try { 119 propValue = (String )MBeanUtil.invoke(objectName, methodName, new Object [] {propName}, types); 120 } catch (Exception ex) { 121 } 123 124 handlerCtx.setOutputValue("value", propValue); 125 126 127 } 128 129 public void getTargetList(RequestContext ctx, HandlerContext handlerCtx) { 130 ObjectName [] clusterNames = (ObjectName [])handlerCtx.getInputValue("clusterRefs"); 131 ObjectName [] serverNames = (ObjectName [])handlerCtx.getInputValue("serverRefs"); 132 String [] targets = null; 133 boolean serverList = false; 134 boolean clusterList = false; 135 if(serverNames != null && serverNames.length >0){ 136 serverList = true; 137 } 138 if(clusterNames != null && clusterNames.length >0){ 139 clusterList = true; 140 } 141 if(serverList && clusterList){ 142 targets = new String [clusterNames.length+serverNames.length]; 143 for (int i=0; clusterNames != null && i < clusterNames.length; i++) { 144 targets[i] = clusterNames[i].getKeyProperty("ref"); 145 } 146 147 148 for (int i=0; serverNames != null && i < serverNames.length; i++) { 149 int start = clusterNames.length; 150 targets[start+i] = serverNames[i].getKeyProperty("ref"); 151 } 152 153 } else if(serverList && !clusterList){ 154 targets = new String [serverNames.length]; 155 for (int i=0; serverNames != null && i < serverNames.length; i++) { 156 targets[i] = serverNames[i].getKeyProperty("ref"); 157 } 158 159 } else if(!serverList && clusterList) { 160 targets = new String [clusterNames.length]; 161 for (int i=0; clusterNames != null && i < clusterNames.length; i++) { 162 targets[i] = clusterNames[i].getKeyProperty("ref"); 163 } 164 } 165 166 handlerCtx.setOutputValue("value", targets); 167 if(targets != null){ 168 handlerCtx.setOutputValue("hasValue", "true"); 169 } 170 171 } 172 173 public void createLBReferences(RequestContext ctx, HandlerContext handlerCtx) { 174 String objectName = (String )handlerCtx.getInputValue("objectName"); 175 String methodName = (String )handlerCtx.getInputValue("methodName"); 176 String key = (String )handlerCtx.getInputValue("key"); 177 String [] targets = (String [])handlerCtx.getInputValue("targets"); 178 String [] refs = (String [])handlerCtx.getInputValue("refs"); 179 if(targets != null){ 180 for(int i = 0; i < targets.length; i++){ 181 boolean foundMatch = false; 182 if(refs != null){ 183 for(int j = 0; j < refs.length; j++){ 184 if(targets[i].equals(refs[j])) { 186 foundMatch = true; 187 } 188 } 189 190 } 191 if(!foundMatch || refs == null) { 192 if(isCluster(targets[i])) { 193 String [] types= new String []{"javax.management.AttributeList"}; 194 AttributeList attrList = new AttributeList (); 195 attrList.add(new Attribute ("ref", targets[i])); 196 Object params[] = {attrList}; 197 MBeanUtil.invoke(objectName, "createClusterRef", params, types); 198 } else { 199 String [] types= new String []{"javax.management.AttributeList"}; 200 AttributeList attrList = new AttributeList (); 201 attrList.add(new Attribute ("ref", targets[i])); 202 Object params[] = {attrList}; 203 MBeanUtil.invoke(objectName, "createServerRef", params, types); 204 } 205 } 206 207 } 208 } 209 } 210 211 public void deleteLBReferences(RequestContext ctx, HandlerContext handlerCtx) { 212 String objectName = (String )handlerCtx.getInputValue("objectName"); 213 String methodName = (String )handlerCtx.getInputValue("methodName"); 214 String key = (String )handlerCtx.getInputValue("key"); 215 String [] deleteTargets = (String [])handlerCtx.getInputValue("availableTargets"); 216 String [] types= new String []{"java.lang.String"}; 217 218 if(deleteTargets != null) { 219 for (int i = 0; i < deleteTargets.length; i++) { 220 Object params[] = {deleteTargets[i]}; 221 if(isCluster(deleteTargets[i])){ 222 223 MBeanUtil.invoke(objectName, methodName, params, types); 224 } else { 225 226 MBeanUtil.invoke(objectName, "removeServerRefByRef", params, types); 227 } 228 229 230 } 231 } 232 } 233 234 public void deleteClusterLBReferences(RequestContext ctx, HandlerContext handlerCtx) { 235 String methodName = (String )handlerCtx.getInputValue("methodName"); 236 String cluster = (String )handlerCtx.getInputValue("cluster"); 237 String [] lbs = (String [])handlerCtx.getInputValue("lbs"); 238 String [] types= new String []{"java.lang.String"}; 239 if(lbs == null) 240 return; 241 242 for (int i = 0; i < lbs.length; i++) { 243 Object params[] = {cluster}; 244 try { 245 MBeanUtil.invoke("com.sun.appserv:type=lb-config,name="+lbs[i]+",category=config", methodName, params, types); 246 } catch (Exception ex) { 247 throw new FrameworkException( 248 "Error while deleting lb ref for '"+cluster+ 249 "'!", ex, handlerCtx.getViewDescriptor(), 250 handlerCtx.getView()); 251 } 252 } 253 } 254 255 public void createLoadBalancer(RequestContext ctx, HandlerContext handlerCtx) { 256 String lbName = (String )handlerCtx.getInputValue("lbName"); 257 Object refName = (Object )handlerCtx.getInputValue("refs"); 258 String [] targets = {""}; 259 if (refName instanceof String ) { 260 String name = (String )refName; 261 targets[0] = name; 262 } else if(refName instanceof String []) { 263 targets = (String [])refName; 264 } 265 266 try { 267 Map params = null; 268 LBConfigHelper helper = new LBConfigHelper(AMXUtil.getDomainRoot()); 269 helper.createLoadbalancer(lbName, true, targets, params); 270 } catch (Exception ex) { 271 if (Util.isLoggableFINE()) { 272 Util.logFINE(ex); 273 } 274 } 275 } 276 277 private boolean isCluster(String name) { 278 Object objName = null; 279 280 String [] types= new String []{"java.lang.String"}; 281 Object params[] = {name}; 282 try { 283 objName = MBeanUtil.invoke("com.sun.appserv:type=clusters,category=config", "getClusterByName", params, types); 284 } catch (Exception ex) { 285 } 287 if(objName != null) { 288 return new Boolean ("true").booleanValue(); 289 } else { 290 return new Boolean ("false").booleanValue(); 291 } 292 293 } 294 295 public void isCluster(RequestContext ctx, HandlerContext handlerCtx) { 296 String name = (String )handlerCtx.getInputValue("serverName"); 297 Object objName = null; 298 if (name != null ) { 299 String [] types= new String []{"java.lang.String"}; 300 Object params[] = {name}; 301 try { 302 objName = MBeanUtil.invoke("com.sun.appserv:type=clusters,category=config", "getClusterByName", params, types); 303 } catch (Exception ex) { 304 } 306 if(objName != null) { 307 handlerCtx.setOutputValue("isCluster", "true"); 308 } else { 309 handlerCtx.setOutputValue("isCluster", "false"); 310 } 311 312 313 } 314 } 315 316 public void getAttributeForClusterRef(RequestContext ctx, HandlerContext handlerCtx) { 317 String [] targets = (String [])handlerCtx.getInputValue("targets"); 318 String cluster = (String )handlerCtx.getInputValue("clusterName"); 319 String attrName = (String )handlerCtx.getInputValue("attrName"); 320 String attrValue = ""; 321 if (targets != null && attrName != null){ 322 for (int i = 0; i < targets.length; i++) { 323 attrValue = (String )MBeanUtil.getAttribute("com.sun.appserv:type=cluster-ref,ref="+cluster+",lb-config="+targets[i]+",category=config" 324 , attrName); 325 } 326 327 handlerCtx.setOutputValue("attrValue", attrValue); 328 } 329 } 330 331 public void getAttributeForLB(RequestContext ctx, HandlerContext handlerCtx) { 332 String objectName = (String )handlerCtx.getInputValue("objectName"); 333 String attrName = (String )handlerCtx.getInputValue("attrName"); 334 if (attrName==null) 335 attrName="lb-config-name"; 336 Object value = null; 337 value = MBeanUtil.getAttribute(objectName, attrName); 338 String result = null; 339 if (value != null) { 340 if (value instanceof String ) { 341 result = (String )value; 342 } else { 343 result = value.toString(); 344 } 345 } 346 handlerCtx.setOutputValue("attrValue", result); 347 348 349 } 350 351 public void setAttributeForLB(RequestContext ctx, HandlerContext handlerCtx) { 352 String [] targets = (String [])handlerCtx.getInputValue("targets"); 353 String cluster = (String )handlerCtx.getInputValue("clusterName"); 354 String policy = (String )handlerCtx.getInputValue("policy"); 355 String module = (String )handlerCtx.getInputValue("module"); 356 if (targets != null && policy != null){ 357 for (int i = 0; i < targets.length; i++) { 358 MBeanUtil.setAttribute("com.sun.appserv:type=cluster-ref,ref="+cluster+",lb-config="+targets[i]+",category=config" 359 , new Attribute ("lb-policy", policy)); 360 if(module != null){ 361 MBeanUtil.setAttribute("com.sun.appserv:type=cluster-ref,ref="+cluster+",lb-config="+targets[i]+",category=config", new Attribute ("lb-policy-module", module)); 362 363 } 364 } 365 366 367 } 368 } 369 370 371 public void setAttribute(RequestContext ctx, HandlerContext handlerCtx) { 372 String objectName = (String )handlerCtx.getInputValue("objectName"); 373 if (objectName == null) { 374 throw new FrameworkException("LB Handler: setAttribute: Parameter 'objectName' not specified"); 375 } 376 String propName = (String )handlerCtx.getInputValue("name"); 377 if (propName == null) { 378 throw new FrameworkException("LB Handler: setAttribute: Parameter 'name' not specified"); 379 } 380 String propValue = (String )handlerCtx.getInputValue("value"); 381 if (propValue == null) { 382 throw new FrameworkException("LB Handler: setAttribute: Parameter 'value' not specified"); 383 } 384 MBeanUtil.setAttribute(objectName, new Attribute (propName, propValue)); 385 386 } 387 388 public void setConfigMonitoringEnabled(RequestContext ctx, HandlerContext handlerCtx) { 389 String objectName = (String )handlerCtx.getInputValue("objectName"); 390 if (objectName == null) { 391 throw new FrameworkException("LB Handler: setConfigMonitoringEnabled: Parameter 'objectName' not specified"); 392 } 393 String propName = (String )handlerCtx.getInputValue("name"); 394 String propValue = (String )handlerCtx.getInputValue("value"); 395 if (propValue == null) { 396 throw new FrameworkException("LB Handler: setConfigMonitoringEnabled: Parameter 'value' not specified"); 397 } 398 399 Set <LBConfig> lbConfigSet = null; 400 QueryMgr queryMgr = AMXUtil.getQueryMgr(); 401 try { 402 lbConfigSet = queryMgr.queryPatternSet(new ObjectName (objectName)); 403 } catch (javax.management.MalformedObjectNameException e) { 404 e.printStackTrace(); 405 } 406 for(LBConfig lbconfig : lbConfigSet){ 407 lbconfig.setMonitoringEnabled(new Boolean (propValue)); 409 } 410 } 411 412 413 public String endEditTabsDisplay(RequestContext ctx, HandlerContext handlerCtx) { 414 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 415 return null; 416 } 417 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 418 String content = dispEvent.getContent(); 419 String key = (String )handlerCtx.getInputValue("key"); 420 String key2 = (String )handlerCtx.getInputValue("key2"); 421 if (key==null) 422 key="editKeyValue"; 423 if (key2==null) 424 key2="lbKey"; 425 content = content.replaceAll(".PLACEHOLDER.", 426 "&"+ key + "=" + ctx.getRequest().getAttribute(key) +"&"+ key2 + "=" + ctx.getRequest().getAttribute(key2)); 427 return content; 428 } 429 430 public void saveHealthChecker(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 431 String lbConfigName = (String )handlerCtx.getInputValue("lbConfigName"); 432 if (lbConfigName == null) { 433 throw new FrameworkException( 434 "lbConfigName not specified in saveHealthChecker."); 435 } 436 437 String methodName = (String )handlerCtx.getInputValue("methodName"); 438 if (methodName == null) { 439 methodName="createHealthChecker"; 440 } 441 442 CCActionTableModelInterface model = 443 (CCActionTableModelInterface)handlerCtx.getInputValue("lbTargetsModel"); 444 447 if (model == null) { 448 throw new FrameworkException("PropertiesHandler.getModel: Parameter 'propertiesModel' not specified"); 449 } 450 model.beforeFirst(); 452 453 while(model.next()) { 454 AttributeList attrs = new AttributeList (); 455 String name = null; 456 Object objName = null; 457 String interval = ""; 458 String timeout = ""; 459 String url = ""; 460 461 interval = (String ) model.getValue("interval"); 462 timeout = (String ) model.getValue("timeout"); 463 url = (String ) model.getValue("url"); 464 name = (String )model.getValue("name"); 465 String [] type = new String []{"javax.management.AttributeList"}; 466 467 468 if (!Util.isEmpty(interval)) { 469 attrs.add(new Attribute ("interval-in-seconds", interval)); 470 if(!Util.isEmpty(timeout)) 471 attrs.add(new Attribute ("timeout-in-seconds", timeout)); 472 if(!Util.isEmpty(interval)) 473 attrs.add(new Attribute ("url", url)); 474 if(!Util.isEmpty(name)) 475 476 if(!Util.isEmpty(url) && !Util.isEmpty(timeout)) { 477 String objectName = null; 478 String target = null; 479 if(isCluster(name)) { 480 objectName = "com.sun.appserv:type=cluster-ref,ref="+name+",lb-config="+lbConfigName+",category=config"; 481 target = "cluster"; 482 483 } else { 484 objectName = "com.sun.appserv:type=server-ref,ref="+name+",lb-config="+lbConfigName+",category=config"; 485 target = "server"; 486 487 } 488 Object [] params = new Object []{attrs}; 489 490 String objectName2 = "com.sun.appserv:type=health-checker,"+target+"-ref="+name+",lb-config="+lbConfigName+",category=config"; 491 if (MBeanUtil.isValidMBean(objectName2)){ 492 try { 493 MBeanUtil.setAttributes(objectName2, attrs); 494 } catch (Exception ex) { 495 throw new FrameworkException( 496 "Error while setting attributes on MBean '"+objectName+ 497 "'!", ex, handlerCtx.getViewDescriptor(), 498 handlerCtx.getView()); 499 } 500 } else { 501 MBeanUtil.invoke(objectName, methodName, params, type); 502 } 503 } 504 } 505 } 506 } 507 508 509 public void saveInstanceWeight(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 510 CCActionTableModelInterface model = 511 (CCActionTableModelInterface)handlerCtx.getInputValue("lbTargetsModel"); 512 515 if (model == null) { 516 throw new FrameworkException("PropertiesHandler.getModel: Parameter 'propertiesModel' not specified"); 517 } 518 model.setRowSelectionType("multiple"); 519 520 model.beforeFirst(); 521 522 while(model.next()) { 523 String weight = ""; 524 String name = ""; 525 name = (String ) model.getValue("name"); 526 weight = (String ) model.getValue("weight"); 527 Attribute attr = null; 528 if(!Util.isEmpty(name) && !Util.isEmpty(weight)) { 529 attr = new Attribute ("lb-weight", weight); 530 try { 531 MBeanUtil.setAttribute("com.sun.appserv:type=server,name="+name+",category=config", attr); 532 } catch (Exception ex) { 533 throw new FrameworkException( 534 "Error while setting lb weight on '"+name+ 535 "'!", ex, handlerCtx.getViewDescriptor(), 536 handlerCtx.getView()); 537 } 538 } 539 540 } 541 } 542 543 544 public void getHealthChecker(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException { 545 String lbConfigName = (String )handlerCtx.getInputValue("lbConfigName"); 546 if (lbConfigName == null) { 547 throw new FrameworkException( 548 "lbConfigName not specified in saveHealthChecker."); 549 } 550 String [] targets = (String [])handlerCtx.getInputValue("targets"); 551 CCActionTableModelInterface model = 552 (CCActionTableModelInterface)handlerCtx.getInputValue("lbTargetsModel"); 553 554 if (model == null) { 555 throw new FrameworkException("PropertiesHandler.getModel: Parameter 'propertiesModel' not specified"); 556 } 557 String Interval = ""; 559 String Timeout = ""; 560 String URL = ""; 561 String name = null; 562 563 ((DefaultModel)model).clear(); 564 model.beforeFirst(); 565 if (targets != null) { 567 for (int i = 0; i < targets.length; i++) { 568 model.appendRow(); 569 String target = null; 571 if(isCluster(targets[i])) { 572 target = "cluster"; 573 } else { 574 target = "server"; 575 576 } 577 String objectName = "com.sun.appserv:type=health-checker,"+target+"-ref="+targets[i]+",lb-config="+lbConfigName+",category=config"; 578 try { 579 Interval = (String )MBeanUtil.getAttribute(objectName, "interval-in-seconds"); 580 Timeout = (String )MBeanUtil.getAttribute(objectName, "timeout-in-seconds"); 581 URL = (String )MBeanUtil.getAttribute(objectName, "url"); 582 if(!isEmpty(URL)) 583 model.setValue("url", URL); 584 if(!isEmpty(Timeout)) 585 model.setValue("timeout", Timeout); 586 if(!isEmpty(Interval)) 587 model.setValue("interval", Interval); 588 } catch (Exception ex) { 589 } 591 if(!isEmpty(targets[i])) 592 model.setValue("name", targets[i]); 593 594 } 595 } 596 } 597 598 public void executeMethodOnInstance(RequestContext ctx, HandlerContext handlerCtx) { 599 View view = handlerCtx.getView(); DescriptorContainerView descView = 603 (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 604 ViewDescriptor vd = descView.getViewDescriptor(); 605 String childName = (String )handlerCtx.getInputValue("tableChildName"); 606 String objectName = (String )handlerCtx.getInputValue("objectName"); 607 if(childName == null) { 608 throw new FrameworkException("executeMethod: childName not specified", vd, view); 609 } 610 611 ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName); 612 if (tableDescriptor == null) { 613 throw new FrameworkException("executeMethod: tableDescriptor is null", vd, view); 614 } 615 if(!(tableDescriptor instanceof CCActionTableDescriptor)) { 616 throw new FrameworkException( 617 "executeMethod: tableDescriptor is of wrong type", tableDescriptor, view); 618 } 619 CCActionTableModelInterface model = 620 ((CCActionTableDescriptor)tableDescriptor).getModel(); 621 String method = (String )handlerCtx.getInputValue("method"); 622 if (method == null) { 623 throw new FrameworkException("executeMethod: method name is null", vd, view); 624 } 625 626 model.setRowSelectionType("multiple"); 627 String [] types = {"java.lang.String"}; 629 try { 630 model.beforeFirst(); 631 while(model.next()) { 632 if (model.isRowSelected()) { 633 if(objectName == null) 634 objectName = (String )model.getValue("objectName"); 635 if (objectName == null) 636 continue; String instanceName = (String ) model.getValue("name"); 638 MBeanUtil.invoke(objectName, method, new Object [] {instanceName}, types); 639 model.setRowSelected(false); 640 } 641 } 642 } catch (Exception ex) { 643 throw new FrameworkException("Error while invoking '" + method + 644 "' on " + objectName, ex, tableDescriptor, view); 645 } 646 ContainerViewBase containerView = 647 (ContainerViewBase)(tableDescriptor.getView(ctx).getParent()); 648 containerView.removeChild(tableDescriptor.getName()); 649 ((DefaultModel)model).clear(); 650 } 651 652 public void getInstancesforQuiesce(RequestContext ctx, HandlerContext handlerCtx) { 653 View view = handlerCtx.getView(); DescriptorContainerView descView = 657 (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 658 ViewDescriptor vd = descView.getViewDescriptor(); 659 String childName = (String )handlerCtx.getInputValue("tableChildName"); 660 if(childName == null) { 661 throw new FrameworkException("executeMethod: childName not specified", vd, view); 662 } 663 664 ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName); 665 if (tableDescriptor == null) { 666 throw new FrameworkException("executeMethod: tableDescriptor is null", vd, view); 667 } 668 if(!(tableDescriptor instanceof CCActionTableDescriptor)) { 669 throw new FrameworkException( 670 "executeMethod: tableDescriptor is of wrong type", tableDescriptor, view); 671 } 672 CCActionTableModelInterface model = 673 ((CCActionTableDescriptor)tableDescriptor).getModel(); 674 675 model.setRowSelectionType("multiple"); 676 try { 677 model.beforeFirst(); 678 String instances = ""; 679 while(model.next()) { 680 if (model.isRowSelected()) { 681 if(instances.equals("")) { 682 instances += (String ) model.getValue("name"); 683 684 } else { 685 instances += "@"+(String ) model.getValue("name"); 686 } 687 model.setRowSelected(false); 688 } 689 } 690 handlerCtx.setOutputValue("value", instances); 691 } catch (Exception ex) { 692 throw new FrameworkException("setQuiescevalues: error getting instance names from table"); 693 } 694 ContainerViewBase containerView = 695 (ContainerViewBase)(tableDescriptor.getView(ctx).getParent()); 696 containerView.removeChild(tableDescriptor.getName()); 697 ((DefaultModel)model).clear(); 698 } 699 700 public void getLB(RequestContext ctx, HandlerContext handlerCtx) { 701 String [] targets = (String [])handlerCtx.getInputValue("targets"); 702 ObjectName [] lbs = (ObjectName [])handlerCtx.getInputValue("lbs"); 703 String key = (String )handlerCtx.getInputValue("key"); 704 if(key == null) 705 key = "name"; 706 707 if (targets != null && lbs != null) { 708 for (int i = 0; i < targets.length; i++) { 709 String lbConfig = ""; 710 String name = ""; 711 for(int j = 0; j < lbs.length; j++){ 712 lbConfig = (String )MBeanUtil.getAttribute(lbs[j].toString(), "lb-config-name"); 713 if(targets[i].equals(lbConfig)) { 714 name = lbs[j].getKeyProperty("name"); 715 } 716 if(!name.equals("")){ 717 handlerCtx.setOutputValue("value", name); 718 return; 719 } 720 } 721 722 } 723 } 724 } 725 726 public void beginQuiesce(RequestContext ctx, HandlerContext handlerCtx) { 727 String instances = (String )handlerCtx.getInputValue("instances"); 728 String lbName = (String )handlerCtx.getInputValue("lbName"); 729 String cluster = (String )handlerCtx.getInputValue("clusterName"); 730 String lbConfig = (String )handlerCtx.getInputValue("lbConfig"); 731 String [] targets = null; 732 if (Util.isEmpty(instances)){ 733 }else{ 735 targets = instances.split("@"); 736 737 } 738 if(targets != null) { 739 for(int i = 0; i< targets.length; i++){ 740 if(cluster != null) { 741 MBeanUtil.setAttribute("com.sun.appserv:type=server-ref,ref="+targets[i]+",cluster="+cluster+",category=config", new Attribute ("lb-enabled", false)); 742 743 } else { 744 MBeanUtil.setAttribute("com.sun.appserv:type=server-ref,ref="+targets[i]+",lb-config="+lbConfig+",category=config", new Attribute ("lb-enabled", false)); 745 746 } 747 } 748 } 749 750 } 751 752 public void stopInstances(RequestContext ctx, HandlerContext handlerCtx) { 753 String instances = (String )handlerCtx.getInputValue("instances"); 754 String [] targets = null; 755 if (Util.isEmpty(instances)){ 756 }else{ 758 targets = instances.split("@"); 759 } 760 try { 761 762 if(targets != null) { 763 for(int i = 0; i< targets.length; i++){ 764 Boolean isRunning = null; 765 isRunning = (Boolean )MBeanUtil.invoke("com.sun.appserv:type=server,name="+targets[i]+",category=config", "isRunning", null, null); 766 if(isRunning.booleanValue()){ 767 MBeanUtil.invoke("com.sun.appserv:type=server,name="+targets[i]+",category=config", "stop", null, null); 768 } 769 770 } 771 } 772 } catch (Exception ex) { 773 throw new FrameworkException("Stop Instances: Error while stopping instances", ex, handlerCtx.getViewDescriptor(), 774 handlerCtx.getView()); 775 } 776 777 778 } 779 780 805 806 public String endDisplayLBTargetsInTable(RequestContext ctx, HandlerContext handlerCtx) { 807 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 808 return null; 809 } 810 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 811 String content = dispEvent.getContent(); 812 String [] targets = (String [])handlerCtx.getInputValue("targets"); 813 String key = (String )handlerCtx.getInputValue("key"); 814 if(key == null) 815 key = "name"; 816 817 String hrefs = ""; 818 if (targets != null) { 819 for (int i = 0; i < targets.length; i++) { 820 if (i > 0) 821 hrefs += "<br />"; 822 823 boolean isCluster = isCluster(targets[i]); 825 String target = null; 826 if(isCluster){ 827 target = "cluster"; 828 } else { 829 target = "serverInstance"; 830 } 831 832 833 String tmp = content.replaceFirst(".TARGETVALUE.", 834 targets[i]+"&nextPage="+target); 835 hrefs += tmp.replaceAll(".TARGETVALUE.", 836 targets[i]); 837 } 838 } 839 return hrefs; 840 } 841 842 public String endDisplayInstanceStatus(RequestContext ctx, HandlerContext handlerCtx) { 843 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 844 return null; 845 } 846 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 847 String content = dispEvent.getContent(); 848 String instances = (String )handlerCtx.getInputValue("instances"); 849 String lbName = (String )handlerCtx.getInputValue("lbName"); 850 851 String text = ""; 852 String [] targets = null; 853 if (Util.isEmpty(instances)){ 854 }else{ 856 targets = instances.split("@"); 858 859 } 863 if (targets != null && lbName != null) { 864 for (int i = 0; i < targets.length; i++) { 865 if (i > 0) 866 text += "<br />"; 867 868 String [] types= new String []{"java.lang.String"}; 869 Object params[] = {targets[i]}; 870 String status = null; 871 try { 872 873 status = (String )MBeanUtil.invoke("amx:j2eeType=X-LoadBalancer,name="+lbName, "getStatus", params, types); 874 } catch (Exception ex) { 875 throw new FrameworkException("Error while getting instance: '"+lbName, ex, handlerCtx.getViewDescriptor(), 876 handlerCtx.getView()); 877 } 878 879 880 881 String tmp = content.replaceFirst(".TARGETVALUE.", 882 targets[i]+": "+status); 883 text += tmp.replaceAll(".TARGETVALUE.", 884 targets[i]+": "+status); 885 } 886 } 887 888 return text; 889 } 890 891 public String endDisplayQuiesceTimeout(RequestContext ctx, HandlerContext handlerCtx) { 892 if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) { 893 return null; 894 } 895 ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent(); 896 String content = dispEvent.getContent(); 897 String instances = (String )handlerCtx.getInputValue("instances"); 898 String cluster = (String )handlerCtx.getInputValue("cluster"); 899 String lbConfig = (String )handlerCtx.getInputValue("lbConfig"); 900 String text = ""; 901 String [] targets = null; 902 if (Util.isEmpty(instances)){ 903 return ""; 904 }else{ 905 targets = instances.split("@"); 906 } 907 908 if(targets != null) { 909 for(int i = 0; i< targets.length; i++){ 910 if (i > 0) 911 text += "<br />"; 912 String timeout = null; 913 914 try { 915 if(cluster != null) { 916 timeout = (String )MBeanUtil.getAttribute("com.sun.appserv:type=server-ref,ref="+targets[i]+",cluster="+cluster+",category=config", "disable-timeout-in-minutes"); 917 918 } else { 919 timeout = (String )MBeanUtil.getAttribute("com.sun.appserv:type=server-ref,ref="+targets[i]+",lb-config="+lbConfig+",category=config", "disable-timeout-in-minutes"); 920 921 } 922 } catch (Exception ex) { 923 throw new FrameworkException("Error while getting quiesce timeout: '"+targets[i], ex, handlerCtx.getViewDescriptor(), 924 handlerCtx.getView()); 925 } 926 String tmp = content.replaceFirst(".TARGETVALUE.", 927 targets[i]+": "+timeout); 928 text += tmp.replaceAll(".TARGETVALUE.", 929 targets[i]+": "+timeout); 930 931 } 932 933 } 934 return text; 935 } 936 937 public void populateLbFilterMenu(RequestContext ctx, HandlerContext handlerCtx) { 938 String lbName = (String )handlerCtx.getInputValue("lbName"); 940 if(lbName == null) { 941 throw new FrameworkException("populateFilterMenu: lbName not specified"); 942 } 943 SelectableGroup dropDownChild = (SelectableGroup) handlerCtx.getView(); 944 OptionList optionList = new OptionList(); 945 LBConfigHelper helper = new LBConfigHelper(AMXUtil.getDomainRoot()); 946 String [] targets = helper.listTargets(lbName); 947 if (targets == null) 948 return; 949 OptionList dropDownMenuOptions = new OptionList(targets, targets); dropDownChild.setOptions(dropDownMenuOptions); 951 if (targets.length > 0) { 952 dropDownChild.setValue(targets[0]); 953 } 954 } 955 956 public void listTargets(RequestContext ctx, HandlerContext handlerCtx) { 957 String lbName = (String )handlerCtx.getInputValue("lbName"); 959 if(lbName == null) { 960 throw new FrameworkException("listTargets: lbName not specified"); 961 } 962 LBConfigHelper helper = new LBConfigHelper(AMXUtil.getDomainRoot()); 963 String [] targets = helper.listTargets(lbName); 964 if (targets == null){ 965 return; 966 } 967 for(int i = 0; i < targets.length; i++){ 968 } 969 handlerCtx.setOutputValue("value", targets); 970 handlerCtx.setOutputValue("hasValue", "true"); 971 972 } 973 974 975 public void populateLbAppsFilterMenu(RequestContext ctx, HandlerContext handlerCtx) { 976 String lbName = (String )handlerCtx.getInputValue("lbName"); 978 String target = (String )handlerCtx.getInputValue("target"); 979 if(lbName == null) { 980 throw new FrameworkException("populateFilterAppsMenu: lbName not specified"); 981 } 982 if(target == null) { 983 return; 984 } 985 SelectableGroup dropDownChild = (SelectableGroup) handlerCtx.getView(); 986 OptionList optionList = new OptionList(); 987 String [] targets = null; 989 boolean isCluster = true; 990 try { 991 targets = (String [])MBeanUtil.invoke("com.sun.appserv:type=cluster,name="+target+",category=config", "listApplicationReferencesAsString", null, null); 992 } catch (Exception ex) { 993 isCluster = false; 995 } 996 if(!isCluster) { 997 try { 998 targets = (String [])MBeanUtil.invoke("com.sun.appserv:type=server,name="+target+",category=config", "listApplicationReferencesAsString", null, null); 999 } catch (Exception ex) { 1000 isCluster = true; 1002 } 1003 } 1004 Vector ctxRootList = new Vector (); 1005 for(int i = 0; i < targets.length; i++){ 1006 String [] ctxRoot = null; 1007 try { 1008 ctxRoot = ApplicationConfigHelper.getAppContextRoots(DeploymentServiceUtils.getConfigContext(),targets[i]); 1009 } catch (Exception ex) { 1010 throw new FrameworkException( 1011 "Error while getting context roots for '"+targets[i]+ 1012 "'!", ex, handlerCtx.getViewDescriptor(), 1013 handlerCtx.getView()); 1014 } 1015 if(ctxRoot != null){ 1016 for(int j = 0; j < ctxRoot.length; j++){ 1017 ctxRootList.add(ctxRoot[j]); 1018 } 1019 } 1020 1021 } 1022 String [] ctxroot = (String [])ctxRootList.toArray(new String [ctxRootList.size()]); 1023 OptionList dropDownMenuOptions = new OptionList(ctxroot, ctxroot); dropDownChild.setOptions(dropDownMenuOptions); 1025 if (ctxroot.length > 0) { 1026 dropDownChild.setValue(ctxroot[0]); 1027 } 1028 } 1029 1030 1031 public void loadInstanceStats(RequestContext ctx, HandlerContext handlerCtx){ 1032 View view = handlerCtx.getView(); 1033 ViewDescriptor desc = null; 1034 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 1035 desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 1036 } else { 1037 DescriptorContainerView descView = (DescriptorContainerView) 1038 (((ViewBase)view).getParentViewBean()); 1039 desc = descView.getViewDescriptor(); 1040 } 1041 String lbName = (String )handlerCtx.getInputValue("lbName"); 1042 if(lbName == null) { 1043 throw new FrameworkException("populateFilterMenu: lbName not specified"); 1044 } 1045 String target = (String )handlerCtx.getInputValue("target"); 1046 boolean allTargets = false; 1047 if(target == null) { 1048 allTargets = true; 1049 } 1050 CCActionTableModelInterface model =(CCActionTableModelInterface)handlerCtx.getInputValue("model"); 1051 if (model == null) { 1052 throw new FrameworkException("loadInstanceStats: No Model Specified.", 1053 desc, handlerCtx.getView()); 1054 } 1055 try { 1056 LBConfigHelper helper = new LBConfigHelper(AMXUtil.getDomainRoot()); 1057 Map stats = helper.getInstanceStats(lbName, target, allTargets); 1058 if(stats == null) 1059 return; 1060 Set s = stats.keySet(); 1061 Iterator iter = s.iterator(); 1062 ((DefaultModel)model).clear(); 1063 model.beforeFirst(); 1064 while (iter.hasNext()) { 1066 Object key = iter.next(); 1067 LoadBalancerServerStats oneRow = (LoadBalancerServerStats)stats.get(key); 1068 StringStatistic health = (StringStatistic)oneRow.getHealth(); 1069 CountStatistic aReqs = (CountStatistic )oneRow.getNumberOfActiveRequests(); 1070 CountStatistic tReqs = (CountStatistic )oneRow.getNumberOfActiveRequests(); 1071 model.appendRow(); 1072 model.setValue("target", key); 1073 model.setValue("health", health.getCurrent()); 1074 model.setValue("activeReqs", aReqs.getCount()); 1075 model.setValue("totalReqs", tReqs.getCount()); 1076 } 1077 } catch (Exception ex) { 1078 return; 1080 } 1081 1082 } 1083 1084 public void loadAppStats(RequestContext ctx, HandlerContext handlerCtx){ 1085 View view = handlerCtx.getView(); 1086 ViewDescriptor desc = null; 1087 if (handlerCtx.getEvent() instanceof BeforeCreateEvent) { 1088 desc = ((BeforeCreateEvent)handlerCtx.getEvent()).getViewDescriptor(); 1089 } else { 1090 DescriptorContainerView descView = (DescriptorContainerView) 1091 (((ViewBase)view).getParentViewBean()); 1092 desc = descView.getViewDescriptor(); 1093 } 1094 1095 String lbName = (String )handlerCtx.getInputValue("lbName"); 1096 String contextRoot = (String )handlerCtx.getInputValue("contextRoot"); 1097 if(contextRoot != null && contextRoot.startsWith("/")) { 1098 String converted = contextRoot.substring(1, contextRoot.length()); 1099 contextRoot = converted; 1100 } 1101 if(lbName == null) { 1102 throw new FrameworkException("loadAppStats: lbName not specified"); 1103 } 1104 String target = (String )handlerCtx.getInputValue("target"); 1105 boolean allTargets = false; 1106 if(target == null) { 1107 allTargets = true; 1108 } 1109 CCActionTableModelInterface model =(CCActionTableModelInterface)handlerCtx.getInputValue("model"); 1110 if (model == null) { 1111 throw new FrameworkException("loadAppStats: No Model Specified.", 1112 desc, handlerCtx.getView()); 1113 } 1114 try { 1115 LBConfigHelper helper = new LBConfigHelper(AMXUtil.getDomainRoot()); 1116 Map stats = helper.getInstanceStats(lbName, contextRoot, target, allTargets); 1117 if(stats == null) 1118 return; 1119 Set s = stats.keySet(); 1120 Iterator iter = s.iterator(); 1121 ((DefaultModel)model).clear(); 1122 model.beforeFirst(); 1123 while (iter.hasNext()) { 1125 Object key = iter.next(); 1126 LoadBalancerContextRootStats oneRow = (LoadBalancerContextRootStats)stats.get(key); 1127 TimeStatistic respTime = (TimeStatistic )oneRow.getResponseTime(); 1128 CountStatistic avgRes = (CountStatistic )oneRow.getAverageResponseTime(); 1129 CountStatistic failOvers = (CountStatistic )oneRow.getFailoverReqCount(); 1130 CountStatistic errorPage = (CountStatistic )oneRow.getErrorRequestCount(); 1131 CountStatistic url = (CountStatistic )oneRow.getIdempotentUrlAccessCount(); 1132 CountStatistic activeReqs = (CountStatistic )oneRow.getActiveRequestCount(); 1133 CountStatistic totalReqs = (CountStatistic )oneRow.getTotalRequestCount(); 1134 model.appendRow(); 1135 model.setValue("target", key); 1136 model.setValue("avgResponse", avgRes.getCount()); 1137 model.setValue("failovers", failOvers.getCount()); 1138 model.setValue("errorPage", errorPage.getCount()); 1139 model.setValue("url", url.getCount()); 1140 model.setValue("activeReqs", activeReqs.getCount()); 1141 model.setValue("totalReqs", totalReqs.getCount()); 1142 model.setValue("maxResponse", respTime.getMaxTime()); 1143 model.setValue("minResponse", respTime.getMaxTime()); 1144 } 1146 } catch (Exception ex) { 1147 return; 1149 } 1150 1151 1152 } 1153 1154 public void deleteLoadBalancer(RequestContext ctx, HandlerContext handlerCtx) { 1155 View view = handlerCtx.getView(); 1156 DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view).getParentViewBean()); 1157 ViewDescriptor vd = descView.getViewDescriptor(); 1158 String childName = (String )handlerCtx.getInputValue("tableChildName"); 1159 if(childName == null) { 1160 throw new FrameworkException("deleteHandler: childName not specified", vd, view); 1161 } 1162 1163 ViewDescriptor tableDescriptor = vd.getChildDescriptor(childName); 1164 if (tableDescriptor == null) { 1165 throw new FrameworkException("deleteHandler: tableDescriptor is null", vd, view); 1166 } 1167 if(!(tableDescriptor instanceof CCActionTableDescriptor)) { 1168 throw new FrameworkException("deleteHandler: tableDescriptor is of wrong type", tableDescriptor, view); 1169 } 1170 CCActionTableModelInterface model = ((CCActionTableDescriptor)tableDescriptor).getModel(); 1171 1172 String deleteKey = (String )handlerCtx.getInputValue("deleteKey"); 1173 1174 if (deleteKey == null) { 1175 throw new FrameworkException("No delete key specified", tableDescriptor, view); 1176 } 1177 model.setRowSelectionType("multiple"); 1178 try { 1179 model.beforeFirst(); 1180 while(model.next()) { 1182 if (model.isRowSelected()) { 1183 LBConfigHelper helper = new LBConfigHelper(AMXUtil.getDomainRoot()); 1184 helper.removeLoadbalancer(model.getValue(deleteKey).toString()); 1185 model.setRowSelected(false); 1186 } 1187 } 1188 ContainerViewBase containerView = 1189 (ContainerViewBase)(tableDescriptor.getView(ctx).getParent()); 1190 containerView.removeChild(tableDescriptor.getName()); 1191 ((DefaultModel)model).clear(); 1192 } catch (Exception ex) { 1193 throw new FrameworkException("Error while deleting from: '"+ 1194 tableDescriptor.getName()+"'", ex, tableDescriptor, view); 1195 } 1196 1197 } 1198 1199 1200 1201 1202 private static boolean isEmpty(String test) { 1203 return ((test == null) || "".equals(test)); 1204 } 1205 1206} 1207 | Popular Tags |