001    /**
002     * Copyright (c) 2000-2013 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, this method checks for
034             * an initialized portal class loader to return. In cases where this method
035             * is invoked from outside of a running portal, such as from a unit test or
036             * from an external tool, no portal class loader will be found. If no portal
037             * class loader is found, the thread's context class loader is returned as a
038             * fallback.
039             * </p>
040             *
041             * @param  contextName the servlet context's name
042             * @return the class loader associated with the context name
043             */
044            public static ClassLoader getClassLoader(String contextName) {
045                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
046    
047                    ClassLoader classLoader = _classLoaders.get(contextName);
048    
049                    if (classLoader == null) {
050                            classLoader = PortalClassLoaderUtil.getClassLoader();
051                    }
052    
053                    if (classLoader == null) {
054                            Thread currentThread = Thread.currentThread();
055    
056                            classLoader = currentThread.getContextClassLoader();
057                    }
058    
059                    return classLoader;
060            }
061    
062            /**
063             * Returns the context name associated with the class loader.
064             *
065             * <p>
066             * If the class loader is <code>null</code> or if no context name is
067             * associated with the class loader, {@link
068             * com.liferay.portal.kernel.util.StringPool#BLANK} is returned.
069             * </p>
070             *
071             * @param  classLoader the class loader
072             * @return the context name associated with the class loader
073             */
074            public static String getContextName(ClassLoader classLoader) {
075                    if (classLoader == null) {
076                            return StringPool.BLANK;
077                    }
078    
079                    String contextName = _contextNames.get(classLoader);
080    
081                    if (contextName == null) {
082                            contextName = StringPool.BLANK;
083                    }
084    
085                    return contextName;
086            }
087    
088            public static void register(String contextName, ClassLoader classLoader) {
089                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
090    
091                    _classLoaders.put(contextName, classLoader);
092                    _contextNames.put(classLoader, contextName);
093            }
094    
095            public static void unregister(ClassLoader classLoader) {
096                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
097    
098                    String contextName = _contextNames.remove(classLoader);
099    
100                    if (contextName != null) {
101                            _classLoaders.remove(contextName);
102                    }
103            }
104    
105            public static void unregister(String contextName) {
106                    PortalRuntimePermission.checkGetBeanProperty(ClassLoaderPool.class);
107    
108                    ClassLoader classLoader = _classLoaders.remove(contextName);
109    
110                    if (classLoader != null) {
111                            _contextNames.remove(classLoader);
112                    }
113            }
114    
115            private static Map<String, ClassLoader> _classLoaders =
116                    new ConcurrentHashMap<String, ClassLoader>();
117            private static Map<ClassLoader, String> _contextNames =
118                    new ConcurrentHashMap<ClassLoader, String>();
119    
120    }