tomcat源码阅读的趣事1

最近在感情上受挫了,然后实在是很无聊了,之后就开始阅读起来tomcat的源代码了,看到了下面比较搞笑的一幕,不知道大家什么体会:

/**
 * System property replacement in the given string.
 * 我很纳闷啊,这个要是spring的风格,指定是给你一个StringUtils的类给出来这个字符串的操作,
 * 不知道tomcat是什么鬼,这么设计这个代码啊
 * 说句实话啊,不是说针对tomcat什么的,我真呢是觉得tomcat的作者是从c++的模式下切换过来的。这代码就是:故布疑阵
 * 感觉tomcat的作者真是逗啊,本来两行就可以解决的问题,非要自己重新写一遍解析的过程,再说了,这个是在启动的时候,就算是两次字符串的替换
 * 对性能也没有什么影响啊
 *
 * @param str The original string
 * @return the modified string
 */
protected String replace(String str) {
    // Implementation is copied from ClassLoaderLogManager.replace(),
    // but added special processing for catalina.home and catalina.base.
    /**
     * 上面的英文翻译:实现是从ClassLoaderLogManager.replace()拷贝的
     * 但是增加了特殊的过程对于catalina.homecatalina.base
     * 评价:感觉tomcat的作者还是没有spring的作者牛逼啊,这种代码都该随便的拷贝啊,真是不怕被骂啊
     */
    String result = str;
    String tempInfoStr = str;
    tempInfoStr = str.replaceAll("\\$\\{" + Globals.CATALINA_BASE_PROP + "\\}", getCatalinaBase());
    tempInfoStr = tempInfoStr.replaceAll("\\$\\{" + Globals.CATALINA_HOME_PROP + "\\}", getCatalinaHome());
    ShowInfo.printDetailInfo(this, "我自己替换的模式", tempInfoStr);
    int pos_start = str.indexOf("${");
    if (pos_start >= 0) {
        StringBuilder builder = new StringBuilder();
        int pos_end = -1;
        while (pos_start >= 0) {
            builder.append(str, pos_end + 1, pos_start);
            pos_end = str.indexOf('}', pos_start + 2);
            if (pos_end < 0) {
                pos_end = pos_start - 1;
                break;
            }
            String propName = str.substring(pos_start + 2, pos_end);
            String replacement;
            if (propName.length() == 0) {
                replacement = null;
            } else if (Globals.CATALINA_HOME_PROP.equals(propName)) {
                replacement = getCatalinaHome();
            } else if (Globals.CATALINA_BASE_PROP.equals(propName)) {
                replacement = getCatalinaBase();
            } else {
                replacement = System.getProperty(propName);
            }
            if (replacement != null) {
                builder.append(replacement);
            } else {
                builder.append(str, pos_start, pos_end + 1);
            }
            pos_start = str.indexOf("${", pos_end + 1);
        }
        builder.append(str, pos_end + 1, str.length());
        result = builder.toString();
    }
    ShowInfo.printDetailInfo(this, "tomcat自己替换的结果:" + result);
    if (result.equals(tempInfoStr)) {
        ShowInfo.printDetailInfo(this, "确实一般无二");
    }
    return result;
}
我的体会就是:tomcat的作者是不是从c++转过来的,总是喜欢亲自操刀动手解剖string为char进行操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值