标签:tomcat 源码 缓存 delegate security
public Servlet loadServlet() throws ServletException { ... String actualClass = servletClass; Loader loader = getLoader(); // Acquire an instance of the class loader to be used if (loader==null) { throw new ServletException("No loader."); } ClassLoader classLoader = loader.getClassLoader(); if (classLoader!=null) { classClass = classLoader.loadClass(actualClass); // Instantiate and initialize an instance of the servlet class itself servlet = (Servlet) classClass.newInstance(); // Call the initialization method of this servlet servlet.init(null); return servlet; }
· 如果该类仍然找不到,抛出 ClassNotFoundException 异常
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { if (debug >= 2) log("loadClass(" + name + ", " + resolve + ")"); Class<?> clazz = null; // Don't load classes if class loader is stopped if (!started) { //若webappclassloader没有启动.... log("Lifecycle error : CL stopped"); throw new ClassNotFoundException(name); } // (0) Check our previously loaded local class cache clazz = findLoadedClass0(name); //看下面临时放进来的代码 等于是先在本地存储的已加载的类(hashmap)中找 if (clazz != null) { if (debug >= 3) log(" Returning class from cache"); if (resolve) resolveClass(clazz); return (clazz); } /* protected Class<?> findLoadedClass0(String name) { ResourceEntry entry = (ResourceEntry) resourceEntries.get(name); if (entry != null) { return entry.loadedClass; } return (null); } */ // (0.1) Check our previously loaded class cache clazz = findLoadedClass(name); //再去虚拟机中去找 if (clazz != null) { if (debug >= 3) log(" Returning class from cache"); if (resolve) resolveClass(clazz); return (clazz); } // (0.2) Try loading the class with the system class loader, to prevent // the webapp from overriding J2SE classes try { //system 为系统类加载器 sun.misc.Launcher$AppClassLoader //先用系统加载器加载 //为什么先用系统加载器 如果你写了一个类 全名为"java.lang.Object" 懂了吧? //大家可能会想 系统加载器不是什么都能加载吗? 那还要后面的代码做什么? //系统加载器的加载目录是什么 大家看看就明白了 clazz = system.loadClass(name); if (clazz != null) { if (resolve) resolveClass(clazz); return (clazz); } } catch (ClassNotFoundException e) { // Ignore } //关于安全的问题 我没有研究过里面具体的代码 // (0.5) Permission to access this class when using a SecurityManager if (securityManager != null) { int i = name.lastIndexOf('.'); if (i >= 0) { try { securityManager.checkPackageAccess(name.substring(0,i)); } catch (SecurityException se) { String error = "Security Violation, attempt to use " + "Restricted Class: " + name; System.out.println(error); se.printStackTrace(); log(error); throw new ClassNotFoundException(error); } } } //delegate默认为false //filter 如果要加载的类是packageTrigers里面所限定的 就返回true //书上说packageTrigers 里面的包是不允许载入的 //可代码里没有体现不能加载呀? 谁知道为什么,麻烦告知我一声 boolean delegateLoad = delegate || filter(name); System.out.println(delegate+" "+filter(name)+" ddd"); // (1) Delegate to our parent if requested if (delegateLoad) { if (debug >= 3) log(" Delegating to parent classloader"); ClassLoader loader = parent; System.out.println(loader+" loader"); if (loader == null) loader = system; try { clazz = loader.loadClass(name); if (clazz != null) { if (debug >= 3) log(" Loading class from parent"); if (resolve) resolveClass(clazz); return (clazz); } } catch (ClassNotFoundException e) { ; } } // (2) Search local repositories //至于repositories属性 是在webappLoader类start方法里 setRepositories()设定的 //使用的各个方法如下 //findClass findClassInternal findResourceInternal //在findResourceInternal中先加载WEB-INF/classes中的文件,然后加载WEB-INF/lib下的jar文件 //最后 resourceEntries.put(name, entry); //会把加载的enety放入resourceEntries中缓存起来 if (debug >= 3) log(" Searching local repositories"); try { clazz = findClass(name); if (clazz != null) { if (debug >= 3) log(" Loading class from local repository"); if (resolve) resolveClass(clazz); return (clazz); } } catch (ClassNotFoundException e) { ; } //系统加载器前面已经加载过来 如果程序能运行到这里就说明系统加载器不行 为什么还要再加载一遍 // (3) Delegate to parent unconditionally if (!delegateLoad) { if (debug >= 3) log(" Delegating to parent classloader"); ClassLoader loader = parent; if (loader == null) loader = system; try { clazz = loader.loadClass(name); if (clazz != null) { if (debug >= 3) log(" Loading class from parent"); if (resolve) resolveClass(clazz); return (clazz); } } catch (ClassNotFoundException e) { ; } } // This class was not found throw new ClassNotFoundException(name); }
Context context = new StandardContext(); // StandardContext's start method adds a default mapper context.setPath("/myApp"); context.setDocBase("myApp");
标签:tomcat 源码 缓存 delegate security
原文地址:http://blog.csdn.net/dlf123321/article/details/40620265