1 23 package com.sun.enterprise.management.support; 24 25 import com.sun.appserv.management.base.AMXDebug; 26 import com.sun.appserv.management.monitor.LoadBalancerMonitor; 27 import com.sun.appserv.management.monitor.LoadBalancerClusterMonitor; 28 import com.sun.appserv.management.monitor.LoadBalancerServerMonitor; 29 import com.sun.appserv.management.monitor.LoadBalancerApplicationMonitor; 30 import com.sun.appserv.management.monitor.LoadBalancerContextRootMonitor; 31 import com.sun.appserv.management.config.DeployedItemRefConfig; 32 import com.sun.appserv.management.config.ClusterRefConfig; 33 import com.sun.appserv.management.config.ClusterConfig; 34 import com.sun.appserv.management.config.ServerRefConfig; 35 import com.sun.appserv.management.config.ServerConfig; 36 import com.sun.appserv.management.config.StandaloneServerConfig; 37 import com.sun.appserv.management.config.LoadBalancerConfig; 38 import com.sun.appserv.management.config.LBConfig; 39 import com.sun.appserv.management.ext.lb.LoadBalancer; 40 import com.sun.appserv.management.monitor.MonitoringRoot; 41 import com.sun.appserv.management.DomainRoot; 42 43 import java.util.Map ; 44 import java.util.HashMap ; 45 import java.io.IOException ; 46 import javax.management.AttributeChangeNotification ; 47 48 import javax.management.InstanceNotFoundException ; 49 import javax.management.JMException ; 50 import javax.management.MBeanServer ; 51 import javax.management.Notification ; 52 import javax.management.NotificationListener ; 53 import javax.management.ObjectName ; 54 55 import com.sun.appserv.management.base.XTypes; 56 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER; 57 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_CONFIG; 58 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_MONITOR; 59 import static com.sun.appserv.management.base.XTypes.LB_CONFIG; 60 61 import com.sun.appserv.management.base.AMX; 62 import static com.sun.appserv.management.base.AMX.J2EE_TYPE_KEY; 63 import static com.sun.appserv.management.base.AMX.NAME_KEY; 64 import static com.sun.appserv.management.base.AMX.JMX_DOMAIN; 65 66 import com.sun.appserv.management.base.Util; 67 import com.sun.appserv.management.client.ProxyFactory; 68 import com.sun.enterprise.management.monitor.LoadBalancerMonitorImpl; 69 import com.sun.enterprise.management.ext.lb.LoadBalancerImpl; 70 71 87 public class LoadBalancerRegistrationListener extends LBBaseMBeanRegistrationListener { 88 89 final static String MONITORING_ENABLED = "MonitoringEnabled"; 90 91 public LoadBalancerRegistrationListener(MBeanServer mBeanServer) 92 throws InstanceNotFoundException , IOException { 93 super(mBeanServer); 94 } 95 96 LoadBalancer registerLoadBalancer(String name) { 97 DomainRoot domainRoot = 99 (DomainRoot) ProxyFactory.getInstance(getConn()).getDomainRoot(); 100 ObjectName domainRootObjName = Util.getObjectName(domainRoot); 101 102 ObjectName loadBalancerObjName = 103 objectNames.buildContaineeObjectName( domainRootObjName, 104 domainRoot.getFullType(), LOAD_BALANCER, name); 105 106 LoadBalancerImpl loadBalancerImpl = new LoadBalancerImpl(getConn()); 107 try { 108 ((MBeanServer )getConn()).registerMBean( 109 loadBalancerImpl, loadBalancerObjName); 110 } catch( JMException ex ) { 111 logWarning( 112 "LoadBalancerRegistrationListener registerLoadBalancer " + 113 "failed. Exception = ", ex); 114 } 115 116 return ProxyFactory.getInstance( 117 getConn()).getProxy(loadBalancerObjName, LoadBalancer.class); 118 } 119 120 protected void mbeanRegistered(final ObjectName objectName) { 121 try { 122 if (LOAD_BALANCER_CONFIG.equals(Util.getJ2EEType(objectName))) { 123 LoadBalancerConfig loadBalancerConfig = 124 ProxyFactory.getInstance(getConn()).getProxy(objectName, LoadBalancerConfig.class); 125 String name = loadBalancerConfig.getName(); 126 String loadBalancerConfigName = loadBalancerConfig.getName(); 127 LoadBalancer loadBalancer = 128 registerLoadBalancer(loadBalancerConfig.getName()); 129 String lbConfigName = loadBalancerConfig.getLbConfigName(); 133 LBConfig lbConfig = 134 getDomainConfig().getLBConfigMap().get(lbConfigName); 135 136 addLBConfigListener(name, lbConfig); 137 138 if (lbConfig.getMonitoringEnabled()) 139 enableMonitoring(name, lbConfig); 140 } 141 } catch (Exception ex) { 142 ex.printStackTrace(); 143 logWarning( 144 "LoadBalancerRegistrationListener mbeanRegistered " + 145 "failed. Exception = ", ex); 146 } 147 } 148 149 private LoadBalancerMonitor registerLoadBalancerMonitor(String name) { 150 151 MonitoringRoot monitoringRoot = (MonitoringRoot) ProxyFactory 153 .getInstance(getConn()).getDomainRoot().getMonitoringRoot(); 154 ObjectName monitoringRootObjName = Util.getObjectName(monitoringRoot); 155 156 ObjectName loadBalancerMonitorObjName = 157 objectNames.buildContaineeObjectName(monitoringRootObjName, 158 monitoringRoot.getFullType(), LOAD_BALANCER_MONITOR, name); 159 160 LoadBalancerMonitorImpl loadBalancerMonitorImpl = 161 new LoadBalancerMonitorImpl(); 162 try { 163 ((MBeanServer )getConn()).registerMBean( 164 loadBalancerMonitorImpl, loadBalancerMonitorObjName); 165 } catch( JMException ex ) { 166 logWarning( 167 "LoadBalancerRegistrationListener registerLoadBalancerMonitor " + 168 "failed. Exception = ", ex); 169 } 170 171 return ProxyFactory.getInstance( 172 getConn()).getProxy(loadBalancerMonitorObjName,LoadBalancerMonitor.class ); 173 } 174 175 void enableMonitoring(String name, LBConfig lbConfig) throws Exception { 176 177 LoadBalancerMonitor lbm = registerLoadBalancerMonitor(name); 179 180 185 Map <String , ServerRefConfig> serverRefConfigMap = 187 lbConfig.getServerRefConfigMap(); 188 189 if (!serverRefConfigMap.isEmpty()) { 190 Map <String ,StandaloneServerConfig> ssConfigMap = 192 getDomainConfig().getStandaloneServerConfigMap(); 193 Map <String , ServerConfig> serverConfigMap = new HashMap <String , ServerConfig>(); 194 for (String key : ssConfigMap.keySet()) 197 serverConfigMap.put(key, (ServerConfig)ssConfigMap.get(key)); 198 199 for (String serverName : serverRefConfigMap.keySet()) { 200 LoadBalancerClusterMonitor lbcm = 201 registerLoadBalancerClusterMonitor(lbm, serverName); 202 registerLoadBalancerServerMonitorTree( 203 lbcm, serverConfigMap, serverName); 204 } 205 } 206 207 211 214 Map <String , ClusterRefConfig> clusterRefConfigMap = 216 lbConfig.getClusterRefConfigMap(); 217 218 if (!clusterRefConfigMap.isEmpty()) 219 for (String clusterName : clusterRefConfigMap.keySet()) 220 registerLoadBalancerClusterMonitorTree(lbm, clusterName); 221 } 222 223 void disableMonitoring(String loadBalancerName) throws Exception { 224 225 LoadBalancerMonitor lbm = 226 getLBDeregistrationUtil((MBeanServer )getConn()) 227 .fetchLBMonitoringRoot(loadBalancerName); 228 229 Map <String ,LoadBalancerClusterMonitor> lbcmMap = 230 lbm.getLoadBalancerClusterMonitorMap(); 231 232 for (LoadBalancerClusterMonitor lbcm : lbcmMap.values()) { 233 Map <String ,LoadBalancerServerMonitor> lbsmMap = 236 lbcm.getLoadBalancerServerMonitorMap(); 237 238 for (LoadBalancerServerMonitor lbsm : lbsmMap.values()) { 239 240 Map <String ,LoadBalancerApplicationMonitor> 241 lbamMap = lbsm.getLoadBalancerApplicationMonitorMap(); 242 243 for (LoadBalancerApplicationMonitor lbam : lbamMap.values()) { 244 Map <String ,LoadBalancerContextRootMonitor> 245 lbcrmMap = lbam.getLoadBalancerContextRootMonitorMap(); 246 for (LoadBalancerContextRootMonitor lbcrm : lbcrmMap.values()) { 247 ObjectName lbcrmObjName = Util.getObjectName(lbcrm); 248 ((MBeanServer )getConn()).unregisterMBean(lbcrmObjName ); 249 } 250 251 ObjectName lbamObjName = Util.getObjectName(lbam); 252 ((MBeanServer )getConn()).unregisterMBean(lbamObjName); 253 } 254 255 ObjectName loadBalancerServerMonitorObjName = 256 Util.getObjectName(lbsm); 257 ((MBeanServer )getConn()) 258 .unregisterMBean(loadBalancerServerMonitorObjName); 259 } 260 261 ObjectName lbcmObjName = Util.getObjectName(lbcm); 262 ((MBeanServer )getConn()).unregisterMBean(lbcmObjName); 263 } 264 265 ObjectName lbmObjName = Util.getObjectName(lbm); 266 ((MBeanServer )getConn()).unregisterMBean(lbmObjName); 267 } 268 269 void addLBConfigListener(String loadBalancerName, LBConfig lbConfig) { 270 LBConfigListener mLBConfigListener = 271 new LBConfigListener(loadBalancerName); 272 getLBDeregistrationUtil((MBeanServer )getConn()) 273 .addLBConfigListener(loadBalancerName, mLBConfigListener, lbConfig); 274 } 275 276 287 private final class LBConfigListener implements NotificationListener { 288 289 String mLoadBalancerConfigName; 290 291 public LBConfigListener(String name) { 292 mLoadBalancerConfigName = name; 293 } 294 295 public void handleNotification( 296 final Notification notif, final Object handback) { 297 try { 298 if (notif.getType().equals( 299 AttributeChangeNotification.ATTRIBUTE_CHANGE)) { 300 301 AttributeChangeNotification attrChangeNotif = 302 (AttributeChangeNotification ) notif; 303 304 if (MONITORING_ENABLED.equals( 305 attrChangeNotif.getAttributeName())) { 306 307 if ((Boolean )attrChangeNotif.getNewValue() != 308 (Boolean )attrChangeNotif.getOldValue()) { 309 310 if (attrChangeNotif.getNewValue().equals(Boolean.TRUE)) { 311 ObjectName lbConfigObjName = 312 (ObjectName ) notif.getSource(); 313 final LBConfig lbConfig = 314 ProxyFactory.getInstance(getConn()).getProxy(lbConfigObjName, LBConfig.class); 315 enableMonitoring(mLoadBalancerConfigName, lbConfig); 316 } else disableMonitoring(mLoadBalancerConfigName); 317 318 } 319 320 } 321 } 322 } catch(Exception ex) { 323 logWarning( 324 "LBBootstrapUtil$LBConfigListener" + 325 " handleNotification failed. Exception = ", ex); 326 } 327 } 328 } 329 } 330 | Popular Tags |