1 10 11 package org.mule.management.agents; 12 13 import java.net.URI ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import javax.management.InstanceNotFoundException ; 18 import javax.management.MBeanException ; 19 import javax.management.MBeanRegistrationException ; 20 import javax.management.MBeanServer ; 21 import javax.management.MBeanServerFactory ; 22 import javax.management.MalformedObjectNameException ; 23 import javax.management.ObjectName ; 24 import javax.management.ReflectionException ; 25 26 import mx4j.log.CommonsLogger; 27 import mx4j.log.Log; 28 import mx4j.tools.adaptor.http.HttpAdaptor; 29 import mx4j.tools.adaptor.http.XSLTProcessor; 30 import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactory; 31 import mx4j.tools.adaptor.ssl.SSLAdaptorServerSocketFactoryMBean; 32 33 import org.apache.commons.logging.LogFactory; 34 import org.mule.config.i18n.Message; 35 import org.mule.config.i18n.Messages; 36 import org.mule.management.support.AutoDiscoveryJmxSupportFactory; 37 import org.mule.management.support.JmxSupport; 38 import org.mule.management.support.JmxSupportFactory; 39 import org.mule.umo.UMOException; 40 import org.mule.umo.lifecycle.InitialisationException; 41 import org.mule.umo.manager.UMOAgent; 42 import org.mule.util.BeanUtils; 43 import org.mule.util.StringUtils; 44 import org.mule.util.SystemUtils; 45 46 50 public class Mx4jAgent implements UMOAgent 51 { 52 public static final String HTTP_ADAPTER_OBJECT_NAME = "name=Mx4jHttpAdapter"; 53 54 private static final org.apache.commons.logging.Log logger = LogFactory.getLog(Mx4jAgent.class); 55 56 private String name = "MX4J Agent"; 57 private String jmxAdaptorUrl = "http://localhost:9999"; 58 private HttpAdaptor adaptor; 59 private MBeanServer mBeanServer; 60 private ObjectName adaptorName; 61 62 private String login = null; 64 65 private String password = null; 66 67 private String authenticationMethod = "basic"; 68 69 private String xslFilePath = null; 70 71 private String pathInJar = null; 72 73 private boolean cacheXsl = true; 74 75 private Map socketFactoryProperties = new HashMap (); 77 78 private JmxSupportFactory jmxSupportFactory = new AutoDiscoveryJmxSupportFactory(); 79 private JmxSupport jmxSupport; 80 81 protected HttpAdaptor createAdaptor() throws Exception 82 { 83 84 Log.redirectTo(new CommonsLogger()); 85 URI uri = new URI (StringUtils.stripToEmpty(jmxAdaptorUrl)); 86 adaptor = new HttpAdaptor(uri.getPort(), uri.getHost()); 87 88 XSLTProcessor processor = new XSLTProcessor(); 90 91 if (StringUtils.isNotBlank(xslFilePath)) 92 { 93 processor.setFile(xslFilePath.trim()); 94 } 95 96 if (StringUtils.isNotBlank(pathInJar)) 97 { 98 processor.setPathInJar(pathInJar.trim()); 99 } 100 101 processor.setUseCache(cacheXsl); 102 103 adaptor.setProcessor(processor); 104 105 if (login != null) 107 { 108 adaptor.addAuthorization(login, password); 109 adaptor.setAuthenticationMethod(authenticationMethod); 110 } 111 112 if (socketFactoryProperties != null && !socketFactoryProperties.isEmpty()) 113 { 114 SSLAdaptorServerSocketFactoryMBean factory; 115 if (SystemUtils.isIbmJDK()) 116 { 117 factory = new IBMSslAdapterServerSocketFactory(); 118 } 119 else 120 { 121 factory = new SSLAdaptorServerSocketFactory(); 123 } 124 BeanUtils.populateWithoutFail(factory, socketFactoryProperties, true); 125 adaptor.setSocketFactory(factory); 126 } 127 128 return adaptor; 129 } 130 131 132 public void initialise() throws InitialisationException 133 { 134 try 135 { 136 jmxSupport = jmxSupportFactory.newJmxSupport(); 137 mBeanServer = (MBeanServer ) MBeanServerFactory.findMBeanServer(null).get(0); 138 139 adaptor = createAdaptor(); 140 adaptorName = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":" + HTTP_ADAPTER_OBJECT_NAME); 141 142 unregisterMBeansIfNecessary(); 143 mBeanServer.registerMBean(adaptor, adaptorName); 144 } 145 catch (Exception e) 146 { 147 throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "mx4j agent"), e, this); 148 } 149 } 150 151 152 public void start() throws UMOException 153 { 154 try 155 { 156 mBeanServer.invoke(adaptorName, "start", null, null); 157 } 158 catch (InstanceNotFoundException e) 159 { 160 throw new JmxManagementException(new Message(Messages.FAILED_TO_START_X, "Mx4j agent"), 161 adaptorName, e); 162 } 163 catch (MBeanException e) 164 { 165 throw new JmxManagementException(new Message(Messages.FAILED_TO_START_X, "Mx4j agent"), 166 adaptorName, e); 167 } 168 catch (ReflectionException e) 169 { 170 } 172 } 173 174 175 public void stop() throws UMOException 176 { 177 try 178 { 179 mBeanServer.invoke(adaptorName, "stop", null, null); 180 } 181 catch (InstanceNotFoundException e) 182 { 183 throw new JmxManagementException(new Message(Messages.FAILED_TO_STOP_X, "Mx4j agent"), 184 adaptorName, e); 185 } 186 catch (MBeanException e) 187 { 188 throw new JmxManagementException(new Message(Messages.FAILED_TO_STOP_X, "Mx4j agent"), 189 adaptorName, e); 190 } 191 catch (ReflectionException e) 192 { 193 } 195 } 196 197 200 protected void unregisterMBeansIfNecessary() 201 throws MalformedObjectNameException , InstanceNotFoundException , MBeanRegistrationException 202 { 203 if (mBeanServer.isRegistered(adaptorName)) 204 { 205 mBeanServer.unregisterMBean(adaptorName); 206 } 207 } 208 209 210 public void dispose() 211 { 212 try 213 { 214 stop(); 215 } 216 catch (Exception e) 217 { 218 logger.warn("Failed to stop Mx4jAgent: " + e.getMessage()); 219 } 220 finally 221 { 222 try 223 { 224 unregisterMBeansIfNecessary(); 225 } 226 catch (Exception e) 227 { 228 logger.error("Couldn't unregister MBean: " 229 + (adaptorName != null ? adaptorName.getCanonicalName() : "null"), e); 230 } 231 } 232 } 233 234 235 public void registered() 236 { 237 } 239 240 241 public void unregistered() 242 { 243 } 245 246 250 251 public String getDescription() 252 { 253 return "MX4J Http adaptor: " + jmxAdaptorUrl; 254 } 255 256 257 public String getName() 258 { 259 return this.name; 260 } 261 262 263 public void setName(String name) 264 { 265 this.name = name; 266 } 267 268 269 public String getJmxAdaptorUrl() 270 { 271 return jmxAdaptorUrl; 272 } 273 274 275 public void setJmxAdaptorUrl(String jmxAdaptorUrl) 276 { 277 this.jmxAdaptorUrl = jmxAdaptorUrl; 278 } 279 280 public Map getSocketFactoryProperties() 281 { 282 return socketFactoryProperties; 283 } 284 285 public void setSocketFactoryProperties(Map socketFactoryProperties) 286 { 287 this.socketFactoryProperties = socketFactoryProperties; 288 } 289 290 public String getLogin() 291 { 292 return login; 293 } 294 295 public void setLogin(String login) 296 { 297 this.login = login; 298 } 299 300 public String getPassword() 301 { 302 return password; 303 } 304 305 public void setPassword(String password) 306 { 307 this.password = password; 308 } 309 310 public String getAuthenticationMethod() 311 { 312 return authenticationMethod; 313 } 314 315 public void setAuthenticationMethod(String authenticationMethod) 316 { 317 this.authenticationMethod = authenticationMethod; 318 } 319 320 public String getXslFilePath() 321 { 322 return xslFilePath; 323 } 324 325 public void setXslFilePath(String xslFilePath) 326 { 327 this.xslFilePath = xslFilePath; 328 } 329 330 public String getPathInJar() 331 { 332 return pathInJar; 333 } 334 335 public void setPathInJar(String pathInJar) 336 { 337 this.pathInJar = pathInJar; 338 } 339 340 public boolean isCacheXsl() 341 { 342 return cacheXsl; 343 } 344 345 public void setCacheXsl(boolean cacheXsl) 346 { 347 this.cacheXsl = cacheXsl; 348 } 349 } 350 | Popular Tags |