Source Code Cross Referenced for ClassLoaderRepository.java in  » J2EE » jfox » org » jfox » framework » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » jfox » org.jfox.framework 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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;
008:
009:        import java.io.File;
010:        import java.net.URL;
011:        import java.net.URLClassLoader;
012:        import java.net.MalformedURLException;
013:        import java.util.HashMap;
014:        import java.util.List;
015:        import java.util.Map;
016:
017:        import org.apache.log4j.Logger;
018:        import org.jfox.util.FileFilterUtils;
019:        import org.jfox.util.FileUtils;
020:
021:        /**
022:         * Class Loader Repository
023:         * <p/>
024:         * 负责管�所有Module Export 的 Class
025:         *
026:         * @author <a href="mailto:jfox.young@gmail.com">Yang Yong</a>
027:         */
028:        public class ClassLoaderRepository extends URLClassLoader {
029:
030:            private Logger logger = Logger
031:                    .getLogger(ClassLoaderRepository.class);
032:
033:            /**
034:             * classname => ClassEntry
035:             */
036:            private Map<String, ClassEntry> exportClassRepo = new HashMap<String, ClassEntry>();
037:
038:            public ClassLoaderRepository(URL[] urls) {
039:                super (urls, ClassLoaderRepository.class.getClassLoader());
040:                loadExternalLibs();
041:            }
042:
043:            public ClassLoaderRepository(URL[] urls, ClassLoader parent) {
044:                super (urls, parent);
045:                loadExternalLibs();
046:            }
047:
048:            /**
049:             * 装载 common 目录下的 jar
050:             */
051:            private void loadExternalLibs() {
052:                File libDir = new File(Constants.getCommonLibPath());
053:                if (libDir.exists() && libDir.isDirectory()) {
054:
055:                    List<File> jarFiles = FileUtils.listFiles(libDir,
056:                            FileFilterUtils.suffixFileFilter(new String[] {
057:                                    "jar", "zip" }));
058:                    for (File file : jarFiles) {
059:                        try {
060:                            addURL(file.toURI().toURL());
061:                        } catch (MalformedURLException e) {
062:                            logger.warn(e);
063:                        }
064:                    }
065:                }
066:            }
067:
068:            /**
069:             * 将 exported class 交给 ClassLoaderRepository 缓存
070:             *
071:             * @param module module name
072:             * @param clz    clz
073:             */
074:            public synchronized void addExportedClass(String module, Class clz) {
075:                logger
076:                        .debug("Exportping Class: " + clz + ", Module: "
077:                                + module);
078:                if (exportClassRepo.containsKey(clz.getName())) {
079:                    logger.warn("Class: " + clz.getName()
080:                            + " to be exported has been exist, overwrite!");
081:                }
082:                ClassEntry classEntry = new ClassEntry(module, clz);
083:                exportClassRepo.put(clz.getName(), classEntry);
084:            }
085:
086:            /**
087:             * 装载 export 类
088:             * 在 ModuleClassLoader loadImportClass 时调用
089:             *
090:             * @param className �装载的 class
091:             * @return Class
092:             * @throws ClassNotFoundException if class not found
093:             */
094:            Class loadExportClass(String className)
095:                    throws ClassNotFoundException {
096:                if (className == null || className.trim().length() == 0) {
097:                    throw new IllegalArgumentException("Class name: "
098:                            + className + " is invalid.");
099:                }
100:                if (exportClassRepo.containsKey(className)) {
101:                    ClassEntry classEntry = exportClassRepo.get(className);
102:                    //�有被外部模�饮用�会增加引用次数,如果是被本模�load,ModuleClassLoader会调用decreaseReference
103:                    //如果是本模�的类,将在 findLoadedClass 中返回,�会�过 ClassLoaderRepository
104:                    classEntry.setReferenced();
105:                    return classEntry.getClazz();
106:                }
107:                throw new ClassNotFoundException("Can not found class: "
108:                        + className + ", it was not exported.");
109:            }
110:
111:            boolean isExportClassLoaded(String className) {
112:                return exportClassRepo.containsKey(className);
113:            }
114:
115:            /**
116:             * 判断一个 Export Class 是�已�被引用,如果已�被引用,将�能销�
117:             * 如果一定�销�,那么也需�引用它的类
118:             *
119:             * @param clz Class
120:             * @return true if referenced
121:             */
122:            boolean isExportedClassReferenced(Class clz) {
123:                if (exportClassRepo.containsKey(clz.getName())) {
124:                    return exportClassRepo.get(clz.getName()).isReferenced();
125:                } else {
126:                    return false;
127:                }
128:            }
129:
130:            /**
131:             * 添加 URL 到 Classpath �索路径
132:             *
133:             * @param url jar url
134:             */
135:            protected void addURL(URL url) {
136:                logger.debug("Add URL " + url.toString()
137:                        + " to ClassLoaderRepository.");
138:                super .addURL(url);
139:            }
140:
141:            /**
142:             * 因为 ClassLoaderRepository 是 LocalClassLoader 的 parent ClassLoader
143:             * LocalClassLoader 会直接调这个方法,所以必须�覆盖
144:             * 首先从 ClassLoaderRepository 的classpath路径中装载类,如果失败, 装载缓存的 LocalClassLoader
145:             * 已�装载的类
146:             *
147:             * @param name    class name
148:             * @param resolve need resole
149:             * @return class
150:             * @throws ClassNotFoundException
151:             */
152:            protected synchronized Class<?> loadClass(String name,
153:                    boolean resolve) throws ClassNotFoundException {
154:                try {
155:                    return super .loadClass(name, resolve);
156:                } catch (ClassNotFoundException e) {
157:                    try {
158:                        Class clz = loadExportClass(name);
159:                        if (resolve) {
160:                            resolveClass(clz);
161:                        }
162:                        return clz;
163:                    } catch (ClassNotFoundException e1) {
164:                        //                e.printStackTrace();
165:                    }
166:                }
167:                throw new ClassNotFoundException(name);
168:            }
169:
170:            /**
171:             * 一个Module export 的类,这些类一旦装载,热部署的时候,也�会�新装载
172:             * 这�类一旦装载,就被缓存在Repository中,以�load的时候,将首先检查缓存
173:             * <p/>
174:             * export的类一般是一些接�或者工具类。
175:             * 如果export的类�生的改�,那么就需��新部署已�引用了export类的模�,
176:             * �则引用模�得�到新的Class,会造� ClassCastException 错误
177:             */
178:            class ClassEntry {
179:                private String module;
180:                private Class clz;
181:                private boolean referenced = false;
182:
183:                public ClassEntry(String module, Class clz) {
184:                    this .module = module;
185:                    this .clz = clz;
186:                }
187:
188:                public Class getClazz() {
189:                    return clz;
190:                }
191:
192:                public String getModule() {
193:                    return module;
194:                }
195:
196:                public boolean isReferenced() {
197:                    return referenced;
198:                }
199:
200:                public void setReferenced() {
201:                    this .referenced = true;
202:                }
203:            }
204:
205:            public static void main(String[] args) {
206:
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.