KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28  
29 /*
30  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/LBBootstrapUtil.java,v 1.3 2005/12/25 03:40:42 tcfujii Exp $
31  * $Revision: 1.3 $
32  * $Date: 2005/12/25 03:40:42 $
33  */

34
35 package com.sun.enterprise.management.support;
36
37 import com.sun.appserv.management.util.jmx.NotificationListenerBase;
38 import com.sun.enterprise.management.ext.lb.LoadBalancerImpl;
39
40 import java.util.Map JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.io.IOException JavaDoc;
43 import javax.management.AttributeChangeNotification JavaDoc;
44
45 import javax.management.InstanceNotFoundException JavaDoc;
46 import javax.management.JMException JavaDoc;
47 import javax.management.MBeanServer JavaDoc;
48 import javax.management.Notification JavaDoc;
49 import javax.management.NotificationListener JavaDoc;
50 import javax.management.ObjectName JavaDoc;
51
52 import com.sun.appserv.management.client.ProxyFactory;
53 import com.sun.appserv.management.base.AMXDebug;
54 import com.sun.appserv.management.config.DomainConfig;
55 import com.sun.appserv.management.config.LBConfig;
56 import com.sun.appserv.management.config.LoadBalancerConfig;
57 import com.sun.appserv.management.ext.lb.LoadBalancer;
58 import com.sun.appserv.management.DomainRoot;
59 import com.sun.appserv.management.base.AMXRootLogger;
60 import com.sun.enterprise.management.support.LBDeregistrationUtil;
61 import static com.sun.appserv.management.base.AMX.JMX_DOMAIN;
62
63 /**
64  * Helps starting LBConfig listener for each LoadBalancerConfig during server
65  * startup. Server startup is a special case because MBeans for config elements
66  * can be registered in any order. Multiple LoadBalancerConfigs can refer to
67  * a single LBConfig. To start a listener for each LoadBalancerConfig listening
68  * on ATTRIBUTE_CHANGE_NOTIFICATION in the referenced LBConfig, this class waits
69  * till AMX has completed loading. The readiness of AMX loading is conveyed
70  * through the AMX_READY_NOTIFICATION. Hence this class listens for that
71  * notification type.
72  *
73  * @see LoadBalancerClusterRefRegistrationListener
74  * @see LoadBalancerApplicationRefRegistrationListener
75  * @see LoadBalancerRegistrationListener
76  */

77 public class LBBootstrapUtil extends NotificationListenerBase {
78     
79     final static String JavaDoc MONITORING_ENABLED = "MonitoringEnabled";
80     static boolean isInitialized = false;
81     LoadBalancerRegistrationListener lbrl = null;
82     final private ObjectNames objectNames = ObjectNames.getInstance(JMX_DOMAIN);
83     
84     public LBBootstrapUtil(ObjectName JavaDoc domainRoot, MBeanServer JavaDoc mBeanServer)
85         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
86         super(mBeanServer, domainRoot, null);
87     }
88         
89     public void handleNotification(
90         final Notification JavaDoc notifIn, final Object JavaDoc handback) {
91         try {
92             String JavaDoc type = notifIn.getType();
93             if ( (type == null) ||
94                  (type.equals(DomainRoot.AMX_READY_NOTIFICATION_TYPE) == false) )
95                 return ;
96
97             //following check added just in case AMX_READY_NOTIFICATION
98
//comes more than once.
99
if (isInitialized) return;
100
101             //now that all MBeans for the configs have been registered it is
102
//safe to start the following listeners that listen on new config
103
//element registrations. I store the handle of the root LB
104
//listener primarily to reuse its ability to enable / disable monitoring
105

106             lbrl = new LoadBalancerRegistrationListener((MBeanServer JavaDoc)getConn());
107             initRestOfLBMBeanRegistrationListeners();
108
109             DomainConfig domainConfig = ProxyFactory.getInstance(getConn())
110                                         .getDomainRoot().getDomainConfig();
111
112             //get ALL AMX <load-balancer> mbeans in the domain
113
Map JavaDoc<String JavaDoc, LoadBalancerConfig> loadBalancerConfigMap =
114                 domainConfig.getLoadBalancerConfigMap();
115             //get ALL AMX <lb-config> mbeans in the domain
116
Map JavaDoc<String JavaDoc, LBConfig> lbConfigMap = domainConfig.getLBConfigMap();
117
118             String JavaDoc lbConfigName = null;
119             LBConfig lbConfig = null;
120             String JavaDoc loadBalancerName = null;
121             boolean isMonitoringEnabled = false;
122
123             for (LoadBalancerConfig loadBalancerConfig : loadBalancerConfigMap.values()) {
124                 loadBalancerName = loadBalancerConfig.getName();
125                 lbConfigName = loadBalancerConfig.getLbConfigName();
126                 lbConfig = lbConfigMap.get(lbConfigName);
127
128                 LoadBalancer loadBalancer =
129                     lbrl.registerLoadBalancer(loadBalancerConfig.getName());
130
131                 isMonitoringEnabled = lbConfig.getMonitoringEnabled();
132                 if (isMonitoringEnabled)
133                     lbrl.enableMonitoring(loadBalancerConfig.getName(), lbConfig);
134
135                 //start a listener on lbConfig for this loadBalancerConfig
136
lbrl.addLBConfigListener(loadBalancerName, lbConfig);
137             }
138             isInitialized = true;
139         } catch (Exception JavaDoc ex) {
140             AMXRootLogger.getInstance().warning(
141                 "LBBootstrapUtil:handleNotification" + " : " + ex.getMessage());
142         }
143     }
144
145     private void initRestOfLBMBeanRegistrationListeners() {
146         
147         MBeanServer JavaDoc mbeanServer = (MBeanServer JavaDoc)getConn();
148         try {
149             new LoadBalancerClusterRefRegistrationListener(mbeanServer);
150             new LoadBalancerServerRefRegistrationListener(mbeanServer);
151             new LoadBalancerApplicationRefRegistrationListener(mbeanServer);
152         } catch(Exception JavaDoc e) {
153             throw new RuntimeException JavaDoc(e);
154         }
155     }
156    
157 }
Popular Tags