KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.JMException JavaDoc;
35 import javax.management.InstanceNotFoundException JavaDoc;
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 /**
90  * Convenience base class for performing registrations
91  * for various load balancer related monitors.
92 */

93 public abstract class LBBaseMBeanRegistrationListener
94     extends MBeanRegistrationListener {
95     
96     final ObjectNames objectNames = ObjectNames.getInstance(JMX_DOMAIN);
97     
98     public LBBaseMBeanRegistrationListener(
99         final MBeanServer JavaDoc mBeanServer, final ObjectName JavaDoc constraint )
100         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
101         
102     super(mBeanServer, constraint);
103     }
104     
105     public LBBaseMBeanRegistrationListener(final MBeanServer JavaDoc mBeanServer)
106         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
107         
108         this(mBeanServer, (ObjectName JavaDoc)null );
109     }
110     
111     public void mbeanUnregistered(final ObjectName JavaDoc objectName) {}
112            
113     protected LoadBalancerClusterMonitor registerLoadBalancerClusterMonitor(
114         LoadBalancerMonitor loadBalancerMonitor, String JavaDoc clusterName)
115         throws JMException JavaDoc {
116         
117         ObjectName JavaDoc loadBalancerMonitorObjName =
118             Util.getObjectName(loadBalancerMonitor);
119
120         ObjectName JavaDoc 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 JavaDoc)getConn()).registerMBean(
130                 loadBalancerClusterMonitorImpl, childClusterRefObjectName);
131         } catch( JMException JavaDoc 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 JavaDoc serverName)
142         throws JMException JavaDoc {
143
144         ObjectName JavaDoc newChildClusterRefObjectName =
145             Util.getObjectName(loadBalancerClusterMonitor);
146
147         ObjectName JavaDoc 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 JavaDoc)getConn()).registerMBean(
157                 loadBalancerServerMonitorImpl, childServerRefObjectName);
158         } catch( JMException JavaDoc 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 JavaDoc appName)
169         throws JMException JavaDoc{
170
171         ObjectName JavaDoc newChildServerRefObjectName =
172             Util.getObjectName(loadBalancerServerMonitor);
173
174         ObjectName JavaDoc 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 JavaDoc)getConn()).registerMBean(
185                 loadBalancerApplicationMonitorImpl,
186                 childApplicationRefObjectName );
187         } catch( JMException JavaDoc 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 JavaDoc ctxRootName) throws JMException JavaDoc {
200      
201         ObjectName JavaDoc newChildAppRefObjectName =
202             Util.getObjectName(loadBalancerApplicationMonitor);
203
204         ObjectName JavaDoc 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 JavaDoc)getConn()).registerMBean(
215             loadBalancerContextRootMonitorImpl,
216             childContextRootObjectName);
217     }
218     
219     protected List JavaDoc<LoadBalancerMonitor> fetchLBMonitoringRoots(
220         LBConfig lbConfig) throws JMException JavaDoc {
221         
222         List JavaDoc<LoadBalancerMonitor> monitoringRoots = new ArrayList JavaDoc<LoadBalancerMonitor>();
223         String JavaDoc name = lbConfig.getName();
224         Map JavaDoc<String JavaDoc, LoadBalancerConfig> loadBalancerConfigMap =
225                 getDomainConfig().getLoadBalancerConfigMap();
226         Map JavaDoc<String JavaDoc, LoadBalancerConfig> relatedLoadBalancerConfigMap =
227                 new HashMap JavaDoc<String JavaDoc, LoadBalancerConfig>();
228         
229         for (String JavaDoc 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 JavaDoc objectName, final Class JavaDoc<T> theClass)
250     {
251         return getProxyFactory().getProxy( objectName, theClass );
252     }
253     
254     protected LoadBalancerMonitor fetchMonitoringPeer(
255         LoadBalancerConfig loadBalancerConfig) throws JMException JavaDoc {
256             String JavaDoc name = loadBalancerConfig.getName();
257             //Step1: Create the root empty LoadBalancerMonitor
258
ObjectName JavaDoc loadBalancerMonitorObjName = new ObjectName JavaDoc(
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 JavaDoc<LoadBalancerClusterMonitor> fetchLoadBalancerClusterMonitors(
268         List JavaDoc<LoadBalancerMonitor> monitoringRoots, String JavaDoc clusterName) {
269         
270         List JavaDoc<LoadBalancerClusterMonitor> result = new ArrayList JavaDoc<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 JavaDoc<LBConfig> fetchLBConfigs(
283         String JavaDoc targetName, boolean isCluster) {
284
285         Map JavaDoc<String JavaDoc,LBConfig> result = new HashMap JavaDoc<String JavaDoc,LBConfig>();
286         Map JavaDoc<String JavaDoc,LBConfig> lbConfigMap = getDomainConfig().getLBConfigMap();
287         
288         if (isCluster) {
289             for (String JavaDoc lbConfigName : lbConfigMap.keySet()) {
290                 LBConfig lbConfig = lbConfigMap.get(lbConfigName);
291                 Map JavaDoc<String JavaDoc,ClusterRefConfig> lbClusterRefConfigMap =
292                     lbConfig.getClusterRefConfigMap();
293                 for (String JavaDoc clusterRef : lbClusterRefConfigMap.keySet()) {
294                     if (clusterRef.equals(targetName)) {
295                         result.put(lbConfigName, lbConfig);
296                         break;
297                     }
298                 }
299             }
300         } else {
301          /*its a serverName which means you have to find LBConfigs containing
302          1. standalone server references with the same name
303          2. clustered server references with the same name */

304             for (String JavaDoc lbConfigName : lbConfigMap.keySet()) {
305                 LBConfig lbConfig = lbConfigMap.get(lbConfigName);
306                 Map JavaDoc<String JavaDoc,ServerRefConfig> lbServerRefConfigMap =
307                     lbConfig.getServerRefConfigMap();
308                 for (String JavaDoc serverRef : lbServerRefConfigMap.keySet()) {
309                     if (serverRef.equals(targetName)) {
310                         result.put(lbConfigName, lbConfig);
311                         break;
312                     }
313                 }
314             }
315             for (String JavaDoc lbConfigName : lbConfigMap.keySet()) {
316                 LBConfig lbConfig = lbConfigMap.get(lbConfigName);
317                 Map JavaDoc<String JavaDoc,ClusterRefConfig> lbClusterRefConfigMap =
318                     lbConfig.getClusterRefConfigMap();
319                 Map JavaDoc<String JavaDoc,ClusterConfig> clusterConfigMap =
320                     getDomainConfig().getClusterConfigMap();
321                 Map JavaDoc<String JavaDoc,ClusterConfig> relevantClusterConfigMap = new HashMap JavaDoc<String JavaDoc,ClusterConfig>();
322                 for (String JavaDoc clusterRef : lbClusterRefConfigMap.keySet())
323                     relevantClusterConfigMap.put(clusterRef,
324                         clusterConfigMap.get(clusterRef));
325                 //so now we have the right set of <cluster> elements
326
for (String JavaDoc clusterName : relevantClusterConfigMap.keySet()) {
327                     ClusterConfig clusterConfig =
328                         relevantClusterConfigMap.get(clusterName);
329                     Map JavaDoc<String JavaDoc,ServerRefConfig> clusteredServerRefConfigMap =
330                         clusterConfig.getServerRefConfigMap();
331                     for (String JavaDoc 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 JavaDoc appName)
345         throws JMException JavaDoc {
346         try{
347             if(ApplicationHelper.isSystemApp(DeploymentServiceUtils.getConfigContext(),appName)) {
348                 return;
349             }
350             LoadBalancerApplicationMonitor loadBalancerApplicationMonitor =
351                     registerLoadBalancerApplicationMonitor(
352                     loadBalancerServerMonitor, appName);
353             String JavaDoc [] ctxRootNames = ApplicationConfigHelper.getAppContextRoots(DeploymentServiceUtils.getConfigContext(),appName);
354             for (String JavaDoc ctxRootName : ctxRootNames) {
355                 registerLoadBalancerContextRootMonitor(
356                         loadBalancerApplicationMonitor, ctxRootName);
357             }
358         } catch(Exception JavaDoc e){
359             e.printStackTrace();
360         }
361     }
362     
363     protected void registerLoadBalancerServerMonitorTree(
364         LoadBalancerClusterMonitor loadBalancerClusterMonitor,
365         Map JavaDoc<String JavaDoc, ServerConfig> serverConfigMap, String JavaDoc serverName)
366         throws JMException JavaDoc {
367
368         LoadBalancerServerMonitor loadBalancerServerMonitor =
369             registerLoadBalancerServerMonitor(
370                 loadBalancerClusterMonitor, serverName);
371         
372         ServerConfig serverConfig =
373             (ServerConfig) serverConfigMap.get(serverName);
374         Map JavaDoc<String JavaDoc,DeployedItemRefConfig> deployedItemRefConfigMap =
375             serverConfig.getDeployedItemRefConfigMap();
376
377         for (String JavaDoc appName : deployedItemRefConfigMap.keySet()) {
378
379             try{
380                 //if the type is user, then only set the lb-enabled to true
381
if(ApplicationHelper.isSystemApp(DeploymentServiceUtils.getConfigContext(),appName)) {
382                     continue;
383                 }
384                 LoadBalancerApplicationMonitor loadBalancerApplicationMonitor =
385                         registerLoadBalancerApplicationMonitor(
386                         loadBalancerServerMonitor, appName);
387                 String JavaDoc [] ctxRootNames = ApplicationConfigHelper.getAppContextRoots(DeploymentServiceUtils.getConfigContext(),appName);
388                 for (String JavaDoc 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 JavaDoc clusterName) throws JMException JavaDoc {
410         
411         LoadBalancerClusterMonitor lbcm =
412             registerLoadBalancerClusterMonitor(lbm, clusterName);
413
414         //1. get ALL <cluster> elements and find the correct <cluster>
415
//element corresponding to the one that is passed in
416
Map JavaDoc<String JavaDoc,ClusterConfig> clusterConfigMap =
417             getDomainConfig().getClusterConfigMap();
418         ClusterConfig clusterConfig =
419             (ClusterConfig)clusterConfigMap.get(clusterName);
420                         
421         //2. Get corresponding <server-ref> elements for this <cluster> element
422
//found in step 1
423
Map JavaDoc <String JavaDoc, ClusteredServerConfig> cServerConfigMap =
424             clusterConfig.getClusteredServerConfigMap();
425         
426         //doing the following to enable reuse of the
427
//registerLoadBalancerServerMonitorTree method
428
Map JavaDoc<String JavaDoc, ServerConfig> serverConfigMap = new HashMap JavaDoc<String JavaDoc, ServerConfig>();
429         for (String JavaDoc key : cServerConfigMap.keySet())
430             serverConfigMap.put(key, (ServerConfig)cServerConfigMap.get(key));
431         
432         for (String JavaDoc 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 JavaDoc mBeanServer) {
445         return LBDeregistrationUtil.getInstance(mBeanServer);
446     }
447     
448     protected void debug(Object JavaDoc o) {
449         if (o instanceof Throwable JavaDoc) ((Throwable JavaDoc)o).printStackTrace();
450         AMXDebug.getInstance().getOutput("TestLoadBalancerLoad")
451             .println(StringUtil.toString(", ", o));
452     }
453     
454     protected void logWarning(String JavaDoc prefix, Exception JavaDoc ex) {
455         AMXRootLogger.getInstance().warning(prefix + " : " + ex.getMessage());
456     }
457 }
Popular Tags