private static void sessionListening(Set<Class<?>> allClass) throws Exception { Set<Class<?>> sessionListenerList = ClassUtil.getSubClassList(HttpSessionListener.class, allClass); if (sessionListenerList.size() == 0) return; final List<HttpSessionListener> listeners = new ArrayList<HttpSessionListener>(sessionListenerList.size()); for (Class<?> clazz : sessionListenerList) { listeners.add((HttpSessionListener) ClassUtil.newInstance(clazz)); } _threadPoolContainer .getEventDispatcher() .addEventListener( SessionEvent.SESSION_EVENT_NAME, new IEventHandler<Event>() { public void doExecute(Event e) throws Exception { Object[] _session_and_Type = (Object[]) e.getSource(); HttpSessionEvent he = new HttpSessionEvent((HttpSession) _session_and_Type[1]); for (HttpSessionListener listener : listeners) { if (((SessionEvent.SessionEventType) _session_and_Type[0]) == SessionEvent.SessionEventType.created) { listener.sessionCreated(he); } else { listener.sessionDestroyed(he); } } } }); }
private static void addEventListeners(Set<Class<?>> allClass) throws Exception { String httpTemplate = new String( FileUtil.readFile( FileUtil.getResourceAsStream( AbstractHttpHandler.class.getPackage().getName().replace(".", "/") + "/TemplateHttpHandler.java.template")), SystemUtil.DEFAULT_CHARSET); DynamicEngine dynamicEngine = DynamicCompileUtil.getDynamicEngine(); Set<Class<?>> helperClassList = ClassUtil.getSubClassList(AbstractHttpHelper.class, allClass); Map<String, Object[]> urlPatternList = new HashMap<String, Object[]>(); Set<String> checkSessionUriList = new HashSet<String>(); for (Class<?> clazz : helperClassList) { try { AbstractHttpHelper helper = (AbstractHttpHelper) ClassUtil.newInstance(clazz); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { String method_str = method.toString(); if (!method_str.startsWith("public") || method_str.contains(" static ")) { continue; } HttpListening e = method.getAnnotation(HttpListening.class); if (e != null) { if (!method_str.contains("Response")) { log.error( "事件监听器错误:HTTP事件监听函数必须抛出返回org.guyou.web.server.Forward对象", new IllegalStateException("事件监听器错误")); System.exit(1); } if (!method_str.contains("throws") || !method_str.contains("Exception")) { log.error("事件监听器错误:HTTP事件监听函数必须抛出Exception", new IllegalStateException("事件监听器错误")); System.exit(1); } if (!e.urlPattern().endsWith(".do")) { log.error( "事件名称不合法:<" + e.urlPattern() + ">必须以\".do\"结尾", new IllegalStateException("事件名称不合法")); System.exit(1); } if (urlPatternList.containsKey(e.urlPattern())) { Object[] objs = urlPatternList.get(e.urlPattern()); if (objs[0] == helper) { log.error( "事件名称重复:<" + e.urlPattern() + ">已经在当前类" + helper.getClass().getName() + "." + objs[1] + "(HttpServletRequest request,HttpServletResponse response,PrintWriter out)方法上监听过了。", new IllegalStateException("监听重复")); } else { log.error( "事件名称重复:<" + e.urlPattern() + ">同时在" + helper.getClass().getName() + "." + objs[1] + "(HttpServletRequest request,HttpServletResponse response,PrintWriter out)和" + clazz.getName() + "." + method.getName() + "(HttpServletRequest request,HttpServletResponse response,PrintWriter out)监听", new IllegalStateException("监听重复")); } System.exit(1); } else { urlPatternList.put(e.urlPattern(), new Object[] {helper, method.getName()}); String className = "HttpHandler_" + e.urlPattern().replace("/", "_").replace(".", "_"); AbstractHttpHandler handler = (AbstractHttpHandler) dynamicEngine.javaCodeToObject( AbstractHttpHandler.class.getPackage().getName() + "." + className, httpTemplate .replace("${className}", className) .replace("${helper}", clazz.getName()) .replace("${function}", method.getName())); ClassUtil.setFieldValue(handler, "helper", helper); _threadPoolContainer.getEventDispatcher().addEventListener(e.urlPattern(), handler); log.error("注册<" + e.urlPattern() + ">监听成功"); if (e.isCheckSession()) checkSessionUriList.add(_servletContext.getContextPath() + e.urlPattern()); } } } } catch (Exception e) { log.error("初始化[" + clazz.getName() + "]异常!!", e); System.exit(1); } } _servletContext.setAttribute(ServletContextKeyEnum.CHECK_SESSION_URI_LIST, checkSessionUriList); urlPatternList.clear(); }