KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > LoadBalancerRegistrationListener


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.io.IOException JavaDoc;
46 import javax.management.AttributeChangeNotification JavaDoc;
47
48 import javax.management.InstanceNotFoundException JavaDoc;
49 import javax.management.JMException JavaDoc;
50 import javax.management.MBeanServer JavaDoc;
51 import javax.management.Notification JavaDoc;
52 import javax.management.NotificationListener JavaDoc;
53 import javax.management.ObjectName JavaDoc;
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 /**
72  * A class that triggers forming LB monitoring tree whenever a new <load-balancer>
73  * gets registered. The registration logic
74  * 1. register a runtime LoadBalancer MBean
75  * 2. register a listener to listen on the attribute monitoring-enabled in the
76  * <lb-config> referenced by this <load-balancer>
77  * 3. create the monitoring tree starting with
78  * a. the root LoadBalancerMonitor
79  * b. LoadBalancerClusterMonitor(s) under root LoadBalancerMonitor
80  * c. LoadBalancerServerMonitor(s) under each LoadBalancerClusterMonitor
81  * d. LoadBalancerApplicationMonitor(s) under each LoadBalancerServerMonitor
82  * e. LoadBalancerContextRootMonitor(s) under each LoadBalancerApplicationMonitor
83  * @see LoadBalancerClusterRefRegistrationListener
84  * @see LoadBalancerApplicationRefRegistrationListener
85  * @see LoadBalancerRegistrationListener
86  */

87 public class LoadBalancerRegistrationListener extends LBBaseMBeanRegistrationListener {
88     
89     final static String JavaDoc MONITORING_ENABLED = "MonitoringEnabled";
90
91     public LoadBalancerRegistrationListener(MBeanServer JavaDoc mBeanServer)
92         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
93         super(mBeanServer);
94     }
95
96     LoadBalancer registerLoadBalancer(String JavaDoc name) {
97         //create load balancer runtime mbean
98
DomainRoot domainRoot =
99             (DomainRoot) ProxyFactory.getInstance(getConn()).getDomainRoot();
100         ObjectName JavaDoc domainRootObjName = Util.getObjectName(domainRoot);
101         
102         ObjectName JavaDoc loadBalancerObjName =
103             objectNames.buildContaineeObjectName( domainRootObjName,
104                 domainRoot.getFullType(), LOAD_BALANCER, name);
105
106         LoadBalancerImpl loadBalancerImpl = new LoadBalancerImpl(getConn());
107         try {
108             ((MBeanServer JavaDoc)getConn()).registerMBean(
109                 loadBalancerImpl, loadBalancerObjName);
110         } catch( JMException JavaDoc 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 JavaDoc 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 JavaDoc name = loadBalancerConfig.getName();
126                 String JavaDoc loadBalancerConfigName = loadBalancerConfig.getName();
127                 LoadBalancer loadBalancer =
128                     registerLoadBalancer(loadBalancerConfig.getName());
129                 //Add a listener for the monitoring-enabled attribute of the
130
//referenced lb config mbean and store the reference to this
131
//listener for removal when this config is unregistered
132
String JavaDoc 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 JavaDoc ex) {
142             ex.printStackTrace();
143             logWarning(
144                 "LoadBalancerRegistrationListener mbeanRegistered " +
145                     "failed. Exception = ", ex);
146         }
147     }
148     
149     private LoadBalancerMonitor registerLoadBalancerMonitor(String JavaDoc name) {
150         
151         //create root load balancer monitor
152
MonitoringRoot monitoringRoot = (MonitoringRoot) ProxyFactory
153             .getInstance(getConn()).getDomainRoot().getMonitoringRoot();
154         ObjectName JavaDoc monitoringRootObjName = Util.getObjectName(monitoringRoot);
155         
156         ObjectName JavaDoc loadBalancerMonitorObjName =
157             objectNames.buildContaineeObjectName(monitoringRootObjName,
158                 monitoringRoot.getFullType(), LOAD_BALANCER_MONITOR, name);
159
160         LoadBalancerMonitorImpl loadBalancerMonitorImpl =
161             new LoadBalancerMonitorImpl();
162         try {
163             ((MBeanServer JavaDoc)getConn()).registerMBean(
164                 loadBalancerMonitorImpl, loadBalancerMonitorObjName);
165         } catch( JMException JavaDoc 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 JavaDoc name, LBConfig lbConfig) throws Exception JavaDoc {
176
177         //Step1: Create the root empty LoadBalancerMonitor
178
LoadBalancerMonitor lbm = registerLoadBalancerMonitor(name);
179         
180         //Step2: Create the default clusters LoadBalancerClusterMonitors and stick
181
//in the monitoring mbeans corresponding to the standalone server
182
//instances - menaing server name and cluster name is same -
183
//in these pseudo LoadBalancerClusterMonitors
184

185         //get <server-ref> elements in this lbConfig
186
Map JavaDoc<String JavaDoc, ServerRefConfig> serverRefConfigMap =
187             lbConfig.getServerRefConfigMap();
188         
189         if (!serverRefConfigMap.isEmpty()) {
190             //get ALL standalone <server> elements
191
Map JavaDoc<String JavaDoc,StandaloneServerConfig> ssConfigMap =
192                 getDomainConfig().getStandaloneServerConfigMap();
193             Map JavaDoc<String JavaDoc, ServerConfig> serverConfigMap = new HashMap JavaDoc<String JavaDoc, ServerConfig>();
194             //doing the following to enable reuse of the
195
//registerLoadBalancerServerMonitorTree base class method
196
for (String JavaDoc key : ssConfigMap.keySet())
197                 serverConfigMap.put(key, (ServerConfig)ssConfigMap.get(key));
198
199             for (String JavaDoc serverName : serverRefConfigMap.keySet()) {
200                 LoadBalancerClusterMonitor lbcm =
201                     registerLoadBalancerClusterMonitor(lbm, serverName);
202                 registerLoadBalancerServerMonitorTree(
203                     lbcm, serverConfigMap, serverName);
204             }
205         }
206      
207         //Step3: Create the LoadBalancerClusterMonitor and children tree based
208
//on the <cluster-ref> elements referred to by this lb-config
209
//there may be no cluster-ref elements.
210

211         //Beware both serverRefConfigMap and clusterRefConfigMap CANNOT be non-empty
212
//DTD says <lb-config> = (<cluster-ref>* | <server-ref>*, property*)
213

214         //get <cluster-ref> elements in this lbConfig
215
Map JavaDoc<String JavaDoc, ClusterRefConfig> clusterRefConfigMap =
216             lbConfig.getClusterRefConfigMap();
217
218         if (!clusterRefConfigMap.isEmpty())
219             for (String JavaDoc clusterName : clusterRefConfigMap.keySet())
220                 registerLoadBalancerClusterMonitorTree(lbm, clusterName);
221     }
222
223     void disableMonitoring(String JavaDoc loadBalancerName) throws Exception JavaDoc {
224         
225        LoadBalancerMonitor lbm =
226            getLBDeregistrationUtil((MBeanServer JavaDoc)getConn())
227            .fetchLBMonitoringRoot(loadBalancerName);
228
229        Map JavaDoc<String JavaDoc,LoadBalancerClusterMonitor> lbcmMap =
230            lbm.getLoadBalancerClusterMonitorMap();
231
232        for (LoadBalancerClusterMonitor lbcm : lbcmMap.values()) {
233            //loadBalancerServerMonitorMap will have only one element for every
234
//standalone server as it is is wrapped up in a default cluster
235
Map JavaDoc<String JavaDoc,LoadBalancerServerMonitor> lbsmMap =
236                 lbcm.getLoadBalancerServerMonitorMap();
237
238            for (LoadBalancerServerMonitor lbsm : lbsmMap.values()) {
239
240                Map JavaDoc<String JavaDoc,LoadBalancerApplicationMonitor>
241                    lbamMap = lbsm.getLoadBalancerApplicationMonitorMap();
242
243                for (LoadBalancerApplicationMonitor lbam : lbamMap.values()) {
244                    Map JavaDoc<String JavaDoc,LoadBalancerContextRootMonitor>
245                         lbcrmMap = lbam.getLoadBalancerContextRootMonitorMap();
246                    for (LoadBalancerContextRootMonitor lbcrm : lbcrmMap.values()) {
247                         ObjectName JavaDoc lbcrmObjName = Util.getObjectName(lbcrm);
248                         ((MBeanServer JavaDoc)getConn()).unregisterMBean(lbcrmObjName );
249                     }
250
251                     ObjectName JavaDoc lbamObjName = Util.getObjectName(lbam);
252                     ((MBeanServer JavaDoc)getConn()).unregisterMBean(lbamObjName);
253                 }
254
255                 ObjectName JavaDoc loadBalancerServerMonitorObjName =
256                     Util.getObjectName(lbsm);
257                 ((MBeanServer JavaDoc)getConn())
258                     .unregisterMBean(loadBalancerServerMonitorObjName);
259             }
260
261             ObjectName JavaDoc lbcmObjName = Util.getObjectName(lbcm);
262                 ((MBeanServer JavaDoc)getConn()).unregisterMBean(lbcmObjName);
263        }
264
265        ObjectName JavaDoc lbmObjName = Util.getObjectName(lbm);
266        ((MBeanServer JavaDoc)getConn()).unregisterMBean(lbmObjName);
267     }
268     
269     void addLBConfigListener(String JavaDoc loadBalancerName, LBConfig lbConfig) {
270         LBConfigListener mLBConfigListener =
271             new LBConfigListener(loadBalancerName);
272         getLBDeregistrationUtil((MBeanServer JavaDoc)getConn())
273             .addLBConfigListener(loadBalancerName, mLBConfigListener, lbConfig);
274     }
275     
276     /**
277      * A class that listens on the attribute monitoring-enabled in the
278      * <lb-config> element. There is one instance of this listener per
279      * <load-balancer>. Hence if multiple <load-balancer>s reference a
280      * <lb-config>, there are multiple listeners for the same <lb-config>
281      * This Map<load-balancer-name, LBConfigListener> is stored in
282      * LBDeregistrationUtil. LoadBalancerConfig.postDeregister removes
283      * the listener corresponding to
284      * @see com.sun.enterprise.management.support.LBDeregistrationUtil
285      *
286      */

287     private final class LBConfigListener implements NotificationListener JavaDoc {
288
289         String JavaDoc mLoadBalancerConfigName;
290         
291         public LBConfigListener(String JavaDoc name) {
292             mLoadBalancerConfigName = name;
293         }
294         
295         public void handleNotification(
296             final Notification JavaDoc notif, final Object JavaDoc handback) {
297             try {
298                 if (notif.getType().equals(
299                     AttributeChangeNotification.ATTRIBUTE_CHANGE)) {
300                     
301                     AttributeChangeNotification JavaDoc attrChangeNotif =
302                         (AttributeChangeNotification JavaDoc) notif;
303                     
304                     if (MONITORING_ENABLED.equals(
305                             attrChangeNotif.getAttributeName())) {
306                      
307                         if ((Boolean JavaDoc)attrChangeNotif.getNewValue() !=
308                                 (Boolean JavaDoc)attrChangeNotif.getOldValue()) {
309                     
310                             if (attrChangeNotif.getNewValue().equals(Boolean.TRUE)) {
311                                 ObjectName JavaDoc lbConfigObjName =
312                                     (ObjectName JavaDoc) 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 JavaDoc ex) {
323                 logWarning(
324                     "LBBootstrapUtil$LBConfigListener" +
325                     " handleNotification failed. Exception = ", ex);
326             }
327         }
328     }
329 }
330
Popular Tags