001
014
015 package com.liferay.portal.kernel.process;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
022 import com.liferay.portal.kernel.util.ServerDetector;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.URLCodec;
027
028 import java.io.File;
029
030 import java.lang.reflect.Method;
031
032 import java.net.URL;
033 import java.net.URLConnection;
034
035 import javax.servlet.ServletContext;
036 import javax.servlet.ServletException;
037
038
041 public class ClassPathUtil {
042
043 public static String getGlobalClassPath() {
044 return _globalClassPath;
045 }
046
047 public static String getPortalClassPath() {
048 return _portalClassPath;
049 }
050
051 public static void initializeClassPaths(ServletContext servletContext) {
052 ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
053
054 if (classLoader == null) {
055 _log.error("Portal ClassLoader is null");
056
057 return;
058 }
059
060 StringBundler sb = new StringBundler(7);
061
062 String appServerGlobalClassPath = _buildClassPath(
063 classLoader, ServletException.class.getName());
064
065 sb.append(appServerGlobalClassPath);
066 sb.append(File.pathSeparator);
067
068 String portalGlobalClassPath = _buildClassPath(
069 classLoader, PortalException.class.getName());
070
071 sb.append(portalGlobalClassPath);
072
073 _globalClassPath = sb.toString();
074
075 sb.append(File.pathSeparator);
076 sb.append(
077 _buildClassPath(
078 classLoader, "com.liferay.portal.servlet.MainServlet"));
079 sb.append(File.pathSeparator);
080 sb.append(servletContext.getRealPath("").concat("/WEB-INF/classes"));
081
082 _portalClassPath = sb.toString();
083 }
084
085 private static String _buildClassPath(
086 ClassLoader classloader, String className) {
087
088 String pathOfClass = StringUtil.replace(
089 className, CharPool.PERIOD, CharPool.SLASH);
090
091 pathOfClass = pathOfClass.concat(".class");
092
093 URL url = classloader.getResource(pathOfClass);
094
095 if (_log.isDebugEnabled()) {
096 _log.debug("Build class path from " + url);
097 }
098
099 String protocol = url.getProtocol();
100
101 if (protocol.equals("bundle") || protocol.equals("bundleresource")) {
102 try {
103 URLConnection urlConnection = url.openConnection();
104
105 Class<?> clazz = urlConnection.getClass();
106
107 Method getLocalURLMethod = clazz.getDeclaredMethod(
108 "getLocalURL");
109
110 getLocalURLMethod.setAccessible(true);
111
112 url = (URL)getLocalURLMethod.invoke(urlConnection);
113 }
114 catch (Exception e) {
115 _log.error("Unable to resolve local URL from bundle", e);
116
117 return StringPool.BLANK;
118 }
119 }
120
121 String path = URLCodec.decodeURL(url.getPath());
122
123 if (_log.isDebugEnabled()) {
124 _log.debug("Path " + path);
125 }
126
127 path = StringUtil.replace(path, CharPool.BACK_SLASH, CharPool.SLASH);
128
129 if (_log.isDebugEnabled()) {
130 _log.debug("Decoded path " + path);
131 }
132
133 if (ServerDetector.isWebLogic() && protocol.equals("zip")) {
134 path = "file:".concat(path);
135 }
136
137 if (ServerDetector.isJBoss() &&
138 (protocol.equals("vfs") || protocol.equals("vfsfile"))) {
139
140 int pos = path.indexOf(".jar/");
141
142 if (pos != -1) {
143 String jarFilePath = path.substring(0, pos + 4);
144
145 File jarFile = new File(jarFilePath);
146
147 if (jarFile.isFile()) {
148 path = jarFilePath + '!' + path.substring(pos + 4);
149 }
150 }
151
152 path = "file:".concat(path);
153 }
154
155 File dir = null;
156
157 int pos = -1;
158
159 if (!path.startsWith("file:") ||
160 ((pos = path.indexOf(CharPool.EXCLAMATION)) == -1)) {
161
162 if (!path.endsWith(pathOfClass)) {
163 _log.error(
164 "Class " + className + " is not loaded from a JAR file");
165
166 return StringPool.BLANK;
167 }
168
169 String classesDirName = path.substring(
170 0, path.length() - pathOfClass.length());
171
172 if (!classesDirName.endsWith("/WEB-INF/classes/")) {
173 _log.error(
174 "Class " + className + " is not loaded from a standard " +
175 "location (/WEB-INF/classes)");
176
177 return StringPool.BLANK;
178 }
179
180 String libDirName = classesDirName.substring(
181 0, classesDirName.length() - "classes/".length());
182
183 libDirName += "/lib";
184
185 dir = new File(libDirName);
186 }
187 else {
188 pos = path.lastIndexOf(CharPool.SLASH, pos);
189
190 dir = new File(path.substring("file:".length(), pos));
191 }
192
193 if (!dir.isDirectory()) {
194 _log.error(dir.toString() + " is not a directory");
195
196 return StringPool.BLANK;
197 }
198
199 File[] files = dir.listFiles();
200
201 StringBundler sb = new StringBundler(files.length * 2);
202
203 for (File file : files) {
204 sb.append(file.getAbsolutePath());
205 sb.append(File.pathSeparator);
206 }
207
208 sb.setIndex(sb.index() - 1);
209
210 return sb.toString();
211 }
212
213 private static Log _log = LogFactoryUtil.getLog(ClassPathUtil.class);
214
215 private static String _globalClassPath;
216 private static String _portalClassPath;
217
218 }