1 23 24 29 package com.sun.appserv.management.helper; 30 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.ResourceBundle ; 41 import java.util.Set ; 42 import java.util.HashSet ; 43 import java.util.Iterator ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 import javax.management.MBeanException ; 47 48 import com.sun.appserv.management.DomainRoot; 49 import com.sun.appserv.management.config.ServerRefConfig; 50 import com.sun.appserv.management.config.ClusterRefConfig; 51 import com.sun.appserv.management.config.LBConfig; 52 import com.sun.appserv.management.config.LoadBalancerConfig; 53 import com.sun.appserv.management.config.DomainConfig; 54 import com.sun.appserv.management.config.ClusterConfig; 55 import com.sun.appserv.management.config.StandaloneServerConfig; 56 import com.sun.appserv.management.config.ClusteredServerConfig; 57 import com.sun.appserv.management.config.DeployedItemRefConfig; 58 import com.sun.appserv.management.config.J2EEApplicationConfig; 59 import com.sun.appserv.management.config.LBConfigKeys; 60 import com.sun.appserv.management.config.ObjectTypeValues; 61 import com.sun.appserv.management.config.ServerConfig; 62 import com.sun.appserv.management.ext.lb.LoadBalancer; 63 import com.sun.appserv.management.monitor.LoadBalancerApplicationMonitor; 64 import com.sun.appserv.management.monitor.LoadBalancerClusterMonitor; 65 import com.sun.appserv.management.monitor.LoadBalancerContextRootMonitor; 66 import com.sun.appserv.management.monitor.LoadBalancerMonitor; 67 import com.sun.appserv.management.monitor.LoadBalancerServerMonitor; 68 import com.sun.appserv.management.monitor.MonitoringRoot; 69 import com.sun.appserv.management.monitor.statistics.LoadBalancerContextRootStats; 70 import com.sun.appserv.management.monitor.statistics.LoadBalancerServerStats; 71 import com.sun.appserv.management.util.misc.Formatter; 72 import com.sun.appserv.management.util.misc.StringSourceBase; 73 74 78 public final class LBConfigHelper { 79 80 83 public LBConfigHelper(final DomainRoot domainRoot) { 84 mDomainConfig = domainRoot.getDomainConfig(); 85 mDomainRoot = domainRoot; 86 mLogger = Logger.getLogger(domainRoot.getMBeanLoggerName()); 87 resBundle = ResourceBundle.getBundle(this.getClass().getPackage().getName()+".LocalStrings"); 88 formatter = new Formatter(new StringSourceBase()); 89 } 90 91 96 public List <LBConfig> getLBConfigsForServer(String serverName) { 97 Map <String ,LBConfig> lbconfigs = mDomainConfig.getLBConfigMap(); 98 List <LBConfig> list = new ArrayList <LBConfig>(); 99 for(LBConfig config:lbconfigs.values()){ 100 Map <String ,ServerRefConfig> map = config.getServerRefConfigMap(); 101 for(String name:map.keySet()){ 102 if(name.equals(serverName)){ 103 list.add(config); 104 } 105 } 106 } 107 return list; 108 } 109 110 115 public List <LBConfig> getLBConfigsForCluster(String clusterName) { 116 Map <String ,LBConfig> lbconfigs = mDomainConfig.getLBConfigMap(); 117 List <LBConfig> list = new ArrayList <LBConfig>(); 118 for(LBConfig config:lbconfigs.values()){ 119 Map <String ,ClusterRefConfig> map = config.getClusterRefConfigMap(); 120 for(String name:map.keySet()){ 121 if(name.equals(clusterName)){ 122 list.add(config); 123 } 124 } 125 } 126 return list; 127 } 128 129 135 public Map <String , ServerRefConfig> getServersInLBConfig(String lbConfigName) { 136 Map <String ,LBConfig> lbconfigs = mDomainConfig.getLBConfigMap(); 137 LBConfig lbconfig = lbconfigs.get(lbConfigName); 138 return lbconfig.getServerRefConfigMap(); 139 } 140 141 147 public Map <String , ClusterRefConfig> getClustersInLBConfig(String lbConfigName) { 148 Map <String ,LBConfig> lbconfigs = mDomainConfig.getLBConfigMap(); 149 LBConfig lbconfig = lbconfigs.get(lbConfigName); 150 return lbconfig.getClusterRefConfigMap(); 151 } 152 153 158 public String [] listTargets() { 159 Set <String > targetSet = new HashSet <String >(); 160 161 Map <String ,ClusterConfig> cConfigMap = 162 mDomainConfig.getClusterConfigMap(); 163 164 if (cConfigMap != null) { 165 targetSet.addAll( cConfigMap.keySet()); 166 } 167 Map <String ,StandaloneServerConfig> ssConfigMap = 168 mDomainConfig.getStandaloneServerConfigMap(); 169 170 if (ssConfigMap != null) { 171 targetSet.addAll( ssConfigMap.keySet()); 172 } 173 174 String [] targetArr = new String [targetSet.size()]; 175 return (String []) targetSet.toArray(targetArr); 176 } 177 178 187 public String [] listTargets(final String lbName) { 188 189 Set <String > targetSet = new HashSet <String >(); 190 191 Map <String , LoadBalancerConfig> lbMap = 192 mDomainConfig.getLoadBalancerConfigMap(); 193 194 if (lbMap == null) { 195 return null; 196 } 197 198 LoadBalancerConfig lb = (LoadBalancerConfig) lbMap.get(lbName); 199 if (lb == null) { 200 return null; 201 } 202 203 String lbConfigName = lb.getLbConfigName(); 204 Map <String , LBConfig> lbConfigMap = mDomainConfig.getLBConfigMap(); 205 if ((lbConfigMap == null) || (lbConfigName == null) ){ 206 return null; 207 } 208 LBConfig lbConfig = (LBConfig) lbConfigMap.get(lbConfigName); 209 if (lbConfig == null) { 210 return null; 211 } 212 Map <String ,ClusterRefConfig> cRefMap = 213 lbConfig.getClusterRefConfigMap(); 214 215 if ( cRefMap != null) { 216 targetSet.addAll(cRefMap.keySet()); 217 } 218 Map <String ,ServerRefConfig> sRefMap = 219 lbConfig.getServerRefConfigMap(); 220 221 if ( sRefMap != null) { 222 targetSet.addAll(sRefMap.keySet()); 223 } 224 225 String [] targetArr = new String [targetSet.size()]; 226 227 return (String []) targetSet.toArray(targetArr); 228 229 } 230 231 251 public LoadBalancerConfig createLoadbalancer(String loadbalancerName, 252 boolean autoApplyEnabled, String [] targets, Map <String ,String > params) { 253 254 256 if ( loadbalancerName == null ) { 257 throw new IllegalArgumentException ( 258 "loadbalancerName can not be null"); 259 } 260 261 String lbConfigName = loadbalancerName + LB_CONFIG_SUFFIX; 263 264 Map <String ,LBConfig> lbconfigMap = mDomainConfig.getLBConfigMap(); 267 if(lbconfigMap != null){ 268 for(int i=1;lbconfigMap.get(lbConfigName) != null;i++){ 270 lbConfigName = loadbalancerName + LB_CONFIG_SUFFIX + "_" + i; 271 } 272 } 273 274 LBConfig lbConfig = mDomainConfig.createLBConfig(lbConfigName, params); 276 277 if ( targets != null) { 278 for (int idx =0; idx < targets.length; idx++) { 279 String targetName = targets[idx]; 280 281 if ( isCluster(targetName)) { 282 lbConfig.createClusterRefConfig(targetName, null); 283 } else if ( isStandaloneServer( targetName)) { 284 lbConfig.createServerRefConfig(targetName, null); 285 } 286 } 287 } 288 289 return mDomainConfig.createLoadBalancerConfig(loadbalancerName, 291 lbConfigName, autoApplyEnabled, null); 292 } 293 294 298 public void removeLoadbalancer(String loadbalancerName) { 299 300 Map <String , LoadBalancerConfig> lbMap = 302 mDomainConfig.getLoadBalancerConfigMap(); 303 if ( lbMap == null) { 304 return; 305 } 306 307 LoadBalancerConfig loadbalancerConfig = lbMap.get(loadbalancerName); 309 if(loadbalancerConfig == null){ 310 return; 311 } 312 String lbConfigName = loadbalancerConfig .getLbConfigName(); 313 314 mDomainConfig.removeLoadBalancerConfig(loadbalancerName); 316 317 lbMap = mDomainConfig.getLoadBalancerConfigMap(); 319 if ( lbMap == null) { 320 return; 321 } 322 323 for(LoadBalancerConfig lbConfig : lbMap.values()){ 325 if ( lbConfig.getLbConfigName().equals(lbConfigName)) { 326 return; 329 } 330 } 331 332 mDomainConfig.removeLBConfig(lbConfigName); 334 } 335 336 337 344 public void disableServer(String target, int timeout) { 345 setServerStatus(target,timeout,false); 346 } 347 348 353 public void enableServer(String target) { 354 setServerStatus(target, 0,true); 355 } 356 357 365 public void disableApplication(String target, String appName, int timeout) { 366 setApplicationStatus(target, appName, timeout, false); 367 } 368 369 375 public void enableApplication(String target, String appName) { 376 setApplicationStatus(target, appName, 0, true); 377 } 378 379 391 public Map <String , LoadBalancerServerStats> getInstanceStats( 392 final String targetLoadBalancer, final String target, boolean allTargets) { 393 394 if ( targetLoadBalancer == null ) 395 throw new IllegalArgumentException ( 396 "Load Balancer Name can not be null"); 397 if ( !allTargets && target == null ) 398 throw new IllegalArgumentException ( 399 "Specify AllTargets or atleast one target"); 400 401 Map <String ,LoadBalancerServerStats> 402 loadBalancerServerStatsMap = new HashMap <String ,LoadBalancerServerStats>(); 403 404 Map <String , LoadBalancerServerMonitor> instanceMonitorMap = 405 getInstanceMonitors(targetLoadBalancer, target, allTargets); 406 407 for (String serverFQName : instanceMonitorMap.keySet()) { 408 LoadBalancerServerMonitor loadBalancerServerMonitor = 409 instanceMonitorMap.get(serverFQName); 410 LoadBalancerServerStats loadBalancerServerStats = 411 loadBalancerServerMonitor.getLoadBalancerServerStats(); 412 loadBalancerServerStatsMap.put(serverFQName, loadBalancerServerStats); 413 } 414 return loadBalancerServerStatsMap; 415 } 416 417 429 public Map <String , LoadBalancerContextRootStats> getInstanceStats( 430 final String targetLoadBalancer, final String contextRoot, 431 final String target, boolean allTargets) { 432 433 if ( contextRoot == null ) 434 throw new IllegalArgumentException ("ContextRoot can not be null"); 435 if ( targetLoadBalancer == null ) 436 throw new IllegalArgumentException ( 437 "Load Balancer Name can not be null"); 438 if ( !allTargets && target == null ) 439 throw new IllegalArgumentException ( 440 "Specify AllTargets or atleast one target"); 441 442 Map <String ,LoadBalancerContextRootStats> 443 loadBalancerContextRootStatsMap = new HashMap <String ,LoadBalancerContextRootStats>(); 444 445 Map <String , LoadBalancerServerMonitor> instanceMonitorMap = 446 getInstanceMonitors(targetLoadBalancer, target, allTargets); 447 448 for (String serverFQName : instanceMonitorMap.keySet()) { 449 LoadBalancerServerMonitor loadBalancerServerMonitor = 450 instanceMonitorMap.get(serverFQName); 451 Map <String , LoadBalancerApplicationMonitor> 452 loadBalancerApplicationMonitorMap = 453 loadBalancerServerMonitor.getLoadBalancerApplicationMonitorMap(); 454 for (String appName : loadBalancerApplicationMonitorMap.keySet()) { 455 LoadBalancerApplicationMonitor loadBalancerApplicationMonitor = 456 loadBalancerApplicationMonitorMap.get(appName); 457 Map <String , LoadBalancerContextRootMonitor> 458 loadBalancerContextRootMonitorMap = 459 loadBalancerApplicationMonitor.getLoadBalancerContextRootMonitorMap(); 460 LoadBalancerContextRootMonitor loadBalancerContextRootMonitor = 461 loadBalancerContextRootMonitorMap.get(contextRoot); 462 loadBalancerContextRootStatsMap.put( 463 serverFQName, (LoadBalancerContextRootStats)loadBalancerContextRootMonitor.getStats()); 464 } 465 } 466 return loadBalancerContextRootStatsMap; 467 } 468 469 489 public String configureHTTPLBConfig(String configName, String target, 490 Map <String ,String > options, String filePath) throws MBeanException { 491 492 String healthCheckerUrl = options.get(HEALTH_CHECKER_URL); 493 String healthCheckerInterval = options.get(HEALTH_CHECKER_INTERVAL); 494 String healthCheckerTimeout = options.get(HEALTH_CHECKER_TIMEOUT); 495 String lbPolicy = options.get(LB_POLICY); 496 String lbPolicyModule = options.get(LB_POLICY_MODULE); 497 boolean isCluster = isCluster(target); 498 Map <String ,String > params = getParams(options); 499 Map <String ,LBConfig> lbconfigs = mDomainConfig.getLBConfigMap(); 500 if(lbconfigs.get(configName)!=null){ 501 String msg = formatter.format(resBundle.getString("LbConfigExists"), 502 configName); 503 throw new MBeanException (new RuntimeException (msg)); 504 } 505 if(!isCluster){ 506 if((lbPolicy!=null) || (lbPolicyModule!=null)){ 507 String msg = formatter.format(resBundle.getString("NotCluster"), 509 target); 510 throw new MBeanException (new RuntimeException (msg)); 511 } 512 } 513 LBConfig lbConfig = mDomainConfig.createLBConfig(configName, params); 515 516 if(isCluster){ 517 518 ClusterRefConfig clusterRefConfig = lbConfig.createClusterRefConfig(target, null,null); 520 if(lbPolicy!=null){ 521 clusterRefConfig.setLBPolicy(lbPolicy); 522 } 523 if(lbPolicyModule!=null){ 524 clusterRefConfig.setLBPolicyModule(lbPolicyModule); 525 } 526 clusterRefConfig.createHealthCheckerConfig(healthCheckerUrl, 528 healthCheckerInterval, healthCheckerTimeout); 529 enableServer(target); 531 }else{ 532 ServerRefConfig serverRefConfig = lbConfig.createServerRefConfig(target,null,true,true); 534 serverRefConfig.createHealthCheckerConfig(healthCheckerUrl, 536 healthCheckerInterval, healthCheckerTimeout); 537 } 538 enableAllApplications(target); 540 if(filePath!=null){ 541 LoadBalancer lb = createLoadBalancer(configName); 543 String lbxml = lb.getLoadBalancerXML(); 545 mDomainConfig.removeLoadBalancerConfig(configName+LB_SUFFIX); 546 return writeToFile(lbxml, filePath); 548 } 549 return null; 550 } 551 552 558 public void configureLBWeight(String clusterName, Map instanceVsWeights) { 559 560 Map <String ,ClusterConfig> clusterConfigMap = mDomainConfig.getClusterConfigMap(); 562 ClusterConfig clusterConfig = clusterConfigMap.get(clusterName); 564 if(clusterConfig == null) 565 throw new IllegalArgumentException (formatter.format(resBundle.getString("InvalidCluster"),clusterName)); 566 Map <String ,ClusteredServerConfig> clusteredServerConfigMap = clusterConfig.getClusteredServerConfigMap(); 568 for(Object instance:instanceVsWeights.keySet()){ 570 ClusteredServerConfig clusteredServerConfig = clusteredServerConfigMap.get(instance); 572 if(clusteredServerConfig == null) 573 throw new IllegalArgumentException (formatter.format(resBundle.getString("InvalidInstance"),instance,clusterName)); 574 clusteredServerConfig.setLBWeight( instanceVsWeights.get(instance).toString()); 576 } 577 } 578 579 583 public void enableAllApplications(String target){ 584 Map <String ,ServerConfig> serverConfigMap = mDomainConfig.getServerConfigMap(); 586 ServerConfig serverConfig = serverConfigMap.get(target); 587 if(serverConfig != null){ 588 Map <String ,DeployedItemRefConfig> deployedItemRefConfigMap = serverConfig.getDeployedItemRefConfigMap(); 590 for (DeployedItemRefConfig deployedItemRefConfig : deployedItemRefConfigMap.values()){ 592 J2EEApplicationConfig app = mDomainConfig.getJ2EEApplicationConfigMap().get(deployedItemRefConfig.getName()); 594 if(app == null) { 595 continue; 596 } 597 if(app.getObjectType().equals(ObjectTypeValues.USER)) { 599 deployedItemRefConfig.setLBEnabled(true); 600 } 601 } 602 } 603 } 604 605 607 boolean isCluster(String name) { 608 Map <String ,ClusterConfig> cConfigMap = 609 mDomainConfig.getClusterConfigMap(); 610 611 if (cConfigMap == null) { 612 return false; 613 } 614 ClusterConfig cConfig = (ClusterConfig) cConfigMap.get(name); 615 if ( cConfig == null) { 616 return false; 617 } else { 618 return true; 619 } 620 } 621 622 boolean isStandaloneServer(String name) { 623 Map <String ,StandaloneServerConfig> ssConfigMap = 624 mDomainConfig.getStandaloneServerConfigMap(); 625 626 if (ssConfigMap == null) { 627 return false; 628 } 629 StandaloneServerConfig ssConfig = (StandaloneServerConfig) 630 ssConfigMap.get(name); 631 if ( ssConfig == null) { 632 return false; 633 } else { 634 return true; 635 } 636 } 637 638 645 private void setServerStatus(String target, int timeout, boolean status) { 646 if (timeout < 0 && !status) { 648 String msg = resBundle.getString("InvalidNumber"); 649 throw new IllegalArgumentException (msg); 650 } 651 652 try { 653 if (isCluster(target)) { 655 ClusterConfig cRef = mDomainConfig.getClusterConfigMap().get(target); 657 if (cRef == null) { 658 mLogger.log(Level.FINEST," server " + target + 659 " does not exist in any cluster in the domain"); 660 String msg = formatter.format(resBundle.getString("ServerNotDefined"), 661 target); 662 throw new MBeanException (new RuntimeException (msg)); 663 } 664 665 for(ServerRefConfig sRef : cRef.getServerRefConfigMap().values()){ 667 setLBEnabled(sRef, status, timeout, target); 669 } 670 } else { ServerRefConfig sRef = null; 672 boolean foundTarget = false; 673 Map <String ,LBConfig> lbConfigs = mDomainConfig.getLBConfigMap(); 675 for(LBConfig lbConfig : lbConfigs.values()){ 677 Map <String ,ServerRefConfig> serverRefs = lbConfig.getServerRefConfigMap(); 679 sRef = serverRefs.get(target); 681 if (sRef == null) { 682 mLogger.log(Level.FINEST," server " + target + 683 " does not exist in " + serverRefs); 684 } else { 685 foundTarget = true; 686 break; 687 } 688 } 689 if (!foundTarget) { 691 sRef = getServerRefConfigFromCluster(target); 693 if (sRef == null) { 694 mLogger.log(Level.FINEST," server " + target + 695 " does not exist in any cluster in the domain"); 696 String msg = formatter.format(resBundle.getString("ServerNotDefined"), 697 target); 698 throw new MBeanException (new RuntimeException (msg)); 699 } 700 } 701 setLBEnabled(sRef, status, timeout, target); 703 } 704 705 } catch(Exception ce) { 706 ce.printStackTrace(); 707 throw new RuntimeException (ce); 708 } 709 710 } 711 712 720 private void setLBEnabled(final ServerRefConfig sRef, final boolean status, 721 final int timeout, final String target) throws MBeanException { 722 int curTout = sRef.getDisableTimeoutInMinutes(); 723 boolean enabled = sRef.getLBEnabled(); 725 if(!status){ 726 if ((enabled == false) && (curTout == timeout)) { 727 String msg = formatter.format(resBundle.getString("ServerDisabled"), 728 sRef.getRef()); 729 throw new MBeanException (new Exception (msg)); 730 } 731 sRef.setDisableTimeoutInMinutes(timeout); 733 sRef.setLBEnabled(false); 735 mLogger.log(Level.INFO,formatter.format(resBundle.getString( 736 "http_lb_admin.ServerDisabled"), target)); 737 }else{ 738 if (enabled == true) { 739 String msg = formatter.format(resBundle.getString("ServerEnabled"), 740 sRef.getRef()); 741 throw new MBeanException (new Exception ("ServerEnabled")); 742 } 743 sRef.setLBEnabled(true); 744 mLogger.log(Level.INFO,formatter.format(resBundle.getString( 745 "http_lb_admin.ServerEnabled"), target)); 746 } 747 } 748 749 757 private void setApplicationStatus(String target, String appName,int timeout, boolean status){ 758 759 if (timeout < 0 && !status) { 761 String msg = resBundle.getString("InvalidNumber"); 762 throw new IllegalArgumentException (msg); 763 } 764 765 try { 766 DeployedItemRefConfig dRef = null; 767 if (isCluster(target)) { 769 Map <String ,ClusterConfig> clusterConfigs = mDomainConfig.getClusterConfigMap(); 771 ClusterConfig clusterConfig = clusterConfigs.get(target); 772 dRef = clusterConfig.getDeployedItemRefConfigMap().get(appName); 774 } else { Map <String ,StandaloneServerConfig> ssConfigMap = 777 mDomainConfig.getStandaloneServerConfigMap(); 778 StandaloneServerConfig ssc = ssConfigMap.get(target); 779 if (ssc == null) { 780 ClusteredServerConfig s = mDomainConfig.getClusteredServerConfigMap().get(target); 782 dRef = s.getDeployedItemRefConfigMap().get(appName); 784 }else{ 785 dRef = ssc.getDeployedItemRefConfigMap().get(appName); 787 } 788 } 789 if (dRef == null) { 790 mLogger.log(Level.FINEST," server " + target + 791 " does not exist in any cluster in the domain"); 792 String msg = formatter.format(resBundle.getString("AppRefNotDefined"), 793 target); 794 throw new MBeanException (new RuntimeException (msg)); 795 } 796 int curTout = Integer.parseInt( 797 dRef.getDisableTimeoutInMinutes()); 798 boolean enabled = dRef.getLBEnabled(); 800 if(!status){ 801 if ((enabled == false) && (curTout == timeout)) { 802 String msg = resBundle.getString("AppDisabledOnServer" 803 ); 804 throw new MBeanException (new Exception (msg)); 805 } 806 dRef.setDisableTimeoutInMinutes( "" + timeout ); 808 dRef.setLBEnabled(false); 810 mLogger.log(Level.INFO,resBundle.getString( 811 "http_lb_admin.ApplicationDisabled")); 812 }else{ 813 if (enabled == true) { 814 String msg = resBundle.getString("AppEnabledOnServer" 815 ); 816 throw new MBeanException (new Exception (msg)); 817 } 818 dRef.setLBEnabled(true); 820 mLogger.log(Level.INFO,resBundle.getString( 821 "http_lb_admin.ApplicationEnabled")); 822 } 823 } catch(Exception ce) { 824 throw new RuntimeException (ce); 825 } 826 827 } 828 829 836 private ServerRefConfig getServerRefConfigFromCluster(String target) 837 throws MBeanException { 838 Map <String ,LBConfig> lbConfigs = mDomainConfig.getLBConfigMap(); 842 for(LBConfig lbConfig : lbConfigs.values()){ 844 Map <String ,ClusterRefConfig> clusterRefs = lbConfig.getClusterRefConfigMap(); 846 for(ClusterRefConfig clusterRef :clusterRefs.values()){ 848 849 String clusterName = clusterRef.getReferencedClusterName(); 851 852 Map <String ,ClusterConfig> clusterConfigs = mDomainConfig.getClusterConfigMap(); 854 ClusterConfig config = clusterConfigs.get(clusterName); 855 856 Map <String ,ServerRefConfig> serverRefConfigs = config.getServerRefConfigMap(); 858 859 for(ServerRefConfig serverRefConfig : serverRefConfigs.values() ){ 861 862 if(serverRefConfig.getName().equals(target)){ 864 return serverRefConfig; 865 } 866 867 } 868 } 869 } 870 return null; 871 } 872 873 885 public Map <String , LoadBalancerServerMonitor> getInstanceMonitors( 886 final String targetLoadBalancer, final String target, boolean allTargets) { 887 888 Map <String ,LoadBalancerServerMonitor> 889 loadBalancerServerMonitorMap = new HashMap <String ,LoadBalancerServerMonitor>(); 890 891 MonitoringRoot mRoot = mDomainRoot.getMonitoringRoot(); 892 Map <String , LoadBalancerMonitor> loadBalancerMonitorMap = 893 mRoot.getLoadBalancerMonitorMap(); 894 LoadBalancerMonitor loadBalancerMonitor = 895 loadBalancerMonitorMap.get(targetLoadBalancer); 896 897 Map <String , LoadBalancerClusterMonitor> loadBalancerClusterMonitorMap = 898 loadBalancerMonitor.getLoadBalancerClusterMonitorMap(); 899 if (!allTargets) { 900 LoadBalancerClusterMonitor loadBalancerClusterMonitor = 901 loadBalancerClusterMonitorMap.get(target); 902 populateLoadBalancerServerMonitorMap(target, 903 loadBalancerServerMonitorMap, loadBalancerClusterMonitor); 904 } else { 905 for (String clusterName : loadBalancerClusterMonitorMap.keySet()) { 906 LoadBalancerClusterMonitor loadBalancerClusterMonitor = 907 loadBalancerClusterMonitorMap.get(clusterName); 908 populateLoadBalancerServerMonitorMap(target, 909 loadBalancerServerMonitorMap, loadBalancerClusterMonitor); 910 } 911 } 912 return loadBalancerServerMonitorMap; 913 } 914 915 924 public Map <String , LoadBalancerServerMonitor> 925 getInstanceAggregateStats(String serverName) { 926 927 Collection <LoadBalancerConfig> loadBalancers = 928 getLoadBalancers(serverName, false).values(); 929 930 Map <String ,LoadBalancerServerMonitor> 931 loadBalancerServerMonitorMap = new HashMap <String ,LoadBalancerServerMonitor>(); 932 933 MonitoringRoot mRoot = mDomainRoot.getMonitoringRoot(); 934 Map <String , LoadBalancerMonitor> loadBalancerMonitorMap = 935 mRoot.getLoadBalancerMonitorMap(); 936 937 for (LoadBalancerConfig loadBalancer : loadBalancers) { 938 String targetLoadBalancer = loadBalancer.getName(); 939 LoadBalancerMonitor loadBalancerMonitor = 940 loadBalancerMonitorMap.get(targetLoadBalancer); 941 942 Map <String , LoadBalancerClusterMonitor> loadBalancerClusterMonitorMap = 943 loadBalancerMonitor.getLoadBalancerClusterMonitorMap(); 944 for (String clusterName : loadBalancerClusterMonitorMap.keySet()) { 945 LoadBalancerClusterMonitor loadBalancerClusterMonitor = 946 loadBalancerClusterMonitorMap.get(clusterName); 947 LoadBalancerServerMonitor loadBalancerServerMonitor = 948 loadBalancerClusterMonitor.getLoadBalancerServerMonitorMap() 949 .get(serverName); 950 loadBalancerServerMonitorMap.put( 951 targetLoadBalancer, loadBalancerServerMonitor); 952 } 953 } 954 return loadBalancerServerMonitorMap; 955 } 956 957 967 public Map <String ,LoadBalancerConfig> getLoadBalancers( 968 String targetName, boolean isCluster) { 969 Map <String , LBConfig> lbConfigMap = fetchLBConfigs(targetName, isCluster); 970 return fetchLoadBalancerConfigs(lbConfigMap); 971 } 972 973 private Map <String , LBConfig> fetchLBConfigs( 974 String targetName, boolean isCluster) { 975 976 Map <String ,LBConfig> result = new HashMap <String ,LBConfig>(); 977 Map <String ,LBConfig> lbConfigMap = mDomainConfig.getLBConfigMap(); 978 979 if (isCluster) { 980 for (String lbConfigName : lbConfigMap.keySet()) { 981 LBConfig lbConfig = lbConfigMap.get(lbConfigName); 982 Map <String ,ClusterRefConfig> lbClusterRefConfigMap = 983 lbConfig.getClusterRefConfigMap(); 984 for (String clusterRef : lbClusterRefConfigMap.keySet()) { 985 if (clusterRef.equals(targetName)) { 986 result.put(lbConfigName, lbConfig); 987 break; 988 } 989 } 990 } 991 } else if (isStandaloneServer(targetName)) { 992 995 for (String lbConfigName : lbConfigMap.keySet()) { 996 LBConfig lbConfig = lbConfigMap.get(lbConfigName); 997 Map <String ,ServerRefConfig> lbServerRefConfigMap = 998 lbConfig.getServerRefConfigMap(); 999 for (String serverRef : lbServerRefConfigMap.keySet()) { 1000 if (serverRef.equals(targetName)) { 1001 result.put(lbConfigName, lbConfig); 1002 break; 1003 } 1004 } 1005 } 1006 } else { for (String lbConfigName : lbConfigMap.keySet()) { 1008 LBConfig lbConfig = lbConfigMap.get(lbConfigName); 1009 Map <String ,ClusterRefConfig> lbClusterRefConfigMap = 1010 lbConfig.getClusterRefConfigMap(); 1011 Map <String ,ClusterConfig> clusterConfigMap = 1012 mDomainConfig.getClusterConfigMap(); 1013 Map <String ,ClusterConfig> relevantClusterConfigMap = new HashMap <String ,ClusterConfig>(); 1014 for (String clusterRef : lbClusterRefConfigMap.keySet()) 1015 relevantClusterConfigMap.put(clusterRef, 1016 clusterConfigMap.get(clusterRef)); 1017 for (String clusterName : relevantClusterConfigMap.keySet()) { 1019 ClusterConfig clusterConfig = 1020 relevantClusterConfigMap.get(clusterName); 1021 Map <String ,ServerRefConfig> clusteredServerRefConfigMap = 1022 clusterConfig.getServerRefConfigMap(); 1023 for (String serverRef : clusteredServerRefConfigMap.keySet()) { 1024 if (serverRef.equals(targetName)) { 1025 result.put(lbConfigName, lbConfig); 1026 break; 1027 } 1028 } 1029 } 1030 } 1031 } 1032 return result; 1033 } 1034 1035 private Map <String ,LoadBalancerConfig> fetchLoadBalancerConfigs( 1036 Map <String , LBConfig> lbConfigMap) { 1037 1038 Map <String ,LoadBalancerConfig> relevantLoadBalancerConfigMap = new HashMap <String ,LoadBalancerConfig>(); 1039 1040 for (String lbConfigName : lbConfigMap.keySet()) { 1041 Map <String ,LoadBalancerConfig> allLoadBalancerConfigMap = 1043 mDomainRoot.getDomainConfig().getLoadBalancerConfigMap(); 1044 1045 for (String loadBalancerName : allLoadBalancerConfigMap.keySet()) { 1046 LoadBalancerConfig loadBalancerConfig = 1047 allLoadBalancerConfigMap.get(loadBalancerName); 1048 if (loadBalancerConfig.getLbConfigName().equals(lbConfigName)) 1049 relevantLoadBalancerConfigMap.put( 1050 loadBalancerName, loadBalancerConfig); 1051 } 1052 } 1053 return relevantLoadBalancerConfigMap; 1054 } 1055 1056 private void populateLoadBalancerServerMonitorMap(String target, 1057 Map <String , LoadBalancerServerMonitor> loadBalancerServerMonitorMap, 1058 LoadBalancerClusterMonitor loadBalancerClusterMonitor) { 1059 1060 Map <String , LoadBalancerServerMonitor> tmpLoadBalancerServerMonitorMap = 1061 loadBalancerClusterMonitor.getLoadBalancerServerMonitorMap(); 1062 for (String serverName : tmpLoadBalancerServerMonitorMap.keySet()) { 1063 LoadBalancerServerMonitor loadBalancerServerMonitor = 1064 tmpLoadBalancerServerMonitorMap.get(serverName); 1065 if (isStandaloneServer(serverName)) 1066 loadBalancerServerMonitorMap.put( 1067 serverName, loadBalancerServerMonitor); 1068 else loadBalancerServerMonitorMap.put( 1069 loadBalancerClusterMonitor.getName() + "." + target, 1070 loadBalancerServerMonitor); 1071 } 1072 } 1073 1074 private LoadBalancer createLoadBalancer(final String configName) { 1075 mDomainConfig.createLoadBalancerConfig( 1076 configName+LB_SUFFIX, configName, false, null); 1077 Map <String ,LoadBalancer> lbs = mDomainRoot.getLoadBalancerMap(); 1078 LoadBalancer lb = lbs.get(configName+LB_SUFFIX); 1079 return lb; 1080 } 1081 1082 1085 private Map <String ,String > getParams(Map <String ,String > options) { 1086 1087 Map <String ,String > params = new HashMap <String ,String >(); 1088 params.put(LBConfigKeys.HTTPS_ROUTING_KEY,options.get(HTTPS_ROUTING)); 1089 params.put(LBConfigKeys.MONITORING_ENABLED_KEY,options.get(MONITOR)); 1090 params.put(LBConfigKeys.RELOAD_POLL_INTERVAL_IN_SECONDS_KEY, options.get(RELOAD_INTERVAL)); 1091 params.put(LBConfigKeys.RESPONSE_TIMEOUT_IN_SECONDS_KEY, options.get(RESPONSE_TIMEOUT)); 1092 params.put(LBConfigKeys.ROUTE_COOKIE_ENABLED_KEY, options.get(ROUTE_COOKIE)); 1093 return params; 1094 } 1095 1096 1099 private String writeToFile(final String lbxml, final String filePath) { 1100 FileOutputStream fo = null; 1101 try{ 1102 fo = new FileOutputStream (filePath); 1103 fo.write(lbxml.getBytes()); 1104 }catch(IOException ioe){ 1105 ioe.printStackTrace(); 1106 }finally{ 1107 try{ 1108 fo.close(); 1109 }catch(IOException e){} 1110 } 1111 1112 return filePath; 1113 } 1114 1115 DomainConfig mDomainConfig = null; 1117 1118 DomainRoot mDomainRoot = null; 1119 1120 static final String LB_CONFIG_SUFFIX = "_LB_CONFIG"; 1121 1122 static final String LB_SUFFIX = "-lb-temp"; 1123 1124 Logger mLogger = null; 1125 1126 ResourceBundle resBundle = null; 1127 1128 Formatter formatter = null; 1129 1130 public static final String RESPONSE_TIMEOUT = "responsetimeout"; 1131 public static final String HTTPS_ROUTING = "httpsrouting"; 1132 public static final String RELOAD_INTERVAL = "reloadinterval"; 1133 public static final String MONITOR = "monitor"; 1134 public static final String ROUTE_COOKIE = "routecookie"; 1135 public static final String HEALTH_CHECKER_URL = "healthcheckerurl"; 1136 public static final String HEALTH_CHECKER_TIMEOUT = "healthcheckertimeout"; 1137 public static final String HEALTH_CHECKER_INTERVAL = "healthcheckerinterval"; 1138 public static final String TARGET = "target"; 1139 public static final String CONFIG = "config"; 1140 public static final String LB_POLICY = "lbpolicy"; 1141 public static final String LB_POLICY_MODULE = "lbpolicymodule"; 1142 1143} 1144 1145 1146 1147 | Popular Tags |