1 16 17 package org.springframework.jmx.export; 18 19 import javax.management.Attribute ; 20 import javax.management.AttributeList ; 21 import javax.management.AttributeNotFoundException ; 22 import javax.management.InstanceNotFoundException ; 23 import javax.management.InvalidAttributeValueException ; 24 import javax.management.MBeanException ; 25 import javax.management.ReflectionException ; 26 import javax.management.RuntimeOperationsException ; 27 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 28 import javax.management.modelmbean.ModelMBeanInfo ; 29 import javax.management.modelmbean.RequiredModelMBean ; 30 31 40 public class SpringModelMBean extends RequiredModelMBean { 41 42 46 private ClassLoader managedResourceClassLoader = Thread.currentThread().getContextClassLoader(); 47 48 49 53 public SpringModelMBean() throws MBeanException , RuntimeOperationsException { 54 super(); 55 } 56 57 61 public SpringModelMBean(ModelMBeanInfo mbi) throws MBeanException , RuntimeOperationsException { 62 super(mbi); 63 } 64 65 66 69 public void setManagedResource(Object managedResource, String managedResourceType) 70 throws MBeanException , InstanceNotFoundException , InvalidTargetObjectTypeException { 71 72 this.managedResourceClassLoader = managedResource.getClass().getClassLoader(); 73 super.setManagedResource(managedResource, managedResourceType); 74 } 75 76 77 82 public Object invoke(final String opName, final Object [] opArgs, final String [] sig) 83 throws MBeanException , ReflectionException { 84 85 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); 86 try { 87 Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); 88 return super.invoke(opName, opArgs, sig); 89 } 90 finally { 91 Thread.currentThread().setContextClassLoader(currentClassLoader); 92 } 93 } 94 95 100 public Object getAttribute(final String attrName) 101 throws AttributeNotFoundException , MBeanException , ReflectionException { 102 103 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); 104 try { 105 Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); 106 return super.getAttribute(attrName); 107 } 108 finally { 109 Thread.currentThread().setContextClassLoader(currentClassLoader); 110 } 111 } 112 113 118 public AttributeList getAttributes(String [] attrNames) { 119 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); 120 try { 121 Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); 122 return super.getAttributes(attrNames); 123 } 124 finally { 125 Thread.currentThread().setContextClassLoader(currentClassLoader); 126 } 127 } 128 129 134 public void setAttribute(Attribute attribute) 135 throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException { 136 137 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); 138 try { 139 Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); 140 super.setAttribute(attribute); 141 } 142 finally { 143 Thread.currentThread().setContextClassLoader(currentClassLoader); 144 } 145 } 146 147 152 public AttributeList setAttributes(AttributeList attributes) { 153 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); 154 try { 155 Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader); 156 return super.setAttributes(attributes); 157 } 158 finally { 159 Thread.currentThread().setContextClassLoader(currentClassLoader); 160 } 161 } 162 163 } 164 | Popular Tags |