1 22 package org.jboss.mx.modelmbean; 23 24 import java.lang.reflect.Constructor ; 25 26 import javax.management.modelmbean.ModelMBean ; 27 import javax.management.modelmbean.ModelMBeanInfo ; 28 29 import org.jboss.mx.server.ServerConstants; 30 import org.jboss.mx.util.PropertyAccess; 31 32 43 public class RequiredModelMBeanInstantiator 44 { 45 public static ModelMBean instantiate() 46 { 47 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 48 String className = getClassName(); 49 try 50 { 51 Class modelMBean = cl.loadClass(className); 52 return (ModelMBean ) modelMBean.newInstance(); 53 } 54 catch (ClassNotFoundException e) 55 { 56 throw new Error ("Cannot instantiate model mbean class. Class " + className + " not found."); 57 } 58 catch (ClassCastException e) 59 { 60 throw new Error ("Cannot instantiate model mbean class. The target class is not an instance of ModelMBean interface."); 61 } 62 catch (Exception e) 63 { 64 throw new Error ("Cannot instantiate model mbean class " + className + " with default constructor: " + e.getMessage()); 65 } 66 } 67 68 public static ModelMBean instantiate(ModelMBeanInfo info) 69 { 70 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 71 String className = getClassName(); 72 try 73 { 74 Class modelMBean = cl.loadClass(className); 75 Constructor constructor = modelMBean.getConstructor(new Class [] { ModelMBeanInfo .class }); 76 return (ModelMBean ) constructor.newInstance(new Object [] { info }); 77 } 78 catch (ClassNotFoundException e) 79 { 80 throw new Error ("Cannot instantiate model mbean class. Class " + className + " not found."); 81 } 82 catch (ClassCastException e) 83 { 84 throw new Error ("Cannot instantiate model mbean class. The target class is not an instance of ModelMBean interface."); 85 } 86 catch (Exception e) 87 { 88 throw new Error ("Cannot instantiate model mbean class " + className + ": " + e.toString()); 89 } 90 } 91 92 public static String getClassName() 93 { 94 return PropertyAccess.getProperty 95 ( 96 ServerConstants.REQUIRED_MODELMBEAN_CLASS_PROPERTY, 97 ServerConstants.DEFAULT_REQUIRED_MODELMBEAN_CLASS 98 ); 99 } 100 } 101 | Popular Tags |