YMP運行初始化步驟
阿新 • • 發佈:2017-09-06
handle ash 模塊初始化 gettime nta www debug any 重復
/*
* Copyright 2007-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.ymate.platform.core;
import net.ymate.platform.core.beans.BeanMeta;import net.ymate.platform.core.beans.IBeanFactory;
import net.ymate.platform.core.beans.IBeanHandler;
import net.ymate.platform.core.beans.annotation.Bean;
import net.ymate.platform.core.beans.annotation.Proxy;
import net.ymate.platform.core.beans.impl.DefaultBeanFactory;
import net.ymate.platform.core.beans.impl.DefaultBeanLoader;import net.ymate.platform.core.beans.impl.proxy.DefaultProxyFactory;
import net.ymate.platform.core.beans.intercept.InterceptProxy;
import net.ymate.platform.core.beans.proxy.IProxy;
import net.ymate.platform.core.beans.proxy.IProxyFactory;
import net.ymate.platform.core.event.Events;
import net.ymate.platform.core.event.annotation.EventRegister;
import net.ymate.platform.core.event.impl.DefaultEventConfig;
import net.ymate.platform.core.handle.EventRegisterHandler;
import net.ymate.platform.core.handle.ModuleHandler;
import net.ymate.platform.core.handle.ProxyHandler;
import net.ymate.platform.core.i18n.I18N;
import net.ymate.platform.core.module.IModule;
import net.ymate.platform.core.module.ModuleEvent;
import net.ymate.platform.core.module.annotation.Module;
import net.ymate.platform.core.support.ConfigBuilder;
import net.ymate.platform.core.util.RuntimeUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* YMP框架核心管理器
*
* @author 劉鎮 ([email protected]) on 2012-12-23 下午5:52:44
* @version 1.0
*/
public class YMP {
public static final Version VERSION = new Version(2, 0, 1, Version.VersionType.Release);
private static final Log _LOG = LogFactory.getLog(YMP.class);
private static final String __YMP_BASE_PACKAGE = "net.ymate.platform";
private static final YMP __instance = new YMP(ConfigBuilder.system().build());
private IConfig __config;
private boolean __inited;
private IBeanFactory __moduleFactory;
private IBeanFactory __beanFactory;
private IProxyFactory __proxyFactory;
private Map<Class<? extends IModule>, IModule> __modules;
private Events __events;
/**
* @return 返回默認YMP框架核心管理器對象實例,若未實例化或已銷毀則重新創建對象實例
*/
public static YMP get() {
return __instance;
}
/**
* 構造方法
*
* @param config YMP框架初始化配置
*/
public YMP(IConfig config) {
__config = config;
}
private void __registerScanPackages(IBeanFactory factory) {
factory.registerPackage(__YMP_BASE_PACKAGE);
for (String _packageName : __config.getAutoscanPackages()) {
factory.registerPackage(_packageName);
}
}
/**
* 初始化YMP框架
*
* @return 返回當前YMP核心框架管理器對象
* @throws Exception 框架初始化失敗時將拋出異常
*/
public YMP init() throws Exception {
if (!__inited) {
//
_LOG.info(" http://www.ymate.net/");
//
StopWatch _watch = new StopWatch();
_watch.start();
//
_LOG.info("Initializing ymate-platform-core-" + VERSION + " - debug:" + __config.isDevelopMode());
// 初始化I18N
I18N.initialize(__config.getDefaultLocale(), __config.getI18NEventHandlerClass());
// 初始化事件管理器,並註冊框架、模塊事件
__events = Events.create(new DefaultEventConfig(__config.getEventConfigs()));
__events.registerEvent(ApplicationEvent.class);
__events.registerEvent(ModuleEvent.class);
// 創建根對象工廠
__beanFactory = new DefaultBeanFactory();
__beanFactory.setLoader(new DefaultBeanLoader(__config.getExcudedFiles()));
__beanFactory.registerHandler(Bean.class);
// 創建模塊對象引用集合
__modules = new HashMap<Class<? extends IModule>, IModule>();
// 創建模塊對象工廠
__moduleFactory = new BeanFactory(this);
__moduleFactory.setLoader(new DefaultBeanLoader(__config.getExcudedFiles()));
__moduleFactory.registerHandler(Module.class, new ModuleHandler(this));
__moduleFactory.registerHandler(Proxy.class, new ProxyHandler(this));
__moduleFactory.registerHandler(EventRegister.class, new EventRegisterHandler(this));
// 設置自動掃描應用包路徑
__registerScanPackages(__moduleFactory);
__registerScanPackages(__beanFactory);
// 創建代理工廠並初始化
__proxyFactory = new DefaultProxyFactory(this).registerProxy(new InterceptProxy());
// 初始化根對象工廠
__moduleFactory.init();
for (IModule _module : __modules.values()) {
if (!_module.isInited()) {
_module.init(this);
// 觸發模塊初始化完成事件
__events.fireEvent(new ModuleEvent(_module, ModuleEvent.EVENT.MODULE_INITED));
}
}
// 初始化對象工廠
__beanFactory.init();
// 初始化對象代理
__beanFactory.initProxy(__proxyFactory);
// IoC依賴註入
__beanFactory.initIoC();
//
__inited = true;
//
_watch.stop();
_LOG.info("Initialization completed, Total time: " + _watch.getTime() + "ms");
// 觸發框架初始化完成事件
__events.fireEvent(new ApplicationEvent(this, ApplicationEvent.EVENT.APPLICATION_INITED));
}
return this;
}
/**
* 銷毀YMP框架
*
* @throws Exception 框架銷毀失敗時將拋出異常
*/
public void destroy() throws Exception {
if (__inited) {
// 觸發框架銷毀事件
__events.fireEvent(new ApplicationEvent(this, ApplicationEvent.EVENT.APPLICATION_DESTROYED));
//
__inited = false;
// 銷毀所有已加載模塊
for (IModule _module : __modules.values()) {
// 觸發模塊銷毀事件
__events.fireEvent(new ModuleEvent(_module, ModuleEvent.EVENT.MODULE_DESTROYED));
//
_module.destroy();
}
__modules = null;
// 銷毀代理工廠
__proxyFactory = null;
// 銷毀根對象工廠
__moduleFactory.destroy();
__moduleFactory = null;
//
__beanFactory.destroy();
__beanFactory = null;
// 銷毀事件管理器
__events.destroy();
}
}
/**
* @return 返回當前配置對象
*/
public IConfig getConfig() {
return __config;
}
/**
* @return 返回YMP框架是否已初始化
*/
public boolean isInited() {
return __inited;
}
/**
* 註冊自定義註解類處理器,重復註冊將覆蓋前者
*
* @param annoClass 自定義註解類型
* @param handler 註解對象處理器
*/
public void registerHandler(Class<? extends Annotation> annoClass, IBeanHandler handler) {
if (annoClass.equals(Module.class) || annoClass.equals(Proxy.class) || annoClass.equals(EventRegister.class)) {
_LOG.warn("Handler [" + annoClass.getSimpleName() + "] duplicate registration is not allowed");
return;
}
__beanFactory.registerHandler(annoClass, handler);
}
public void registerHandler(Class<? extends Annotation> annoClass) {
registerHandler(annoClass, IBeanHandler.DEFAULT_HANDLER);
}
/**
* 註冊排除的接口類
*
* @param excludedClass 預排除的接口類型
*/
public void registerExcludedClass(Class<?> excludedClass) {
__beanFactory.registerExcludedClass(excludedClass);
}
/**
* 註冊類
*
* @param clazz 目標類
*/
public void registerBean(Class<?> clazz) {
__beanFactory.registerBean(clazz);
}
public void registerBean(Class<?> clazz, Object object) {
__beanFactory.registerBean(clazz, object);
}
public void registerBean(BeanMeta beanMeta) {
__beanFactory.registerBean(beanMeta);
}
/**
* @param <T> 返回類型
* @param clazz 接口類型
* @return 提取類型為clazz的對象實例
*/
public <T> T getBean(Class<T> clazz) {
return __beanFactory.getBean(clazz);
}
/**
* 向工廠註冊代理類對象
*
* @param proxy 目標代理類
*/
public void registerProxy(IProxy proxy) {
__proxyFactory.registerProxy(proxy);
}
public void registerProxy(Collection<? extends IProxy> proxies) {
__proxyFactory.registerProxy(proxies);
}
/**
* 註冊模塊實例(此方法僅在YMP框架核心管理器未初始化前有效)
*
* @param module 目標模塊
*/
public void registerModule(IModule module) {
if (!__inited) {
if (module != null) {
__moduleFactory.registerBean(module.getClass(), module);
__modules.put(module.getClass(), module);
}
}
}
/**
* @param moduleClass 模塊類型
* @param <T> 模塊類型
* @return 獲取模塊類實例對象
*/
public <T extends IModule> T getModule(Class<T> moduleClass) {
return __moduleFactory.getBean(moduleClass);
}
/**
* @return 獲取事件管理器
*/
public Events getEvents() {
return __events;
}
/**
* @param targetFactory 目標對象工廠
* @param <T> 對象工廠類型
* @return 將目標對象工廠的Parent設置為當前YMP容器的對象工廠
*/
public <T extends IBeanFactory> T bindBeanFactory(T targetFactory) {
targetFactory.setParent(__beanFactory);
return targetFactory;
}
/**
* YMP框架根對象工廠類
*/
private static class BeanFactory extends DefaultBeanFactory {
private final YMP __owner;
public BeanFactory(YMP owner) {
this.__owner = owner;
}
@Override
public <T> T getBean(Class<T> clazz) {
T _bean = super.getBean(clazz);
// 重寫此方法是為了在獲取模塊對象時始終保證其已被初始化
if (_bean instanceof IModule) {
IModule _module = (IModule) _bean;
if (!_module.isInited()) {
if (__owner.getConfig().getExcludedModules().contains(_module.getName())) {
return null;
}
try {
_module.init(__owner);
// 觸發模塊初始化完成事件
__owner.getEvents().fireEvent(new ModuleEvent(_module, ModuleEvent.EVENT.MODULE_INITED));
} catch (Exception e) {
throw new RuntimeException(RuntimeUtils.unwrapThrow(e));
}
}
}
return _bean;
}
}
}
YMP運行初始化步驟