一、前言
关于jasper的jasper-compiler源码中基于java.net.URLClassLoader自定义org.apache.jasper.servlet.JasperLoader网络类加载器,具体实现部分详情源码说明。
二、源码说明
package org.apache.jasper.servlet;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.net.URL;@b@import java.net.URLClassLoader;@b@import java.security.CodeSource;@b@import java.security.PermissionCollection;@b@@b@public class JasperLoader extends URLClassLoader@b@{@b@ private PermissionCollection permissionCollection;@b@ private CodeSource codeSource;@b@ private String className;@b@ private ClassLoader parent;@b@ private SecurityManager securityManager;@b@@b@ public JasperLoader(URL[] urls, ClassLoader parent, PermissionCollection permissionCollection, CodeSource codeSource)@b@ {@b@ super(urls, parent);@b@ this.permissionCollection = permissionCollection;@b@ this.codeSource = codeSource;@b@ this.parent = parent;@b@ this.securityManager = System.getSecurityManager();@b@ }@b@@b@ public Class loadClass(String name)@b@ throws ClassNotFoundException@b@ {@b@ return loadClass(name, false);@b@ }@b@@b@ public Class loadClass(String name, boolean resolve)@b@ throws ClassNotFoundException@b@ {@b@ Class clazz = null;@b@@b@ clazz = findLoadedClass(name);@b@ if (clazz != null) {@b@ if (resolve)@b@ resolveClass(clazz);@b@ return clazz;@b@ }@b@@b@ if (this.securityManager != null) {@b@ int dot = name.lastIndexOf(46);@b@ if (dot >= 0)@b@ try@b@ {@b@ if (!("org.apache.jasper.runtime".equalsIgnoreCase(name.substring(0, dot))))@b@ this.securityManager.checkPackageAccess(name.substring(0, dot));@b@ }@b@ catch (SecurityException se) {@b@ String error = "Security Violation, attempt to use Restricted Class: " + name;@b@@b@ se.printStackTrace();@b@ throw new ClassNotFoundException(error);@b@ }@b@@b@ }@b@@b@ if (!(name.startsWith("org.apache.jsp")))@b@ {@b@ clazz = this.parent.loadClass(name);@b@ if (resolve)@b@ resolveClass(clazz);@b@ return clazz;@b@ }@b@@b@ return findClass(name);@b@ }@b@@b@ public InputStream getResourceAsStream(String name)@b@ {@b@ InputStream is = this.parent.getResourceAsStream(name);@b@ if (is == null) {@b@ URL url = findResource(name);@b@ if (url != null)@b@ try {@b@ is = url.openStream();@b@ } catch (IOException e) {@b@ is = null;@b@ }@b@ }@b@@b@ return is;@b@ }@b@@b@ public final PermissionCollection getPermissions(CodeSource codeSource)@b@ {@b@ return this.permissionCollection;@b@ }@b@}