1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.servlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
29  import com.liferay.portal.kernel.util.ServerDetector;
30  import com.liferay.portal.kernel.util.StringPool;
31  
32  import java.io.File;
33  
34  import java.util.Set;
35  
36  import javax.servlet.ServletContext;
37  
38  /**
39   * <a href="ServletContextUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class ServletContextUtil {
45  
46      public static long getLastModified(ServletContext servletContext) {
47          return getLastModified(servletContext, StringPool.SLASH);
48      }
49  
50      public static long getLastModified(
51          ServletContext servletContext, String resourcePath) {
52  
53          return getLastModified(servletContext, resourcePath, false);
54      }
55  
56      public static long getLastModified(
57          ServletContext servletContext, String resourcePath, boolean cache) {
58  
59          if (cache) {
60              Long lastModified = (Long)servletContext.getAttribute(
61                  ServletContextUtil.class.getName() + StringPool.PERIOD +
62                      resourcePath);
63  
64              if (lastModified != null) {
65                  return lastModified.longValue();
66              }
67          }
68  
69          long lastModified = 0;
70  
71          Set<String> resourcePaths = servletContext.getResourcePaths(
72              resourcePath);
73  
74          if (resourcePaths != null) {
75              for (String curResourcePath : resourcePaths) {
76                  if (curResourcePath.endsWith(StringPool.SLASH)) {
77                      long curLastModified = getLastModified(
78                          servletContext, curResourcePath);
79  
80                      if (curLastModified > lastModified) {
81                          lastModified = curLastModified;
82                      }
83                  }
84                  else {
85                      String realPath = getRealPath(
86                          servletContext, curResourcePath);
87  
88                      if (realPath == null) {
89                          _log.error(
90                              "Real path for " + curResourcePath + " is null");
91  
92                          continue;
93                      }
94  
95                      File file = new File(realPath);
96  
97                      if (file.lastModified() > lastModified) {
98                          lastModified = file.lastModified();
99                      }
100                 }
101             }
102         }
103 
104         if (cache) {
105             servletContext.setAttribute(
106                 ServletContextUtil.class.getName() + StringPool.PERIOD +
107                     resourcePath,
108                 new Long(lastModified));
109         }
110 
111         return lastModified;
112     }
113 
114     public static String getRealPath(
115         ServletContext servletContext, String path) {
116 
117         String realPath = servletContext.getRealPath(path);
118 
119         if ((realPath == null) && ServerDetector.isWebLogic()) {
120             String rootDir = getRootDir(servletContext);
121 
122             if (path.startsWith(StringPool.SLASH)) {
123                 realPath = rootDir + path.substring(1);
124             }
125             else {
126                 realPath = rootDir + path;
127             }
128 
129             if (!FileUtil.exists(realPath)) {
130                 realPath = null;
131             }
132         }
133 
134         return realPath;
135     }
136 
137     protected static String getRootDir(ServletContext servletContext) {
138         String key = ServletContextUtil.class.getName() + ".rootDir";
139 
140         String rootDir = (String)servletContext.getAttribute(key);
141 
142         if (rootDir == null) {
143             ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
144                 PortletServlet.PORTLET_CLASS_LOADER);
145 
146             if (classLoader == null) {
147                 classLoader = PortalClassLoaderUtil.getClassLoader();
148             }
149 
150             rootDir = WebDirDetector.getRootDir(classLoader);
151 
152             servletContext.setAttribute(key, rootDir);
153         }
154 
155         return rootDir;
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(ServletContextUtil.class);
159 
160 }