001    /**
002     * Copyright (c) 2000-2010 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.servlet;
016    
017    import com.liferay.portal.kernel.util.ClassUtil;
018    
019    /**
020     * @author Brian Wing Shun Chan
021     */
022    public class WebDirDetector {
023    
024            public static String getLibDir(ClassLoader classLoader) {
025                    String libDir = ClassUtil.getParentPath(
026                            classLoader, "com.liferay.util.bean.PortletBeanLocatorUtil");
027    
028                    if (libDir.endsWith("/WEB-INF/classes/")) {
029                            libDir = libDir.substring(0, libDir.length() - 8) + "lib/";
030                    }
031                    else {
032                            int pos = libDir.indexOf("/WEB-INF/lib/");
033    
034                            if (pos != -1) {
035                                    libDir = libDir.substring(0, pos) + "/WEB-INF/lib/";
036                            }
037                    }
038    
039                    return libDir;
040            }
041    
042            public static String getRootDir(ClassLoader classLoader) {
043                    return getRootDir(getLibDir(classLoader));
044            }
045    
046            public static String getRootDir(String libDir) {
047                    String rootDir = libDir;
048    
049                    if (rootDir.endsWith("/WEB-INF/lib/")) {
050                            rootDir = rootDir.substring(0, rootDir.length() - 12);
051                    }
052    
053                    return rootDir;
054            }
055    
056    }