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.net.URL;
031
032 import javax.servlet.ServletContext;
033 import javax.servlet.ServletException;
034
035
038 public class ClassPathUtil {
039
040 public static String getGlobalClassPath() {
041 return _globalClassPath;
042 }
043
044 public static String getPortalClassPath() {
045 return _portalClassPath;
046 }
047
048 public static void initializeClassPaths(ServletContext servletContext) {
049 ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
050
051 if (classLoader == null) {
052 _log.error("Portal ClassLoader is null");
053
054 return;
055 }
056
057 StringBundler sb = new StringBundler(7);
058
059 String appServerGlobalClassPath = _buildClassPath(
060 classLoader, ServletException.class.getName());
061
062 sb.append(appServerGlobalClassPath);
063 sb.append(File.pathSeparator);
064
065 String portalGlobalClassPath = _buildClassPath(
066 classLoader, PortalException.class.getName());
067
068 sb.append(portalGlobalClassPath);
069
070 _globalClassPath = sb.toString();
071
072 sb.append(File.pathSeparator);
073 sb.append(
074 _buildClassPath(
075 classLoader, "com.liferay.portal.servlet.MainServlet"));
076 sb.append(File.pathSeparator);
077 sb.append(servletContext.getRealPath("").concat("/WEB-INF/classes"));
078
079 _portalClassPath = sb.toString();
080 }
081
082 private static String _buildClassPath(
083 ClassLoader classloader, String className) {
084
085 String pathOfClass = StringUtil.replace(
086 className, CharPool.PERIOD, CharPool.SLASH);
087
088 pathOfClass = pathOfClass.concat(".class");
089
090 URL url = classloader.getResource(pathOfClass);
091
092 String path = URLCodec.decodeURL(url.getPath());
093
094 if (ServerDetector.isWebLogic()) {
095 String protocol = url.getProtocol();
096
097 if (protocol.equals("zip")) {
098 path = "file:".concat(path);
099 }
100 }
101
102 if (ServerDetector.isJBoss()) {
103 path = StringUtil.replace(
104 path, CharPool.BACK_SLASH, CharPool.SLASH);
105
106 String protocol = url.getProtocol();
107
108 if (protocol.equals("vfs")) {
109 int pos = path.indexOf(".jar/");
110
111 if (pos != -1) {
112 String jarFilePath = path.substring(0, pos + 4);
113
114 File jarFile = new File(jarFilePath);
115
116 if (jarFile.isFile()) {
117 path = jarFilePath + '!' + path.substring(pos + 4);
118 }
119 }
120
121 path = "file:".concat(path);
122 }
123 }
124
125 File dir = null;
126
127 int pos = -1;
128
129 if (!path.startsWith("file:") ||
130 ((pos = path.indexOf(CharPool.EXCLAMATION)) == -1)) {
131
132 if (!path.endsWith(pathOfClass)) {
133 _log.error(
134 "Class " + className + " is not loaded from a JAR file");
135
136 return StringPool.BLANK;
137 }
138
139 String classesDirName =
140 path.substring(0, path.length() - pathOfClass.length());
141
142 if (!classesDirName.endsWith("/WEB-INF/classes/")) {
143 _log.error(
144 "Class " + className + " is not loaded from a standard " +
145 "location (/WEB-INF/classes)");
146
147 return StringPool.BLANK;
148 }
149
150 String libDirName = classesDirName.substring(
151 0, classesDirName.length() - "classes/".length());
152
153 libDirName += "/lib";
154
155 dir = new File(libDirName);
156 }
157 else {
158 pos = path.lastIndexOf(CharPool.SLASH, pos);
159
160 dir = new File(path.substring("file:".length(), pos));
161 }
162
163 if (!dir.isDirectory()) {
164 _log.error(dir.toString() + " is not a directory");
165
166 return StringPool.BLANK;
167 }
168
169 File[] files = dir.listFiles();
170
171 StringBundler sb = new StringBundler(files.length * 2);
172
173 for (File file : files) {
174 sb.append(file.getAbsolutePath());
175 sb.append(File.pathSeparator);
176 }
177
178 sb.setIndex(sb.index() - 1);
179
180 return sb.toString();
181 }
182
183 private static Log _log = LogFactoryUtil.getLog(ClassPathUtil.class);
184
185 private static String _globalClassPath;
186 private static String _portalClassPath;
187
188 }