⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frameworkengine.java

📁 一个简单的java邮件系统源码
💻 JAVA
字号:
package com.easyjf.web.core;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import com.easyjf.beans.BeanWrapper;
import com.easyjf.container.Container;
import com.easyjf.web.Action;
import com.easyjf.web.FormConfig;
import com.easyjf.web.Globals;
import com.easyjf.web.IWebAction;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebConfig;
import com.easyjf.web.WebForm;
import com.easyjf.web.interceptor.IRequestInterceptor;

public abstract class FrameworkEngine {
	private static final Logger logger =Logger.getLogger(FrameworkEngine.class);
	private static WebConfig webConfig;
	private static Container container;
	public static WebConfig getWebConfig() {
		return webConfig;
	}	
	public static void setWebConfig(WebConfig webConfig) {
		FrameworkEngine.webConfig = webConfig;
	}
	public static Container getContainer() {
		return container;
	}
	public static void setContainer(Container container) {
		FrameworkEngine.container = container;
	}
	public static  Page findPage(java.util.Map pages,String name) {
		return (Page) pages.get(name);
	}

	public static FormConfig findForm(java.util.Map forms,String name) {
		return (FormConfig) forms.get(name);
	}

	public static  Module findModule(java.util.Map modules,String name) {
		return (Module) modules.get(name);
	}
	
	/**
	 * 根据request创建一个form对象
	 * 
	 * @param request
	 * @param formName
	 * @return 封装了用户数据的Form
	 */
	public static WebForm creatWebForm(HttpServletRequest request,
			String formName) {
		Map textElement = new HashMap();
		Map fileElement = new HashMap();
		String contentType = request.getContentType();
		String reMethod = request.getMethod();
		if ((contentType != null)
				&& (contentType.startsWith("multipart/form-data"))
				&& (reMethod.equalsIgnoreCase("POST"))) {
			// 二进制 multipart/form-data
			DiskFileUpload df = new DiskFileUpload();
			df.setHeaderEncoding(request.getCharacterEncoding());
			df.setSizeMax(1024 * 1024 * 5);
			df.setSizeThreshold(1024 * 20);
			List reqPars = null;
			try {
				reqPars = df.parseRequest(request);
				for (int i = 0; i < reqPars.size(); i++) {
					FileItem it = (FileItem) reqPars.get(i);
					if (it.isFormField()) {
						textElement.put(it.getFieldName(), it.getString(request
								.getCharacterEncoding()));// 文本字段需要转码
					} else {
						fileElement.put(it.getFieldName(), it);// 文件不需要转码
					}
				}
			} catch (Exception e) {
				logger.error(e);
			}
		} else if ((contentType != null) && contentType.equals("text/xml")) {
			StringBuffer buffer = new StringBuffer();
			try {
				String s = request.getReader().readLine();
				while (s != null) {
					buffer.append(s + "\n");
					s = request.getReader().readLine();
				}
			} catch (Exception e) {
				logger.error(e);
			}
			textElement.put("xml", buffer.toString());
		} else {
			textElement = request2map(request);
		}
		// logger.debug("表单数据处理完毕!");
		WebForm wf = findForm(formName);
		if (wf != null) {
			wf.setFileElement(fileElement);
			wf.setTextElement(textElement);
		}
		return wf;
	}

	/**
	 * 根据formName查找(创建)一个Form对象
	 * 
	 * @param formName
	 * @return 查找Form
	 */
	public static WebForm findForm(String formName) {
		WebForm wf = null;
		String formClass = Globals.DEFAULT_FORM_CLASS;
		if (formName != null && (!formName.equals(""))) {
		FormConfig form =FrameworkEngine.findForm(webConfig.getForms(), formName);
			if (form != null)
				formClass = form.getBean();
			// logger.debug("创建配置文件中的表单!"+formClass);
			if (formClass == null || formClass.equals(""))
				formClass = Globals.DEFAULT_FORM_CLASS;
			try {
				wf = (WebForm) Class.forName(formClass).newInstance();
				wf.setProperty(FrameworkEngine.findForm(webConfig.getForms(), formName).getPropertys());
				wf.setFormConfig(form);
			} catch (Exception e) {
				logger.error("创建表单错误" + formClass + e);
				// throw "Can't ctead form of the type"+formClass;
			}
		} else {
			wf = new WebForm();
		}
		// logger.debug(wf.getClass().getName());
		return wf;
	}

	/**
	 * 根据路径返回一个IWebAction
	 * 
	 * @param path
	 * @return action
	 */
	public static IWebAction findAction(String path) {
		//首先从容器中加载
		Object bean=container.getBean(path);
		if(bean!=null && bean instanceof IWebAction)
		{
			return (IWebAction)bean;
		}
		IWebAction wf = null;	
		String actionClass = Globals.DEFAULT_ACTION_CLASS;
		if (path != null && (!path.equals(""))) {
			Module module = FrameworkEngine.findModule(webConfig.getModules(),path);
			if (module != null)
				actionClass = module.getAction();
			if (actionClass == null || actionClass.equals(""))
				actionClass = Globals.DEFAULT_ACTION_CLASS;
			try {
				wf = (IWebAction) Class.forName(actionClass).newInstance();
			} catch (Exception e) {
				logger.error("创建Action错误" + actionClass + e);
			}
		} else {
			if (wf == null)
				wf = new Action();
		}

		return wf;
	}

	/**
	 * 根据Module返回一个一个IWebAction
	 * 
	 * @param module
	 * @return 相应的Action类
	 */
	public static IWebAction findAction(Module module) {
		IWebAction wf = null;		
		Object bean=container.getBean(module.getPath());
		if(bean!=null && bean instanceof IWebAction)
		{
			return (IWebAction)bean;
		}
		String actionClass = null;
		if (module != null)
			actionClass = module.getAction();
		if (actionClass == null || actionClass.equals(""))
			actionClass = Globals.DEFAULT_ACTION_CLASS;
		try {
			wf = (IWebAction) Thread.currentThread().getContextClassLoader().loadClass(actionClass).newInstance();
			// 尝试使用代理对象加载拦截器,此处需要再修改

		} catch (Exception e) {
			logger.error("创建Action错误" + actionClass + e);
		}
		return wf;
	}

	/**
	 * 根据路径返回一个Module,首先从配置文件中查找,如果配置文件中没有,则生成缺省的Module
	 * 
	 * @param pathName
	 * @return 相应的Action类
	 */
	public static Module findModule(String pathName) {
		Module m = new Module();
		String actionClass = getActionName(pathName);
		m.setAction(actionClass);
		m.setDefaultPage("default");
		m.setPath(pathName);
		Page p = new Page();
		p.setName("default");
		p.setType(Globals.PAGE_TEMPLATE_TYPE);
		p.setUrl(pathName + "." + Globals.DEFAULT_TEMPLATE_EXT);
		Map pages = new HashMap();
		pages.put(p.getName(), p);
		m.setPages(pages);
		return m;
	}

	public  static IRequestInterceptor findRequestInterceptor(String name) {
		IRequestInterceptor interceptor = (IRequestInterceptor)webConfig.getInterceptors().get(name);
		return interceptor;
	}

	public static Iterator getRequestInterceptors() {
		return webConfig.getInterceptors().values().iterator();
	}

	/**
	 * 根据url路径生成默认形式的IWebAction类全名
	 * 
	 * @param pathName
	 * @return actionName
	 */
	public static String getActionName(String pathName) {

		String[] s = pathName.split("/");
		String actionName = "";
		if (s != null) {
			for (int i = 0; i < s.length; i++) {
				if (s[i] != null && (!s[i].equals("")))
					actionName += s[i] + (i < s.length - 1 ? "_" : "");
			}
		}
		String actionClass = Globals.DEFAULT_ACTTION_PACKAGE + "." + actionName
				+ "Action";
		return actionClass;
	}

	/**
	 * 一个通用reuqest数据到map的转换
	 * 
	 * @param request
	 * @return Map
	 */
	public static Map request2map(HttpServletRequest request) {
		Map map = new HashMap();
		java.util.Enumeration s = request.getParameterNames();
		// System.out.println("参数个数:"+request.getParameterMap().size());
		while (s.hasMoreElements()) {
			String name = (String) s.nextElement();
			//if (!request.getParameter(name).equals("")) {
				map.put(name, eliminateScript((String)request.getParameter(name)));
				// System.out.println(name+":"+request.getParameter(name)+"<br>");
			//}
		}
		return map;
	}
	
	public static void form2Obj(Map map, Object obj) {
		BeanWrapper wrapper=new BeanWrapper(obj);
		Iterator names = map.keySet().iterator();
		while (names.hasNext()) {
		String name = (String) names.next();
		if(wrapper.isWritableProperty(name))			
			wrapper.setPropertyValue(name,map.get(name));
		}	
		/*		
		while (names.hasNext()) {
			String name = (String) names.next();
			if (BeanUtilsBean.getInstance().getPropertyUtils().isWriteable(obj,
					name)) {
				Object value = map.get(name);
				try {
					BeanUtilsBean.getInstance().copyProperty(obj, name, value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}*/
	}
	/**
	 * 根据Module和pageName返回按指定策略寻找的Page;
	 * 
	 * @param module,pageName
	 * @return Page
	 */
	public static Page findPage(Module module, String pageName) {
		String basePath =webConfig.getTemplateBasePath();
		if ("".equals(basePath)) {
			basePath = Globals.DEFAULT_TEMPLATE_PATH;
		}
		List allPage = new ArrayList();
		getAllPages(allPage, basePath);
		// 1,按照module_pagename寻找:
		String name = module.getPath();
		name = name.substring(name.lastIndexOf("/") + 1);

		String target = name + "_" + pageName + "."+ Globals.DEFAULT_TEMPLATE_EXT;
		boolean find = false;
		boolean tag = false;
		for (Iterator it = allPage.iterator(); it.hasNext();) {
			String temp = (String) it.next();
			if (temp.equalsIgnoreCase(target)) {
				find = true;
				tag = true;
				break;
			} else if (temp.equalsIgnoreCase(pageName + "."+ Globals.DEFAULT_TEMPLATE_EXT)) {
				find = true;
				tag = false;
				break;
			}
		}
		if (!find) {
			return null;
		} else {
			if (!tag) {
				target = pageName + "." + Globals.DEFAULT_TEMPLATE_EXT;
			}
		}
		Page page = new Page();
		page.setName(pageName);
		page.setType(Globals.PAGE_TEMPLATE_TYPE);

		String url = "/" + target;

		page.setUrl(url);
		module.getPages().put(page.getName(), page);
		return page;
	}

	private static void getAllPages(List allPage, String basePath) {
		String fileName = Globals.APP_BASE_DIR + basePath.substring(1);
		File file = new File(fileName);
		if (file != null && file.isDirectory()) {
			String[] pages = file.list();
			if (pages.length > 0) {
				for (int i = 0; i < pages.length; i++) {
					allPage.add(pages[i]);
				}
			}
		}
	}
	public static String eliminateScript(String value)
	{		
		return value.replaceAll("<","&lt;").replaceAll("%3c", "&lt;");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -