Source Code Cross Referenced for Framework.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.io.IOException;
011:        import java.net.URL;
012:        import java.util.ArrayList;
013:        import java.util.Collections;
014:        import java.util.HashMap;
015:        import java.util.List;
016:        import java.util.Map;
017:
018:        import org.jfox.framework.component.Module;
019:        import org.jfox.framework.component.ModuleResolvedFailedException;
020:        import org.jfox.framework.component.SystemModule;
021:        import org.jfox.framework.event.FrameworkStartedEvent;
022:        import org.jfox.framework.event.FrameworkStoppedEvent;
023:        import org.jfox.util.FileUtils;
024:        import org.jfox.util.PlaceholderUtils;
025:        import org.apache.log4j.Logger;
026:
027:        /**
028:         * JFoxNG framework.
029:         *
030:         * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
031:         */
032:        public class Framework {
033:
034:            private static final Logger logger = Logger
035:                    .getLogger(Framework.class);
036:
037:            /**
038:             * ClassLoader Repository,作为Thread ContextClassLoader
039:             * 缓存已加载的 export class
040:             */
041:            private ClassLoaderRepository clRepo = new ClassLoaderRepository(
042:                    new URL[0], Framework.class.getClassLoader());
043:
044:            /**
045:             * 事件监�器
046:             */
047:            private EventManager listenerManager = new EventManager();
048:
049:            /**
050:             * 系统 Module,用�加载 Framework 内注册的 Component
051:             */
052:            private SystemModule systemModule;
053:
054:            /**
055:             * Module name => Module object
056:             */
057:            private Map<String, Module> modules = new HashMap<String, Module>();
058:
059:            private boolean started = false;
060:
061:            public static final String MODULE_ARCHIVE_SUFFIX = ".zip";
062:
063:            public Framework() {
064:                Thread.currentThread().setContextClassLoader(clRepo);
065:                logger
066:                        .debug("Set thread context classloader to Framework ClassLoaderRepository.");
067:
068:                try {
069:                    //load global properties
070:                    PlaceholderUtils
071:                            .loadGlobalProperty(Constants.GLOBAL_PROPERTIES);
072:                    logger.info("Loaded global placeholder config file: "
073:                            + Constants.GLOBAL_PROPERTIES);
074:                } catch (IOException e) {
075:                    logger.warn(
076:                            "Failed to load global placeholder properties: "
077:                                    + Constants.GLOBAL_PROPERTIES, e);
078:                }
079:
080:                initSystemModule();
081:            }
082:
083:            private void initSystemModule() {
084:                try {
085:                    systemModule = new SystemModule(this );
086:                    logger.info("System Module created.");
087:                } catch (ModuleResolvedFailedException e) {
088:                    logger.fatal("Failed to create SystemModule!", e);
089:                    System.exit(1);
090:                }
091:            }
092:
093:            public boolean isStarted() {
094:                return started;
095:            }
096:
097:            public ClassLoaderRepository getClassLoaderRepository() {
098:                return clRepo;
099:            }
100:
101:            public EventManager getEventManager() {
102:                return listenerManager;
103:            }
104:
105:            /**
106:             * 获得内置的系统模�
107:             */
108:            public Module getSystemModule() {
109:                return systemModule;
110:            }
111:
112:            public Module getModule(String name) {
113:                if (SystemModule.name.equals(name)) {
114:                    return getSystemModule();
115:                } else {
116:                    return modules.get(name);
117:                }
118:            }
119:
120:            /**
121:             * 装载一个Module
122:             *
123:             *
124:             * @param dir Moudle所在的目录
125:             * @return 返回生�的 Module 实例
126:             */
127:            public Module loadModule(File dir) {
128:                if (!dir.exists()) {
129:                    logger.error("Load module failed, not exists module file: "
130:                            + dir);
131:                    return null;
132:                }
133:                logger.info("Starting to load module from "
134:                        + dir.getAbsolutePath());
135:                try {
136:                    //.zip 是 module 的压缩文件�缀
137:                    if (dir.isFile()
138:                            && dir.getName().endsWith(MODULE_ARCHIVE_SUFFIX)) {
139:                        dir = dir.getAbsoluteFile(); // 因为 getParentFile 是通过计算 path �推断的
140:                        File toDir = new File(dir.getParentFile(), dir
141:                                .getName().substring(
142:                                        0,
143:                                        dir.getName().length()
144:                                                - MODULE_ARCHIVE_SUFFIX
145:                                                        .length()));
146:                        FileUtils.extractJar(dir, toDir);
147:                        dir = toDir;
148:                    }
149:
150:                    Module module = new Module(this , dir);
151:                    modules.put(module.getName(), module);
152:                    logger.info("Module: " + module.getName()
153:                            + " loaded, from " + dir.getAbsolutePath());
154:
155:                    if (isStarted()) {// 如果 Framework 已��动,则�续装载的 Module 立��动
156:                        module.start();
157:                    }
158:                    return module;
159:                } catch (Exception e) {
160:                    logger.error("Load Module from " + dir.getAbsolutePath()
161:                            + " failed.", e);
162:                    return null;
163:                }
164:            }
165:
166:            /**
167:             * �载一个模�
168:             *
169:             * @param name 模å?—å??
170:             */
171:            public void unloadModule(String name) {
172:                Module module = modules.remove(name);
173:                if (module != null) {
174:                    module.unload();
175:                }
176:            }
177:
178:            /**
179:             * unload, then re-load
180:             *
181:             * @param name module name
182:             */
183:            public Module reloadModule(String name) {
184:                logger.info("Reload module: " + name);
185:                Module module = modules.get(name);
186:                if (module != null) {
187:                    File dir = module.getModuleDir();
188:                    unloadModule(name);
189:                    return loadModule(dir);
190:                }
191:                return null;
192:            }
193:
194:            /**
195:             * �动所有 Module
196:             *
197:             * @throws Exception any exception
198:             */
199:            public synchronized void start() throws Exception {
200:                if (started) {
201:                    logger
202:                            .warn("Framework has been started, if you want restart, please stop first!");
203:                    return;
204:                }
205:                try {
206:                    systemModule.start();
207:                } catch (Exception e) {
208:                    logger.fatal("Failed to start SystemModule!", e);
209:                    System.exit(1);
210:                }
211:
212:                List<Module> allModules = new ArrayList<Module>(modules
213:                        .values());
214:                Collections.sort(allModules);
215:                for (Module module : allModules) {
216:                    module.start();
217:                }
218:                started = true;
219:                getEventManager().fireFrameworkEvent(
220:                        new FrameworkStartedEvent(this ));
221:                logger.info("Framework started!");
222:            }
223:
224:            public void stop() {
225:                List<Module> allModules = new ArrayList<Module>(getAllModules());
226:                Collections.reverse(allModules);
227:                for (Module module : allModules) {
228:                    module.unload();
229:                }
230:                systemModule.unload();
231:                started = false;
232:                getEventManager().fireFrameworkEvent(
233:                        new FrameworkStoppedEvent(this ));
234:                logger.info("Framework stopped!");
235:            }
236:
237:            public List<Module> getAllModules() {
238:                List<Module> allModules = new ArrayList<Module>(modules
239:                        .values());
240:                Collections.sort(allModules);
241:                Collections.reverse(allModules);
242:                return Collections.unmodifiableList(allModules);
243:            }
244:
245:            public static void main(String[] args) throws Exception {
246:                Framework framework = new Framework();
247:                framework.start();
248:                Thread.sleep(5000);
249:                framework.stop();
250:            }
251:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.