📄 mbeaninstantiator.java
字号:
/* * @(#)MBeanInstantiator.java 1.40 07/07/23 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.jmx.mbeanserver;import java.io.*;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.util.Map;import javax.management.*; import com.sun.jmx.trace.Trace;import sun.reflect.misc.ReflectUtil;/** * Implements the MBeanInstantiator interface. Provides methods for * instantiating objects, finding the class given its name and using * different class loaders, deserializing objects in the context of a * given class loader. * * @since 1.5 * @since.unbundled JMX RI 1.2 */public class MBeanInstantiator { private final ModifiableClassLoaderRepository clr; // private MetaData meta = null; /** The name of this class to be used for tracing */ private final static String dbgTag = "MBeanInstantiator"; MBeanInstantiator(ModifiableClassLoaderRepository clr) { this.clr = clr; } /** * This methods tests if the MBean class makes it possible to * instantiate an MBean of this class in the MBeanServer. * e.g. it must have a public constructor, be a concrete class... */ public void testCreation(Class c) throws NotCompliantMBeanException { Introspector.testCreation(c); } /** * Loads the class with the specified name using this object's * Default Loader Repository. **/ public Class findClassWithDefaultLoaderRepository(String className) throws ReflectionException { Class theClass; if (className == null) { throw new RuntimeOperationsException(new IllegalArgumentException("The class name cannot be null"), "Exception occurred during object instantiation"); } try { if (clr == null) throw new ClassNotFoundException(className); theClass = clr.loadClass(className); } catch (ClassNotFoundException ee) { throw new ReflectionException(ee, "The MBean class could not be loaded by the default loader repository"); } return theClass; } /** * Gets the class for the specified class name using the MBean * Interceptor's classloader */ public Class findClass(String className, ClassLoader loader) throws ReflectionException { return loadClass(className,loader); } /** * Gets the class for the specified class name using the specified * class loader */ public Class findClass(String className, ObjectName aLoader) throws ReflectionException, InstanceNotFoundException { Class theClass = null; if (aLoader == null) throw new RuntimeOperationsException(new IllegalArgumentException(), "Null loader passed in parameter"); // Retrieve the class loader from the repository ClassLoader loader = null; synchronized(this) { if (clr!=null) loader = clr.getClassLoader(aLoader); } if (loader == null) { throw new InstanceNotFoundException("The loader named " + aLoader + " is not registered in the MBeanServer"); } return findClass(className,loader); } /** * Return an array of Class corresponding to the given signature, using * the specified class loader. */ public Class[] findSignatureClasses(String signature[], ClassLoader loader) throws ReflectionException { if (signature == null) return null; final ClassLoader aLoader = (ClassLoader) 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 // if (aLoader != null) { // We need to load the class through the class // loader of the target object. // tab[i] = Class.forName(signature[i], false, aLoader); } else { // Load through the default class loader // tab[i] = findClass(signature[i], this.getClass().getClassLoader()); } } } 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; } /** * Instantiates an object given its class, using its empty constructor. * The call returns a reference to the newly created object. */ public Object instantiate(Class theClass) throws ReflectionException, MBeanException { Object moi = null; // ------------------------------ // ------------------------------ Constructor cons = findConstructor(theClass, null); if (cons == null) { throw new ReflectionException(new NoSuchMethodException("No such constructor")); } // Instantiate the new object try { ReflectUtil.checkPackageAccess(theClass); moi= cons.newInstance(); } catch (InvocationTargetException e) { // Wrap the exception. Throwable t = e.getTargetException(); if (t instanceof RuntimeException) { throw new RuntimeMBeanException((RuntimeException)t, "RuntimeException thrown in the MBean's empty constructor"); } else if (t instanceof Error) { throw new RuntimeErrorException((Error) t, "Error thrown in the MBean's empty constructor"); } else { throw new MBeanException((Exception) t, "Exception thrown in the MBean's empty constructor"); } } catch (NoSuchMethodError error) { throw new ReflectionException(new NoSuchMethodException("No constructor"), "No such constructor"); } catch (InstantiationException e) { throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor"); } catch (IllegalAccessException e) { throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor"); } catch (IllegalArgumentException e) { throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor"); } return moi; } /** * Instantiates an object given its class, the parameters and * signature of its constructor The call returns a reference to * the newly created object. */ public Object instantiate(Class theClass, Object params[], String signature[], ClassLoader loader) throws ReflectionException, MBeanException { // Instantiate the new object // ------------------------------ // ------------------------------ final Class[] tab; Object moi= null; try { // Build the signature of the method // ClassLoader aLoader= (ClassLoader) theClass.getClassLoader(); // Build the signature of the method // tab = ((signature == null)?null: findSignatureClasses(signature,aLoader)); } // Exception IllegalArgumentException raised in Jdk1.1.8 catch (IllegalArgumentException e) { throw new ReflectionException(e, "The constructor parameter classes could not be loaded"); } // Query the metadata service to get the right constructor Constructor cons = null; cons = findConstructor(theClass, tab); if (cons == null) { throw new ReflectionException(new NoSuchMethodException("No such constructor")); } try { ReflectUtil.checkPackageAccess(theClass); moi = cons.newInstance(params); } catch (NoSuchMethodError error) { throw new ReflectionException(new NoSuchMethodException("No such constructor found"), "No such constructor" ); } catch (InstantiationException e) { throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's constructor"); } catch (IllegalAccessException e) { throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's constructor"); } catch (InvocationTargetException e) { // Wrap the exception. Throwable th = e.getTargetException(); if (th instanceof RuntimeException) { throw new RuntimeMBeanException((RuntimeException)th, "RuntimeException thrown in the MBean's constructor"); } else if (th instanceof Error) { throw new RuntimeErrorException((Error) th, "Error thrown in the MBean's constructor"); } else { throw new MBeanException((Exception) th, "Exception thrown in the MBean's constructor"); } } return moi; } /** * De-serializes a byte array in the context of a classloader. * * @param loader the classloader to use for de-serialization * @param data The byte array to be de-sererialized. * * @return The de-serialized object stream. * * @exception OperationsException Any of the usual Input/Output related * exceptions. */ public ObjectInputStream deserialize(ClassLoader loader, byte[] data) throws OperationsException { // 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"); } // Object deserialization ByteArrayInputStream bIn; ObjectInputStream objIn; String typeStr; bIn = new ByteArrayInputStream(data); try { objIn = new ObjectInputStreamWithLoader(bIn,loader); } catch (IOException e) { throw new OperationsException( "An IOException occurred trying to de-serialize the data"); } return objIn; } /** * De-serializes a byte array in the context of a given MBean class loader. * <P>The class loader is the one that loaded the class with name * "className". * <P>The name of the class loader to be used for loading the specified * class is specified. If null, a default one has to be provided (for a * MBean Server, its own class loader will be used). * * @param className The name of the class whose class loader should * be used for the de-serialization. * @param data The byte array to be de-sererialized. * @param loaderName The name of the class loader to be used for loading * the specified class. If null, a default one has to be provided (for a * MBean Server, its own class loader will be used). * * @return The de-serialized object stream. * * @exception InstanceNotFoundException The specified class loader MBean is * not found. * @exception OperationsException Any of the usual Input/Output related * exceptions. * @exception ReflectionException The specified class could not be loaded * by the specified class loader. */ public ObjectInputStream deserialize(String className, ObjectName loaderName, byte[] data, ClassLoader loader)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -