|
楼主 |
发表于 2005-8-19 09:08:39
|
显示全部楼层
配置文件读取类
- /*
- * 创建日期 2005-3-31
- *
- */
- package com.bupticet.strutsinterceptor;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- 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.digester.Digester;
- import org.apache.log4j.Logger;
- import org.apache.struts.config.ActionConfig;
- import org.springframework.web.context.WebApplicationContext;
- import org.xml.sax.SAXException;
- import com.bupticet.base.AbstractBaseObject;
- import com.bupticet.strutsinterceptor.model.ActionModel;
- import com.bupticet.strutsinterceptor.model.DefaultInterceptorModel;
- import com.bupticet.strutsinterceptor.model.ExcludeDefaultModel;
- import com.bupticet.strutsinterceptor.model.ExcludeModel;
- import com.bupticet.strutsinterceptor.model.InterceptorRefModel;
- import com.bupticet.strutsinterceptor.model.InterceptorStackModel;
- import com.bupticet.strutsinterceptor.model.MethodModel;
- import com.bupticet.strutsinterceptor.model.OrderInterceptorModel;
- import com.bupticet.strutsinterceptor.model.StackRefModel;
- import com.bupticet.strutsinterceptor.model.StrutsInterceptorModel;
- import com.bupticet.util.FileUtils;
- /**
- * <p>
- * Title: InterceptorConfig
- * </p>
- * <p>
- * Description: 配置文件读取类
- * </p>
- * <p>
- * Copyright: Copyright (c)北京邮电大学网络教育技术研究所[[url]www.buticet.com][/url] 2005
- * </p>
- * <p>
- * Company: 北京邮电大学网络教育技术研究所[[url]www.buticet.com][/url]
- * </p>
- *
- * @author LJ-silver E-mail:LJ-silver@163.com
- * @version 1.0
- */
- public class InterceptorConfig {
- /**
- * Logger for this class
- */
- private static final Logger logger = Logger
- .getLogger(InterceptorConfig.class);
- private static StrutsInterceptorModel strutsInterceptor;
- private static Map methodConfig;
- private static Map actionConfig;
- private static long modifyTime;
- private static String absolutePath;
- private static WebApplicationContext wac;
- private final static String filePathName = "/config/strutsinterceptor/struts-interceptor.xml";
- /**
- * 返回对应请求(request)的ActionInterceptor数组
- *
- * @param request
- * @param wac
- * @return ActionInterceptor[]
- */
- public static ActionInterceptor[] getInterceptorArray(
- HttpServletRequest request, WebApplicationContext wac) {
- ActionConfig actionConfig = (ActionConfig) request
- .getAttribute("org.apache.struts.action.mapping.instance");
- String path = actionConfig.getPath();
- String method = null;
- if (null != actionConfig.getParameter()) {
- String parameter = actionConfig.getParameter();
- method = request.getParameter(parameter);
- } else {
- method = "execute";
- }
- return getInterceptorArray(path, method, wac);
- }
- /**
- * 返回对应path和method的ActionInterceptor数组,[尚未实现消除重复]
- *
- * @param path
- * @param method
- * @param wac
- * @return ActionInterceptor[]
- */
- public static ActionInterceptor[] getInterceptorArray(String path,
- String method, WebApplicationContext wac) {
- init(wac);
- List list = getInterceptorList(path, method);
- int size = list.size();
- ActionInterceptor[] arrays = new ActionInterceptor[size];
- for (int i = 0; i < size; i++) {
- arrays[i] = (ActionInterceptor) list.get(i);
- }
- return arrays;
- }
- /**
- * 返回对应path和method的bean List
- *
- * @param path
- * @param method
- * @return List
- */
- private static List getInterceptorList(String path, String method) {
- List interceptorName = null;
- if (methodConfig.containsKey(new PathNMethod(path, method))) {
- interceptorName = (List) methodConfig.get(new PathNMethod(path,
- method));
- } else if (actionConfig.containsKey(path)) {
- interceptorName = (List) actionConfig.get(path);
- } else {
- interceptorName = getDefaultInterceptors(strutsInterceptor
- .isOrderInterceptor());
- }
- List interceptorClass = new ArrayList();
- for (int i = 0; i < interceptorName.size(); i++) {
- String name = (String) interceptorName.get(i);
- Object bean = wac.getBean(name);
- if (bean == null) {
- throw new RuntimeException("在spring容器中找不到:" + name + "!");
- }
- interceptorClass.add(bean);
- }
- return interceptorClass;
- }
- /**
- * @param b
- * @return List
- */
- private static List getDefaultInterceptors(boolean b) {
- List l = new ArrayList();
- if (b) {
- if (strutsInterceptor.getFirstOrderDefaultInterceptors() != null
- && strutsInterceptor.getFirstOrderDefaultInterceptors()
- .size() != 0) {
- addDefaultInterceptorsCollection(l, strutsInterceptor
- .getFirstOrderDefaultInterceptors());
- }
- if (strutsInterceptor.getLastOrderDefaultInterceptors() != null
- && strutsInterceptor.getLastOrderDefaultInterceptors()
- .size() != 0) {
- addDefaultInterceptorsCollection(l, strutsInterceptor
- .getLastOrderDefaultInterceptors());
- }
- } else {
- Collection collection = strutsInterceptor.getDefaultInterceptors()
- .values();
- if (collection != null && collection.size() != 0) {
- addDefaultInterceptorsCollection(l, collection);
- }
- }
- return l;
- }
- /**
- * 加入包含DefaultInterceptorModel模型的DefaultInterceptorsCollection
- *
- * @param l
- * @param c
- */
- private static void addDefaultInterceptorsCollection(List l, Collection c) {
- if (c == null || c.size() == 0) {
- return;
- } else {
- for (Iterator it = c.iterator(); it.hasNext();) {
- DefaultInterceptorModel model = (DefaultInterceptorModel) it
- .next();
- l.add(model.getBean());
- }
- }
- }
- /**
- * 判断是否需要载入或者重新载入配置文件并执行
- *
- * @param wac
- */
- private static synchronized void init(WebApplicationContext wac) {
- if (InterceptorConfig.wac == null) {
- InterceptorConfig.wac = wac;
- }
- if (methodConfig == null || actionConfig == null) {
- initResource();
- } else if (new File(absolutePath).lastModified() != modifyTime) {
- initResource();
- } else {
- return;
- }
- }
- /**
- * 载入或者重新载入配置文件
- */
- private static void initResource() {
- if (absolutePath == null) {
- absolutePath = FileUtils.getAbsolutePath(InterceptorConfig.class,
- filePathName);
- }
- File file = new File(absolutePath);
- modifyTime = file.lastModified();
- readConfig();
- checkStackBeanExists();
- checkDefaultBeanExists();
- if (strutsInterceptor.isOrderInterceptor()) {
- prepareSort();
- }
- buildConfig();
- // 清理无用缓存
- strutsInterceptor.setInterceptorStacks(null);
- strutsInterceptor.setActions(null);
- }
- /**
- * 构建配置
- */
- private static void buildConfig() {
- try {
- Map methodMap = new HashMap();
- Map actionMap = new HashMap();
- List actions = strutsInterceptor.getActions();
- for (int i = 0; i < actions.size(); i++) {
- ActionModel action = (ActionModel) actions.get(i);
- ExcludeDefaultModel actionExclude = action.getExcludeDefault();
- checkRefDefault(actionExclude);
- List methodsList = action.getMethods();
- Collection collection = strutsInterceptor
- .getDefaultInterceptors().values();
- for (int j = 0; j < methodsList.size(); j++) {
- MethodModel method = (MethodModel) methodsList.get(j);
- String[] methodArray = method.getNameArray();
- if (methodArray.length == 0) {
- throw new RuntimeException(
- "method name不能为空,action name:"
- + action.getPath());
- }
- ExcludeDefaultModel methodExclude = method
- .getExcludeDefault();
- checkRefDefault(methodExclude);
- if (actionExclude != null
- && actionExclude.getExcludes().size() != 0) {
- if (methodExclude == null) {
- methodExclude = new ExcludeDefaultModel();
- }
- methodExclude.getExcludes().addAll(
- actionExclude.getExcludes());
- }
- String methodName = methodArray[0];
- List interceptorName = new ArrayList();
- if (strutsInterceptor.isOrderInterceptor()) {
- if ((actionExclude == null || !actionExclude.isAll())
- && (methodExclude == null || !methodExclude
- .isAll())) {
- addUnExcludeInterceptor(
- interceptorName,
- strutsInterceptor
- .getFirstOrderDefaultInterceptors(),
- methodExclude);
- }
- List interceptorRefs = method.getInterceptorRefs();
- List stackRefs = method.getStackRefs();
- addRefs(interceptorName, interceptorRefs, stackRefs,
- true);
- if ((actionExclude == null || !actionExclude.isAll())
- && (methodExclude == null || !methodExclude
- .isAll())) {
- addUnExcludeInterceptor(interceptorName,
- strutsInterceptor
- .getLastOrderDefaultInterceptors(),
- methodExclude);
- }
- } else {
- if ((actionExclude == null || !actionExclude.isAll())
- && (methodExclude == null || !methodExclude
- .isAll())) {
- if (collection != null && collection.size() != 0) {
- List defaultInterceptors = new ArrayList();
- defaultInterceptors.addAll(collection);
- addUnExcludeInterceptor(interceptorName,
- defaultInterceptors, methodExclude);
- }
- }
- List interceptorRefs = method.getInterceptorRefs();
- List stackRefs = method.getStackRefs();
- addRefs(interceptorName, interceptorRefs, stackRefs,
- false);
- }
- if (interceptorName == null) {
- interceptorName = new ArrayList();
- }
- if (methodMap.containsKey(new PathNMethod(action.getPath(),
- methodName))) {
- throw new RuntimeException("method重复定义,action="
- + action.getPath() + " , method=" + methodName);
- } else {
- methodMap.put(new PathNMethod(action.getPath(),
- methodName), interceptorName);
- }
- if (methodArray.length > 1) {
- for (int k = 1; k < methodArray.length; k++) {
- if (methodMap.containsKey(new PathNMethod(action
- .getPath(), methodName))) {
- throw new RuntimeException("method重复定义,action="
- + action.getPath() + " , method="
- + methodName);
- } else {
- methodMap.put(new PathNMethod(action.getPath(),
- methodArray[k]), interceptorName);
- }
- }
- }
- }
- List actionInterceptorName = new ArrayList();
- if (strutsInterceptor.isOrderInterceptor()) {
- if (actionExclude == null || !actionExclude.isAll()) {
- addUnExcludeInterceptor(actionInterceptorName,
- strutsInterceptor
- .getFirstOrderDefaultInterceptors(),
- actionExclude);
- addUnExcludeInterceptor(actionInterceptorName,
- strutsInterceptor
- .getLastOrderDefaultInterceptors(),
- actionExclude);
- }
- } else {
- if (actionExclude == null || !actionExclude.isAll()) {
- if (collection != null && collection.size() != 0) {
- List defaultInterceptors = new ArrayList();
- defaultInterceptors.addAll(collection);
- addUnExcludeInterceptor(actionInterceptorName,
- defaultInterceptors, actionExclude);
- }
- }
- }
- if (actionMap.containsKey(action.getPath())) {
- throw new RuntimeException("action重复定义,action="
- + action.getPath());
- } else {
- actionMap.put(action.getPath(), actionInterceptorName);
- }
- }
- methodConfig = methodMap;
- actionConfig = actionMap;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 加入引用的interceptor和stack
- *
- * @param interceptorName
- * @param interceptorRefs
- * @param stackRefs
- * @param order
- */
- private static void addRefs(List interceptorName, List interceptorRefs,
- List stackRefs, boolean order) {
- Map stacks = strutsInterceptor.getInterceptorStacks();
- if (interceptorRefs == null) {
- interceptorRefs = new ArrayList();
- }
- if (stackRefs != null && stackRefs.size() != 0) {
- interceptorRefs.addAll(stackRefs);
- }
- Collections.sort(interceptorRefs);
- for (int q = 0; q < interceptorRefs.size(); q++) {
- InterceptorRefModel icpor = (InterceptorRefModel) interceptorRefs
- .get(q);
- String icporName = icpor.getName();
- if (icpor instanceof StackRefModel) {
- checkRefStack(icporName);
- addStack(interceptorName, icporName);
- } else {
- checkBeanExists(icporName);
- interceptorName.add(icporName);
- }
- }
- }
- /**
- * 将指定名称的stack加入
- *
- * @param list
- * @param stackName
- */
- private static void addStack(List list, String stackName) {
- InterceptorStackModel stack = (InterceptorStackModel) strutsInterceptor
- .getInterceptorStacks().get(stackName);
- List refs = stack.getInterceptorRefs();
- if (refs == null || refs.size() == 0) {
- return;
- }
- for (int i = 0; i < refs.size(); i++) {
- InterceptorRefModel model = (InterceptorRefModel) refs.get(i);
- list.add(model.getName());
- }
- }
- /**
- * 向list中加入未被排除的defaultInterceptor
- *
- * @param list
- * @param defaultInterceptor
- * @param exclude
- */
- private static void addUnExcludeInterceptor(List list,
- List defaultInterceptor, ExcludeDefaultModel exclude) {
- if (exclude == null || exclude.getExcludes().size() == 0) {
- list.addAll(defaultInterceptor);
- } else {
- List excludeList = exclude.getExcludes();
- for (int i = 0; i < defaultInterceptor.size(); i++) {
- DefaultInterceptorModel model = (DefaultInterceptorModel) defaultInterceptor
- .get(i);
- String beanName = model.getBean();
- if (!excludeList.contains(beanName)) {
- list.add(beanName);
- }
- }
- }
- }
- /**
- * 预排序
- */
- private static void prepareSort() {
- strutsInterceptor.sortDefaultInterceptors();
- strutsInterceptor.sortInterceptorStacks();
- }
- /**
- * 检查在spring容器中是否存在bean
- *
- * @param beanName
- */
- private static void checkBeanExists(String beanName) {
- if (!wac.containsBean(beanName)) {
- throw new RuntimeException("在spring容器中找不到:" + beanName + "!");
- }
- }
- /**
- * 检查default-interceptor中引用的interceptor是否存在
- */
- private static void checkDefaultBeanExists() {
- Collection collection = strutsInterceptor.getDefaultInterceptors()
- .values();
- if (collection == null || collection.size() == 0) {
- return;
- } else {
- for (Iterator it = collection.iterator(); it.hasNext();) {
- DefaultInterceptorModel model = (DefaultInterceptorModel) it
- .next();
- if (model == null) {
- throw new RuntimeException("default-interceptor定义错误!");
- } else {
- checkBeanExists(model.getBean());
- }
- }
- }
- }
- /**
- * 检查stack中引用的interceptor是否存在
- */
- private static void checkStackBeanExists() {
- Collection collection = strutsInterceptor.getInterceptorStacks()
- .values();
- if (collection == null || collection.size() == 0) {
- return;
- } else {
- for (Iterator it = collection.iterator(); it.hasNext();) {
- InterceptorStackModel model = (InterceptorStackModel) it.next();
- if (model == null) {
- throw new RuntimeException("interceptor-stack定义错误!");
- } else {
- List refList = model.getInterceptorRefs();
- if (refList == null || refList.size() == 0) {
- continue;
- } else {
- for (int i = 0; i < refList.size(); i++) {
- InterceptorRefModel ref = (InterceptorRefModel) refList
- .get(i);
- if (ref == null) {
- throw new RuntimeException(
- "interceptor-stack中的引用定义错误,interceptor-stack name:"
- + model.getName());
- }
- checkBeanExists(ref.getName());
- }
- }
- }
- }
- }
- }
- /**
- * 检查interceptor-stack引用是否存在
- *
- * @param name
- */
- private static void checkRefStack(String name) {
- if (!strutsInterceptor.getInterceptorStacks().containsKey(name)) {
- throw new RuntimeException("找不到name为" + name
- + "对应的interceptor-stack定义!");
- }
- }
- /**
- * 检查default-interceptor引用是否存在
- *
- * @param name
- */
- private static void checkRefDefault(String name) {
- if (!strutsInterceptor.getDefaultInterceptors().containsKey(name)) {
- throw new RuntimeException("找不到name为:" + name
- + "的default-interceptor定义!");
- }
- }
- /**
- * 检查default-interceptor引用是否存在
- *
- * @param exclude
- */
- private static void checkRefDefault(ExcludeDefaultModel exclude) {
- if (exclude == null) {
- return;
- }
- List list = exclude.getExcludes();
- if (list == null || list.size() == 0) {
- return;
- } else {
- for (int i = 0; i < list.size(); i++) {
- String name = (String) list.get(i);
- checkRefDefault(name);
- }
- }
- }
- /**
- * 读配置文件
- */
- private static void readConfig() {
- Digester digester = new Digester();
- StrutsInterceptorModel strutsInterceptorModel = new StrutsInterceptorModel();
- digester.push(strutsInterceptorModel);
- addDigesterRules(digester);
- try {
- File srcfile = new File(absolutePath);
- digester.parse(srcfile);
- } catch (IOException e) {
- logger.error("In readConfig(), Exception Occured ! Info :"
- + e.getLocalizedMessage());
- throw new RuntimeException("Error reading input file:"
- + e.getLocalizedMessage());
- } catch (SAXException e) {
- logger.error("In readConfig(), Exception Occured ! Info :"
- + e.getLocalizedMessage());
- throw new RuntimeException("Error parsing input file:"
- + e.getLocalizedMessage());
- }
- strutsInterceptor = strutsInterceptorModel;
- }
- /**
- * 添加Digester Rules
- *
- * @param d
- */
- private static void addDigesterRules(Digester d) {
- // order-interceptor
- d.addObjectCreate("struts-interceptor/config/order-interceptor",
- OrderInterceptorModel.class);
- d.addSetProperties("struts-interceptor/config/order-interceptor");
- d.addSetNext("struts-interceptor/config/order-interceptor",
- "setOrderInterceptor");
- // default-interceptors
- d.addObjectCreate(
- "struts-interceptor/default-interceptors/default-interceptor",
- DefaultInterceptorModel.class);
- d
- .addSetProperties("struts-interceptor/default-interceptors/default-interceptor");
- d.addSetNext(
- "struts-interceptor/default-interceptors/default-interceptor",
- "addDefaultInterceptor");
- // interceptor-stacks
- d.addObjectCreate(
- "struts-interceptor/interceptor-stacks/interceptor-stack",
- InterceptorStackModel.class);
- d
- .addSetProperties("struts-interceptor/interceptor-stacks/interceptor-stack");
- d.addSetNext("struts-interceptor/interceptor-stacks/interceptor-stack",
- "addInterceptorStackModel");
- d
- .addObjectCreate(
- "struts-interceptor/interceptor-stacks/interceptor-stack/interceptor-ref",
- InterceptorRefModel.class);
- d
- .addSetProperties("struts-interceptor/interceptor-stacks/interceptor-stack/interceptor-ref");
- d
- .addSetNext(
- "struts-interceptor/interceptor-stacks/interceptor-stack/interceptor-ref",
- "addInterceptorRef");
- // action-interceptors
- d.addObjectCreate("struts-interceptor/action-interceptors/action",
- ActionModel.class);
- d.addSetProperties("struts-interceptor/action-interceptors/action");
- d.addSetNext("struts-interceptor/action-interceptors/action",
- "addAction");
- d
- .addObjectCreate(
- "struts-interceptor/action-interceptors/action/exclude-default",
- ExcludeDefaultModel.class);
- d
- .addSetProperties("struts-interceptor/action-interceptors/action/exclude-default");
- d
- .addSetNext(
- "struts-interceptor/action-interceptors/action/exclude-default",
- "setExcludeDefault");
- d
- .addObjectCreate(
- "struts-interceptor/action-interceptors/action/exclude-default/exclude",
- ExcludeModel.class);
- d
- .addSetProperties("struts-interceptor/action-interceptors/action/exclude-default/exclude");
- d
- .addSetNext(
- "struts-interceptor/action-interceptors/action/exclude-default/exclude",
- "addExclude");
- d.addObjectCreate(
- "struts-interceptor/action-interceptors/action/method",
- MethodModel.class);
- d
- .addSetProperties("struts-interceptor/action-interceptors/action/method");
- d.addSetNext("struts-interceptor/action-interceptors/action/method",
- "addMothod");
- d
- .addObjectCreate(
- "struts-interceptor/action-interceptors/action/method/exclude-default",
- ExcludeDefaultModel.class);
- d
- .addSetProperties("struts-interceptor/action-interceptors/action/method/exclude-default");
- d
- .addSetNext(
- "struts-interceptor/action-interceptors/action/method/exclude-default",
- "setExcludeDefault");
- d
- .addObjectCreate(
- "struts-interceptor/action-interceptors/action/method/interceptor-ref",
- InterceptorRefModel.class);
- d
- .addSetProperties("struts-interceptor/action-interceptors/action/method/interceptor-ref");
- d
- .addSetNext(
- "struts-interceptor/action-interceptors/action/method/interceptor-ref",
- "addInterceptorRef");
- d
- .addObjectCreate(
- "struts-interceptor/action-interceptors/action/method/stack-ref",
- StackRefModel.class);
- d
- .addSetProperties("struts-interceptor/action-interceptors/action/method/stack-ref");
- d
- .addSetNext(
- "struts-interceptor/action-interceptors/action/method/stack-ref",
- "addStackRef");
- }
- /**
- * <p>
- * Title:PathNMethod
- * </p>
- * <p>
- * Description:模型类
- * </p>
- * <p>
- * Copyright:Copyright (c) 北京邮电大学网络教育技术研究所 (Institute of Cyber-Educational
- * Technology of Beijing University of Posts and
- * Telecommunications),[[url]www.bupticet.com][/url] 2005
- * </p>
- * <p>
- * Company:北京邮电大学网络教育技术研究所 (Institute of Cyber-Educational Technology of
- * Beijing University of Posts and Telecommunications),[[url]www.bupticet.com][/url]
- * </p>
- *
- * @author LJ-silver
- * @version
- */
- static class PathNMethod extends AbstractBaseObject {
- /**
- *
- */
- private static final long serialVersionUID = 3257281422610674232L;
- private String path;
- private String method;
- /**
- *
- */
- public PathNMethod() {
- super();
- }
- /**
- * @param path
- * @param method
- */
- public PathNMethod(String path, String method) {
- super();
- this.path = path;
- this.method = method;
- }
- /**
- * @return Returns the method.
- */
- public String getMethod() {
- return method;
- }
- /**
- * @param method
- * The method to set.
- */
- public void setMethod(String method) {
- this.method = method;
- }
- /**
- * @return Returns the path.
- */
- public String getPath() {
- return path;
- }
- /**
- * @param path
- * The path to set.
- */
- public void setPath(String path) {
- this.path = path;
- }
- }
- /**
- * for test
- *
- * @param args
- */
- public static void main(String[] args) {
- initResource();
- StrutsInterceptorModel strutsInterceptorModel = strutsInterceptor;
- Map map = methodConfig;
- System.out.println(strutsInterceptorModel.toString());
- System.out.println(map.size());
- }
- }
复制代码 |
|