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