我们回到MainServlet类的init()方法中,在执行processStartupEvents()之后,我们开始初始化Servlet上下文池:

  1. if (_log.isDebugEnabled()) { 
  2.             _log.debug("Initialize servlet context pool"); 
  3.         } 
  4.  
  5.         try { 
  6.             initServletContextPool(); 
  7.         } 
  8.         catch (Exception e) { 
  9.             _log.error(e, e); 
  10.         } 

它会调用以下方法:

  1. protected void initServletContextPool() throws Exception { 
  2.         ServletContext servletContext = getServletContext(); 
  3.  
  4.         String contextPath = PortalUtil.getPathContext(); 
  5.  
  6.         ServletContextPool.put(contextPath, servletContext); 
  7.     } 

首先,它获取Servlet上下文对象,然后它去取得contextPath.这个字符串是通过PortalUtil.getPathContext()获得,进而调用PortalImpl的getPathContext()方法:

  1. public String getPathContext() { 
  2.     return _pathContext; 

这个值设置在PortalImpl类的构造器中,我们将其解析出来:

  1. .. 
  2. // Paths 
  3. _pathProxy = PropsValues.PORTAL_PROXY_PATH; 
  4. _pathContext = ContextPathUtil.getContextPath(PropsValues.PORTAL_CTX); 
  5. _pathContext = _pathProxy.concat(_pathContext); 
  6. .. 

 

解析_pathProxy=PropsValues.PORTAL_PROXY_PATH:

这个值最终定义在portal.properties中:

  1.    # Set this property if the application server is served behind a proxy and 
  2.    # a prefix needs to be added to the portal servlet context path. This prefix 
  3.    # will also be added to static resources served by layout templates, 
  4.    # portlets, and themes. 
  5.    # 
  6.    portal.proxy.path= 

 

解析_pathContext=ContextPathUtil.getContextPath(PropsValues.PORTAL_CTX): 

先找到PropsValues.PORTAL_CTX的取值,很容易发现它最终定义在portal.properties中:

  1.   # Specify the path of the portal servlet context. This is needed because 
  2.   # javax.servlet.ServletContext did not have access to the context path until 
  3.   # Java EE 5. 
  4.   # 
  5.   # Set this property if you deploy the portal to another path besides root. 
  6.   # 
  7.   portal.ctx=/ 

然后,我们来解析 ContextPathUtil.getContextPath("/"):

对应解析代码为:

  1. public static String getContextPath(String contextPath) { 
  2.         contextPath = GetterUtil.getString(contextPath); 
  3.  
  4.         if ((contextPath.length() == 0) || 
  5.             contextPath.equals(StringPool.SLASH)) { 
  6.  
  7.             contextPath = StringPool.BLANK; 
  8.         } 
  9.         else if (!contextPath.startsWith(StringPool.SLASH)) { 
  10.             contextPath = StringPool.SLASH.concat(contextPath); 
  11.         } 
  12.  
  13.         return contextPath; 
  14.     } 

先看02行,它调用GetterUtil的getString方法:

  1. public static String getString(String value) { 
  2.     return getString(value, DEFAULT_STRING); 

最终调用的代码为:

  1. public static String get(String value, String defaultValue) { 
  2.     if (value == null) { 
  3.         return defaultValue; 
  4.     } 
  5.  
  6.     value = value.trim(); 
  7.  
  8.     if (value.indexOf(CharPool.RETURN) != -1) { 
  9.         value = StringUtil.replace( 
  10.             value, StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE); 
  11.     } 
  12.  
  13.     return value; 

所以,他没有进入任何判断,直接返回"/",

然后我们看03行:

可以发现它匹配contextPath.equals(StringPool.SLASH)),所以最终执行 contextPath = StringPool.BLANK, 也就是把contextPath设为空串。

它就是_pathContext的值。

 

解析_pathContext = _pathProxy.concat(_pathContext); 

因为我们_pathProxy为空,而_pathContext也为空,所以最终结果_pathContext为空。

 

最终,我们在MainServlet的initServletContextPool方法中执行以下语句:

  1. ServletContextPool.put(contextPath, servletContext); 

它就是在ServletContextPool中添加一个key-value对,于是就完成了ServletContextPool的初始化。