FileDocCategorySizeDatePackage
MBeanClassLoader.javaAPI DocGlassfish v2 API8870Fri May 04 22:24:10 BST 2007com.sun.enterprise.admin.mbeans.custom.loading

MBeanClassLoader

public final class MBeanClassLoader extends ClassLoader
A custom class loader to load the MBeans defined by users. This class-loader delegates to system-class-loader, sun.misc.Launcher$AppClassLoader@169e11 for Sun's JRE. One instance of this class-loader is created at the startup and it loads the classes from a specific location that is passed to it. If the bits of the class that has been loaded by this instance of the class are now changed on the disk, a new instance of the class loader is first created and that instance of this class-loader loads the new bits giving birth to a new class. More about this in the design document.
since
SJSAS 9.0

Fields Summary
private URL
url
private ClassLoader
parent
Constructors Summary
public MBeanClassLoader()
Creates a new instance of MBeanClassLoader

        super(MBeanClassLoader.class.getClassLoader());
        init();
    
public MBeanClassLoader(ClassLoader delegatee)

        super(delegatee);
        init();
    
Methods Summary
protected java.lang.ClassfindClass(java.lang.String className)

        final byte[] cd = getClassData(className);
        if (cd == null)
            throw new ClassNotFoundException(CMBStrings.get("findClassFailed", className));
        return ( defineClass(className, cd, 0, cd.length) );
    
protected java.net.URLfindResource(java.lang.String name)

        File searchResource = new File(url.getFile(), name);
        URL result = null;
        
        if ( searchResource.exists() )
        {
            try
            {
                return searchResource.toURL();
            }
            catch (MalformedURLException mfe)
            {
            }
        }
        return result;
  
private byte[]getClassData(java.lang.String cn)

        byte[] cd = null;
        try {
            if(isURLFile()) {
                cd = getClassDataFromFile(url.getFile(), cn);
            } else {
                cd = getClassDataFromURL();
            }
        } catch(final Exception e) {
            // the caller will throw a ClassNotFoundException
        }
        return ( cd );
    
private byte[]getClassDataFromFile(java.lang.String base, java.lang.String cn)

        String path                         = base + '/" + cn.replace('.", '/") + ".class";
        path                                = prunePath(path);
        final FileInputStream fis           = new FileInputStream(path);
        final BufferedInputStream bis       = new BufferedInputStream(fis);
        final ByteArrayOutputStream bos     = new ByteArrayOutputStream();
        byte[] cd = null;
        try {
            byte[] buf = new byte[1024]; // reading several bytes
            int br = bis.read(buf, 0, buf.length);
            while (br != -1) {
                bos.write(buf, 0, br);
                br = bis.read(buf, 0, buf.length);
            }
            cd = bos.toByteArray();
        } catch(final FileNotFoundException fe) {
            throw fe; 
        } catch (final IOException e) {
            throw e;
        } finally {
            try {
                //This is the single most important item in loading the classes!
                bis.close();
                bos.close();
            } catch(Throwable t) {}
            return ( cd );
        }
    
private byte[]getClassDataFromURL()

        throw new UnsupportedOperationException(CMBStrings.get("InternalError", "getClassDataFromURL() -- not implemented yet"));
    
private java.io.FilegetDefaultMBeanDirectory()

        /* Ideally, PEFileLayout should be utilized here which should be the central place
         * to get all these paths, locations. But that is too cryptic to find out. Hence
         * I am relying on the properties that are passed to the VM at startup.
         * These properties include the SystemPropertyConstants.INSTANCE_ROOT which always
         * points to where the server instance or domain configuration is stored.
         */
        final File appsFolder = new File(System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), PEFileLayout.APPLICATIONS_DIR);
        return ( new File(appsFolder, PEFileLayout.MBEAN_FOLDER_NAME) );
    
private voidinit()

        try {
            final File mbf      = getDefaultMBeanDirectory();
            this.url = mbf.toURL();
        } catch (final MalformedURLException m) {
            String msg = CMBStrings.get("cmb.classloader.init", m.getLocalizedMessage());
            throw new CustomMBeanException(msg, m);
        }        
    
private booleanisURLFile()

        return ( "file".equals(url.getProtocol()) );
    
private java.lang.StringprunePath(java.lang.String path)

        /* don't know why url.toFile has got a leading '/' on Windows */
        if (OS.isWindows() && path.charAt(0) == '/") {
            return ( path.substring(1) );
        }
        else
            return ( path ) ;