/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.kernel.servlet;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Shuyang Zhou
 */
public class ServletContextClassLoaderPool {

	public static ClassLoader getClassLoader(String servletContextName) {
		if (servletContextName == null) {
			return null;
		}

		return _classLoaders.get(servletContextName);
	}

	public static String getServletContextName(ClassLoader classLoader) {
		if (classLoader == null) {
			return null;
		}

		return _servletContextNames.get(classLoader);
	}

	public static void register(
		String servletContextName, ClassLoader classLoader) {

		_classLoaders.put(servletContextName, classLoader);
		_servletContextNames.put(classLoader, servletContextName);
	}

	public static void unregister(String servletContextName) {
		ClassLoader classLoader = _classLoaders.remove(servletContextName);

		if (classLoader != null) {
			_servletContextNames.remove(classLoader);
		}
	}

	private static final Map<String, ClassLoader> _classLoaders =
		new ConcurrentHashMap<>();
	private static final Map<ClassLoader, String> _servletContextNames =
		new ConcurrentHashMap<>();

}