package com.smtservlet.core; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.mybatis.spring.SqlSessionTemplate; import org.quartz.Trigger; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.core.io.Resource; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.method.HandlerMethod; import com.smtservlet.database.SMTDaoAbstract; import com.smtservlet.database.SMTDaoMybatis; import com.smtservlet.util.Json; import com.smtservlet.util.SMTStatic; import net.sf.ehcache.CacheManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * SMTApp全局应用类 */ public class SMTApp { public static class SMTMethodHandle { public Object _clsObj; public Method _method; public SMTMethodHandle(Object clsObj, Method method) { _clsObj = clsObj; _method = method; } public Object invokeMethod(Object[] args) throws Exception { return _method.invoke(_clsObj, args); } } public static interface SMTStartupInstance { void initInstance(Object thisApp, WebApplicationContext webApplicationContext)throws Exception; void exitInstance()throws Exception; } public static class SMTScheduleConfig { public String _cronExpr; public Object _cronObject; public String _cronMethod; public SMTScheduleConfig(String cronExpr, Object cronObject, String cornMethod) { _cronExpr = cronExpr; _cronObject = cronObject; _cronMethod = cornMethod; } } public static interface SMTScheduleInstance { SMTScheduleConfig[] getScheduleConfigs(); } public static interface SMTSessionListenerEvent { void sessionCreated(HttpSessionEvent event) throws Exception; void sessionDestroyed(HttpSessionEvent event) throws Exception; } public static interface SMTEhCacheManagerInitialize { void initEhCacheManager(CacheManager cacheManager); void clearEhCacheManager(CacheManager cacheManager); } /*** * request_map配置中配置的信息对应的对象 */ public static class SMTRequestConfig { /*** * request_map对应的业务号的内部配置 */ public Json _jsonConfig = null; /*** * request_map对应的业务号的处理事件类 */ public Object _mapBean = null; /*** * request_map对应的业务号的处理函数 */ public Method _mapMethod = null; /*** * request_map对应的业务号的业务处理方法(在第一次相应业务时被初始化,如果是注入方式则不会被设置) */ public HandlerMethod _mapHandler = null; public Class _clzTranReq = null; } /*** * 内部使用的分析 request_map配置用的类 */ private static class SMTRequestClassCache { /** * 类定义对象 */ public Object _mapBean = null; /** * 当前类内部包含的函数对象 */ public Map _mapName2Method = new HashMap(); } /** * convFilePath内部使用的格式化变量 */ protected static SMTStatic.StringNamedFormat[] _nmfmtConvFilePath = new SMTStatic.StringNamedFormat[]{ new SMTStatic.StringNamedFormat("WEB-ROOT", 0), new SMTStatic.StringNamedFormat("WEB-INF", 1), new SMTStatic.StringNamedFormat("TMP-ROOT", 2), }; /** * log对象 */ protected Logger _logger = LogManager.getLogger(SMTApp.class); /** * 当前SMTApp实例 */ protected volatile static SMTApp _ThisPtr; public static void setThis(SMTApp ThisPtr) { _ThisPtr = ThisPtr; } /** * 获取当前App实例(必须已经分配好的) * * @return 返回实例 */ public static SMTApp getThis() { if(_ThisPtr == null) throw new RuntimeException("SMTApp is not instance"); return _ThisPtr; } /** * Web应用的根目录地址 */ protected String _webRootPath = null; /** * 扫描的sql session对象 */ protected Map _mapName2Dao = new HashMap(); /** * 缺省的dao对象 */ protected SMTDaoAbstract _defaultSMTDao = null; /** * 扫描的RequestConfig对象 */ protected Map _mapName2RequestConfig = new HashMap(); protected List _listStartupInstance = null; /** * 应用程序的全局配置 */ protected Json _jsonConfig = null; /** * 请求发生异常时,自动跳转的url */ protected String _requestExceptionUrl = null; /** * 临时文件根路径 */ protected String _tmpFileRoot = null; /** * 临时文件请求url */ protected String _tmpUrlRoot = null; /** * 缺省无需登录的url正则表达式 */ protected Pattern _urlAuthorityNoLogin = Pattern.compile(".+"); /** * 缺省只需登录的url正则表达式 */ protected Pattern _urlAuthorityLogin = Pattern.compile(".+"); /** * 操作method handle的锁 */ protected Object _lockMapMethodHandle = new Object(); /** * 保存method handle用的map */ protected Map _mapName2MethodHandle = new ConcurrentHashMap(); /** * 应用程序启动时间 */ protected long _appStartupTime = 0; protected int _maxLogSize = 256; protected WebApplicationContext _webApplicationContext; protected Map _mapReplaceReqMapJson = new HashMap(); protected Class _requestClzType = SMTRequest.class; protected Set _setNoShrioUrls = new HashSet(); protected List _listSessionListenerEvent = new ArrayList(); protected List _listEhCacheManager = new ArrayList(); @Autowired @Qualifier("SMTAppResource") protected SMTAppResource _appResource; /** * 是否已经初始化过 */ protected boolean _webStartup = false; public SMTApp() { _appStartupTime = new Date().getTime(); } public void setMaxLogSize(int size) { this._maxLogSize = size; } public int getMaxLogSize() { return this._maxLogSize; } public Object getAppResource(String key) { return _appResource.getResource(key); } public void setRequestClassType(String clzName) throws Exception { _requestClzType = Class.forName(clzName); } protected void setReplaceReqMapJson(String src, String tag) { _mapReplaceReqMapJson.put(src, tag); } @PostConstruct private void springPostConstruct() throws Exception { initInsance(); } @PreDestroy private void springPreDestroy() throws Exception { exitInstance(); } @SuppressWarnings("unchecked") public Class getRequestClassType() { return (Class)_requestClzType; } /** * 在Spring中设置web root的地址 * * @param value 地址 */ public void setWebRoot(String value) { if(!SMTStatic.isNullOrEmpty(value) && !value.endsWith("/") && !value.endsWith("\\")) value += "/"; _webRootPath = value; } /*** * 应用程序初始化 * */ protected void initInsance() throws Exception { } public boolean isNoShrioURL(String url) { if(_setNoShrioUrls.contains(url)) return true; return false; } /*** * 由外部调用的web启动初始化函数(不可重载) * * @param webApplicationContext */ public final void webStartup(WebApplicationContext webApplicationContext) throws Exception { if(!_webStartup) { _webApplicationContext = webApplicationContext; onWebStartup(webApplicationContext); _webStartup = true; } } /** * 读取全局配置文件 * * @param jsonFile - 配置文件名 * @return - 返回配置的json对象 */ protected Json readConfigJson(File jsonFile) { return Json.read(jsonFile, null, new String[]{ "db_q", "'#link_map,db_fields" + Json._jsonPathSplit + "{0},I,^[aq]_,',\"is_query\":true", "db_w", "'#link_map,db_fields" + Json._jsonPathSplit + "{0},I,^[aw]_,',\"is_query\":false", "db_f", "'#link_map,db_fields" + Json._jsonPathSplit + "{0},I,^._field,field'" }); } /*** * 内部实际运行的初始化函数 * * @param webApplicationContext */ @SuppressWarnings("unchecked") protected void onWebStartup(WebApplicationContext webApplicationContext) throws Exception { // 如果外部未指定,则通过servlet获取web root的真实地址 if(SMTStatic.isNullOrEmpty(_webRootPath)) { _webRootPath = webApplicationContext.getServletContext().getRealPath("/"); if(SMTStatic.isNullOrEmpty(_webRootPath)) throw new Exception("can't find web root path from webApplicationContext.getServletContext().getRealPath()"); else if(!_webRootPath.endsWith("/") && !_webRootPath.endsWith("\\")) _webRootPath += "/"; } _logger.debug(String.format("web_root_path=%s", _webRootPath)); // 初始化EhCache EhCacheCacheManager springCacheManager = SMTApp.getBean(EhCacheCacheManager.class); if(springCacheManager != null) { CacheManager cacheManager = springCacheManager.getCacheManager(); Map mapStartup = BeanFactoryUtils.beansOfTypeIncludingAncestors(webApplicationContext, SMTEhCacheManagerInitialize.class, true, false); if(mapStartup != null) { // 扫描所有需要初始化encache的bean for(Entry entry : mapStartup.entrySet()) { _listEhCacheManager.add(entry.getValue()); entry.getValue().initEhCacheManager(cacheManager); } } } // 追加侦听 List list = SMTApp.getSessionListenerEventList(); if(list != null) _listSessionListenerEvent.addAll(list); SessionEventHttpSessionListenerAdapter sessionListener = SMTApp.getBean(SessionEventHttpSessionListenerAdapter.class); Field field = sessionListener.getClass().getDeclaredField("listeners"); field.setAccessible(true); List sessionListenerList = (List) field.get(sessionListener); sessionListenerList.add(new HttpSessionListener() { public void sessionCreated(HttpSessionEvent se) { for(SMTSessionListenerEvent evnet : _listSessionListenerEvent) { try { evnet.sessionCreated(se); } catch(Exception ex) { _logger.fatal("sessionCreated exception", ex); } } } public void sessionDestroyed(HttpSessionEvent se) { for(SMTSessionListenerEvent evnet : _listSessionListenerEvent) { try { evnet.sessionDestroyed(se); } catch(Exception ex) { _logger.fatal("sessionDestroyed exception", ex); } } } }); // 加载配置文件之前复制一些系统文件 beforeLoadJsonConfig(webApplicationContext, _webRootPath); // 读取配置文件 loadDaoConfig(webApplicationContext); loadRequestConfigList(webApplicationContext, ((SMTAppResLocations)getAppResource("RequestMapJsonLocations")).getLocations()); loadStartupInstance(webApplicationContext); loadSMTSessionListenerEvent(webApplicationContext); loadScheduleInstance(webApplicationContext); } public void clearEhCacheManager() { EhCacheCacheManager springCacheManager = SMTApp.getBean(EhCacheCacheManager.class); if(springCacheManager != null) { CacheManager cacheManager = springCacheManager.getCacheManager(); for(SMTEhCacheManagerInitialize ehCache : _listEhCacheManager) { ehCache.clearEhCacheManager(cacheManager); } } } protected String replaceRequestMapJsonString(String jsonStr) { for(Entry entry : _mapReplaceReqMapJson.entrySet()) { jsonStr = jsonStr.replace(entry.getKey(), entry.getValue()); } return jsonStr; } protected void loadRequestConfigList(WebApplicationContext webApplicationContext, Resource[] resList) throws Exception { if(resList == null) return; for(Resource res : resList) { String jsonText = SMTStatic.readTextStream(res.getInputStream()); jsonText = replaceRequestMapJsonString(jsonText); Json jsonConfig = Json.read(jsonText, true); loadRequestConfigList(webApplicationContext, jsonConfig); } } @SuppressWarnings("unchecked") protected void loadRequestConfigList(WebApplicationContext webApplicationContext, Json jsonConfig) throws Exception { Map mapName2Class = new HashMap(); StringBuilder sbLog = new StringBuilder(); for(Entry entry : jsonConfig.asJsonMap().entrySet()) { String name = entry.getKey(); Json json = entry.getValue(); // 创建request config对象 SMTRequestConfig requestConfig = newRequestConfig(webApplicationContext, jsonConfig); // 读取映射请求配置 if(json.has("map")) { Json jsonRequest = json.getJson("map"); String clsName = jsonRequest.getJson("class").asString(); String tranReqClz = jsonRequest.safeGetStr("req_clz", null); if(!SMTStatic.isNullOrEmpty(tranReqClz)) requestConfig._clzTranReq = (Class)Class.forName(tranReqClz); // 从缓存中查询对应的类和函数,如果未发现则创建 SMTRequestClassCache clsCache = mapName2Class.get(clsName); if(clsCache == null) { clsCache = new SMTRequestClassCache(); if(clsName.charAt(0) == '#') { clsCache._mapBean = webApplicationContext.getBean(clsName.substring(1)); } else { clsCache._mapBean = webApplicationContext.getBean(Class.forName(clsName)); } //clsCache._clsObject = webApplicationContext.getBean(clsBean); for(Method method : clsCache._mapBean.getClass().getMethods()) { clsCache._mapName2Method.put(method.getName(), method); } mapName2Class.put(clsName, clsCache); } // 创建对象 String method = jsonRequest.getJson("method").asString(); requestConfig._mapBean = clsCache._mapBean; requestConfig._mapMethod = clsCache._mapName2Method.get(method); if(requestConfig._mapMethod == null) throw new Exception(String.format("can't find method[%s#%s]", clsName, method)); // 如果需要跳过shiro,则需要注册 if(jsonRequest.safeGetBoolean("no_shrio", false)) { _setNoShrioUrls.add(name); } sbLog.append(String.format("RequestMap:[%s]:Method:[%s#%s]", name, clsName, method) + "\n"); } else { sbLog.append(String.format("RequestMap:[%s]:Method:[N/A]", name) + "\n"); } // 读取用户配置 requestConfig._jsonConfig = json; _mapName2RequestConfig.put(name, requestConfig); } _logger.debug("Request Map List : \n" + sbLog.toString()); } protected void copyResourceToFile(String resourcePath, String outFileMacro) throws Exception { String path = SMTApp.convFilePath(outFileMacro); if(new File(path).isFile()) { _logger.debug("copyResourceToFile : " + resourcePath + " to " + outFileMacro + " is exist"); return; } _logger.debug("copyResourceToFile : " + resourcePath + " to " + outFileMacro); InputStream is = this.getClass().getResourceAsStream(resourcePath); if(is == null) throw new Exception("can't find resource : " + resourcePath); try { OutputStream os = new FileOutputStream(path); try { int ch; while((ch = is.read()) >= 0) { os.write(ch); } } finally { os.close(); } } finally { is.close(); } } protected void beforeLoadJsonConfig(WebApplicationContext webApplicationContext, String webRootPath) throws Exception { } /** * 获取全局配置json * * @return 全局配置json */ public static Json getJsonConfig() { return getThis()._jsonConfig; } /** * 初始化所有带SMTStartupInstance的接口 * * @param webApplicationContext - web context * @param jsonConfig - config 配置 */ protected void loadSMTSessionListenerEvent(WebApplicationContext webApplicationContext) throws Exception { Map mapStartup = BeanFactoryUtils.beansOfTypeIncludingAncestors(webApplicationContext, SMTSessionListenerEvent.class, true, false); if(mapStartup == null || mapStartup.size() == 0) return; this._listSessionListenerEvent = new ArrayList(); // 将获得的sql对象保存在对应的dao后,保存在map中 for(Entry entry : mapStartup.entrySet()) { this._listSessionListenerEvent.add(entry.getValue()); } } /** * 初始化所有带SMTStartupInstance的接口 * * @param webApplicationContext - web context * @param jsonConfig - config 配置 */ protected void loadStartupInstance(WebApplicationContext webApplicationContext) throws Exception { Map mapStartup = BeanFactoryUtils.beansOfTypeIncludingAncestors(webApplicationContext, SMTStartupInstance.class, true, false); if(mapStartup == null || mapStartup.size() == 0) return; this._listStartupInstance = new ArrayList(); // 将获得的sql对象保存在对应的dao后,保存在map中 for(Entry entry : mapStartup.entrySet()) { entry.getValue().initInstance(entry.getValue(), webApplicationContext); this._listStartupInstance.add(entry.getValue()); } } protected void loadScheduleInstance(WebApplicationContext webApplicationContext) throws Exception { Map mapSchedule = BeanFactoryUtils.beansOfTypeIncludingAncestors(webApplicationContext, SMTScheduleInstance.class, true, false); if(mapSchedule == null || mapSchedule.size() == 0) return; // 将获得的sql对象保存在对应的dao后,保存在map中 @SuppressWarnings("static-access") SchedulerFactoryBean fact = this.getBean(SchedulerFactoryBean.class); List listTrigger = new ArrayList<>(); for(Entry entry : mapSchedule.entrySet()) { SMTScheduleConfig[] listSchedule = entry.getValue().getScheduleConfigs(); if(listSchedule != null && listSchedule.length > 0) { for(SMTScheduleConfig schedule : listSchedule) { MethodInvokingJobDetailFactoryBean job = new MethodInvokingJobDetailFactoryBean(); job.setTargetObject(schedule._cronObject); job.setTargetMethod(schedule._cronMethod); job.setConcurrent(false); job.afterPropertiesSet(); CronTriggerFactoryBean trigger = new CronTriggerFactoryBean(); trigger.setJobDetail(job.getObject()); trigger.setCronExpression(schedule._cronExpr); trigger.afterPropertiesSet(); listTrigger.add(trigger.getObject()); } } } fact.setTriggers(listTrigger.toArray(new Trigger[listTrigger.size()])); fact.setAutoStartup(true); fact.afterPropertiesSet(); fact.start(); } /** * 创建request config对象,也可在派生类中使用自定义的request config派生类 * * @param webApplicationContext - webApplicationContext对象 * @param jsonConfig - 全局配置json * @return - 返回创建的SMTRequestConfig对象 */ protected SMTRequestConfig newRequestConfig(WebApplicationContext webApplicationContext, Json jsonConfig) { return new SMTRequestConfig(); } /** * 由onWebStartup调用的读取dao_config的解析类 * * @param webApplicationContext - webApplicationContext对象 * @param jsonConfig - 全局配置json */ protected void loadDaoConfig(WebApplicationContext webApplicationContext) throws Exception { // 从配置中读取需要扫描的信息 Class sqlSessionType = SqlSessionTemplate.class; Class SqlDaoType = SMTDaoMybatis.class; // 扫描bean获得对应的sql session对象 Map mapSqlSession = webApplicationContext.getBeansOfType(sqlSessionType); // 将获得的sql对象保存在对应的dao后,保存在map中 for(Entry entry : mapSqlSession.entrySet()) { _defaultSMTDao = (SMTDaoAbstract) SqlDaoType.newInstance(); _defaultSMTDao.initInstance(entry.getValue()); _mapName2Dao.put(entry.getKey().toString(), _defaultSMTDao); } } /** * 获取当前时间(缺省为机器时间) * * @return */ protected Date getNewToday() { return new Date(); } /** * 获取MethodHandle缓存,如果不存在则创建 * * @param className - 类名 * @param methodName - 方法名 * @param parameterTypes - 参数类型 * @return - 返回handle */ protected SMTMethodHandle getMethodHandle(String className, String methodName, Class[] parameterTypes) throws Exception { String key = String.format("%s#%s", className, methodName); SMTMethodHandle handle = _mapName2MethodHandle.get(key); if(handle == null) { synchronized(_lockMapMethodHandle) { handle = _mapName2MethodHandle.get(key); if(handle == null) { Object clsObj = null; if(className.startsWith("@")) clsObj = SMTApp.getBean(className.substring(1)); else clsObj = SMTApp.getBean(Class.forName(className)); Method method = clsObj.getClass().getMethod(methodName, parameterTypes); if(method == null) throw new Exception(String.format("method [%s][%s] is not found", className, methodName)); handle = new SMTMethodHandle(clsObj, method); _mapName2MethodHandle.put(key, handle); } } } return handle; } /*** * 应用程序退出 */ protected void exitInstance() throws Exception { // 将获得的sql对象保存在对应的dao后,保存在map中 if(_listStartupInstance != null) { for(SMTStartupInstance item : _listStartupInstance) { item.exitInstance(); } } // 退出dao对象 for(Entry entry : _mapName2Dao.entrySet()) { try { entry.getValue().exitInstance(); } catch(Exception ex) { _logger.fatal(String.format("exception for exit SMTDaoAbstract:%s", entry.getKey()), ex); } } } /** * 获取字典映射表的列表(需要在派生类中重写) * * @return - 返回字典信息 */ protected Map>> getDictionaryMapList() { return null; } /** * 通过请求的url获得对应的code * * @param request - 请求的request * @return - 返回code,如果为 null表示不包含code */ public static String getRequestCode(HttpServletRequest request) { String servletPath = request.getServletPath(); String pathInfo = request.getPathInfo(); if (!SMTStatic.isNullOrEmpty(pathInfo)) { servletPath = servletPath + pathInfo; } if(servletPath.startsWith("/")) servletPath = servletPath.substring(1); return servletPath; // String url = request.getServletPath(); // if(url.startsWith("/")) // url = url.substring(1); // // return url; } /** * 通过请求的url获取根目录 * * @param request * @return */ public static String getRequestWebRoot(HttpServletRequest request) { StringBuilder sb = new StringBuilder(); String[] split = getRequestCode(request).split("/", -1); if(split.length > 1) { for(int i = 0; i < split.length - 1; i ++) sb.append("../"); sb.setLength(sb.length() - 1); } return sb.toString(); //return request.getContextPath(); } /** * 通过request对象获得对应的request config对象 * * @param request - request对象 * @return 返回对应的request config对象 */ public static SMTRequestConfig getRequestConfig(HttpServletRequest request) { String code = getRequestCode(request); return getRequestConfig(code); } /** * 通过request code获得对应的request config对象 * * @param requestCode - request code * @return - 返回request config */ public static SMTRequestConfig getRequestConfig(String requestCode) { if(requestCode == null) return null; SMTRequestConfig requestConfig = _ThisPtr._mapName2RequestConfig.get(requestCode); if(requestConfig == null || requestConfig._mapBean == null) _ThisPtr._logger.debug("can't invoke SMTRequestMapping for url : " + requestCode); return requestConfig; } public static boolean defaultAuthority(String requestCode, boolean loginMode) { if(loginMode && _ThisPtr._urlAuthorityLogin != null && _ThisPtr._urlAuthorityLogin.matcher(requestCode).find()) return true; if(_ThisPtr._urlAuthorityNoLogin != null && _ThisPtr._urlAuthorityNoLogin.matcher(requestCode).find()) return true; return false; } /** * 获取servlet启动时的秒值 * * @return - servlet启动时的秒值 */ public static long getStartupTime() { return _ThisPtr._appStartupTime; } /** * 获取缺省的dao对象 * * @return - 返回缺省的dao对象 */ public static SMTDaoAbstract getSMTDaoDefault() { return _ThisPtr._defaultSMTDao; } public static SMTDaoAbstract getSMTDaoById(String id) throws Exception { SMTDaoAbstract dao = _ThisPtr._mapName2Dao.get(id); if(dao == null) throw new Exception("can't find dao : " + id); return dao; } /** * 将路径的宏定义转换成实际的路径 * * @param macro - 要转换的路径 ,包含{WEB-ROOT},{WEB-INF} * @return - 实际的物理路径 */ public static String convFilePath(String macro) { return SMTStatic.stringFormat(macro, _ThisPtr._webRootPath, _ThisPtr._webRootPath + "WEB-INF", _ThisPtr._tmpFileRoot, _nmfmtConvFilePath); } public static String convTmpFilePath(String baseName, String id) { return SMTApp.convFilePath(String.format("{TMP-ROOT}/%s-%s", baseName, id)); } public static String convTmpFileUrl(String id) { return String.format("{WEB_ROOT}/%s?id=%s", _ThisPtr._tmpUrlRoot, id); } /** * 通过名字获取bean * * @param name - bean名 * @return - 返回获取的对象 */ public static Object getBean(String name) { WebApplicationContext webApp = _ThisPtr._webApplicationContext != null ? _ThisPtr._webApplicationContext : ContextLoader.getCurrentWebApplicationContext(); Object obj = webApp.getBean(name); return obj; } /** * 通过类型获得bean * * @param type - 类型 * @return - 返回获取的对象 */ public static T getBean(Class type) { WebApplicationContext webApp = _ThisPtr._webApplicationContext != null ? _ThisPtr._webApplicationContext : ContextLoader.getCurrentWebApplicationContext(); return (T)webApp.getBean(type); } /** * 获取当前请求需要实例化的request class,如果当前请求的class是定义的子类,则返回请求的class,否则返回当前class * * @param type - 请求的class * @return - request class */ public static Class getRequestClass(Class type) throws Exception { Class curClz = _ThisPtr.getRequestClassType(); if(curClz.isAssignableFrom(type)) return type; if(!type.isAssignableFrom(curClz)) throw new Exception(String.format("class [%s] is not base from [%s]", type.getName(), curClz.getName())); return curClz; } /** * 获取当request发生错误时,自动跳转的url * * @return - 要跳转的url */ public static String getRequestExeptionUrl() { return _ThisPtr._requestExceptionUrl; } public static Date newToday() { return _ThisPtr.getNewToday(); } /** * 获取字典映射表的列表 * * @param name - 字典类型名 * @return - 返回字典信息 */ public static Map> getDictionaryMap(String type) { Map>> mapList = _ThisPtr.getDictionaryMapList(); if(mapList == null) return null; return mapList.get(type); } public static String getTmpUrlRoot() { return _ThisPtr._tmpUrlRoot; } public static SMTMethodHandle getMethodHandleObject(String className, String methodName, Class[] parameterTypes) throws Exception { return _ThisPtr.getMethodHandle(className, methodName, parameterTypes); } public static Object invokeMethodHandle(String className, String methodName, Class[] parameterTypes, Object[] args) throws Exception { return _ThisPtr.getMethodHandle(className, methodName, parameterTypes).invokeMethod(args); } public static List getSessionListenerEventList() { return _ThisPtr._listSessionListenerEvent; } protected void onQueryPermissionRequestCode(String userId, String applicationId, Set r_setRequestCode) throws Exception { } public static void queryPermissionRequestCode(String userId, String applicationId, Set r_setRequestCode) throws Exception { _ThisPtr.onQueryPermissionRequestCode(userId, applicationId, r_setRequestCode); } public static Map getRequestConfigMap() { return _ThisPtr._mapName2RequestConfig; } }