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: }
|