1 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 ; 31 import java.security.MessageDigest ; 32 import java.security.NoSuchAlgorithmException ; 33 import java.util.Date ; 34 import java.util.logging.Level ; 35 import javax.management.MBeanServerConnection ; 36 import javax.management.MalformedObjectNameException ; 37 38 import javax.management.ObjectName ; 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 53 public final class LoadBalancerImpl extends AMXImplBase implements LoadBalancer { 54 55 private static final String COMMA = ","; 56 private static final String COLON = ":"; 57 private static final String EQUAL = "="; 58 59 public static final String NAME_PROP_VALUE = "load-balancer"; 60 public static final String LAST_APPLIED_PROPERTY = "last-applied"; 61 public static final String LAST_APPLIED_HASH_PROPERTY = "last-applied-message-digest"; 62 public static final String LAST_EXPORTED_PROPERTY = "last-exported"; 63 64 public static final String 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 LBCONFIG_OBJECT_NAME= 69 JMX_DOMAIN+COLON+J2EE_TYPE_KEY+EQUAL+ 70 LB_CONFIG+COMMA+NAME_KEY+EQUAL; 71 72 public static final String 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 lbName = null; 78 private String lbConfigName = null; 79 private LoadBalancerConfig loadBalancerConfig = null; 80 private static final String XML_COMMENT_START = "<!--"; 81 private static final String MD5 = "MD5"; 82 private final MBeanServerConnection mServer; 83 84 public LoadBalancerImpl( final MBeanServerConnection server) { 85 super( ); 86 mServer = server; 87 PluggableFeatureFactory featureFactory = 88 ApplicationServer.getServerContext().getPluggableFeatureFactory(); 89 90 lbFactory = featureFactory.getLBFeatureFactory(); 91 92 } 93 94 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 ().getTime())); 106 else 107 loadBalancerConfig.setPropertyValue(LAST_APPLIED_PROPERTY,String.valueOf(new Date ().getTime())); 108 String 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 119 public boolean isApplyChangeRequired() { 120 boolean isRequired = true; 121 try{ 122 String digest = getMessageDigest(); 123 String lastDigest = getLastAppliedMessageDigest(); 124 if(lastDigest!=null) 125 isRequired = !digest.equals(lastDigest); 126 }catch (Throwable t){ 127 getMBeanLogger().warning(t.getMessage()); 128 if(getMBeanLogger().isLoggable(Level.FINE)) 129 t.printStackTrace(); 130 } 131 return isRequired; 132 133 } 134 135 139 public Date getLastApplied() { 140 lbName = getName(); 141 loadBalancerConfig = getLoadBalancerConfig(lbName); 142 if(loadBalancerConfig.existsProperty(LAST_APPLIED_PROPERTY)) 143 return new Date (Long.valueOf(loadBalancerConfig.getPropertyValue(LAST_APPLIED_PROPERTY))); 144 return null; 145 } 146 147 152 public String 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 164 public Date 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 (Long.valueOf(lbConfig.getPropertyValue(LAST_EXPORTED_PROPERTY))); 173 return null; 174 } 175 176 181 public String getLoadBalancerXML() { 182 return getLoadBalancerXML(true); 183 } 184 185 186 private LoadBalancerConfig getLoadBalancerConfig(String lbName){ 187 ObjectName loadBalancerConfigObjName = null; 188 try{ 189 loadBalancerConfigObjName = 190 new ObjectName (LOADBALANCER_CONFIG_OBJECT_NAME+lbName); 191 } catch ( MalformedObjectNameException 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 lbConfigName){ 201 ObjectName lbConfigObjName = null; 202 try{ 203 lbConfigObjName = 204 new ObjectName (LBCONFIG_OBJECT_NAME+lbConfigName); 205 } catch ( MalformedObjectNameException e ){ 206 e.printStackTrace(); 207 } 208 LBConfig lbConfig = getProxy(lbConfigObjName, LBConfig.class); 209 return lbConfig; 210 } 211 212 private String 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 ().getTime())); 222 else 223 lbConfig.setPropertyValue(LAST_EXPORTED_PROPERTY,String.valueOf(new Date ().getTime())); 224 } 225 226 return lbFactory.getLoadBalancerXML(lbConfigName,lbName); 227 228 } 229 230 private String getMessageDigest(){ 231 try { 232 String lbxml = getLoadBalancerXML(false).split(XML_COMMENT_START)[0]; 233 MessageDigest md = MessageDigest.getInstance(MD5); 234 md.update(lbxml.getBytes()); 235 String hash = new BigInteger (md.digest()).toString(16); 236 return hash; 237 }catch(NoSuchAlgorithmException e){ 238 getMBeanLogger().warning(e.getMessage()); 239 if(getMBeanLogger().isLoggable(Level.FINE)) 240 e.printStackTrace(); 241 } 242 return ""; 243 } 244 245 246 249 public Date getLastResetTime() { 250 return null; 251 } 252 253 256 public void resetStats() { 257 String lbName = getName(); 258 String lbConfigName = null; 259 loadBalancerConfig = getLoadBalancerConfig(lbName); 260 if(loadBalancerConfig!=null) { 261 lbConfigName = loadBalancerConfig.getLbConfigName(); 262 } 263 lbFactory.resetStats(lbConfigName,lbName); 264 } 265 266 269 public boolean testConnection() { 270 String lbName = getName(); 271 String lbConfigName = null; 272 loadBalancerConfig = getLoadBalancerConfig(lbName); 273 if(loadBalancerConfig!=null) { 274 lbConfigName = loadBalancerConfig.getLbConfigName(); 275 } 276 return lbFactory.testConnection(lbConfigName,lbName); 277 } 278 279 283 public String getStatus(String instanceName) { 284 return null; 285 } 286 } 287 288 289 290 291 292 293 294 295 296 | Popular Tags |