KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > ext > lb > LoadBalancerImpl


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 package com.sun.enterprise.management.ext.lb;
26
27 import com.sun.appserv.management.config.LBConfig;
28 import com.sun.enterprise.server.ApplicationServer;
29 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory;
30 import java.math.BigInteger JavaDoc;
31 import java.security.MessageDigest JavaDoc;
32 import java.security.NoSuchAlgorithmException JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.MalformedObjectNameException JavaDoc;
37
38 import javax.management.ObjectName JavaDoc;
39
40 import com.sun.appserv.management.base.AMX;
41 import static com.sun.appserv.management.base.AMX.*;
42 import static com.sun.appserv.management.base.XTypes.*;
43 import com.sun.appserv.management.ext.lb.LoadBalancer;
44 import com.sun.appserv.management.config.LoadBalancerConfig;
45 import com.sun.appserv.management.client.ProxyFactory;
46 import com.sun.enterprise.management.support.AMXImplBase;
47 import com.sun.enterprise.server.pluggable.LBFeatureFactory;
48 /**
49  * Implmentation class for LoadBalancer AMX MBean
50  * @author Harsha R A
51  * @since Appserver 9.0
52  */

53 public final class LoadBalancerImpl extends AMXImplBase implements LoadBalancer {
54     
55     private static final String JavaDoc COMMA = ",";
56     private static final String JavaDoc COLON = ":";
57     private static final String JavaDoc EQUAL = "=";
58
59     public static final String JavaDoc NAME_PROP_VALUE = "load-balancer";
60     public static final String JavaDoc LAST_APPLIED_PROPERTY = "last-applied";
61     public static final String JavaDoc LAST_APPLIED_HASH_PROPERTY = "last-applied-message-digest";
62     public static final String JavaDoc LAST_EXPORTED_PROPERTY = "last-exported";
63
64     public static final String JavaDoc LOADBALANCER_CONFIG_OBJECT_NAME=
65                     JMX_DOMAIN+COLON+J2EE_TYPE_KEY+EQUAL+
66                     LOAD_BALANCER_CONFIG+COMMA+NAME_KEY+EQUAL;
67
68     public static final String JavaDoc LBCONFIG_OBJECT_NAME=
69                     JMX_DOMAIN+COLON+J2EE_TYPE_KEY+EQUAL+
70                     LB_CONFIG+COMMA+NAME_KEY+EQUAL;
71
72     public static final String JavaDoc LOADBALANCER_OBJECT_NAME =
73                     JMX_DOMAIN+COLON+J2EE_TYPE_KEY+EQUAL+
74                     LOAD_BALANCER+COMMA+NAME_KEY+EQUAL;
75
76     private LBFeatureFactory lbFactory = null;
77     private String JavaDoc lbName = null;
78     private String JavaDoc lbConfigName = null;
79     private LoadBalancerConfig loadBalancerConfig = null;
80     private static final String JavaDoc XML_COMMENT_START = "<!--";
81     private static final String JavaDoc MD5 = "MD5";
82     private final MBeanServerConnection JavaDoc mServer;
83
84     public LoadBalancerImpl( final MBeanServerConnection JavaDoc server) {
85         super( );
86         mServer = server;
87         PluggableFeatureFactory featureFactory =
88                 ApplicationServer.getServerContext().getPluggableFeatureFactory();
89         
90         lbFactory = featureFactory.getLBFeatureFactory();
91         
92     }
93     
94     /**
95      * Applies changes in the corresponding configuration to this LB
96      */

97     public void applyLBChanges() {
98         lbName = getName();
99         loadBalancerConfig = getLoadBalancerConfig(lbName);
100         if(loadBalancerConfig!=null) {
101             lbConfigName = loadBalancerConfig.getLbConfigName();
102         }
103         lbFactory.applyChanges(lbConfigName,lbName);
104         if(!loadBalancerConfig.existsProperty(LAST_APPLIED_PROPERTY))
105             loadBalancerConfig.createProperty(LAST_APPLIED_PROPERTY,String.valueOf(new Date JavaDoc().getTime()));
106         else
107             loadBalancerConfig.setPropertyValue(LAST_APPLIED_PROPERTY,String.valueOf(new Date JavaDoc().getTime()));
108         String JavaDoc digest = getMessageDigest();
109         if(!loadBalancerConfig.existsProperty(LAST_APPLIED_HASH_PROPERTY))
110             loadBalancerConfig.createProperty(LAST_APPLIED_HASH_PROPERTY,digest);
111         else
112             loadBalancerConfig.setPropertyValue(LAST_APPLIED_HASH_PROPERTY,digest);
113     }
114
115     /**
116      * checks if apply change is required
117      * @return true if there are pending changes for this LB
118      */

119     public boolean isApplyChangeRequired() {
120         boolean isRequired = true;
121         try{
122         String JavaDoc digest = getMessageDigest();
123         String JavaDoc lastDigest = getLastAppliedMessageDigest();
124         if(lastDigest!=null)
125             isRequired = !digest.equals(lastDigest);
126         }catch (Throwable JavaDoc t){
127             getMBeanLogger().warning(t.getMessage());
128             if(getMBeanLogger().isLoggable(Level.FINE))
129                 t.printStackTrace();
130         }
131         return isRequired;
132                 
133     }
134
135     /**
136      * Returns the timestamp of the most recent application of referenced LBConfig
137      * @return Date the timestamp when the changes were applied to the load balancer
138      */

139     public Date JavaDoc getLastApplied() {
140         lbName = getName();
141         loadBalancerConfig = getLoadBalancerConfig(lbName);
142         if(loadBalancerConfig.existsProperty(LAST_APPLIED_PROPERTY))
143             return new Date JavaDoc(Long.valueOf(loadBalancerConfig.getPropertyValue(LAST_APPLIED_PROPERTY)));
144         return null;
145     }
146     
147     /**
148      * method to return the message digest of the load balaner.xml that was last
149      * applied
150      * @return String last-applied-digest property of the load balancer from domain.xml
151      */

152     public String JavaDoc getLastAppliedMessageDigest() {
153         lbName = getName();
154         loadBalancerConfig = getLoadBalancerConfig(lbName);
155         if(loadBalancerConfig.existsProperty(LAST_APPLIED_HASH_PROPERTY))
156             return loadBalancerConfig.getPropertyValue(LAST_APPLIED_HASH_PROPERTY);
157         return null;
158     }
159
160     /**
161      * Returns the timestamp of the most recent export of referenced LBConfig
162      * @return Date timestamp
163      */

164     public Date JavaDoc getLastExported() {
165         lbName = getName();
166         getLoadBalancerConfig(lbName);
167         if(loadBalancerConfig!=null) {
168             lbConfigName = loadBalancerConfig.getLbConfigName();
169         }
170         LBConfig lbConfig = getLBConfig(lbConfigName);
171         if(lbConfig.existsProperty(LAST_EXPORTED_PROPERTY))
172             return new Date JavaDoc(Long.valueOf(lbConfig.getPropertyValue(LAST_EXPORTED_PROPERTY)));
173         return null;
174     }
175
176     /**
177      * Exports the corresponding LBConfig information and returns the contents as a string.
178      * @see com.sun.appserv.management.config.LBConfig
179      * @return String the loadbalancer.xml as a string
180      */

181     public String JavaDoc getLoadBalancerXML() {
182         return getLoadBalancerXML(true);
183     }
184     
185
186     private LoadBalancerConfig getLoadBalancerConfig(String JavaDoc lbName){
187         ObjectName JavaDoc loadBalancerConfigObjName = null;
188         try{
189             loadBalancerConfigObjName =
190                     new ObjectName JavaDoc(LOADBALANCER_CONFIG_OBJECT_NAME+lbName);
191         } catch ( MalformedObjectNameException JavaDoc e ){
192             if(getMBeanLogger().isLoggable(Level.FINE))
193                 e.printStackTrace();
194         }
195         loadBalancerConfig =
196                 getProxy(loadBalancerConfigObjName, LoadBalancerConfig.class);
197         return loadBalancerConfig;
198     }
199     
200     private LBConfig getLBConfig(String JavaDoc lbConfigName){
201         ObjectName JavaDoc lbConfigObjName = null;
202         try{
203             lbConfigObjName =
204                     new ObjectName JavaDoc(LBCONFIG_OBJECT_NAME+lbConfigName);
205         } catch ( MalformedObjectNameException JavaDoc e ){
206             e.printStackTrace();
207         }
208         LBConfig lbConfig = getProxy(lbConfigObjName, LBConfig.class);
209         return lbConfig;
210     }
211     
212     private String JavaDoc getLoadBalancerXML(boolean updateTimeStamp) {
213         lbName = getName();
214         getLoadBalancerConfig(lbName);
215         if(loadBalancerConfig!=null) {
216             lbConfigName = loadBalancerConfig.getLbConfigName();
217         }
218         LBConfig lbConfig = getLBConfig(lbConfigName);
219         if(updateTimeStamp) {
220             if(!lbConfig.existsProperty(LAST_EXPORTED_PROPERTY))
221                 lbConfig.createProperty(LAST_EXPORTED_PROPERTY,String.valueOf(new Date JavaDoc().getTime()));
222             else
223                 lbConfig.setPropertyValue(LAST_EXPORTED_PROPERTY,String.valueOf(new Date JavaDoc().getTime()));
224         }
225
226         return lbFactory.getLoadBalancerXML(lbConfigName,lbName);
227                 
228     }
229     
230     private String JavaDoc getMessageDigest(){
231         try {
232             String JavaDoc lbxml = getLoadBalancerXML(false).split(XML_COMMENT_START)[0];
233             MessageDigest JavaDoc md = MessageDigest.getInstance(MD5);
234             md.update(lbxml.getBytes());
235             String JavaDoc hash = new BigInteger JavaDoc(md.digest()).toString(16);
236             return hash;
237         }catch(NoSuchAlgorithmException JavaDoc e){
238             getMBeanLogger().warning(e.getMessage());
239             if(getMBeanLogger().isLoggable(Level.FINE))
240                 e.printStackTrace();
241         }
242         return "";
243     }
244
245
246     /**
247       Returns the timestamp of the last time the stats on this loadbalancer were reset
248      */

249     public Date JavaDoc getLastResetTime() {
250         return null;
251     }
252
253     /**
254       Reset the monitoring stats on this loadbalancer.
255      */

256     public void resetStats() {
257         String JavaDoc lbName = getName();
258         String JavaDoc lbConfigName = null;
259         loadBalancerConfig = getLoadBalancerConfig(lbName);
260         if(loadBalancerConfig!=null) {
261             lbConfigName = loadBalancerConfig.getLbConfigName();
262         }
263         lbFactory.resetStats(lbConfigName,lbName);
264     }
265
266     /**
267       Test the LB and Domain Application Server setup
268      */

269     public boolean testConnection() {
270         String JavaDoc lbName = getName();
271         String JavaDoc lbConfigName = null;
272         loadBalancerConfig = getLoadBalancerConfig(lbName);
273         if(loadBalancerConfig!=null) {
274             lbConfigName = loadBalancerConfig.getLbConfigName();
275         }
276         return lbFactory.testConnection(lbConfigName,lbName);
277     }
278             
279     /**
280      * Returns the uhealthy/healthy/quiesced status for an insatnce load balanced
281      * by this load balancer.
282      */

283     public String JavaDoc getStatus(String JavaDoc instanceName) {
284         return null;
285     }
286 }
287
288
289
290
291
292
293
294
295
296
Popular Tags