1 16 package org.outerj.daisy.jmx; 17 18 import java.io.ObjectInputStream ; 19 import java.util.Set ; 20 import java.util.List ; 21 22 import javax.management.Attribute ; 23 import javax.management.AttributeList ; 24 import javax.management.AttributeNotFoundException ; 25 import javax.management.InstanceAlreadyExistsException ; 26 import javax.management.InstanceNotFoundException ; 27 import javax.management.IntrospectionException ; 28 import javax.management.InvalidAttributeValueException ; 29 import javax.management.ListenerNotFoundException ; 30 import javax.management.MBeanException ; 31 import javax.management.MBeanInfo ; 32 import javax.management.MBeanRegistrationException ; 33 import javax.management.MBeanServer ; 34 import javax.management.MBeanServerFactory ; 35 import javax.management.NotCompliantMBeanException ; 36 import javax.management.NotificationFilter ; 37 import javax.management.NotificationListener ; 38 import javax.management.ObjectInstance ; 39 import javax.management.ObjectName ; 40 import javax.management.OperationsException ; 41 import javax.management.QueryExp ; 42 import javax.management.ReflectionException ; 43 import javax.management.loading.ClassLoaderRepository ; 44 45 import mx4j.log.Log; 46 import mx4j.log.Logger; 47 import mx4j.tools.adaptor.http.HttpAdaptor; 48 import mx4j.tools.adaptor.http.XSLTProcessor; 49 50 import org.apache.avalon.framework.activity.Initializable; 51 import org.apache.avalon.framework.activity.Startable; 52 import org.apache.avalon.framework.configuration.Configurable; 53 import org.apache.avalon.framework.configuration.Configuration; 54 import org.apache.avalon.framework.configuration.ConfigurationException; 55 import org.apache.avalon.framework.logger.AbstractLogEnabled; 56 57 61 public class MBeanServerImpl extends AbstractLogEnabled implements MBeanServer , Initializable, Configurable, Startable { 62 private MBeanServer delegate; 63 64 private int httpAdaptorPort; 65 private String httpAdaptorHost; 66 private String httpAdaptorAuthMethod; 67 private String httpAdaptorUser; 68 private String httpAdaptorPassword; 69 70 private int xmlHttpAdaptorPort; 71 private String xmlHttpAdaptorHost; 72 private String xmlHttpAdaptorAuthMethod; 73 private String xmlHttpAdaptorUser; 74 private String xmlHttpAdaptorPassword; 75 76 private HttpAdaptor httpAdaptor; 77 private HttpAdaptor xmlHttpAdaptor; 78 79 public void configure(Configuration configuration) throws ConfigurationException { 80 Configuration httpAdaptorConf = configuration.getChild("httpAdaptor"); 81 httpAdaptorPort = httpAdaptorConf.getAttributeAsInteger("port"); 82 httpAdaptorHost = httpAdaptorConf.getAttribute("host", "localhost"); 83 httpAdaptorAuthMethod = httpAdaptorConf.getAttribute("authenticationMethod", "none"); 84 httpAdaptorUser = httpAdaptorConf.getAttribute("username", null); 85 httpAdaptorPassword = httpAdaptorConf.getAttribute("password", null); 86 87 Configuration xmlHttpAdaptorConf = configuration.getChild("xmlHttpAdaptor"); 88 xmlHttpAdaptorPort = xmlHttpAdaptorConf.getAttributeAsInteger("port"); 89 xmlHttpAdaptorHost = xmlHttpAdaptorConf.getAttribute("host", "localhost"); 90 xmlHttpAdaptorAuthMethod = xmlHttpAdaptorConf.getAttribute("authenticationMethod", "none"); 91 xmlHttpAdaptorUser = xmlHttpAdaptorConf.getAttribute("username", null); 92 xmlHttpAdaptorPassword = xmlHttpAdaptorConf.getAttribute("password", null); 93 } 94 95 public void initialize() throws Exception { 96 MyLogger.avalonLogger = getLogger(); 98 Log.redirectTo(new MyLogger()); 99 100 List list = MBeanServerFactory.findMBeanServer(null); 102 if (list != null && list.size() > 0) { 103 delegate = (MBeanServer )list.get(0); 104 } else { 105 delegate = MBeanServerFactory.createMBeanServer("daisy"); 106 } 107 108 { 109 httpAdaptor = new HttpAdaptor(); 110 ObjectName httpAdaptorName = new ObjectName ("Server:name=HttpAdaptor"); 111 registerMBean(httpAdaptor, httpAdaptorName); 112 configureHttpAdaptor(httpAdaptor, httpAdaptorPort, httpAdaptorHost, httpAdaptorAuthMethod, 113 httpAdaptorUser, httpAdaptorPassword); 114 } 115 116 { 117 xmlHttpAdaptor = new HttpAdaptor(); 118 ObjectName xmlHttpAdaptorName = new ObjectName ("Server:name=XmlHttpAdaptor"); 119 registerMBean(xmlHttpAdaptor, xmlHttpAdaptorName); 120 configureHttpAdaptor(xmlHttpAdaptor, xmlHttpAdaptorPort, xmlHttpAdaptorHost, xmlHttpAdaptorAuthMethod, 121 xmlHttpAdaptorUser, xmlHttpAdaptorPassword); 122 } 123 124 ObjectName xsltProcessorName = new ObjectName ("Server:name=XSLTProcessor"); 125 XSLTProcessor xsltProcessor = new XSLTProcessor(); 126 registerMBean(xsltProcessor, xsltProcessorName); 127 httpAdaptor.setProcessorName(xsltProcessorName); 128 } 129 130 private void configureHttpAdaptor(HttpAdaptor httpAdaptor, int port, String host, String authMethod, String user, String password) { 131 httpAdaptor.setPort(port); 132 httpAdaptor.setHost(host); 133 httpAdaptor.setAuthenticationMethod(authMethod); 134 if (user != null) { 135 httpAdaptor.addAuthorization(user, password); 136 } 137 } 138 139 public void start() throws Exception { 140 httpAdaptor.start(); 141 xmlHttpAdaptor.start(); 142 } 143 144 public void stop() throws Exception { 145 httpAdaptor.stop(); 146 xmlHttpAdaptor.stop(); 147 } 148 149 public static class MyLogger extends Logger { 150 public static org.apache.avalon.framework.logger.Logger avalonLogger; 152 153 protected void log(int priority, Object message, Throwable throwable) { 154 switch (priority) { 155 case FATAL: 156 if (throwable == null) 157 avalonLogger.fatalError(message.toString()); 158 else 159 avalonLogger.fatalError(message.toString(), throwable); 160 break; 161 case ERROR: 162 if (throwable == null) 163 avalonLogger.error(message.toString()); 164 else 165 avalonLogger.error(message.toString(), throwable); 166 break; 167 case WARN: 168 if (throwable == null) 169 avalonLogger.warn(message.toString()); 170 else 171 avalonLogger.warn(message.toString(), throwable); 172 break; 173 case INFO: 174 if (avalonLogger.isInfoEnabled()) { 175 if (throwable == null) 176 avalonLogger.info(message.toString()); 177 else 178 avalonLogger.info(message.toString(), throwable); 179 } 180 break; 181 case DEBUG: 182 case TRACE: 183 if (avalonLogger.isDebugEnabled()) { 184 if (throwable == null) 185 avalonLogger.debug(message.toString()); 186 else 187 avalonLogger.debug(message.toString(), throwable); 188 } 189 break; 190 } 191 } 192 193 public int getPriority() { 194 if (avalonLogger.isFatalErrorEnabled()) { 195 return FATAL; 196 } else if (avalonLogger.isErrorEnabled()) { 197 return ERROR; 198 } else if (avalonLogger.isWarnEnabled()) { 199 return WARN; 200 } else if (avalonLogger.isInfoEnabled()) { 201 return INFO; 202 } else { 203 return DEBUG; 204 } 205 } 206 } 207 208 public void addNotificationListener(ObjectName objectName, NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException { 209 delegate.addNotificationListener(objectName, notificationListener, notificationFilter, o); 210 } 211 212 public void addNotificationListener(ObjectName objectName, ObjectName objectName1, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException { 213 delegate.addNotificationListener(objectName, objectName1, notificationFilter, o); 214 } 215 216 public void removeNotificationListener(ObjectName objectName, ObjectName objectName1) throws InstanceNotFoundException , ListenerNotFoundException { 217 delegate.removeNotificationListener(objectName, objectName1); 218 } 219 220 public void removeNotificationListener(ObjectName objectName, NotificationListener notificationListener) throws InstanceNotFoundException , ListenerNotFoundException { 221 delegate.removeNotificationListener(objectName, notificationListener); 222 } 223 224 public void removeNotificationListener(ObjectName objectName, ObjectName objectName1, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException , ListenerNotFoundException { 225 delegate.removeNotificationListener(objectName, objectName1, notificationFilter, o); 226 } 227 228 public void removeNotificationListener(ObjectName objectName, NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws InstanceNotFoundException , ListenerNotFoundException { 229 delegate.removeNotificationListener(objectName, notificationListener, notificationFilter, o); 230 } 231 232 public MBeanInfo getMBeanInfo(ObjectName objectName) throws InstanceNotFoundException , IntrospectionException , ReflectionException { 233 return delegate.getMBeanInfo(objectName); 234 } 235 236 public boolean isInstanceOf(ObjectName objectName, String s) throws InstanceNotFoundException { 237 return delegate.isInstanceOf(objectName, s); 238 } 239 240 public String [] getDomains() { 241 return delegate.getDomains(); 242 } 243 244 public String getDefaultDomain() { 245 return delegate.getDefaultDomain(); 246 } 247 248 public ObjectInstance createMBean(String s, ObjectName objectName) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException { 249 return delegate.createMBean(s, objectName); 250 } 251 252 public ObjectInstance createMBean(String s, ObjectName objectName, ObjectName objectName1) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException { 253 return delegate.createMBean(s, objectName, objectName1); 254 } 255 256 public ObjectInstance createMBean(String s, ObjectName objectName, Object [] objects, String [] strings) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException { 257 return delegate.createMBean(s, objectName, objects, strings); 258 } 259 260 public ObjectInstance createMBean(String s, ObjectName objectName, ObjectName objectName1, Object [] objects, String [] strings) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException { 261 return delegate.createMBean(s, objectName, objectName1, objects, strings); 262 } 263 264 public void unregisterMBean(ObjectName objectName) throws InstanceNotFoundException , MBeanRegistrationException { 265 delegate.unregisterMBean(objectName); 266 } 267 268 public Object getAttribute(ObjectName objectName, String s) throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException { 269 return delegate.getAttribute(objectName, s); 270 } 271 272 public void setAttribute(ObjectName objectName, Attribute attribute) throws InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException { 273 delegate.setAttribute(objectName, attribute); 274 } 275 276 public AttributeList getAttributes(ObjectName objectName, String [] strings) throws InstanceNotFoundException , ReflectionException { 277 return delegate.getAttributes(objectName, strings); 278 } 279 280 public AttributeList setAttributes(ObjectName objectName, AttributeList attributeList) throws InstanceNotFoundException , ReflectionException { 281 return delegate.setAttributes(objectName, attributeList); 282 } 283 284 public Object invoke(ObjectName objectName, String s, Object [] objects, String [] strings) throws InstanceNotFoundException , MBeanException , ReflectionException { 285 return delegate.invoke(objectName, s, objects, strings); 286 } 287 288 public Integer getMBeanCount() { 289 return delegate.getMBeanCount(); 290 } 291 292 public boolean isRegistered(ObjectName objectName) { 293 return delegate.isRegistered(objectName); 294 } 295 296 public ObjectInstance getObjectInstance(ObjectName objectName) throws InstanceNotFoundException { 297 return delegate.getObjectInstance(objectName); 298 } 299 300 public Set queryMBeans(ObjectName objectName, QueryExp queryExp) { 301 return delegate.queryMBeans(objectName, queryExp); 302 } 303 304 public Set queryNames(ObjectName objectName, QueryExp queryExp) { 305 return delegate.queryNames(objectName, queryExp); 306 } 307 308 public Object instantiate(String s) throws ReflectionException , MBeanException { 309 return delegate.instantiate(s); 310 } 311 312 public Object instantiate(String s, ObjectName objectName) throws ReflectionException , MBeanException , InstanceNotFoundException { 313 return delegate.instantiate(s, objectName); 314 } 315 316 public Object instantiate(String s, Object [] objects, String [] strings) throws ReflectionException , MBeanException { 317 return delegate.instantiate(s, objects, strings); 318 } 319 320 public Object instantiate(String s, ObjectName objectName, Object [] objects, String [] strings) throws ReflectionException , MBeanException , InstanceNotFoundException { 321 return delegate.instantiate(s, objectName, objects, strings); 322 } 323 324 public ObjectInstance registerMBean(Object o, ObjectName objectName) throws InstanceAlreadyExistsException , MBeanRegistrationException , NotCompliantMBeanException { 325 return delegate.registerMBean(o, objectName); 326 } 327 328 public ObjectInputStream deserialize(String s, ObjectName objectName, byte[] bytes) throws InstanceNotFoundException , OperationsException , ReflectionException { 329 return delegate.deserialize(s, objectName, bytes); 330 } 331 332 public ObjectInputStream deserialize(String s, byte[] bytes) throws OperationsException , ReflectionException { 333 return delegate.deserialize(s, bytes); 334 } 335 336 public ObjectInputStream deserialize(ObjectName objectName, byte[] bytes) throws InstanceNotFoundException , OperationsException { 337 return delegate.deserialize(objectName, bytes); 338 } 339 340 public ClassLoader getClassLoaderFor(ObjectName objectName) throws InstanceNotFoundException { 341 return delegate.getClassLoaderFor(objectName); 342 } 343 344 public ClassLoader getClassLoader(ObjectName objectName) throws InstanceNotFoundException { 345 return delegate.getClassLoader(objectName); 346 } 347 348 public ClassLoaderRepository getClassLoaderRepository() { 349 return delegate.getClassLoaderRepository(); 350 } 351 352 } 353 | Popular Tags |