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

📄 defaultwebconfig.java.svn-base

📁 EasyJWeb是基于java技术
💻 SVN-BASE
字号:
package com.easyjf.web.config;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.easyjf.container.BeanDefinition;
import com.easyjf.util.StringUtils;
import com.easyjf.web.Module;
import com.easyjf.web.WebConfig;
import com.easyjf.web.interceptor.ExceptionInterceptor;

/**
 * 
 * <p>
 * Title:配置信息
 * </p>
 * <p>
 * Description:调用配置信息处理类,处理并存放easyjf-web.xml中的配置信息
 * </p>
 * <p>
 * Copyright:Copyright (c) 2006
 * </p>
 * <p>
 * Company: www.easyjf.com
 * </p>
 * 
 * @author 蔡世友
 * @version 1.0
 */
public class DefaultWebConfig implements WebConfig {

	// 所有模块
	private final Map modules = new HashMap();

	private final Map moduleAlias = new HashMap();
	// 所有定义的表单
	private final Map forms = new HashMap();

	// 所有页面
	private final Map pages = new HashMap();

	// 所有初始化程序
	private final List initApps = new ArrayList();

	// 所有全局拦截器
	private final List interceptors = new ArrayList();

	// 所有错误处理程序
	private final List errorHandler = new java.util.ArrayList();

	// Bean配置信息
	private List beanDefinitions = new ArrayList();

	private List configManagers = new ArrayList();

	// 模板路径
	private String templateBasePath;

	// 是否调试
	private boolean debug = false;

	// 默认为5M
	private int maxUploadFileSize = 1024 * 1024 * 5;

	// 缓存30K
	private int uploadSizeThreshold = 1024 * 30;

	// 配置信息类二进制数据
	private String[] configures;

	// 最多直接跳转Action次数
	private Integer maxDirectJumpToActionTimes = 3;

	private java.util.List importsConfigures = new java.util.ArrayList();

	private ConfigureResourceLoader resourceLoader;

	private String[] defaultActionPackages = {};

	public void setResourceLoader(ConfigureResourceLoader loader) {
		this.resourceLoader = loader;
	}

	private static final Logger logger = Logger
			.getLogger(DefaultWebConfig.class);

	public DefaultWebConfig() {
		configManagers.add(com.easyjf.web.ajax.AjaxConfigManager.getInstance());
	}

	public void init() {
		// 执行配置信息初始化
		// 清空配置信息
		modules.clear();
		forms.clear();
		pages.clear();
		initApps.clear();
		interceptors.clear();
		errorHandler.clear();
		beanDefinitions.clear();
		templateBasePath = "";
		try {
			logger.info("系统初始化!");
			parseConfigures(configures);
			while (importsConfigures.size() > 0) {
				String[] s = new String[importsConfigures.size()];
				importsConfigures.toArray(s);
				importsConfigures.clear();
				parseConfigures(s);
			}
			// 从bean定义信息中读取errorHandler模块配置信息
			for (int i = 0; i < beanDefinitions.size(); i++) {
				BeanDefinition bd = (BeanDefinition) beanDefinitions.get(i);
				if (ExceptionInterceptor.class.isAssignableFrom(bd
						.getBeanClass()))
					errorHandler.add(bd.getBeanClass());
			}
			// 把Module的信息转换成Bean信息,添加到容器配置信息中
			beanDefinitions.addAll(BeanConfigReader
					.parseBeansFromModules(modules));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void parseConfigures(String[] configures) throws Exception {
		// 可读取多个配置文件
		for (int k = 0; k < configures.length; k++) {
			logger.info("加载配置文件:"+configures[k]);
			parseConfig(resourceLoader.loadResource(configures[k]));
		}
	}

	public void parseConfig(java.io.InputStream in) throws Exception {
		java.io.BufferedInputStream is = new java.io.BufferedInputStream(in);
		IConfigFactory icf = new XMLConfigFactory(is);// (IConfigFactory)
		// //
		// Class.forName(
		// Globals.CONFIG_FACTORY_CLASS).newInstance();
		// 高用配置工厂方法
		icf.initForm(forms);// 表单
		icf.initModule(modules);// 模块
		icf.initPage(pages);// 页面模块配置
		Map map = new HashMap();
		map = icf.initOther();// 其它配置信息,除了form,module及page以外的其它配置信息
		if (map != null) {
			// 若设置了模版根目录则使用模版根目录路径
			if (map.get("TemplateBasePath") != null)
				templateBasePath = (String) map.get("TemplateBasePath");
			// 是否调试模式
			if (map.get(IConfigFactory.DEBUG) != null) {
				debug = Boolean.valueOf((String) map.get(IConfigFactory.DEBUG))
						.booleanValue();
			}
			// 文件上传大小
			if (map.get(IConfigFactory.MaxUploadFileSize) != null) {
				int maxSize = Integer.valueOf(
						(String) map.get(IConfigFactory.MaxUploadFileSize))
						.intValue() * 1024;
				if (maxSize > 0)
					maxUploadFileSize = maxSize;
			}
			// 上传文件缓存大小
			if (map.get(IConfigFactory.UploadSizeThreshold) != null) {
				int maxSize = Integer.valueOf(
						(String) map.get(IConfigFactory.UploadSizeThreshold))
						.intValue() * 1024;
				if (maxSize > 0)
					uploadSizeThreshold = maxSize;
			}
			// 最大直接跳转Action次数
			if (map.get(IConfigFactory.MaxDirectJumpToActionTimes) != null) {
				int maxTimes = Integer
						.valueOf(
								(String) map
										.get(IConfigFactory.MaxDirectJumpToActionTimes))
						.intValue();
				if (maxTimes >= 0)
					maxDirectJumpToActionTimes = maxTimes;
			}
			/**
			 * 读取系统中配置需要扫描的包
			 */
			if (map.get(IConfigFactory.DefaultActionPackage) != null) {
				String packages = (String) map
						.get(IConfigFactory.DefaultActionPackage);
				String[] ps = StringUtils.tokenizeToStringArray(packages, ",");
				if (ps != null && ps.length > 0)
					this.defaultActionPackages = ps;
			}

			importsConfigures.addAll((List) map.get("importResources"));

			// 处理需要启动时初始化的程序
			handleInitApp((List) map.get("initApp"));
			// 处理拦截器
			handleInterceptors((List) map.get("interceptors"));
			// 错误信息处理
			handleErrorHandler((List) map.get("errorHandler"));
			// 增加Bean配置信息
			beanDefinitions.addAll((List) map.get("beanDefinitions"));
			// 将此条语句放到循环外
			// beanDefinitions.addAll(BeanConfigReader.parseBeansFromModules(modules));
			// 把Module及WebForm等相关对象转换成Bean存入到容器中
			// WebForm并没有转换成Bean存入到容器中
		}

		java.util.Iterator ocs = configManagers.iterator();
		while (ocs.hasNext()) {
			ConfigManager cfm = (ConfigManager) ocs.next();
			cfm.parseConfig(icf.getDoc());
		}

	}

	private void handleInitApp(List list) throws Exception {
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				// String[] appParms = ((String)
				// list.get(i)).split(";");
				Map appMap = (Map) list.get(i);
				String classname = (String) appMap.get("class");
				String initmethod = (String) appMap.get("init-method");
				String destroymethod = (String) appMap.get("destroy-method");
				Object obj = null;
				Method init_method = null;
				Map app = new HashMap();
				if (StringUtils.hasLength(classname)) {
					obj = Class.forName(classname).newInstance();
					app.put("classname", obj);
					if (StringUtils.hasLength(initmethod)) {
						init_method = obj.getClass().getMethod(initmethod);
						app.put("init-method", init_method);
					}
					if (StringUtils.hasLength(destroymethod)) {
						Method destroy_method = obj.getClass().getMethod(
								destroymethod);
						app.put("destroy-method", destroy_method);
					}
				}
				initApps.add(app);
			}
		}
	}

	private void handleInterceptors(List list) throws Exception {
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				try {
					Map app = (Map) list.get(i);
					String name = (String) app.get("name");
					String method = (String) app.get("method");
					String classname = (String) app.get("class");
					Object obj = null;
					Method app_method = null;

					if (StringUtils.hasLength(classname)) {
						obj = Class.forName(classname).newInstance();
						app.put("classname", obj);
						if (StringUtils.hasLength(name)) {
							app.put("name", name);
						}
						if (StringUtils.hasLength(method)) {
							app_method = obj.getClass().getMethod(method);
							app.put("method", app_method);
						}
					}
					interceptors.add(app);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void handleErrorHandler(List list) throws Exception {
		if (list != null) {

		}
	}

	public String getTemplateBasePath() {
		return templateBasePath;
	}

	public void setTemplateBasePath(String templateBasePath) {
		this.templateBasePath = templateBasePath;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public Map getForms() {
		return forms;
	}

	public Map getModules() {
		return modules;
	}

	public Map getPages() {
		return pages;
	}

	public List getInitApps() {
		return initApps;
	}

	public List getInterceptors() {
		return interceptors;
	}

	public int getMaxUploadFileSize() {
		return maxUploadFileSize;
	}

	public void setMaxUploadFileSize(int maxUploadFileSize) {
		this.maxUploadFileSize = maxUploadFileSize;
	}

	public int getUploadSizeThreshold() {
		return uploadSizeThreshold;
	}

	public void setUploadSizeThreshold(int uploadSizeThreshold) {
		this.uploadSizeThreshold = uploadSizeThreshold;
	}

	public void setConfigures(String[] configures) {
		this.configures = configures;
	}

	public List getErrorHandler() {
		return errorHandler;
	}

	public List getBeanDefinitions() {
		return beanDefinitions;
	}

	public Integer getMaxDirectJumpToActionTimes() {
		return maxDirectJumpToActionTimes;
	}

	public String[] getDefaultActionPackages() {
		return defaultActionPackages;
	}

	public void loadAlias() {
		java.util.Iterator it = this.modules.values().iterator();
		while (it.hasNext()) {
			Module m = (Module) it.next();
			for (int i = 0; i < m.getAlias().size(); i++) {
				this.moduleAlias.put(m.getAlias().get(i), m.getPath());
			}
		}
	}

	public Module findModule(String name) {
		Module ret = (Module) this.modules.get(name);
		if (ret == null) {
			String realPath = (String) this.moduleAlias.get(name);
			if (realPath != null)
				ret = (Module) this.modules.get(realPath);
		}
		return ret;
	}

}

⌨️ 快捷键说明

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