KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import java.util.Map 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.AMXDebug;
38 import com.sun.appserv.management.base.Container;
39 import com.sun.appserv.management.base.Util;
40 import com.sun.appserv.management.client.ProxyFactory;
41 import com.sun.enterprise.management.support.ObjectNames;
42
43 import com.sun.appserv.management.monitor.LoadBalancerMonitor;
44 import com.sun.appserv.management.monitor.LoadBalancerClusterMonitor;
45 import com.sun.appserv.management.monitor.LoadBalancerServerMonitor;
46 import com.sun.appserv.management.monitor.LoadBalancerApplicationMonitor;
47 import com.sun.appserv.management.monitor.LoadBalancerContextRootMonitor;
48
49 import static com.sun.appserv.management.base.XTypes.DEPLOYED_ITEM_REF_CONFIG;
50 import com.sun.appserv.management.config.DeployedItemRefConfig;
51 import com.sun.appserv.management.config.ClusterConfig;
52 import com.sun.appserv.management.config.ServerConfig;
53 import com.sun.appserv.management.config.LBConfig;
54
55 /**
56  * A class that triggers forming LB monitoring tree whenever a new <application-ref>
57  * gets added to a <server> or <cluster> elements. The registration logic
58  * Determines parent object <server> or <cluster>.
59  *
60  * 0. If <server>
61  * 1. locate the referring <lb-config> elements (via <server-ref> under these
62  * <lb-config> elements
63  * 2. locate the referring <load-balancer> elements and fetch corresponding
64  * root LoadBalancerMonitors
65  * 3. locate LoadBalancerServerMonitors under these root LoadBalancerMonitors
66  * (each LoadBalancerServerMonitor will reside under LoadBalancerClusterMonitor
67  * with name equal to the name of the <server> in step 0.)
68  * 4. create LoadBalancerApplicationMonitor the rest of the monitoring tree under
69  * this LoadBalancerApplicationMonitor as governed by context roots
70  * associated with this <application> element
71  *
72  * 0. If <cluster>
73  * 1. locate the referring <lb-config> elements (via <cluster-ref> under these
74  * <lb-config> elements
75  * 2. locate the referring <load-balancer> elements and fetch corresponding
76  * root LoadBalancerMonitors
77  * 3. locate LoadBalancerServerMonitors under these root LoadBalancerMonitors
78  * (each LoadBalancerServerMonitor will reside under LoadBalancerClusterMonitor
79  * with name equal to the name of the <server> in step 0.)
80  * 4. create LoadBalancerApplicationMonitor the rest of the monitoring tree
81  * under this LoadBalancerApplicationMonitor as governed by context roots
82  * associated with this <application> element
83  *
84  * @see LoadBalancerClusterRefRegistrationListener
85  * @see LoadBalancerServerRefRegistrationListener
86  * @see LoadBalancerRegistrationListener
87  */

88
89 public class LoadBalancerApplicationRefRegistrationListener
90     extends LBBaseMBeanRegistrationListener {
91
92     public LoadBalancerApplicationRefRegistrationListener(MBeanServer JavaDoc mBeanServer)
93         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
94         super(mBeanServer);
95     }
96
97     protected void mbeanRegistered(final ObjectName JavaDoc objectName) {
98         try {
99             if (DEPLOYED_ITEM_REF_CONFIG.equals(Util.getJ2EEType(objectName))) {
100                 DeployedItemRefConfig appRefConfig =
101                     ProxyFactory.getInstance(getConn()).getProxy(objectName, DeployedItemRefConfig.class);
102                 String JavaDoc appName = appRefConfig.getRef();
103                 Container parent = appRefConfig.getContainer();
104
105                 if (parent instanceof ServerConfig) {
106                     Collection JavaDoc<LBConfig> affectedLBConfigArr =
107                         fetchLBConfigs(((ServerConfig)parent).getName(), false);
108                     for (LBConfig lbConfig : affectedLBConfigArr) {
109                         List JavaDoc<LoadBalancerMonitor> monitoringRoots =
110                             fetchLBMonitoringRoots(lbConfig);
111                         List JavaDoc<LoadBalancerServerMonitor> serverMonitors =
112                             fetchLoadBalancerServerMonitors(monitoringRoots,
113                                 ((ServerConfig)parent).getName(), false);
114                         for (LoadBalancerServerMonitor lbsm : serverMonitors)
115                             registerLoadBalancerApplicationMonitorTree(lbsm, appName);
116                     }
117                 } //end if (parent instanceof ServerConfig)
118

119                 else if (parent instanceof ClusterConfig) {
120                     Collection JavaDoc<LBConfig> affectedLBConfigArr =
121                         fetchLBConfigs(((ClusterConfig)parent).getName(), true);
122                     for (LBConfig lbConfig : affectedLBConfigArr) {
123                         List JavaDoc<LoadBalancerMonitor> monitoringRoots =
124                             fetchLBMonitoringRoots(lbConfig);
125                         List JavaDoc<LoadBalancerServerMonitor> serverMonitors =
126                             fetchLoadBalancerServerMonitors(monitoringRoots,
127                                 ((ClusterConfig)parent).getName(), true);
128                         for (LoadBalancerServerMonitor lbsm : serverMonitors)
129                             registerLoadBalancerApplicationMonitorTree(lbsm, appName);
130                     }
131                 }//end if (parent instanceof ClusterConfig)
132

133             }
134     } catch (Exception JavaDoc ex) {
135             logWarning(
136                 "LoadBalancerApplicationRefRegistrationListener" +
137                 " mbeanRegistered failed. Exception = ", ex);
138     }
139     }
140     
141     private List JavaDoc<LoadBalancerServerMonitor> fetchLoadBalancerServerMonitors(
142         List JavaDoc<LoadBalancerMonitor> monitoringRoots, String JavaDoc targetName, boolean isCluster) {
143         
144         List JavaDoc<LoadBalancerServerMonitor> serverMonitors = new ArrayList JavaDoc<LoadBalancerServerMonitor>();
145         
146         if (isCluster) {
147             List JavaDoc<LoadBalancerClusterMonitor> clusterMonitors =
148                 fetchLoadBalancerClusterMonitors(monitoringRoots, targetName);
149          
150             for (LoadBalancerClusterMonitor lbcm : clusterMonitors) {
151                 Map JavaDoc<String JavaDoc, LoadBalancerServerMonitor> lbsmMap =
152                     lbcm.getLoadBalancerServerMonitorMap();
153                 serverMonitors.addAll(lbsmMap.values());
154             }
155         } else {
156          /* its a serverName which means we have to find LoadBalancerServerMonitors
157          1. for standalone server references with the same name
158          2. for clustered server references with the same name
159          The logic below will take care of both the cases as every standalone server
160          monitor is wrapped in its single cluster monitor.*/

161             for (LoadBalancerMonitor lbm : monitoringRoots) {
162                 Map JavaDoc<String JavaDoc, LoadBalancerClusterMonitor> lbcmMap =
163                     lbm.getLoadBalancerClusterMonitorMap();
164                 for (LoadBalancerClusterMonitor lbcm : lbcmMap.values()) {
165                     Map JavaDoc<String JavaDoc, LoadBalancerServerMonitor> lbsmMap =
166                         lbcm.getLoadBalancerServerMonitorMap();
167                     for (LoadBalancerServerMonitor lbsm : lbsmMap.values())
168                         if (lbsm.getName().equals(targetName))
169                             serverMonitors.add(lbsm);
170                 }
171             }
172         }
173         return serverMonitors;
174     }
175 }
Popular Tags