001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.framework.component;
008:
009: import java.net.URL;
010: import java.util.Arrays;
011:
012: import org.jfox.framework.ClassLoaderRepository;
013:
014: /**
015: * Module ClassLoader 用æ?¥åŠ 载整个 Module 的类和资æº?
016: * Module ClassLoader �始化的时候,会使用ASM读�所有的Class,并记录Class的Annotation,
017: * è¿™æ ·å?¯ä»¥ä¸?需è¦? XML é…?ç½®æ–‡ä»¶ï¼Œè€Œç›´æŽ¥æ ¹æ?® Annotation å?‘现 Component
018: *
019: * @author <a href="mailto:jfox.young@gmail.com">Yang Yong</a>
020: */
021: public class ModuleClassLoader extends ASMClassLoader {
022:
023: /**
024: * 该 ModuleClass 负责的模�
025: */
026: private Module module;
027:
028: /**
029: * global Class ClassLoaderRepository
030: */
031: private ClassLoaderRepository repo;
032:
033: /**
034: * the jars in the urls can be load by Repository
035: *
036: * @param module module
037: */
038: public ModuleClassLoader(Module module) {
039: super (module.getClasspathURLs(), module.getFramework()
040: .getClassLoaderRepository());
041: this .module = module;
042: this .repo = module.getFramework().getClassLoaderRepository();
043: initASM();
044: }
045:
046: protected URL[] getASMClasspathURLs() {
047: // 返回 Module 对 classpath URLs
048: return module.getClasspathURLs();
049: }
050:
051: public Module getModule() {
052: return module;
053: }
054:
055: public ClassLoaderRepository getClassLoaderRepository() {
056: return repo;
057: }
058:
059: /**
060: * 必须首先得找 export 列表,å?¦åˆ™ä¼šå‡ºçŽ°é‡?新装载,é€ æˆ? ClassCastException
061: *
062: * @param className class className
063: * @return class
064: * @throws ClassNotFoundException
065: */
066: public Class<?> loadClass(String className)
067: throws ClassNotFoundException {
068: // 如果是本模å?—的类,将在 findLoadedClass ä¸ç›´æŽ¥è¿”回,ä¸?需è¦?ç»?过 ClassLoaderRepository
069: Class clz = super .loadClass(className);
070: if (clz.getClassLoader() instanceof ModuleClassLoader) {
071: ModuleClassLoader mcl = (ModuleClassLoader) clz
072: .getClassLoader();
073: // å?ªæœ‰è‡ªå·±export 或者 ref-moudles ä¸ export çš„class,æ‰?能返回
074: if ((mcl == getModule().getModuleClassLoader())
075: || Arrays.asList(module.getRefModules()).contains(
076: mcl.getModule().getName())) {
077: return clz;
078: } else {
079: String errorMsg = "Class: " + className
080: + " 's export Module ["
081: + mcl.getModule().getName()
082: + "] is not in Module ["
083: + getModule().getName()
084: + "] 's ref-modules list.";
085: logger.info(errorMsg);
086: throw new ClassNotFoundException(errorMsg);
087: }
088: } else {
089: return clz;
090: }
091: }
092:
093: /**
094: * export class �从本地 Module load
095: *
096: * @param name className
097: * @return class
098: * @throws ClassNotFoundException if class not foud
099: */
100: Class<?> loadExportClass(String name) throws ClassNotFoundException {
101: logger.debug("Load export class: " + name + ", module: "
102: + getModule().getName());
103: // First, check if the class has already been loaded
104: Class c = findLoadedClass(name);
105: if (c == null) {
106: // If still not found, then invoke findClass in order
107: // to find the class.
108: c = findClass(name);
109: }
110: return c;
111: }
112:
113: protected Class<?> findClass(final String name)
114: throws ClassNotFoundException {
115: logger.trace("Loading class: " + name + ", Module: "
116: + getModule().getName());
117: return super .findClass(name);
118: }
119:
120: /**
121: * 仅从本Module findResource
122: *
123: * @param name resource name
124: */
125: public URL getResource(String name) {
126: return findResource(name);
127: }
128:
129: public String toString() {
130: return super.toString() + Arrays.toString(getURLs());
131: }
132:
133: }
|