1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.io.IOException ; 31 32 import javax.management.MBeanServer ; 33 import javax.management.ObjectName ; 34 import javax.management.JMException ; 35 import javax.management.InstanceNotFoundException ; 36 37 import com.sun.appserv.management.base.AMX; 38 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_MONITOR; 39 import static com.sun.appserv.management.base.XTypes.SERVER_REF_CONFIG; 40 import static com.sun.appserv.management.base.XTypes.CLUSTER_REF_CONFIG; 41 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_CLUSTER_MONITOR; 42 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_SERVER_MONITOR; 43 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_APPLICATION_MONITOR; 44 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_CONTEXT_ROOT_MONITOR; 45 import static com.sun.appserv.management.base.AMX.J2EE_TYPE_KEY; 46 import static com.sun.appserv.management.base.AMX.NAME_KEY; 47 import static com.sun.appserv.management.base.AMX.JMX_DOMAIN; 48 import com.sun.appserv.management.base.Container; 49 import com.sun.appserv.management.base.Util; 50 51 import com.sun.appserv.management.client.ProxyFactory; 52 import com.sun.appserv.management.base.AMXDebug; 53 import com.sun.appserv.management.util.misc.StringUtil; 54 55 import com.sun.enterprise.management.monitor.LoadBalancerClusterMonitorImpl; 56 import com.sun.enterprise.management.monitor.LoadBalancerMonitorImpl; 57 import com.sun.enterprise.management.monitor.LoadBalancerServerMonitorImpl; 58 import com.sun.enterprise.management.monitor.LoadBalancerApplicationMonitorImpl; 59 import com.sun.enterprise.management.monitor.LoadBalancerContextRootMonitorImpl; 60 import com.sun.enterprise.management.support.ObjectNames; 61 import com.sun.appserv.management.util.jmx.MBeanRegistrationListener; 62 63 import com.sun.appserv.management.monitor.LoadBalancerMonitor; 64 import com.sun.appserv.management.monitor.LoadBalancerClusterMonitor; 65 import com.sun.appserv.management.monitor.LoadBalancerServerMonitor; 66 import com.sun.appserv.management.monitor.LoadBalancerApplicationMonitor; 67 import com.sun.appserv.management.monitor.LoadBalancerContextRootMonitor; 68 69 import com.sun.appserv.management.config.DeployedItemRefConfig; 70 import com.sun.appserv.management.config.ClusterRefConfig; 71 import com.sun.appserv.management.config.ClusterConfig; 72 import com.sun.appserv.management.config.ServerRefConfig; 73 import com.sun.appserv.management.config.ServerConfig; 74 import com.sun.appserv.management.config.StandaloneServerConfig; 75 import com.sun.appserv.management.config.ClusteredServerConfig; 76 import com.sun.appserv.management.config.LoadBalancerConfig; 77 import com.sun.appserv.management.config.LBConfig; 78 import com.sun.appserv.management.config.DomainConfig; 79 80 import com.sun.appserv.management.base.AMXRootLogger; 81 82 import com.sun.appserv.management.config.J2EEApplicationConfig; 83 import com.sun.enterprise.config.ConfigException; 84 import com.sun.enterprise.config.serverbeans.ApplicationHelper; 85 import com.sun.enterprise.deployment.backend.IASDeploymentException; 86 import com.sun.enterprise.deployment.phasing.ApplicationConfigHelper; 87 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils; 88 89 93 public abstract class LBBaseMBeanRegistrationListener 94 extends MBeanRegistrationListener { 95 96 final ObjectNames objectNames = ObjectNames.getInstance(JMX_DOMAIN); 97 98 public LBBaseMBeanRegistrationListener( 99 final MBeanServer mBeanServer, final ObjectName constraint ) 100 throws InstanceNotFoundException , IOException { 101 102 super(mBeanServer, constraint); 103 } 104 105 public LBBaseMBeanRegistrationListener(final MBeanServer mBeanServer) 106 throws InstanceNotFoundException , IOException { 107 108 this(mBeanServer, (ObjectName )null ); 109 } 110 111 public void mbeanUnregistered(final ObjectName objectName) {} 112 113 protected LoadBalancerClusterMonitor registerLoadBalancerClusterMonitor( 114 LoadBalancerMonitor loadBalancerMonitor, String clusterName) 115 throws JMException { 116 117 ObjectName loadBalancerMonitorObjName = 118 Util.getObjectName(loadBalancerMonitor); 119 120 ObjectName childClusterRefObjectName = 121 objectNames.buildContaineeObjectName( 122 loadBalancerMonitorObjName, 123 loadBalancerMonitor.getFullType(), 124 LOAD_BALANCER_CLUSTER_MONITOR, clusterName); 125 126 LoadBalancerClusterMonitorImpl loadBalancerClusterMonitorImpl = 127 new LoadBalancerClusterMonitorImpl(); 128 try { 129 ((MBeanServer )getConn()).registerMBean( 130 loadBalancerClusterMonitorImpl, childClusterRefObjectName); 131 } catch( JMException ex ) { 132 logWarning( 133 "LBBaseMBeanRegistrationListener" + 134 " registerLoadBalancerClusterMonitor failed. Exception = ", ex); 135 } 136 137 return getProxy(childClusterRefObjectName, LoadBalancerClusterMonitor.class ); 138 } 139 140 protected LoadBalancerServerMonitor registerLoadBalancerServerMonitor( 141 LoadBalancerClusterMonitor loadBalancerClusterMonitor, String serverName) 142 throws JMException { 143 144 ObjectName newChildClusterRefObjectName = 145 Util.getObjectName(loadBalancerClusterMonitor); 146 147 ObjectName childServerRefObjectName = 148 objectNames.buildContaineeObjectName( 149 newChildClusterRefObjectName, 150 loadBalancerClusterMonitor.getFullType(), 151 LOAD_BALANCER_SERVER_MONITOR, serverName); 152 153 LoadBalancerServerMonitorImpl loadBalancerServerMonitorImpl 154 = new LoadBalancerServerMonitorImpl(); 155 try { 156 ((MBeanServer )getConn()).registerMBean( 157 loadBalancerServerMonitorImpl, childServerRefObjectName); 158 } catch( JMException ex ) { 159 logWarning( 160 "LoadBalancerServerRefRegistrationListener" + 161 " registerLoadBalancerServerMonitor failed. Exception = ", ex); 162 } 163 164 return getProxy(childServerRefObjectName, LoadBalancerServerMonitor.class); 165 } 166 167 protected LoadBalancerApplicationMonitor registerLoadBalancerApplicationMonitor( 168 LoadBalancerServerMonitor loadBalancerServerMonitor, String appName) 169 throws JMException { 170 171 ObjectName newChildServerRefObjectName = 172 Util.getObjectName(loadBalancerServerMonitor); 173 174 ObjectName childApplicationRefObjectName = 175 objectNames.buildContaineeObjectName( 176 newChildServerRefObjectName, 177 loadBalancerServerMonitor.getFullType(), 178 LOAD_BALANCER_APPLICATION_MONITOR, appName); 179 180 LoadBalancerApplicationMonitorImpl 181 loadBalancerApplicationMonitorImpl = 182 new LoadBalancerApplicationMonitorImpl(); 183 try { 184 ((MBeanServer )getConn()).registerMBean( 185 loadBalancerApplicationMonitorImpl, 186 childApplicationRefObjectName ); 187 } catch( JMException ex ) { 188 logWarning( 189 "LoadBalancerServerRefRegistrationListener" + 190 " registerLoadBalancerApplicationMonitor failed. " + 191 "Exception = ", ex); 192 } 193 194 return getProxy(childApplicationRefObjectName, LoadBalancerApplicationMonitor.class); 195 } 196 197 protected void registerLoadBalancerContextRootMonitor( 198 LoadBalancerApplicationMonitor loadBalancerApplicationMonitor, 199 String ctxRootName) throws JMException { 200 201 ObjectName newChildAppRefObjectName = 202 Util.getObjectName(loadBalancerApplicationMonitor); 203 204 ObjectName childContextRootObjectName = 205 objectNames.buildContaineeObjectName( 206 newChildAppRefObjectName, 207 loadBalancerApplicationMonitor.getFullType(), 208 LOAD_BALANCER_CONTEXT_ROOT_MONITOR, 209 ctxRootName); 210 LoadBalancerContextRootMonitorImpl 211 loadBalancerContextRootMonitorImpl = 212 new LoadBalancerContextRootMonitorImpl(); 213 214 ((MBeanServer )getConn()).registerMBean( 215 loadBalancerContextRootMonitorImpl, 216 childContextRootObjectName); 217 } 218 219 protected List <LoadBalancerMonitor> fetchLBMonitoringRoots( 220 LBConfig lbConfig) throws JMException { 221 222 List <LoadBalancerMonitor> monitoringRoots = new ArrayList <LoadBalancerMonitor>(); 223 String name = lbConfig.getName(); 224 Map <String , LoadBalancerConfig> loadBalancerConfigMap = 225 getDomainConfig().getLoadBalancerConfigMap(); 226 Map <String , LoadBalancerConfig> relatedLoadBalancerConfigMap = 227 new HashMap <String , LoadBalancerConfig>(); 228 229 for (String loadBalancerName : loadBalancerConfigMap.keySet()) { 230 LoadBalancerConfig loadBalancerConfig = 231 loadBalancerConfigMap.get(loadBalancerName); 232 if (loadBalancerConfig.getLbConfigName().equals(name)) { 233 relatedLoadBalancerConfigMap.put( 234 loadBalancerName, loadBalancerConfig); 235 monitoringRoots.add(fetchMonitoringPeer(loadBalancerConfig)); 236 } 237 } 238 239 return monitoringRoots; 240 } 241 242 protected ProxyFactory 243 getProxyFactory() 244 { 245 return ProxyFactory.getInstance( getConn() ); 246 } 247 248 protected <T extends AMX> T 249 getProxy( final ObjectName objectName, final Class <T> theClass) 250 { 251 return getProxyFactory().getProxy( objectName, theClass ); 252 } 253 254 protected LoadBalancerMonitor fetchMonitoringPeer( 255 LoadBalancerConfig loadBalancerConfig) throws JMException { 256 String name = loadBalancerConfig.getName(); 257 ObjectName loadBalancerMonitorObjName = new ObjectName ( 259 JMX_DOMAIN + ":" + J2EE_TYPE_KEY + "=" + LOAD_BALANCER_MONITOR + 260 "," + NAME_KEY + "=" + name); 261 LoadBalancerMonitor loadBalancerMonitor = 262 getProxy(loadBalancerMonitorObjName, LoadBalancerMonitor.class); 263 264 return loadBalancerMonitor; 265 } 266 267 protected List <LoadBalancerClusterMonitor> fetchLoadBalancerClusterMonitors( 268 List <LoadBalancerMonitor> monitoringRoots, String clusterName) { 269 270 List <LoadBalancerClusterMonitor> result = new ArrayList <LoadBalancerClusterMonitor>(); 271 272 for (LoadBalancerMonitor monitoringRoot : monitoringRoots) { 273 LoadBalancerClusterMonitor loadBalancerClusterMonitor = 274 monitoringRoot.getLoadBalancerClusterMonitorMap().get(clusterName); 275 if (loadBalancerClusterMonitor != null) 276 result.add(loadBalancerClusterMonitor); 277 } 278 279 return result; 280 } 281 282 protected Collection <LBConfig> fetchLBConfigs( 283 String targetName, boolean isCluster) { 284 285 Map <String ,LBConfig> result = new HashMap <String ,LBConfig>(); 286 Map <String ,LBConfig> lbConfigMap = getDomainConfig().getLBConfigMap(); 287 288 if (isCluster) { 289 for (String lbConfigName : lbConfigMap.keySet()) { 290 LBConfig lbConfig = lbConfigMap.get(lbConfigName); 291 Map <String ,ClusterRefConfig> lbClusterRefConfigMap = 292 lbConfig.getClusterRefConfigMap(); 293 for (String clusterRef : lbClusterRefConfigMap.keySet()) { 294 if (clusterRef.equals(targetName)) { 295 result.put(lbConfigName, lbConfig); 296 break; 297 } 298 } 299 } 300 } else { 301 304 for (String lbConfigName : lbConfigMap.keySet()) { 305 LBConfig lbConfig = lbConfigMap.get(lbConfigName); 306 Map <String ,ServerRefConfig> lbServerRefConfigMap = 307 lbConfig.getServerRefConfigMap(); 308 for (String serverRef : lbServerRefConfigMap.keySet()) { 309 if (serverRef.equals(targetName)) { 310 result.put(lbConfigName, lbConfig); 311 break; 312 } 313 } 314 } 315 for (String lbConfigName : lbConfigMap.keySet()) { 316 LBConfig lbConfig = lbConfigMap.get(lbConfigName); 317 Map <String ,ClusterRefConfig> lbClusterRefConfigMap = 318 lbConfig.getClusterRefConfigMap(); 319 Map <String ,ClusterConfig> clusterConfigMap = 320 getDomainConfig().getClusterConfigMap(); 321 Map <String ,ClusterConfig> relevantClusterConfigMap = new HashMap <String ,ClusterConfig>(); 322 for (String clusterRef : lbClusterRefConfigMap.keySet()) 323 relevantClusterConfigMap.put(clusterRef, 324 clusterConfigMap.get(clusterRef)); 325 for (String clusterName : relevantClusterConfigMap.keySet()) { 327 ClusterConfig clusterConfig = 328 relevantClusterConfigMap.get(clusterName); 329 Map <String ,ServerRefConfig> clusteredServerRefConfigMap = 330 clusterConfig.getServerRefConfigMap(); 331 for (String serverRef : clusteredServerRefConfigMap.keySet()) { 332 if (serverRef.equals(targetName)) { 333 result.put(lbConfigName, lbConfig); 334 break; 335 } 336 } 337 } 338 } 339 } 340 return result.values(); 341 } 342 343 protected void registerLoadBalancerApplicationMonitorTree( 344 LoadBalancerServerMonitor loadBalancerServerMonitor, String appName) 345 throws JMException { 346 try{ 347 if(ApplicationHelper.isSystemApp(DeploymentServiceUtils.getConfigContext(),appName)) { 348 return; 349 } 350 LoadBalancerApplicationMonitor loadBalancerApplicationMonitor = 351 registerLoadBalancerApplicationMonitor( 352 loadBalancerServerMonitor, appName); 353 String [] ctxRootNames = ApplicationConfigHelper.getAppContextRoots(DeploymentServiceUtils.getConfigContext(),appName); 354 for (String ctxRootName : ctxRootNames) { 355 registerLoadBalancerContextRootMonitor( 356 loadBalancerApplicationMonitor, ctxRootName); 357 } 358 } catch(Exception e){ 359 e.printStackTrace(); 360 } 361 } 362 363 protected void registerLoadBalancerServerMonitorTree( 364 LoadBalancerClusterMonitor loadBalancerClusterMonitor, 365 Map <String , ServerConfig> serverConfigMap, String serverName) 366 throws JMException { 367 368 LoadBalancerServerMonitor loadBalancerServerMonitor = 369 registerLoadBalancerServerMonitor( 370 loadBalancerClusterMonitor, serverName); 371 372 ServerConfig serverConfig = 373 (ServerConfig) serverConfigMap.get(serverName); 374 Map <String ,DeployedItemRefConfig> deployedItemRefConfigMap = 375 serverConfig.getDeployedItemRefConfigMap(); 376 377 for (String appName : deployedItemRefConfigMap.keySet()) { 378 379 try{ 380 if(ApplicationHelper.isSystemApp(DeploymentServiceUtils.getConfigContext(),appName)) { 382 continue; 383 } 384 LoadBalancerApplicationMonitor loadBalancerApplicationMonitor = 385 registerLoadBalancerApplicationMonitor( 386 loadBalancerServerMonitor, appName); 387 String [] ctxRootNames = ApplicationConfigHelper.getAppContextRoots(DeploymentServiceUtils.getConfigContext(),appName); 388 for (String ctxRootName : ctxRootNames) { 389 if(ctxRootName.startsWith("/")) 390 ctxRootName = ctxRootName.substring(1); 391 registerLoadBalancerContextRootMonitor( 392 loadBalancerApplicationMonitor, ctxRootName); 393 } 394 }catch(ConfigException ex){ 395 logWarning( 396 "LoadBalancerServerRefRegistrationListener" + 397 " registerLoadBalancerServerMonitorTree failed. " + 398 "Exception = ", ex); 399 }catch(IASDeploymentException ex){ 400 logWarning( 401 "LoadBalancerServerRefRegistrationListener" + 402 " registerLoadBalancerServerMonitorTree failed. " + 403 "Exception = ", ex); 404 } 405 } 406 } 407 408 protected void registerLoadBalancerClusterMonitorTree( 409 LoadBalancerMonitor lbm , String clusterName) throws JMException { 410 411 LoadBalancerClusterMonitor lbcm = 412 registerLoadBalancerClusterMonitor(lbm, clusterName); 413 414 Map <String ,ClusterConfig> clusterConfigMap = 417 getDomainConfig().getClusterConfigMap(); 418 ClusterConfig clusterConfig = 419 (ClusterConfig)clusterConfigMap.get(clusterName); 420 421 Map <String , ClusteredServerConfig> cServerConfigMap = 424 clusterConfig.getClusteredServerConfigMap(); 425 426 Map <String , ServerConfig> serverConfigMap = new HashMap <String , ServerConfig>(); 429 for (String key : cServerConfigMap.keySet()) 430 serverConfigMap.put(key, (ServerConfig)cServerConfigMap.get(key)); 431 432 for (String serverName : serverConfigMap.keySet()) { 433 registerLoadBalancerServerMonitorTree( 434 lbcm, serverConfigMap, serverName); 435 } 436 } 437 438 protected DomainConfig getDomainConfig() { 439 return 440 ProxyFactory.getInstance(getConn()).getDomainRoot().getDomainConfig(); 441 } 442 443 protected LBDeregistrationUtil getLBDeregistrationUtil( 444 MBeanServer mBeanServer) { 445 return LBDeregistrationUtil.getInstance(mBeanServer); 446 } 447 448 protected void debug(Object o) { 449 if (o instanceof Throwable ) ((Throwable )o).printStackTrace(); 450 AMXDebug.getInstance().getOutput("TestLoadBalancerLoad") 451 .println(StringUtil.toString(", ", o)); 452 } 453 454 protected void logWarning(String prefix, Exception ex) { 455 AMXRootLogger.getInstance().warning(prefix + " : " + ex.getMessage()); 456 } 457 } | Popular Tags |