001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.net.URL;
031    
032    import javax.servlet.ServletContext;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class ClassPathUtil {
038    
039            public static String getGlobalClassPath() {
040                    return _globalClassPath;
041            }
042    
043            public static String getPortalClassPath() {
044                    return _portalClassPath;
045            }
046    
047            public static void initializeClassPaths(ServletContext servletContext) {
048                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
049    
050                    if (classLoader == null) {
051                            _log.error("Portal ClassLoader is null");
052    
053                            return;
054                    }
055    
056                    _globalClassPath = _buildClassPath(
057                            classLoader, PortalException.class.getName());
058    
059                    StringBundler sb = new StringBundler(5);
060    
061                    sb.append(_globalClassPath);
062                    sb.append(File.pathSeparator);
063                    sb.append(
064                            _buildClassPath(
065                                    classLoader, "com.liferay.portal.servlet.MainServlet"));
066                    sb.append(File.pathSeparator);
067                    sb.append(servletContext.getRealPath("").concat("/WEB-INF/classes"));
068    
069                    _portalClassPath = sb.toString();
070            }
071    
072            private static String _buildClassPath(
073                    ClassLoader classloader, String className) {
074    
075                    String pathOfClass = StringUtil.replace(
076                            className, CharPool.PERIOD, CharPool.SLASH);
077    
078                    pathOfClass = pathOfClass.concat(".class");
079    
080                    URL url = classloader.getResource(pathOfClass);
081    
082                    String path = URLCodec.decodeURL(url.getPath());
083    
084                    if (ServerDetector.isWebLogic()) {
085                            String protocol = url.getProtocol();
086    
087                            if (protocol.equals("zip")) {
088                                    path = "file:".concat(path);
089                            }
090                    }
091    
092                    if (ServerDetector.isJBoss()) {
093                            path = StringUtil.replace(
094                                    path, CharPool.BACK_SLASH, CharPool.SLASH);
095    
096                            String protocol = url.getProtocol();
097    
098                            if (protocol.equals("vfs")) {
099                                    int pos = path.indexOf(".jar/");
100    
101                                    if (pos != -1) {
102                                            String jarFilePath = path.substring(0, pos + 4);
103    
104                                            File jarFile = new File(jarFilePath);
105    
106                                            if (jarFile.isFile()) {
107                                                    path = jarFilePath + '!' + path.substring(pos + 4);
108                                            }
109                                    }
110    
111                                    path = "file:".concat(path);
112                            }
113                    }
114    
115                    File dir = null;
116    
117                    int pos = -1;
118    
119                    if (!path.startsWith("file:") ||
120                            ((pos = path.indexOf(CharPool.EXCLAMATION)) == -1)) {
121    
122                            if (!path.endsWith(pathOfClass)) {
123                                    _log.error(
124                                            "Class " + className + " is not loaded from a JAR file");
125    
126                                    return StringPool.BLANK;
127                            }
128    
129                            String classesDirName =
130                                    path.substring(0, path.length() - pathOfClass.length());
131    
132                            if (!classesDirName.endsWith("/WEB-INF/classes/")) {
133                                    _log.error(
134                                            "Class " + className + " is not loaded from a standard " +
135                                                    "location (/WEB-INF/classes)");
136    
137                                    return StringPool.BLANK;
138                            }
139    
140                            String libDirName = classesDirName.substring(
141                                    0, classesDirName.length() - "classes/".length());
142    
143                            libDirName += "/lib";
144    
145                            dir = new File(libDirName);
146                    }
147                    else {
148                            pos = path.lastIndexOf(CharPool.SLASH, pos);
149    
150                            dir = new File(path.substring("file:".length(), pos));
151                    }
152    
153                    if (!dir.isDirectory()) {
154                            _log.error(dir.toString() + " is not a directory");
155    
156                            return StringPool.BLANK;
157                    }
158    
159                    File[] files = dir.listFiles();
160    
161                    StringBundler sb = new StringBundler(files.length * 2);
162    
163                    for (File file : files) {
164                            sb.append(file.getAbsolutePath());
165                            sb.append(File.pathSeparator);
166                    }
167    
168                    sb.setIndex(sb.index() - 1);
169    
170                    return sb.toString();
171            }
172    
173            private static Log _log = LogFactoryUtil.getLog(ClassPathUtil.class);
174    
175            private static String _globalClassPath;
176            private static String _portalClassPath;
177    
178    }