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.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 servlet context's class loader.
024     *
025     * @author Shuyang Zhou
026     */
027    public class ClassLoaderPool {
028    
029            /**
030             * Returns the class loader associated with the context name.
031             *
032             * <p>
033             * If no class loader is found for the context name, the thread's context
034             * class loader is returned as a fallback.
035             * </p>
036             *
037             * @param  contextName the servlet context's name
038             * @return the class loader associated with the context name
039             */
040            public static ClassLoader getClassLoader(String contextName) {
041                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
042    
043                    ClassLoader classLoader = null;
044    
045                    if ((contextName != null) && !contextName.equals(StringPool.NULL)) {
046                            classLoader = _classLoaders.get(contextName);
047                    }
048    
049                    if (classLoader == null) {
050                            Thread currentThread = Thread.currentThread();
051    
052                            classLoader = currentThread.getContextClassLoader();
053                    }
054    
055                    return classLoader;
056            }
057    
058            /**
059             * Returns the context name associated with the class loader.
060             *
061             * <p>
062             * If the class loader is <code>null</code> or if no context name is
063             * associated with the class loader, {@link
064             * com.liferay.portal.kernel.util.StringPool#<code>NULL</code>} is returned.
065             * </p>
066             *
067             * @param  classLoader the class loader
068             * @return the context name associated with the class loader
069             */
070            public static String getContextName(ClassLoader classLoader) {
071                    if (classLoader == null) {
072                            return StringPool.NULL;
073                    }
074    
075                    String contextName = _contextNames.get(classLoader);
076    
077                    if (contextName == null) {
078                            contextName = StringPool.NULL;
079                    }
080    
081                    return contextName;
082            }
083    
084            public static void register(String contextName, ClassLoader classLoader) {
085                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
086    
087                    _classLoaders.put(contextName, classLoader);
088                    _contextNames.put(classLoader, contextName);
089            }
090    
091            public static void unregister(ClassLoader classLoader) {
092                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
093    
094                    String contextName = _contextNames.remove(classLoader);
095    
096                    if (contextName != null) {
097                            _classLoaders.remove(contextName);
098                    }
099            }
100    
101            public static void unregister(String contextName) {
102                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
103    
104                    ClassLoader classLoader = _classLoaders.remove(contextName);
105    
106                    if (classLoader != null) {
107                            _contextNames.remove(classLoader);
108                    }
109            }
110    
111            private static final Map<String, ClassLoader> _classLoaders =
112                    new ConcurrentHashMap<>();
113            private static final Map<ClassLoader, String> _contextNames =
114                    new ConcurrentHashMap<>();
115    
116    }