Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71358
  • 博文数量: 14
  • 博客积分: 570
  • 博客等级: 中士
  • 技术积分: 153
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 17:53
文章分类

全部博文(14)

文章存档

2009年(6)

2008年(8)

我的朋友

分类: Java

2008-11-16 21:56:23

我目前的系统可能需要自己实现类加载器,想要参考Tomcat的实现。关于Tomcat的类加载机制,网上文章很多,当然大多都是互相copy,有价值的信息并不多,不得已我开始看Tomcat代码,略有所得,记录起来。主要针对WebappClassLoader。
 
负责Web应用的类加载的是org.apache.catalina.loader.WebappClassLoader,它几个比较重要的方法:findClass(),loadClass(),findClassInternal(),findResourceInternal().类加载器被用来加载一个类的时候,loadClass()会被调用,loadClass()则调用findClass()。后两个方法是WebappClassLoader的私有方法,findClass()调用findClassInternal()来创建class对象,而findClassInternal()则需要findResourceInternal()来查找.class文件。
 
通常自己实现类记载器只要实现findclass即可,这里为了实现特殊目的而override了loadClass().
 
下面是精简过的代码(去除了几乎全部关于log、异常和安全控制的代码):
 
findClass:
 

public Class findClass(String name) throws ClassNotFoundException {
        // 先试图自己加载类,找不到则请求parent来加载
        // 注意这点和java默认的双亲委托模式不同
        Class clazz = null;
        clazz = findClassInternal(name);
        if ((clazz == null) && hasExternalRepositories) {
            synchronized (this) {
                clazz = super.findClass(name);
            }
        }
        if (clazz == null) {
            throw new ClassNotFoundException(name);
        }

        return (clazz);
    }

loadClass:

    public Class loadClass(String name, boolean resolve)
            throws ClassNotFoundException {
        Class clazz = null;
        // (0) 先从自己的缓存中查找,有则返回,无则继续
        clazz = findLoadedClass0(name);
        if (clazz != null) {
            if (resolve) resolveClass(clazz);            
            return (clazz);
        }
        // (0.1) 再从parent的缓存中查找
        clazz = findLoadedClass(name);
        if (clazz != null) {
            if (resolve) resolveClass(clazz);
            return (clazz);
        }
        // (0.2) 缓存中没有,则首先使用system类加载器来加载
        clazz = system.loadClass(name);
         if (clazz != null) {
             if (resolve) resolveClass(clazz);
             return (clazz);
         }

        //判断是否需要先让parent代理
        boolean delegateLoad = delegate || filter(name);
        // (1) 先让parent加载,通常delegateLoad == false,即这一步不会执行

        if (delegateLoad) {
            ClassLoader loader = parent;
            if (loader == null)
                loader = system;
            clazz = loader.loadClass(name);
            if (clazz != null) {
                if (resolve) resolveClass(clazz);
                return (clazz);
            }
        }
        // (2) delegateLoad == false 或者 parent加载失败,调用自身的加载机制
        clazz = findClass(name);
        if (clazz != null) {
            if (resolve) resolveClass(clazz);
            return (clazz);
        }
        // (3) 自己加载失败,则请求parent代理加载

        if (!delegateLoad) {
            ClassLoader loader = parent;
            if (loader == null)
                loader = system;
            clazz = loader.loadClass(name);
            if (clazz != null) {
                return (clazz);
            }
        }
        throw new ClassNotFoundException(name);
    }

 
findClassInternal:
 

protected Class findClassInternal(String name)
        throws ClassNotFoundException {
        if (!validate(name))
            throw new ClassNotFoundException(name);
        //根据类名查找资源
        String tempPath = name.replace('.', '/');
        String classPath = tempPath + ".class";
        ResourceEntry entry = null;
        entry = findResourceInternal(name, classPath);

        if (entry == null)
            throw new ClassNotFoundException(name);
        //如果以前已经加载成功过这个类,直接返回

        Class clazz = entry.loadedClass;
        if (clazz != null)
            return clazz;
        //以下根据找到的资源(.class文件)进行:1、定义package;2、对package安全检查;3、定义class,即创建class对象
        synchronized (this) {
            if (entry.binaryContent == null && entry.loadedClass == null)
                throw new ClassNotFoundException(name);
            // Looking up the package
            String packageName = null;
            int pos = name.lastIndexOf('.');
            if (pos != -1)
                packageName = name.substring(0, pos);
            Package pkg = null;
            if (packageName != null) {
                pkg = getPackage(packageName);
                // Define the package (if null)
                if (pkg == null) {
                    //定义package的操作,此处省略,具体参看源码 
                    pkg = getPackage(packageName);
                }
            }
            if (securityManager != null) {
                //安全检查操作,此处省略,具体参看源码 
            }
            //创建class对象并返回
            if (entry.loadedClass == null) {
                try {
                    clazz = defineClass(name, entry.binaryContent, 0,
                            entry.binaryContent.length,
                            new CodeSource(entry.codeBase, entry.certificates));
                } catch (UnsupportedClassVersionError ucve) {
                    throw new UnsupportedClassVersionError(
                            ucve.getLocalizedMessage() + " " +
                            sm.getString("webappClassLoader.wrongVersion",
                                    name));
                }
                entry.loadedClass = clazz;
                entry.binaryContent = null;
                entry.source = null;
                entry.codeBase = null;
                entry.manifest = null;
                entry.certificates = null;
            } else {
                clazz = entry.loadedClass;
            }
        }
        return clazz;
    }

findResouceInternal():

 

下几篇介绍WebappLoader,StandardContext,StandardWrapper,ApplicationDispatcher在类加载中的作用。其中ApplicationDispatcher是核心。

    //要先加载相关实体资源(.jar) 再加载查找的资源本身
    protected ResourceEntry findResourceInternal(String name, String path) {
        //先根据类名从缓存中查找对应资源 ,有则直接返回 
        ResourceEntry entry = (ResourceEntry) resourceEntries.get(name);
        if (entry != null)
            return entry;
        int contentLength = -1;//资源二进制数据长度
        InputStream binaryStream = null;//资源二进制输入流

        int jarFilesLength = jarFiles.length;//classpath中的jar包个数
        int repositoriesLength = repositories.length;//仓库数(classpath每一段称为repository仓库)
        int i;
        Resource resource = null;//加载的资源实体
        boolean fileNeedConvert = false;
        //对每个仓库迭代,直接找到相应的entry,如果查找的资源是一个独立的文件,在这个代码块可以查找到相应资源,
        //如果是包含在jar包中的类,这段代码并不能找出其对应的资源
        for (i = 0; (entry == null) && (i < repositoriesLength); i++) {
            try {
                String fullPath = repositories[i] + path;//仓库路径 加资源路径得到全路径
                Object lookupResult = resources.lookup(fullPath);//从资源库中查找资源

                if (lookupResult instanceof Resource) {
                    resource = (Resource) lookupResult;
                }
                //到这里没有抛出异常,说明资源已经找到,现在构造entry对象
                 if (securityManager != null) {
                    PrivilegedAction dp =
                        new PrivilegedFindResource(files[i], path);
                    entry = (ResourceEntry)AccessController.doPrivileged(dp);
                 } else {
                    entry = findResourceInternal(files[i], path);//这个方式只是构造entry并给其codebase和source赋值
                 }
                 //获取资源长度和最后修改时间
                ResourceAttributes attributes =
                    (ResourceAttributes) resources.getAttributes(fullPath);
                contentLength = (int) attributes.getContentLength();
                entry.lastModified = attributes.getLastModified();
                //资源找到,将二进制输入流赋给binaryStream
                if (resource != null) {
                    try {
                        binaryStream = resource.streamContent();
                    } catch (IOException e) {
                        return null;
                    }
                    //将资源的最后修改时间加到列表中去,代码略去,参加源码 

                }

            } catch (NamingException e) {
            }
        }
        if ((entry == null) && (notFoundResources.containsKey(name)))
            return null;
        //开始从jar包中查找
        JarEntry jarEntry = null;
        synchronized (jarFiles) {
            if (!openJARs()) {
                return null;
            }
            for (i = 0; (entry == null) && (i < jarFilesLength); i++) {
                jarEntry = jarFiles[i].getJarEntry(path);//根据路径从jar包中查找资源
                //如果jar包中找到资源,则定义entry并将二进制流等数据赋给entry,同时将jar包解压到workdir.
                if (jarEntry != null) {
                    entry = new ResourceEntry();
                    try {
                        entry.codeBase = getURL(jarRealFiles[i], false);
                        String jarFakeUrl = getURI(jarRealFiles[i]).toString();
                        jarFakeUrl = "jar:" + jarFakeUrl + "!/" + path;
                        entry.source = new URL(jarFakeUrl);
                        entry.lastModified = jarRealFiles[i].lastModified();
                    } catch (MalformedURLException e) {
                        return null;
                    }
                    contentLength = (int) jarEntry.getSize();
                    try {
                        entry.manifest = jarFiles[i].getManifest();
                        binaryStream = jarFiles[i].getInputStream(jarEntry);
                    } catch (IOException e) {
                        return null;
                    }
                    if (antiJARLocking && !(path.endsWith(".class"))) {
                        //解压jar包代码,参见源码
                    }
                }
            }
            if (entry == null) {
                synchronized (notFoundResources) {
                    notFoundResources.put(name, name);
                }
                return null;
            }
            //从二进制流将资源内容读出
            if (binaryStream != null) {
                byte[] binaryContent = new byte[contentLength];
                //读二进制流的代码,这里省去,参见源码
                entry.binaryContent = binaryContent;
            }
        }
        // 将资源加到缓存中
        synchronized (resourceEntries) {
            ResourceEntry entry2 = (ResourceEntry) resourceEntries.get(name);
            if (entry2 == null) {
                resourceEntries.put(name, entry);
            } else {
                entry = entry2;
            }
        }
        return entry;
    }

阅读(12355) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~