为UrlRewrite的多个XML配置文件的加载提供模糊匹配

 

 

UrlRewrite的多个XML配置文件的加载提供模糊匹配

 

@for&ever 2009-11-27

 

之前写了一篇文章,《修改UrlRewriter使其由单配置文件到支持多配置文件》,网址http://blog.csdn.net/forandever/archive/2009/08/07/4423808.aspx ,对 UrlRewrite的代码进行了简单修改,

文章中的能够支持多个配置文件的情况。

        

         最近,在项目中使用觉得还是不太方便,每次增加一个配置文件,都需要修改web.xml ,于是,在上次修改的基础上,继续修改代码,增加模糊匹配某个目录下的一定规则的文件的功能。

                  

一、需求:

         能够实现在 web.xml 中通过 * 的方式配置,一次即可配置好一系列的配置文件。例如:

                  

web.xml中如下配置:

        

    <filter>

        <filter-name>UrlRewriteFilter</filter-name>

        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

        <init-param>

            <param-name>confPath</param-name>

            <param-value>/WEB-INF/urlrewrite.xml /WEB-INF/conf/urlrewrite/abc*.xml</param-value>

        </init-param>

    </filter>

 

要能够实现加载文件/WEB-INF/urlrewrite.xml/WEB-INF/conf/urlrewrite/目录下所有文件名匹配abc*.xml格式的文件的功能。

 

二、实现:                     

         具体步骤如下:

                  

1、添加一个类,用来实现文件过滤器功能:

 

写一个XmlFileFilter,用来过滤模糊匹配目录下面的文件,代码如下:

 

import java.io.File;

import java.io.FilenameFilter;

 

/**

 * XML文件过滤器

 * @author for@ever

 * @date 2009-10-20

 */

public class XmlFileFilter implements FilenameFilter{

 

         private String matchPathName;

        

         public XmlFileFilter(String matchPathName) {

                   super();

                   this.matchPathName = matchPathName.replace("*", "(//w+)");

         }

 

         public boolean accept(File dir, String name) {

//               matchPathName = matchPathName.replace("*", "(//w+)");

                   if(name.matches(matchPathName)

//               && name.endsWith(".xml")   

                   ){

                            return true;

                   }

                   return false;

         }

}

 

 

2、修改类UrlRewriteFilter的初始化加载XML方法:

UrlRewriteFilter.java 中,修改 init 方法:

         在方法 public void init(final FilterConfig filterConfig) throws ServletException 中:

        

            String modRewriteConf = filterConfig.getInitParameter("modRewriteConf");

        if (!StringUtils.isBlank(modRewriteConf)) {

            modRewriteStyleConf = "true".equals(StringUtils.trim(modRewriteConf).toLowerCase());

        }

 

        if (!StringUtils.isBlank(confPathStr)) {

            confPath = StringUtils.trim(confPathStr);

        } else {

            confPath = modRewriteStyleConf ? DEFAULT_MOD_REWRITE_STYLE_CONF_PATH : DEFAULT_WEB_CONF_PATH;

        }

       

        //XXXXXXXXXXXXX 我们的代码放在这个位置 XXXXXXXXXXXXXXXXXXXX     

       

        log.debug("confPath set to " + confPath);

 

        // status enabled (default true)

        if (statusEnabledConf != null && !"".equals(statusEnabledConf)) {

            log.debug("statusEnabledConf set to " + statusEnabledConf);

            statusEnabled = "true".equals(statusEnabledConf.toLowerCase());

        }

        

         在上面你的代码段中,中间注释的位置,增加如下的代码:

                      

        String tmpPath = "";

        confPath = replaceALL_TabBrBlank_2Blank(confPath);// 替换web.xml中路径配置的所有的 tab/多个空格/回车 为一个空格

               

      

        for(String path : confPath.split(" ")){   

 

                 // 处理 * ,即目录的情况

                            // path=/urlrewrite/abc*.xml

                            if (path.indexOf("*") != -1) {

                                     int indexLine = path.lastIndexOf("/");

                                     String pathPre = path.substring(0, indexLine); // pathPre==/urlrewrite/

                                     String matchPathName = path.substring(indexLine + 1); // matchPathName ==abc*.xml

 

                     File dirs = new File(context.getRealPath("/") + pathPre);  

                     if(dirs.isDirectory()){

                               String [] dir = dirs.list(new XmlFileFilter(matchPathName));

                               for(String file : dir){

                                        tmpPath += " " + pathPre + "/" + file;

                               }

                     }

            }else{

                     // 普通情况

                     tmpPath += path;

            }

                 tmpPath += " ";

                

        }

       

        confPath = tmpPath;

 

 

3、增加方法replaceALL_TabBrBlank_2Blank,用来替换web.xml中路径配置的所有的 tab/多个空格/回车 为一个空格:

 

         public static String replaceALL_TabBrBlank_2Blank(String str) {

        return str.replaceAll("/n", " ")

                   .replaceAll("/t", " ")

                   .replaceAll("( )+", " ");

    }

        

        

好了,存盘。

        

我们要实现的功能即完成了。

        

 

@forandever 2009-11-27

        

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值