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.util;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    /**
023     * Maps servlet context names to/from the ServletContext's ClassLoader.
024     *
025     * <ul>
026     * <li>
027     * For an unknown context names are mapped to the PortalClassLoader.
028     * </li>
029     * <li>
030     * For an unknown ClassLoaders are mapped to the ROOT context name
031     * {@link com.liferay.portal.kernel.util.StringPool#BLANK}
032     * </li>
033     * </ul>
034     *
035     * @author Shuyang Zhou
036     */
037    public class ClassLoaderPool {
038    
039            public static ClassLoader getClassLoader(String contextName) {
040                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
041    
042                    ClassLoader classLoader = _classLoaders.get(contextName);
043    
044                    if (classLoader == null) {
045                            classLoader = PortalClassLoaderUtil.getClassLoader();
046                    }
047    
048                    return classLoader;
049            }
050    
051            public static String getContextName(ClassLoader classLoader) {
052                    if (classLoader == null) {
053                            return StringPool.BLANK;
054                    }
055    
056                    String contextName = _contextNames.get(classLoader);
057    
058                    if (contextName == null) {
059                            contextName = StringPool.BLANK;
060                    }
061    
062                    return contextName;
063            }
064    
065            public static void register(String contextName, ClassLoader classLoader) {
066                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
067    
068                    _classLoaders.put(contextName, classLoader);
069                    _contextNames.put(classLoader, contextName);
070            }
071    
072            public static void unregister(ClassLoader classLoader) {
073                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
074    
075                    String contextName = _contextNames.remove(classLoader);
076    
077                    if (contextName != null) {
078                            _classLoaders.remove(contextName);
079                    }
080            }
081    
082            public static void unregister(String contextName) {
083                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
084    
085                    ClassLoader classLoader = _classLoaders.remove(contextName);
086    
087                    if (classLoader != null) {
088                            _contextNames.remove(classLoader);
089                    }
090            }
091    
092            private static Map<String, ClassLoader> _classLoaders =
093                    new ConcurrentHashMap<String, ClassLoader>();
094            private static Map<ClassLoader, String> _contextNames =
095                    new ConcurrentHashMap<ClassLoader, String>();
096    
097    }