ͬHostConfigÒ»Ñù£¬ContextConfigÒ²ÊÇÔÚDigester½âÎöserver.xmlµÄʱºòÌí¼Óµ½StandardContextÉϵļàÌýÆ÷£¬ContextConfigÖ÷ÒªÊÇ´¦ÀíwebÓ¦ÓõÄÅäÖÃÎļþ£¬ÏÈ¿´¿´ËüµÄinit·½·¨×öÁËÄÄЩÊ£¿
-
protected void init() {
-
-
Digester contextDigester = createContextDigester();
-
contextDigester.getParser();
-
if (log.isDebugEnabled())
-
log.debug(sm.getString("contextConfig.init"));
-
context.setConfigured(false);
-
ok = true;
-
-
contextConfig(contextDigester);
-
-
createWebXmlDigester(context.getXmlNamespaceAware(),
-
context.getXmlValidation());
-
-
try {
-
fixDocBase();
-
} catch (IOException e) {
-
log.error(sm.getString(
-
"contextConfig.fixDocBase", context.getName()), e);
-
}
-
-
}
1¡¢´¦ÀíContextµÄÁ½¸öĬÈÏÅäÖÃÎļþ£ºconf/context.xmlºÍ/conf/[enginename]/[hostname]/context.xml.default£¬½âÎöµ½contextÖУ»
2¡¢¶Ôwar°ü½øÐÐУÑ飺Ö÷ÒªÊÇУÑéĿ¼½á¹¹£¨ÊÇ·ñÓÐWEB-INFĿ¼£¬ÊÇ·ñÓÐclassesĿ¼ºÍMETA-INFĿ¼µÈ£©
3¡¢¶ÔÓÚûÓнâѹµÄÎļþ»¹»á½«Æä½âѹ£ºÊÇÔÚExpandWarÀàµÄexpand·½·¨ÖÐÍê³ÉµÄ
-
public static String expand(Host host, URL war, String pathname)
-
throws IOException {
-
...
-
-
JarURLConnection juc = (JarURLConnection) war.openConnection();
-
juc.setUseCaches(false);
-
JarFile jarFile = null;
-
InputStream input = null;
-
boolean success = false;
-
try {
-
jarFile = juc.getJarFile();
-
Enumeration<JarEntry> jarEntries = jarFile.entries();
-
while (jarEntries.hasMoreElements()) {
-
JarEntry jarEntry = jarEntries.nextElement();
-
String name = jarEntry.getName();
-
File expandedFile = new File(docBase, name);
-
if (!expandedFile.getCanonicalPath().startsWith(
-
canonicalDocBasePrefix)) {
-
-
}
-
int last = name.lastIndexOf('/');
-
if (last >= 0) {
-
File parent = new File(docBase,
-
name.substring(0, last));
-
if (!parent.mkdirs() && !parent.isDirectory()) {
-
throw new IOException(
-
sm.getString("expandWar.createFailed", parent));
-
}
-
}
-
input = jarFile.getInputStream(jarEntry);
-
expand(input, expandedFile);
-
input.close();
-
} catch (IOException e) {
-
-
} finally {
-
-
}
-
}
(1)¡¢½«warÎļþת»»³ÉjarEntries£¬²¢±éÀú
(2)¡¢Èç¹ûÔÚwebappsÍâÖ±½ÓÅ×Òì³£
(3)¡¢Èç¹ûΪĿ¼£¬¾ÍÔÚ¶ÔӦλÖô´½¨Ä¿Â¼£¬·ñÔò¾Í½«Îļþ¶ÁÈëinputÖУ¬µ÷ÓÃexpand(input, expandedFile·½·¨½«ÎļþÄÚÈÝcopyµ½½âѹºóµÄĿ¼ÖÐ
-
private static void expand(InputStream input, File file)
-
throws IOException {
-
BufferedOutputStream output = null;
-
try {
-
output =
-
new BufferedOutputStream(new FileOutputStream(file));
-
byte buffer[] = new byte[2048];
-
while (true) {
-
int n = input.read(buffer);
-
if (n <= 0)
-
break;
-
output.write(buffer, 0, n);
-
}
-
} finally {
-
if (output != null) {
-
try {
-
output.close();
-
} catch (IOException e) {
-
-
}
-
}
-
}
-
}
¾ÍÊÇÒ»¸öIO²Ù×÷£¬Ã¿´Î¶ÁÈ¡2kÊý¾Ýµ½ÎļþÖС£
˳×ÅÉúÃüÖÜÆÚ£¬initºó¾ÍÓ¦¸ÃÊÇconfigureStart£¬configureStart·½·¨ÖÐ×öµÄ×îÖØÒªµÄÒ»¼þʾÍÊÇÅäÖÃweb.xmlÎļþ£¬ÊµÏÖϸ½ÚÔÚwebConfigÖÐÍê³ÉµÄ£º
1¡¢É¨Ãè/META-INF/lib/Ŀ¼ÏµÄjarÎļþ£¬Èç¹ûÔÚMETA-INFϺ¬ÓÐweb-fragment.xmlÎļþ£¬½âÎöËü£»
2¡¢È·¶¨È·¶¨ÕâЩxmlƬ¶ÎµÄ˳Ðò,servlet 3.0¿ÉÒÔ½«ÅäÖÃÎļþ·ÖÉ¢ÔÚ¶à¸öjar°üÀïÃ棬¶øÇÒ»¹¿ÉÒÔ¶¨ÒåÕâЩÅäÖÃÎļþµÄ˳Ðò¡£·ÖΪ¾ø¶Ô˳ÐòºÍÏà¶Ô˳Ðò£¬¾ø¶Ô˳ÐòÊÇͨ¹ýabsolute-ordering±êÇ©¶¨ÒåµÄ
<web-app>
<name>...</name>
<absolute-ordering>
<name>fragment1</name>
<name>fragment2</name>
</absolute-ordering>
</web-app>
»¹¿ÉÒÔÔÚweb-fragment.xmlÀïÃæͨ¹ýbefore,after±êÇ©À´¶¨ÒåÕâЩÅäÖÃÎļþµÄÏȺó˳Ðò£¬ÕâÀï²»ÔÙ¾ÙÀý
Õâ²½Ö÷ÒªÊǸù¾Ý˳Ðò£¬½«ÕâЩÅäÖÃÎļþ¼Óµ½¼¯ºÏorderedFragmentsÖÐ
3¡¢´¦ÀíServletContainerInitializersµÄʵÏÖÀ࣬ÕâÒ²ÊÇservlet 3.0ÐÂÔöµÄÌØÐÔ£¬ÈÝÆ÷ÔÚÆô¶¯Ê±Ê¹Óà JAR ·þÎñ API(JAR Service API) À´·¢ÏÖ ServletContainerInitializer µÄʵÏÖÀ࣬²¢ÇÒÈÝÆ÷½« WEB-INF/lib Ŀ¼Ï JAR °üÖеÄÀ඼½»¸ø¸ÃÀàµÄ onStartup() ·½·¨´¦Àí£¬ÎÒÃÇͨ³£ÐèÒªÔÚ¸ÃʵÏÖÀàÉÏʹÓà @HandlesTypes ×¢½âÀ´Ö¸¶¨Ï£Íû±»´¦ÀíµÄÀ࣬¹ýÂ˵ô²»Ï£Íû¸ø onStartup() ´¦ÀíµÄÀà¡£ÔÚonStartup·½·¨ÖпÉÒÔÓÅÏȼÓÔØÕâЩÀ࣬»òÕßÐÞ¸ÄÆäÖеķ½·¨µÈ¡£Õâ²½Ö÷ÒªÊÇ°ÑÕâЩÀàÕÒµ½·Åµ½Set<ServletContainerInitializer> scisÖУ»
4¡¢½«Ó¦ÓÃÖеÄweb.xmlÓëorderedFragments½øÐкϲ¢£¬ºÏ²¢ÔÚWebXmlÀàµÄmerge·½·¨ÖÐʵÏÖ
5¡¢½«Ó¦ÓÃÖеÄweb.xmlÓëÈ«¾ÖµÄweb.xmlÎļþ£¨conf/web.xmlºÍweb.xml.default£©½øÐкϲ¢
6¡¢Óúϲ¢ºÃµÄWebXmlÀ´ÅäÖÃContext£¬ÕâÒ»²½ÔÚ´¦Àíservletʱ£¬»áΪÿ¸öservlet´´½¨Ò»¸öwrapper£¬²¢µ÷ÓÃaddChild½«Ã¿¸öwrapper×÷Ϊcontext×ÓÈÝÆ÷,ºóÐø·ÖÎö
ÏÂÃæ¼òµ¥Á˽âÒ»ÏÂWebXmlµÄmerge·½·¨µÄʵÏÖ£¬WebXmlÀàÊÇweb.xmlÅäÖÃÎļþÀ࣬ÀïÃæ°üº¬web.xmlµÄËùÓбêÇ©£¬ÔÚËüÀïÃæά»¤ÁËÕâЩ±êÇ©µÄ˳Ðò£¬merge·½·¨ÊµÏÖÈçÏ£º
-
public boolean merge(Set<WebXml> fragments) {
-
-
-
-
-
-
WebXml temp = new WebXml();
-
-
for (WebXml fragment : fragments) {
-
if (!mergeMap(fragment.getContextParams(), contextParams,
-
temp.getContextParams(), fragment, "Context Parameter")) {
-
return false;
-
}
-
}
-
contextParams.putAll(temp.getContextParams());
-
...
-
}
-
private static <T> boolean mergeMap(Map<String,T> fragmentMap,
-
Map<String,T> mainMap, Map<String,T> tempMap, WebXml fragment,
-
String mapName) {
-
for (Entry<String, T> entry : fragmentMap.entrySet()) {
-
final String key = entry.getKey();
-
if (!mainMap.containsKey(key)) {
-
-
T value = entry.getValue();
-
if (tempMap.containsKey(key)) {
-
if (value != null && !value.equals(
-
tempMap.get(key))) {
-
log.error(sm.getString(
-
"webXml.mergeConflictString",
-
mapName,
-
key,
-
fragment.getName(),
-
fragment.getURL()));
-
return false;
-
}
-
} else {
-
tempMap.put(key, value);
-
}
-
}
-
}
-
return true;
-
}
»ù±¾ÉÏÿ¸ö±êÇ©¶¼ÊÇ°´ÉÏÃæµÄÀý×Ӻϲ¢µÄ£¬¼òµ¥ËµÒ»ÏÂÕâ¸öËã·¨:
ÓÐÁ½¸ömap£ºmapA,mapB,Òª½«mapAÖÐÓеÄÔÚmapBÖÐûÓеÄÔªËؼӵ½mapBÖУ¬ËüµÄ×ö·¨ÊDZéÀúmapA,Èç¹ûûÓÐÔÚmapBÖоͽ«Æä·ÅÈëtmpMapÖУ¬Ñ»·ÍêºómapB.putAll(tmpMap).
(ÔðÈα༣ºIT) |