📄 mbeaninstantiator.java
字号:
throws InstanceNotFoundException, OperationsException, ReflectionException { // Check parameter validity if (data == null) { throw new RuntimeOperationsException(new IllegalArgumentException(), "Null data passed in parameter"); } if (data.length == 0) { throw new RuntimeOperationsException(new IllegalArgumentException(), "Empty data passed in parameter"); } if (className == null) { throw new RuntimeOperationsException(new IllegalArgumentException(), "Null className passed in parameter"); } Class theClass = null; if (loaderName == null) { // Load the class using the agent class loader theClass = findClass(className, loader); } else { // Get the class loader MBean try { ClassLoader instance = null; if (clr!=null) instance = clr.getClassLoader(loaderName); if (instance == null) throw new ClassNotFoundException(className); theClass = Class.forName(className, false, instance); } catch (ClassNotFoundException e) { throw new ReflectionException(e, "The MBean class could not be loaded by the " + loaderName.toString() + " class loader"); } } // Object deserialization ByteArrayInputStream bIn; ObjectInputStream objIn; String typeStr; bIn = new ByteArrayInputStream(data); try { objIn = new ObjectInputStreamWithLoader(bIn, theClass.getClassLoader()); } catch (IOException e) { throw new OperationsException( "An IOException occurred trying to de-serialize the data"); } return objIn; } /** * Instantiates an object using the list of all class loaders registered * in the MBean Interceptor * (using its {@link javax.management.loading.ClassLoaderRepository}). * <P>The object's class should have a public constructor. * <P>It returns a reference to the newly created object. * <P>The newly created object is not registered in the MBean Interceptor. * * @param className The class name of the object to be instantiated. * * @return The newly instantiated object. * * @exception ReflectionException Wraps a * <CODE>java.lang.ClassNotFoundException</CODE> or the * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the * object's constructor. * @exception MBeanException The constructor of the object has thrown an * exception * @exception RuntimeOperationsException Wraps a * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in * parameter is null. */ public Object instantiate(String className) throws ReflectionException, MBeanException { return instantiate(className, (Object[]) null, (String[]) null, null); } /** * Instantiates an object using the class Loader specified by its * <CODE>ObjectName</CODE>. * <P>If the loader name is null, a default one has to be provided (for a * MBean Server, the ClassLoader that loaded it will be used). * <P>The object's class should have a public constructor. * <P>It returns a reference to the newly created object. * <P>The newly created object is not registered in the MBean Interceptor. * * @param className The class name of the MBean to be instantiated. * @param loaderName The object name of the class loader to be used. * * @return The newly instantiated object. * * @exception ReflectionException Wraps a * <CODE>java.lang.ClassNotFoundException</CODE> or the * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the * object's constructor. * @exception MBeanException The constructor of the object has thrown an * exception. * @exception InstanceNotFoundException The specified class loader is not * registered in the MBeanServerInterceptor. * @exception RuntimeOperationsException Wraps a * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in * parameter is null. */ public Object instantiate(String className, ObjectName loaderName, ClassLoader loader) throws ReflectionException, MBeanException, InstanceNotFoundException { return instantiate(className, loaderName, (Object[]) null, (String[]) null, loader); } /** * Instantiates an object using the list of all class loaders registered * in the MBean server * (using its {@link javax.management.loading.ClassLoaderRepository}). * <P>The object's class should have a public constructor. * <P>The call returns a reference to the newly created object. * <P>The newly created object is not registered in the MBean Interceptor. * * @param className The class name of the object to be instantiated. * @param params An array containing the parameters of the constructor to * be invoked. * @param signature An array containing the signature of the constructor to * be invoked. * * @return The newly instantiated object. * * @exception ReflectionException Wraps a * <CODE>java.lang.ClassNotFoundException</CODE> or the * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the * object's constructor. * @exception MBeanException The constructor of the object has thrown an * exception * @exception RuntimeOperationsException Wraps a * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in * parameter is null. */ public Object instantiate(String className, Object params[], String signature[], ClassLoader loader) throws ReflectionException, MBeanException { Class theClass = findClassWithDefaultLoaderRepository(className); return instantiate(theClass, params, signature, loader); } /** * Instantiates an object. The class loader to be used is identified by its * object name. * <P>If the object name of the loader is null, a default has to be * provided (for example, for a MBean Server, the ClassLoader that loaded * it will be used). * <P>The object's class should have a public constructor. * <P>The call returns a reference to the newly created object. * <P>The newly created object is not registered in the MBean server. * * @param className The class name of the object to be instantiated. * @param params An array containing the parameters of the constructor to * be invoked. * @param signature An array containing the signature of the constructor to * be invoked. * @param loaderName The object name of the class loader to be used. * * @return The newly instantiated object. * * @exception ReflectionException Wraps a * <CODE>java.lang.ClassNotFoundException</CODE> or the * <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the * object's constructor. * @exception MBeanException The constructor of the object has thrown an * exception * @exception InstanceNotFoundException The specified class loader is not * registered in the MBean Interceptor. * @exception RuntimeOperationsException Wraps a * <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in * parameter is null. */ public Object instantiate(String className, ObjectName loaderName, Object params[], String signature[], ClassLoader loader) throws ReflectionException, MBeanException, InstanceNotFoundException { // ------------------------------ // ------------------------------ Class theClass; if (loaderName == null) { theClass = findClass(className, loader); } else { theClass = findClass(className, loaderName); } return instantiate(theClass, params, signature, loader); } /** * Return the Default Loader Repository used by this instantiator object. **/ public ModifiableClassLoaderRepository getClassLoaderRepository() { return clr; } /** * Load a class with the specified loader, or with this object * class loader if the specified loader is null. **/ static Class loadClass(String className, ClassLoader loader) throws ReflectionException { Class theClass = null; if (className == null) { throw new RuntimeOperationsException(new IllegalArgumentException("The class name cannot be null"), "Exception occurred during object instantiation"); } try { if (loader == null) loader = MBeanInstantiator.class.getClassLoader(); if (loader != null) { theClass = Class.forName(className, false, loader); } else { theClass = Class.forName(className); } } catch (ClassNotFoundException e) { throw new ReflectionException(e, "The MBean class could not be loaded by the context classloader"); } return theClass; } /** * Load the classes specified in the signature with the given loader, * or with this object class loader. **/ static Class[] loadSignatureClasses(String signature[], ClassLoader loader) throws ReflectionException { if (signature == null) return null; final ClassLoader aLoader = (loader==null?MBeanInstantiator.class.getClassLoader():loader); final int length= signature.length; final Class tab[]=new Class[length]; if (length == 0) return tab; try { for (int i= 0; i < length; i++) { // Start handling primitive types (int. boolean and so // forth) // final Class primCla = primitiveClasses.get(signature[i]); if (primCla != null) { tab[i] = primCla; continue; } // Ok we do not have a primitive type ! We need to build // the signature of the method // // We need to load the class through the class // loader of the target object. // tab[i] = Class.forName(signature[i], false, aLoader); } } catch (ClassNotFoundException e) { debugX("findSignatureClasses",e); throw new ReflectionException(e, "The parameter class could not be found"); } catch (RuntimeException e) { debugX("findSignatureClasses",e); throw e; } return tab; } private Constructor<?> findConstructor(Class<?> c, Class<?>[] params) { try { return c.getConstructor(params); } catch (Exception e) { return null; } } // TRACES & DEBUG //--------------- private static boolean isTraceOn() { return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); } private static void trace(String clz, String func, String info) { Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info); } private static void trace(String func, String info) { trace(dbgTag, func, info); } private static boolean isDebugOn() { return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); } private static void debug(String clz, String func, String info) { Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info); } private static void debug(String func, String info) { debug(dbgTag, func, info); } private static void debugX(String func,Throwable e) { if (isDebugOn()) { final StringWriter s = new StringWriter(); e.printStackTrace(new PrintWriter(s)); final String stack = s.toString(); debug(dbgTag,func,"Exception caught in "+ func+"(): "+e); debug(dbgTag,func,stack); // java.lang.System.err.println("**** Exception caught in "+ // func+"(): "+e); // java.lang.System.err.println(stack); } } private static final Map<String, Class<?>> primitiveClasses = Util.newMap(); static { for (Class<?> c : new Class[] {byte.class, short.class, int.class, long.class, float.class, double.class, char.class, boolean.class}) primitiveClasses.put(c.getName(), c); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -