KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > config > DeployedItemRefConfigImpl


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
24 /**
25 */

26
27 package com.sun.enterprise.management.config;
28
29 import com.sun.enterprise.management.support.LBDeregistrationUtil;
30 import com.sun.enterprise.management.support.ObjectNames;
31 import javax.management.ObjectName JavaDoc;
32
33 import com.sun.enterprise.management.config.AMXConfigImplBase;
34 import com.sun.enterprise.management.support.Delegate;
35     
36 import com.sun.enterprise.management.support.AMXAttributeNameMapper;
37
38 import java.util.Collection JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import javax.management.InstanceNotFoundException JavaDoc;
45 import javax.management.JMException JavaDoc;
46 import javax.management.MBeanServer JavaDoc;
47 import javax.management.ObjectName JavaDoc;
48
49 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER;
50 import static com.sun.appserv.management.base.XTypes.LOAD_BALANCER_MONITOR;
51 import static com.sun.appserv.management.base.AMX.J2EE_TYPE_KEY;
52 import static com.sun.appserv.management.base.AMX.NAME_KEY;
53 import static com.sun.appserv.management.base.AMX.JMX_DOMAIN;
54
55 import com.sun.appserv.management.base.Util;
56 import com.sun.appserv.management.base.AMXDebug;
57 import com.sun.appserv.management.monitor.LoadBalancerMonitor;
58 import com.sun.appserv.management.monitor.LoadBalancerClusterMonitor;
59 import com.sun.appserv.management.monitor.LoadBalancerServerMonitor;
60 import com.sun.appserv.management.monitor.LoadBalancerApplicationMonitor;
61 import com.sun.appserv.management.monitor.LoadBalancerContextRootMonitor;
62 import com.sun.appserv.management.config.DeployedItemRefConfig;
63 import com.sun.appserv.management.config.ClusterRefConfig;
64 import com.sun.appserv.management.config.ClusterConfig;
65 import com.sun.appserv.management.config.ServerRefConfig;
66 import com.sun.appserv.management.config.ServerConfig;
67 import com.sun.appserv.management.config.LoadBalancerConfig;
68 import com.sun.appserv.management.config.LBConfig;
69 import com.sun.appserv.management.client.ProxyFactory;
70 /**
71     Configuration for the <application-ref> element
72     found within a <server>
73 */

74 public final class DeployedItemRefConfigImpl extends AMXConfigImplBase
75 {
76         DeployedItemRefDeRegistrationHelper mHelper = null;
77         
78         public
79     DeployedItemRefConfigImpl( final Delegate delegate )
80     {
81         super( delegate );
82     }
83     
84     /*
85         protected void
86     addCustomMappings( final AttributeNameMapper mapper )
87     {
88         super.addCustomMappings( mapper );
89         // these require custom mappings due to different names...
90         mapper.addMapping( "lb-enabled", "LBEnabled" );
91     }
92     */

93     
94         protected void unregisterMisc() {
95             try {
96                 mHelper = new DeployedItemRefDeRegistrationHelper(
97                                 getName(), getMBeanServer());
98             } catch (Exception JavaDoc ex) {
99                 logWarning("DeployedItemRefConfig unregisterMisc failed. " +
100                     "DeployedItemRefDeregistrationHelper creation failed");
101             }
102         }
103
104         public void postDeregister() {
105             super.postDeregister();
106             try {
107                 
108                 Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap =
109                     new HashMap JavaDoc<String JavaDoc,LoadBalancerConfig>();
110                 if (getContainer() instanceof ServerConfig) {
111                     relevantLoadBalancerConfigMap =
112                         mHelper.fetchLoadBalancerConfigs((ServerConfig)getContainer());
113                     mHelper.unregisterMonitors(relevantLoadBalancerConfigMap);
114                 } else if (getContainer() instanceof ClusterConfig) {
115                     relevantLoadBalancerConfigMap =
116                         mHelper.fetchLoadBalancerConfigs((ClusterConfig)getContainer());
117                     mHelper.unregisterMonitors(relevantLoadBalancerConfigMap);
118                 }
119                 mHelper = null;
120              } catch (Exception JavaDoc ex) {
121                 logWarning("DeployedItemRef postDeregistration failed. " +
122                     "Load Balancer Monitoring MBeans might be lying around " +
123                     "if this application is being load balanced " );
124              }
125         }
126
127         private class DeployedItemRefDeRegistrationHelper {
128
129             final static String JavaDoc J2EE_TYPE = "j2eeType";
130             final ObjectNames objectNames = ObjectNames.getInstance(JMX_DOMAIN);
131             String JavaDoc appName = null;
132             MBeanServer JavaDoc mbs = null;
133             
134             public DeployedItemRefDeRegistrationHelper(String JavaDoc appName, MBeanServer JavaDoc mbs) {
135                 this.mbs = mbs;
136                 this.appName = appName;
137             }
138
139             void unregisterMonitors(
140                 Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap)
141                 throws JMException JavaDoc {
142
143                 for (String JavaDoc loadBalancerName : relevantLoadBalancerConfigMap.keySet()) {
144                    LoadBalancerConfig loadBalancerConfig =
145                        relevantLoadBalancerConfigMap.get(loadBalancerName);
146                    LoadBalancerMonitor loadBalancerMonitor =
147                        LBDeregistrationUtil.getInstance(mbs)
148                            .fetchLBMonitoringRoot(loadBalancerName);
149                    Collection JavaDoc<LoadBalancerApplicationMonitor> lbamList =
150                        fetchLoadBalancerApplicationMonitors(appName, loadBalancerMonitor);
151                    for (LoadBalancerApplicationMonitor lbam : lbamList) {
152                         Map JavaDoc<String JavaDoc,LoadBalancerContextRootMonitor>
153                             lbcrmm = lbam.getLoadBalancerContextRootMonitorMap();
154                        for (String JavaDoc ctxRootName : lbcrmm.keySet()) {
155                             LoadBalancerContextRootMonitor loadBalancerContextRootMonitor =
156                                 lbcrmm.get(ctxRootName);
157                             ObjectName JavaDoc loadBalancerContextRootMonitorObjName =
158                                 Util.getObjectName(loadBalancerContextRootMonitor);
159                             mbs.unregisterMBean(loadBalancerContextRootMonitorObjName);
160                         }
161                         ObjectName JavaDoc lbamObjName = Util.getObjectName(lbam);
162                         mbs.unregisterMBean(lbamObjName);
163                     }
164                 }
165             }
166
167             Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
168                 ClusterConfig clusterConfig) {
169
170                 String JavaDoc clusterName = clusterConfig.getName();
171                 //collect all cluster-ref elements which refer to this cluster
172
Map JavaDoc<String JavaDoc,LBConfig> allLBConfigMap =
173                      getDomainRoot().getDomainConfig().getLBConfigMap();
174
175                 Map JavaDoc<String JavaDoc, LBConfig> relevantLBConfigMap = new HashMap JavaDoc<String JavaDoc, LBConfig>();
176                 for (String JavaDoc lbConfigName : allLBConfigMap.keySet()) {
177                     LBConfig lbConfig = allLBConfigMap.get(lbConfigName);
178                     Map JavaDoc<String JavaDoc,ClusterRefConfig> clusterRefConfigMap =
179                         lbConfig.getClusterRefConfigMap();
180                     for (String JavaDoc clusterRef : clusterRefConfigMap.keySet()) {
181                         if (clusterRef.equals(clusterName)) {
182                             relevantLBConfigMap.put(lbConfigName, lbConfig);
183                             break;
184                         }
185                     }
186                 }
187                 return fetchLoadBalancerConfigs(relevantLBConfigMap);
188             }
189
190             Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
191                 ServerConfig serverConfig) {
192
193                 Map JavaDoc<String JavaDoc, LBConfig> relevantLBConfigMap = new HashMap JavaDoc<String JavaDoc, LBConfig>();
194
195                 String JavaDoc serverName = serverConfig.getName();
196
197                 //get all <lb-config> elements in the domain
198
Map JavaDoc<String JavaDoc,LBConfig> allLBConfigMap =
199                      getDomainRoot().getDomainConfig().getLBConfigMap();
200                 //get all <cluster> elements in the domain
201
Map JavaDoc<String JavaDoc,ClusterConfig> allClusterConfigMap =
202                      getDomainRoot().getDomainConfig().getClusterConfigMap();
203
204                 for (String JavaDoc lbConfigName : allLBConfigMap.keySet()) {
205                     LBConfig lbConfig = allLBConfigMap.get(lbConfigName);
206
207                     Map JavaDoc<String JavaDoc,ClusterConfig>
208                         relevantClusterConfigMap = new HashMap JavaDoc<String JavaDoc,ClusterConfig>();
209
210                     Map JavaDoc<String JavaDoc,ClusterRefConfig> clusterRefConfigMap =
211                         lbConfig.getClusterRefConfigMap();
212                     for (String JavaDoc clusterName : clusterRefConfigMap.keySet())
213                         relevantClusterConfigMap.put(
214                             clusterName, allClusterConfigMap.get(clusterName));
215
216                     for (ClusterConfig clusterConfig :
217                             relevantClusterConfigMap.values()) {
218                         Map JavaDoc<String JavaDoc,ServerRefConfig> serverRefConfigMap =
219                             clusterConfig.getServerRefConfigMap();
220                         for (String JavaDoc serverRef : serverRefConfigMap.keySet()) {
221                             if (serverRef.equals(serverName)) {
222                                 relevantLBConfigMap.put(lbConfigName, lbConfig);
223                                 break;
224                             }
225                         }
226                     }
227
228                     Map JavaDoc<String JavaDoc,ServerRefConfig> serverRefConfigMap =
229                         lbConfig.getServerRefConfigMap();
230                     for (String JavaDoc serverRef : serverRefConfigMap.keySet()) {
231                         if (serverRef.equals(serverName)) {
232                             relevantLBConfigMap.put(lbConfigName, lbConfig);
233                             break;
234                         }
235                     }
236                 }
237
238                 return fetchLoadBalancerConfigs(relevantLBConfigMap);
239             }
240
241             private Collection JavaDoc<LoadBalancerApplicationMonitor>
242                 fetchLoadBalancerApplicationMonitors(
243                     String JavaDoc ipAppName, LoadBalancerMonitor loadBalancerMonitor) {
244
245                 Collection JavaDoc<LoadBalancerApplicationMonitor> result = new ArrayList JavaDoc<LoadBalancerApplicationMonitor>();
246                 Map JavaDoc<String JavaDoc,LoadBalancerClusterMonitor> lbcmMap =
247                         loadBalancerMonitor.getLoadBalancerClusterMonitorMap();
248                 for (String JavaDoc clusterName : lbcmMap.keySet()) {
249                     LoadBalancerClusterMonitor lbcm = lbcmMap.get(clusterName);
250                     Map JavaDoc<String JavaDoc,LoadBalancerServerMonitor> lbsmMap =
251                         lbcm.getLoadBalancerServerMonitorMap();
252                     for (String JavaDoc serverName : lbsmMap.keySet()) {
253                         LoadBalancerServerMonitor lbsm = lbsmMap.get(serverName);
254                         Map JavaDoc<String JavaDoc, LoadBalancerApplicationMonitor> lbamMap =
255                             lbsm.getLoadBalancerApplicationMonitorMap();
256                         if (lbamMap.get(ipAppName) != null)
257                             result.add(lbamMap.get(ipAppName));
258                     }
259                 }
260                 return result;
261             }
262
263             private Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
264                 LBConfig lbConfig) {
265                 Map JavaDoc<String JavaDoc, LBConfig> lbConfigMap = new HashMap JavaDoc<String JavaDoc, LBConfig>();
266                 lbConfigMap.put(lbConfig.getName(), lbConfig);
267                 return fetchLoadBalancerConfigs(lbConfigMap);
268             }
269
270             private Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
271                 Map JavaDoc<String JavaDoc, LBConfig> lbConfigMap) {
272
273                 Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap =
274                         new HashMap JavaDoc<String JavaDoc,LoadBalancerConfig>();
275
276                 for (String JavaDoc lbConfigName : lbConfigMap.keySet()) {
277                     //collect all load-balancer elements which refer to this lb-config
278
Map JavaDoc<String JavaDoc,LoadBalancerConfig> allLoadBalancerConfigMap =
279                          getDomainRoot().getDomainConfig().getLoadBalancerConfigMap();
280
281
282                     for (String JavaDoc loadBalancerName : allLoadBalancerConfigMap.keySet()) {
283                         LoadBalancerConfig loadBalancerConfig =
284                             allLoadBalancerConfigMap.get(loadBalancerName);
285                         if (loadBalancerConfig.getLbConfigName().equals(lbConfigName))
286                             relevantLoadBalancerConfigMap.put(
287                                 loadBalancerName, loadBalancerConfig);
288                     }
289                 }
290                 return relevantLoadBalancerConfigMap;
291             }
292         }
293 }
Popular Tags