1 22 package org.jboss.mx.standardmbean; 23 24 import java.lang.reflect.Method ; 25 import java.util.Iterator ; 26 27 import javax.management.Attribute ; 28 import javax.management.AttributeList ; 29 import javax.management.AttributeNotFoundException ; 30 import javax.management.InvalidAttributeValueException ; 31 import javax.management.JMException ; 32 import javax.management.MBeanException ; 33 import javax.management.MBeanInfo ; 34 import javax.management.NotCompliantMBeanException ; 35 import javax.management.ReflectionException ; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.mx.loading.LoaderRepository; 39 import org.jboss.mx.metadata.StandardMetaData; 40 import org.jboss.mx.server.ExceptionHandler; 41 42 55 public class StandardMBeanImpl implements StandardMBeanDelegate 56 { 57 private static final Logger log = Logger.getLogger(StandardMBeanImpl.class); 58 59 62 private Object implementation; 63 64 67 private Class mbeanInterface; 68 69 72 private MBeanInfo cachedMBeanInfo; 73 74 76 78 89 public StandardMBeanImpl(Object implementation, Class mbeanInterface) 90 throws NotCompliantMBeanException 91 { 92 this.implementation = implementation; 93 this.mbeanInterface = mbeanInterface; 94 } 95 96 107 protected StandardMBeanImpl(Class mbeanInterface) 108 throws NotCompliantMBeanException 109 { 110 this.implementation = this; 111 this.mbeanInterface = mbeanInterface; 112 } 113 114 119 public Object getImplementation() 120 { 121 return implementation; 122 } 123 124 134 public void setImplementation(Object implementation) 135 throws NotCompliantMBeanException 136 { 137 if (implementation == null) 138 throw new IllegalArgumentException ("Null implementation"); 139 this.implementation = implementation; 140 } 141 142 147 public Class getImplementationClass() 148 { 149 return implementation.getClass(); 150 } 151 152 157 public final Class getMBeanInterface() 158 { 159 return mbeanInterface; 160 } 161 162 public Object getAttribute(String attribute) 163 throws AttributeNotFoundException , MBeanException , ReflectionException 164 { 165 try 166 { 167 Method method = implementation.getClass().getMethod("get" + attribute, null); 168 return method.invoke(implementation, new Object [0]); 169 } 170 catch (Exception e) 171 { 172 JMException result = ExceptionHandler.handleException(e); 173 if (result instanceof AttributeNotFoundException ) 174 throw (AttributeNotFoundException )result; 175 if (result instanceof MBeanException ) 176 throw (MBeanException )result; 177 if (result instanceof ReflectionException ) 178 throw (ReflectionException )result; 179 throw new MBeanException (e, "Cannot get attribute: " + attribute); 180 } 181 } 182 183 public void setAttribute(Attribute attribute) 184 throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 185 { 186 try 187 { 188 Class [] clArr = null; 189 if (attribute.getValue() != null) 190 { 191 clArr = new Class []{attribute.getValue().getClass()}; 192 } 193 Method method = implementation.getClass().getMethod("set" + attribute.getName(), clArr); 194 method.invoke(implementation, new Object []{attribute.getValue()}); 195 } 196 catch (Exception e) 197 { 198 JMException result = ExceptionHandler.handleException(e); 199 if (result instanceof AttributeNotFoundException ) 200 throw (AttributeNotFoundException )result; 201 if (result instanceof InvalidAttributeValueException ) 202 throw (InvalidAttributeValueException )result; 203 if (result instanceof MBeanException ) 204 throw (MBeanException )result; 205 if (result instanceof ReflectionException ) 206 throw (ReflectionException )result; 207 throw new MBeanException (e, "Cannot set attribute: " + attribute); 208 } 209 } 210 211 public AttributeList getAttributes(String [] attributes) 212 { 213 try 214 { 215 AttributeList attrList = new AttributeList (attributes.length); 216 for (int i = 0; i < attributes.length; i++) 217 { 218 String name = attributes[i]; 219 Object value = getAttribute(name); 220 attrList.add(new Attribute (name, value)); 221 } 222 return attrList; 223 } 224 catch (Exception e) 225 { 226 JMException result = ExceptionHandler.handleException(e); 227 throw new RuntimeException ("Cannot get attributes", result); 229 } 230 } 231 232 233 public AttributeList setAttributes(AttributeList attributes) 234 { 235 try 236 { 237 AttributeList attrList = new AttributeList (attributes.size()); 238 Iterator it = attributes.iterator(); 239 while (it.hasNext()) 240 { 241 Attribute attr = (Attribute ) it.next(); 242 setAttribute(attr); 243 String name = attr.getName(); 244 Object value = getAttribute(name); 245 attrList.add(new Attribute (name, value)); 246 } 247 return attrList; 248 } 249 catch (Exception e) 250 { 251 JMException result = ExceptionHandler.handleException(e); 252 throw new RuntimeException ("Cannot set attributes", result); 254 } 255 } 256 257 public Object invoke(String actionName, Object [] params, String [] signature) 258 throws MBeanException , ReflectionException 259 { 260 try 261 { 262 Class [] sigcl = new Class [signature.length]; 263 for (int i = 0; i < signature.length; i++) 264 { 265 sigcl[i] = loadClass(signature[i]); 266 } 267 Method method = implementation.getClass().getMethod(actionName, sigcl); 268 return method.invoke(implementation, params); 269 } 270 catch (Exception e) 271 { 272 JMException result = ExceptionHandler.handleException(e); 273 if (result instanceof MBeanException ) 274 throw (MBeanException )result; 275 if (result instanceof ReflectionException ) 276 throw (ReflectionException )result; 277 throw new MBeanException (e, "Cannot invoke: " + actionName); 278 } 279 } 280 281 284 private Class loadClass(String className) throws ClassNotFoundException 285 { 286 Class clazz = LoaderRepository.getNativeClassForName(className); 287 if (clazz == null) { 288 ClassLoader cl = getClass().getClassLoader(); 289 clazz = cl.loadClass(className); 290 } 291 return clazz; 292 } 293 294 public MBeanInfo getMBeanInfo() 295 { 296 MBeanInfo info = getCachedMBeanInfo(); 297 if (info == null) 298 { 299 try 300 { 301 info = buildMBeanInfo(); 302 cacheMBeanInfo(info); 303 } 304 catch (NotCompliantMBeanException e) 305 { 306 log.error("Unexcepted exception", e); 307 throw new IllegalStateException ("Unexcepted exception " + e.toString()); 308 } 309 310 } 311 return info; 312 } 313 314 319 public MBeanInfo getCachedMBeanInfo() 320 { 321 return cachedMBeanInfo; 322 } 323 324 330 public void cacheMBeanInfo(MBeanInfo info) 331 { 332 cachedMBeanInfo = info; 333 } 334 335 341 public MBeanInfo buildMBeanInfo() 342 throws NotCompliantMBeanException 343 { 344 if (implementation == null) 345 throw new IllegalArgumentException ("Null implementation"); 346 347 StandardMetaData metaData = new StandardMetaData(implementation, mbeanInterface); 348 this.mbeanInterface = metaData.getMBeanInterface(); 349 return metaData.build(); 350 } 351 352 } 354 | Popular Tags |