001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
039     * @author Shuyang Zhou
040     */
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()) {
134                            if (protocol.equals("zip")) {
135                                    path = "file:".concat(path);
136                            }
137                    }
138    
139                    if (ServerDetector.isJBoss()) {
140                            if (protocol.equals("vfs")) {
141                                    int pos = path.indexOf(".jar/");
142    
143                                    if (pos != -1) {
144                                            String jarFilePath = path.substring(0, pos + 4);
145    
146                                            File jarFile = new File(jarFilePath);
147    
148                                            if (jarFile.isFile()) {
149                                                    path = jarFilePath + '!' + path.substring(pos + 4);
150                                            }
151                                    }
152    
153                                    path = "file:".concat(path);
154                            }
155                    }
156    
157                    File dir = null;
158    
159                    int pos = -1;
160    
161                    if (!path.startsWith("file:") ||
162                            ((pos = path.indexOf(CharPool.EXCLAMATION)) == -1)) {
163    
164                            if (!path.endsWith(pathOfClass)) {
165                                    _log.error(
166                                            "Class " + className + " is not loaded from a JAR file");
167    
168                                    return StringPool.BLANK;
169                            }
170    
171                            String classesDirName = path.substring(
172                                    0, path.length() - pathOfClass.length());
173    
174                            if (!classesDirName.endsWith("/WEB-INF/classes/")) {
175                                    _log.error(
176                                            "Class " + className + " is not loaded from a standard " +
177                                                    "location (/WEB-INF/classes)");
178    
179                                    return StringPool.BLANK;
180                            }
181    
182                            String libDirName = classesDirName.substring(
183                                    0, classesDirName.length() - "classes/".length());
184    
185                            libDirName += "/lib";
186    
187                            dir = new File(libDirName);
188                    }
189                    else {
190                            pos = path.lastIndexOf(CharPool.SLASH, pos);
191    
192                            dir = new File(path.substring("file:".length(), pos));
193                    }
194    
195                    if (!dir.isDirectory()) {
196                            _log.error(dir.toString() + " is not a directory");
197    
198                            return StringPool.BLANK;
199                    }
200    
201                    File[] files = dir.listFiles();
202    
203                    StringBundler sb = new StringBundler(files.length * 2);
204    
205                    for (File file : files) {
206                            sb.append(file.getAbsolutePath());
207                            sb.append(File.pathSeparator);
208                    }
209    
210                    sb.setIndex(sb.index() - 1);
211    
212                    return sb.toString();
213            }
214    
215            private static Log _log = LogFactoryUtil.getLog(ClassPathUtil.class);
216    
217            private static String _globalClassPath;
218            private static String _portalClassPath;
219    
220    }