All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.kernel.template.TemplateManagerUtil Maven / Gradle / Ivy

Go to download

Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.

There is a newer version: 7.0.0-nightly
Show newest version
/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.template;

import com.liferay.portal.kernel.configuration.Filter;
import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.Validator;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Tina Tian
 * @author Raymond Augé
 */
public class TemplateManagerUtil {

	public static void destroy() {
		Map templateManagers = _getTemplateManagers();

		for (TemplateManager templateManager : templateManagers.values()) {
			templateManager.destroy();
		}

		templateManagers.clear();
	}

	public static void destroy(ClassLoader classLoader) {
		Map templateManagers = _getTemplateManagers();

		for (TemplateManager templateManager : templateManagers.values()) {
			templateManager.destroy(classLoader);
		}
	}

	public static Set getSupportedLanguageTypes(String propertyKey) {
		Set supportedLanguageTypes = _supportedLanguageTypes.get(
			propertyKey);

		if (supportedLanguageTypes != null) {
			return supportedLanguageTypes;
		}

		Map templateManagers = _getTemplateManagers();

		supportedLanguageTypes = new HashSet();

		for (String templateManagerName : templateManagers.keySet()) {
			String content = PropsUtil.get(
				propertyKey, new Filter(templateManagerName));

			if (Validator.isNotNull(content)) {
				supportedLanguageTypes.add(templateManagerName);
			}
		}

		supportedLanguageTypes = Collections.unmodifiableSet(
			supportedLanguageTypes);

		_supportedLanguageTypes.put(propertyKey, supportedLanguageTypes);

		return supportedLanguageTypes;
	}

	public static Template getTemplate(
			String templateManagerName, TemplateResource templateResource,
			boolean restricted)
		throws TemplateException {

		TemplateManager templateManager = _getTemplateManager(
			templateManagerName);

		return templateManager.getTemplate(templateResource, restricted);
	}

	public static Template getTemplate(
			String templateManagerName, TemplateResource templateResource,
			TemplateResource errorTemplateResource, boolean restricted)
		throws TemplateException {

		TemplateManager templateManager = _getTemplateManager(
			templateManagerName);

		return templateManager.getTemplate(
			templateResource, errorTemplateResource, restricted);
	}

	public static TemplateManager getTemplateManager(
		String templateManagerName) {

		Map templateManagers = _getTemplateManagers();

		return templateManagers.get(templateManagerName);
	}

	public static Set getTemplateManagerNames() {
		Map templateManagers = _getTemplateManagers();

		return templateManagers.keySet();
	}

	public static Map getTemplateManagers() {
		return Collections.unmodifiableMap(_getTemplateManagers());
	}

	public static boolean hasTemplateManager(String templateManagerName) {
		Map templateManagers = _getTemplateManagers();

		return templateManagers.containsKey(templateManagerName);
	}

	public static void init() throws TemplateException {
		Map templateManagers = _getTemplateManagers();

		for (TemplateManager templateManager : templateManagers.values()) {
			templateManager.init();
		}
	}

	public static void registerTemplateManager(TemplateManager templateManager)
		throws TemplateException {

		templateManager.init();

		Map templateManagers = _getTemplateManagers();

		templateManagers.put(templateManager.getName(), templateManager);
	}

	public static void unregisterTemplateManager(String templateManagerName) {
		Map templateManagers = _getTemplateManagers();

		TemplateManager templateManager = templateManagers.remove(
			templateManagerName);

		if (templateManager != null) {
			templateManager.destroy();
		}
	}

	public void setTemplateManagers(List templateManagers) {
		PortalRuntimePermission.checkSetBeanProperty(getClass());

		Map templateManagersMap =
			_getTemplateManagers();

		for (TemplateManager templateManager : templateManagers) {
			templateManagersMap.put(templateManager.getName(), templateManager);
		}
	}

	private static TemplateManager _getTemplateManager(
			String templateManagerName)
		throws TemplateException {

		Map templateManagers = _getTemplateManagers();

		TemplateManager templateManager = templateManagers.get(
			templateManagerName);

		if (templateManager == null) {
			throw new TemplateException(
				"Unsupported template manager " + templateManagerName);
		}

		return templateManager;
	}

	private static Map _getTemplateManagers() {
		PortalRuntimePermission.checkGetBeanProperty(TemplateManagerUtil.class);

		return _templateManagers;
	}

	private static Map> _supportedLanguageTypes =
		new ConcurrentHashMap>();
	private static Map _templateManagers =
		new ConcurrentHashMap();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy