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.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.URLCodec;
026    
027    import java.io.File;
028    
029    import java.net.URL;
030    
031    import javax.servlet.ServletContext;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class ClassPathUtil {
037    
038            public static String getGlobalClassPath() {
039                    return _globalClassPath;
040            }
041    
042            public static String getPortalClassPath() {
043                    return _portalClassPath;
044            }
045    
046            public static void initializeClassPaths(ServletContext servletContext) {
047                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
048    
049                    if (classLoader == null) {
050                            _log.error("Portal ClassLoader is null");
051    
052                            return;
053                    }
054    
055                    _globalClassPath = _buildClassPath(
056                            classLoader, PortalException.class.getName());
057    
058                    StringBundler sb = new StringBundler(5);
059    
060                    sb.append(_globalClassPath);
061                    sb.append(File.pathSeparator);
062                    sb.append(
063                            _buildClassPath(
064                                    classLoader, "com.liferay.portal.servlet.MainServlet"));
065                    sb.append(File.pathSeparator);
066                    sb.append(servletContext.getRealPath("").concat("/WEB-INF/classes"));
067    
068                    _portalClassPath = sb.toString();
069            }
070    
071            private static String _buildClassPath(
072                    ClassLoader classloader, String className) {
073    
074                    String pathOfClass = StringUtil.replace(
075                            className, CharPool.PERIOD, CharPool.SLASH);
076    
077                    pathOfClass = pathOfClass.concat(".class");
078    
079                    URL url = classloader.getResource(pathOfClass);
080    
081                    String path = URLCodec.decodeURL(url.getPath());
082    
083                    File dir = null;
084    
085                    int pos = -1;
086    
087                    if (!path.startsWith("file:") ||
088                            ((pos = path.indexOf(CharPool.EXCLAMATION)) == -1)) {
089    
090                            if (!path.endsWith(pathOfClass)) {
091                                    _log.error(
092                                            "Class " + className + " is not loaded from a JAR file");
093    
094                                    return StringPool.BLANK;
095                            }
096    
097                            String classesDirName =
098                                    path.substring(0, path.length() - pathOfClass.length());
099    
100                            if (!classesDirName.endsWith("/WEB-INF/classes/")) {
101                                    _log.error(
102                                            "Class " + className + " is not loaded from a standard " +
103                                                    "location (/WEB-INF/classes)");
104    
105                                    return StringPool.BLANK;
106                            }
107    
108                            String libDirName = classesDirName.substring(
109                                    0, classesDirName.length() - "classes/".length());
110    
111                            libDirName += "/lib";
112    
113                            dir = new File(libDirName);
114                    }
115                    else {
116                            pos = path.lastIndexOf(CharPool.SLASH, pos);
117    
118                            dir = new File(path.substring("file:".length(), pos));
119                    }
120    
121                    if (!dir.isDirectory()) {
122                            _log.error(dir.toString() + " is not a directory");
123    
124                            return StringPool.BLANK;
125                    }
126    
127                    File[] files = dir.listFiles();
128    
129                    StringBundler sb = new StringBundler(files.length * 2);
130    
131                    for (File file : files) {
132                            sb.append(file.getAbsolutePath());
133                            sb.append(File.pathSeparator);
134                    }
135    
136                    sb.setIndex(sb.index() - 1);
137    
138                    return sb.toString();
139            }
140    
141            private static Log _log = LogFactoryUtil.getLog(ClassPathUtil.class);
142    
143            private static String _globalClassPath;
144            private static String _portalClassPath;
145    
146    }