您的位置:首页 > 运维架构 > Tomcat

tomcat源码阅读的趣事1

2017-03-11 21:19 267 查看
最近在感情上受挫了,然后实在是很无聊了,之后就开始阅读起来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.home和catalina.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进行操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomca string