KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Configuration for the <load-balancer> element.
26  */

27
28 package com.sun.enterprise.management.config;
29
30 import com.sun.enterprise.management.support.LBDeregistrationUtil;
31 import com.sun.enterprise.management.support.ObjectNames;
32 import java.util.Map JavaDoc;
33
34 import com.sun.appserv.management.base.XTypes;
35 import com.sun.appserv.management.base.Util;
36 import com.sun.enterprise.management.support.Delegate;
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 javax.management.InstanceNotFoundException JavaDoc;
41 import javax.management.JMException JavaDoc;
42 import javax.management.MBeanServer JavaDoc;
43 import javax.management.ObjectName JavaDoc;
44 import com.sun.appserv.management.monitor.LoadBalancerMonitor;
45 import com.sun.appserv.management.monitor.LoadBalancerClusterMonitor;
46 import com.sun.appserv.management.monitor.LoadBalancerServerMonitor;
47 import com.sun.appserv.management.monitor.LoadBalancerApplicationMonitor;
48 import com.sun.appserv.management.monitor.LoadBalancerContextRootMonitor;
49 import com.sun.appserv.management.config.DeployedItemRefConfig;
50 import com.sun.appserv.management.config.ClusterRefConfig;
51 import com.sun.appserv.management.config.ClusterConfig;
52 import com.sun.appserv.management.config.ServerRefConfig;
53 import com.sun.appserv.management.config.ServerConfig;
54 import com.sun.appserv.management.config.StandaloneServerConfig;
55 import com.sun.appserv.management.config.LoadBalancerConfig;
56 import com.sun.appserv.management.config.LBConfig;
57 import com.sun.appserv.management.ext.lb.LoadBalancer;
58 import com.sun.appserv.management.monitor.MonitoringRoot;
59 import com.sun.appserv.management.DomainRoot;
60 import com.sun.appserv.management.client.ProxyFactory;
61
62 /**
63   Configuration for the <lb-config> element.
64 */

65 public final class LoadBalancerConfigImpl extends AMXConfigImplBase {
66     
67     LoadBalancerDeregistrationHelper mHelper = null;
68     String JavaDoc lbConfigName = null;
69     
70     public LoadBalancerConfigImpl(final Delegate delegate) {
71         super(delegate);
72     }
73     
74     protected void unregisterMisc() {
75         try {
76             LoadBalancerConfig lbc = ProxyFactory.getInstance(getMBeanServer())
77                 .getDomainRoot().getDomainConfig().getLoadBalancerConfigMap()
78                 .get(getName());
79             LBConfig lbConfig = getDomainRoot().getDomainConfig()
80                                 .getLBConfigMap().get(lbConfigName);
81             mHelper = new LoadBalancerDeregistrationHelper(
82                 getName(), lbConfig, getMBeanServer());
83         } catch (Exception JavaDoc ex) {
84             logWarning("LoadBalancerConfig unregisterMisc failed. " +
85                     "LoadBalancerDeregistrationHelper creation failed");
86         }
87     }
88
89     public void postRegisterHook(Boolean JavaDoc registrationSucceeded) {
90         super.postRegisterHook(registrationSucceeded);
91         if (registrationSucceeded) {
92             LoadBalancerConfig lbc = ProxyFactory.getInstance(getMBeanServer())
93                 .getDomainRoot().getDomainConfig().getLoadBalancerConfigMap()
94                 .get(getName());
95             lbConfigName = lbc.getLbConfigName();
96         }
97     }
98     
99     public void postDeregister() {
100         super.postDeregister();
101         try {
102             mHelper.unregisterMonitors();
103             mHelper.unregisterRuntimeMBean();
104             mHelper.removeLBConfigListener();
105             mHelper = null;
106         } catch (Exception JavaDoc ex) {
107             logWarning("LoadBalancerConfig postDeregistration failed. " +
108                     "Load Balancer Monitoring MBeans might be lying around");
109         }
110     }
111
112     private class LoadBalancerDeregistrationHelper {
113
114         final static String JavaDoc J2EE_TYPE = "j2eeType";
115         final ObjectNames objectNames = ObjectNames.getInstance(JMX_DOMAIN);
116         MBeanServer JavaDoc mbs = null;
117         String JavaDoc loadBalancerName = null;
118         LBConfig lbConfig = null;
119         
120         public LoadBalancerDeregistrationHelper(
121             String JavaDoc loadBalancerName, LBConfig lbConfig, MBeanServer JavaDoc mbs) {
122             
123             this.mbs = mbs;
124             this.loadBalancerName = loadBalancerName;
125             this.lbConfig = lbConfig;
126         }
127
128         void removeLBConfigListener() throws JMException JavaDoc {
129             LBDeregistrationUtil.getInstance(mbs)
130                 .removeLBConfigListener(loadBalancerName, lbConfig);
131         }
132         
133         void unregisterRuntimeMBean() throws JMException JavaDoc {
134             LoadBalancer loadBalancer = ProxyFactory.getInstance(mbs)
135                 .getDomainRoot().getLoadBalancerMap().get(loadBalancerName);
136
137             ObjectName JavaDoc lbObjName = Util.getObjectName(loadBalancer);
138             mbs.unregisterMBean(lbObjName);
139         }
140         
141         void unregisterMonitors() throws JMException JavaDoc {
142
143             if (!lbConfig.getMonitoringEnabled()) return;
144             
145             LoadBalancerMonitor lbm = LBDeregistrationUtil.getInstance(mbs)
146                 .fetchLBMonitoringRoot(loadBalancerName);
147
148            Map JavaDoc<String JavaDoc,LoadBalancerClusterMonitor> lbcmMap =
149                lbm.getLoadBalancerClusterMonitorMap();
150             
151            for (LoadBalancerClusterMonitor lbcm : lbcmMap.values()) {
152                //loadBalancerServerMonitorMap will have only one element for every
153
//standalone server as it is is wrapped up in a default cluster
154
Map JavaDoc<String JavaDoc,LoadBalancerServerMonitor> lbsmMap =
155                     lbcm.getLoadBalancerServerMonitorMap();
156
157                for (LoadBalancerServerMonitor lbsm : lbsmMap.values()) {
158
159                    Map JavaDoc<String JavaDoc,LoadBalancerApplicationMonitor>
160                        lbamMap = lbsm.getLoadBalancerApplicationMonitorMap();
161                    
162                    for (LoadBalancerApplicationMonitor lbam : lbamMap.values()) {
163                         Map JavaDoc<String JavaDoc,LoadBalancerContextRootMonitor>
164                             lbcrmMap = lbam.getLoadBalancerContextRootMonitorMap();
165                        for (LoadBalancerContextRootMonitor lbcrm : lbcrmMap.values()) {
166                             ObjectName JavaDoc lbcrmObjName = Util.getObjectName(lbcrm);
167                             mbs.unregisterMBean(lbcrmObjName );
168                         }
169
170                         ObjectName JavaDoc lbamObjName = Util.getObjectName(lbam);
171                         mbs.unregisterMBean(lbamObjName);
172                     }
173
174                     ObjectName JavaDoc loadBalancerServerMonitorObjName =
175                         Util.getObjectName(lbsm);
176                         mbs.unregisterMBean(loadBalancerServerMonitorObjName);
177                 }
178
179                 ObjectName JavaDoc lbcmObjName = Util.getObjectName(lbcm);
180                 mbs.unregisterMBean(lbcmObjName);
181            }
182             
183            ObjectName JavaDoc lbmObjName = Util.getObjectName(lbm);
184            mbs.unregisterMBean(lbmObjName);
185         }
186     }
187 }
Popular Tags