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 (ServerDetector.isWebLogic()) {
124 if (protocol.equals("zip")) {
125 path = "file:".concat(path);
126 }
127 }
128
129 if (ServerDetector.isJBoss()) {
130 path = StringUtil.replace(
131 path, CharPool.BACK_SLASH, CharPool.SLASH);
132
133 if (protocol.equals("vfs")) {
134 int pos = path.indexOf(".jar/");
135
136 if (pos != -1) {
137 String jarFilePath = path.substring(0, pos + 4);
138
139 File jarFile = new File(jarFilePath);
140
141 if (jarFile.isFile()) {
142 path = jarFilePath + '!' + path.substring(pos + 4);
143 }
144 }
145
146 path = "file:".concat(path);
147 }
148 }
149
150 File dir = null;
151
152 int pos = -1;
153
154 if (!path.startsWith("file:") ||
155 ((pos = path.indexOf(CharPool.EXCLAMATION)) == -1)) {
156
157 if (!path.endsWith(pathOfClass)) {
158 _log.error(
159 "Class " + className + " is not loaded from a JAR file");
160
161 return StringPool.BLANK;
162 }
163
164 String classesDirName =
165 path.substring(0, path.length() - pathOfClass.length());
166
167 if (!classesDirName.endsWith("/WEB-INF/classes/")) {
168 _log.error(
169 "Class " + className + " is not loaded from a standard " +
170 "location (/WEB-INF/classes)");
171
172 return StringPool.BLANK;
173 }
174
175 String libDirName = classesDirName.substring(
176 0, classesDirName.length() - "classes/".length());
177
178 libDirName += "/lib";
179
180 dir = new File(libDirName);
181 }
182 else {
183 pos = path.lastIndexOf(CharPool.SLASH, pos);
184
185 dir = new File(path.substring("file:".length(), pos));
186 }
187
188 if (!dir.isDirectory()) {
189 _log.error(dir.toString() + " is not a directory");
190
191 return StringPool.BLANK;
192 }
193
194 File[] files = dir.listFiles();
195
196 StringBundler sb = new StringBundler(files.length * 2);
197
198 for (File file : files) {
199 sb.append(file.getAbsolutePath());
200 sb.append(File.pathSeparator);
201 }
202
203 sb.setIndex(sb.index() - 1);
204
205 return sb.toString();
206 }
207
208 private static Log _log = LogFactoryUtil.getLog(ClassPathUtil.class);
209
210 private static String _globalClassPath;
211 private static String _portalClassPath;
212
213 }