KickJava   Java API By Example, From Geeks To Geeks.

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


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

71 public final class ServerRefConfigImpl extends AMXConfigImplBase
72         implements ConfigFactoryCallback
73 {
74     // implements ServerRefConfig
75
ServerRefDeregistrationHelper mHelper = null;
76     
77     public ServerRefConfigImpl(final Delegate delegate)
78     {
79         super(delegate);
80     }
81
82
83     public ObjectName JavaDoc getHealthCheckerConfigObjectName()
84     {
85         return(getContaineeObjectName(XTypes.HEALTH_CHECKER_CONFIG));
86     }
87
88     private HealthCheckerConfigFactory getHealthCheckerConfigFactory() {
89         return new HealthCheckerConfigFactory(this);
90     }
91
92         public ObjectName JavaDoc
93     createHealthCheckerConfig(
94         String JavaDoc url,
95         String JavaDoc intervalInSeconds,
96         String JavaDoc timeoutInSeconds)
97     {
98         return getHealthCheckerConfigFactory().create(
99             url,intervalInSeconds, timeoutInSeconds);
100     }
101
102     public void removeHealthCheckerConfig(final String JavaDoc name)
103     {
104         final ObjectName JavaDoc item = getHealthCheckerConfigObjectName();
105         getHealthCheckerConfigFactory().remove(item);
106     }
107
108     protected void unregisterMisc() {
109         try {
110             mHelper = new ServerRefDeregistrationHelper(
111                             getName(), getMBeanServer());
112         } catch (Exception JavaDoc ex) {
113             logWarning("ServerRefConfig unregisterMisc failed. " +
114                     "ServerRefDeregistrationHelper creation failed");
115         }
116     }
117
118     public void postDeregister() {
119         super.postDeregister();
120         try {
121             if (getContainer() instanceof LBConfig) {
122                 //collect all load-balancer elements which refer
123
//to this lb-config
124
String JavaDoc lbConfigName = ((LBConfig)getContainer()).getName();
125                 Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap =
126                      mHelper.fetchLoadBalancerConfigs((LBConfig)getContainer());
127                 mHelper.unregisterMonitors(relevantLoadBalancerConfigMap);
128             } else if (getContainer() instanceof ClusterConfig) {
129                 String JavaDoc clusterName = ((ClusterConfig)getContainer()).getName();
130                 Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap =
131                     mHelper.fetchLoadBalancerConfigs((ClusterConfig)getContainer());
132                 mHelper.unregisterMonitors(clusterName, relevantLoadBalancerConfigMap);
133             }
134             mHelper = null;
135         } catch (Exception JavaDoc ex) {
136             logWarning("ServerRefConfig postDeregistration failed. " +
137                     "Load Balancer Monitoring MBeans might be lying around");
138         }
139     }
140
141     private class ServerRefDeregistrationHelper {
142
143         final static String JavaDoc J2EE_TYPE = "j2eeType";
144         final ObjectNames objectNames = ObjectNames.getInstance(JMX_DOMAIN);
145         String JavaDoc serverName = null;
146         MBeanServer JavaDoc mbs = null;
147
148         public ServerRefDeregistrationHelper(String JavaDoc serverName, MBeanServer JavaDoc mbs) {
149             this.mbs = mbs;
150             this.serverName = serverName;
151         }
152         
153         void unregisterMonitors(Map JavaDoc<String JavaDoc,LoadBalancerConfig>
154             relevantLoadBalancerConfigMap) throws JMException JavaDoc {
155             
156             unregisterMonitors(serverName, relevantLoadBalancerConfigMap);
157         }
158         
159         void unregisterMonitors(String JavaDoc clusterName,
160             Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap)
161             throws JMException JavaDoc {
162
163             for (String JavaDoc loadBalancerName : relevantLoadBalancerConfigMap.keySet()) {
164                LoadBalancerMonitor loadBalancerMonitor =
165                     LBDeregistrationUtil.getInstance(mbs).
166                        fetchLBMonitoringRoot(loadBalancerName);
167
168                Map JavaDoc<String JavaDoc,LoadBalancerClusterMonitor> loadBalancerClusterMonitorMap =
169                    loadBalancerMonitor.getLoadBalancerClusterMonitorMap();
170
171                LoadBalancerClusterMonitor loadBalancerClusterMonitor =
172                    loadBalancerClusterMonitorMap.get(clusterName);
173
174                //loadBalancerServerMonitorMap will have only one element for every
175
//standalone server as it is is wrapped up in a default cluster
176
Map JavaDoc<String JavaDoc,LoadBalancerServerMonitor> loadBalancerServerMonitorMap =
177                     loadBalancerClusterMonitor.getLoadBalancerServerMonitorMap();
178
179                for (String JavaDoc serverName : loadBalancerServerMonitorMap.keySet()) {
180                    LoadBalancerServerMonitor loadBalancerServerMonitor =
181                        loadBalancerServerMonitorMap.get(serverName);
182                    Map JavaDoc<String JavaDoc,LoadBalancerApplicationMonitor>
183                        loadBalancerApplicationMonitorMap =
184                            loadBalancerServerMonitor
185                            .getLoadBalancerApplicationMonitorMap();
186
187                    for (String JavaDoc appName : loadBalancerApplicationMonitorMap.keySet()) {
188                         LoadBalancerApplicationMonitor loadBalancerApplicationMonitor =
189                             loadBalancerApplicationMonitorMap.get(appName);
190                         Map JavaDoc<String JavaDoc,LoadBalancerContextRootMonitor>
191                             lbcrmm = loadBalancerApplicationMonitor
192                                 .getLoadBalancerContextRootMonitorMap();
193                        for (String JavaDoc ctxRootName : lbcrmm.keySet()) {
194                             LoadBalancerContextRootMonitor loadBalancerContextRootMonitor =
195                                 lbcrmm.get(ctxRootName);
196                             ObjectName JavaDoc loadBalancerContextRootMonitorObjName =
197                                 Util.getObjectName(loadBalancerContextRootMonitor);
198                             mbs.unregisterMBean(loadBalancerContextRootMonitorObjName);
199                         }
200
201                         ObjectName JavaDoc loadBalancerApplicationMonitorObjName =
202                         Util.getObjectName(loadBalancerApplicationMonitor);
203                         mbs.unregisterMBean(
204                             loadBalancerApplicationMonitorObjName);
205                     }
206
207                     ObjectName JavaDoc loadBalancerServerMonitorObjName =
208                         Util.getObjectName(loadBalancerServerMonitor);
209                         mbs.unregisterMBean(loadBalancerServerMonitorObjName);
210                 }
211
212                 ObjectName JavaDoc loadBalancerClusterMonitorObjName =
213                     Util.getObjectName(loadBalancerClusterMonitor);
214                 mbs.unregisterMBean(loadBalancerClusterMonitorObjName);
215             }
216         }
217         
218         Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
219             LBConfig lbConfig) {
220             Map JavaDoc<String JavaDoc, LBConfig> lbConfigMap = new HashMap JavaDoc<String JavaDoc, LBConfig>();
221             lbConfigMap.put(lbConfig.getName(), lbConfig);
222             return fetchLoadBalancerConfigs(lbConfigMap);
223         }
224         
225         Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
226             ClusterConfig clusterConfig) {
227
228             String JavaDoc clusterName = clusterConfig.getName();
229             //collect all cluster-ref elements which refer to this cluster
230
Map JavaDoc<String JavaDoc,LBConfig> allLBConfigMap =
231                  getDomainRoot().getDomainConfig().getLBConfigMap();
232
233             Map JavaDoc<String JavaDoc, LBConfig> relevantLBConfigMap = new HashMap JavaDoc<String JavaDoc, LBConfig>();
234             for (String JavaDoc lbConfigName : allLBConfigMap.keySet()) {
235                 LBConfig lbConfig = allLBConfigMap.get(lbConfigName);
236                 Map JavaDoc<String JavaDoc,ClusterRefConfig> clusterRefConfigMap =
237                     lbConfig.getClusterRefConfigMap();
238                 for (String JavaDoc clusterRef : clusterRefConfigMap.keySet()) {
239                     if (clusterRef.equals(clusterName)) {
240                         relevantLBConfigMap.put(lbConfigName, lbConfig);
241                         break;
242                     }
243                 }
244             }
245             return fetchLoadBalancerConfigs(relevantLBConfigMap);
246         }
247         
248         private Map JavaDoc<String JavaDoc,LoadBalancerConfig> fetchLoadBalancerConfigs(
249             Map JavaDoc<String JavaDoc, LBConfig> lbConfigMap) {
250
251             Map JavaDoc<String JavaDoc,LoadBalancerConfig> relevantLoadBalancerConfigMap =
252                 new HashMap JavaDoc<String JavaDoc,LoadBalancerConfig>();
253
254             for (String JavaDoc lbConfigName : lbConfigMap.keySet()) {
255                 //collect all load-balancer elements which refer to this lb-config
256
Map JavaDoc<String JavaDoc,LoadBalancerConfig> allLoadBalancerConfigMap =
257                      getDomainRoot().getDomainConfig().getLoadBalancerConfigMap();
258
259
260                 for (String JavaDoc loadBalancerName : allLoadBalancerConfigMap.keySet()) {
261                     LoadBalancerConfig loadBalancerConfig =
262                         allLoadBalancerConfigMap.get(loadBalancerName);
263                     if (loadBalancerConfig.getLbConfigName().equals(lbConfigName))
264                         relevantLoadBalancerConfigMap.put(
265                             loadBalancerName, loadBalancerConfig);
266                 }
267             }
268             return relevantLoadBalancerConfigMap;
269         }
270
271     }
272 }
Popular Tags