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.template;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.Collections;
023    import java.util.HashSet;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Set;
027    import java.util.concurrent.ConcurrentHashMap;
028    
029    /**
030     * @author Tina Tian
031     * @author Raymond Augé
032     */
033    public class TemplateManagerUtil {
034    
035            public static void destroy() {
036                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
037    
038                    for (TemplateManager templateManager : templateManagers.values()) {
039                            templateManager.destroy();
040                    }
041    
042                    templateManagers.clear();
043            }
044    
045            public static void destroy(ClassLoader classLoader) {
046                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
047    
048                    for (TemplateManager templateManager : templateManagers.values()) {
049                            templateManager.destroy(classLoader);
050                    }
051            }
052    
053            public static Set<String> getSupportedLanguageTypes(String propertyKey) {
054                    Set<String> supportedLanguageTypes = _supportedLanguageTypes.get(
055                            propertyKey);
056    
057                    if (supportedLanguageTypes != null) {
058                            return supportedLanguageTypes;
059                    }
060    
061                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
062    
063                    supportedLanguageTypes = new HashSet<String>();
064    
065                    for (String templateManagerName : templateManagers.keySet()) {
066                            String content = PropsUtil.get(
067                                    propertyKey, new Filter(templateManagerName));
068    
069                            if (Validator.isNotNull(content)) {
070                                    supportedLanguageTypes.add(templateManagerName);
071                            }
072                    }
073    
074                    supportedLanguageTypes = Collections.unmodifiableSet(
075                            supportedLanguageTypes);
076    
077                    _supportedLanguageTypes.put(propertyKey, supportedLanguageTypes);
078    
079                    return supportedLanguageTypes;
080            }
081    
082            public static Template getTemplate(
083                            String templateManagerName, TemplateResource templateResource,
084                            TemplateContextType templateContextType)
085                    throws TemplateException {
086    
087                    TemplateManager templateManager = _getTemplateManager(
088                            templateManagerName);
089    
090                    return templateManager.getTemplate(
091                            templateResource, templateContextType);
092            }
093    
094            public static Template getTemplate(
095                            String templateManagerName, TemplateResource templateResource,
096                            TemplateResource errorTemplateResource,
097                            TemplateContextType templateContextType)
098                    throws TemplateException {
099    
100                    TemplateManager templateManager = _getTemplateManager(
101                            templateManagerName);
102    
103                    return templateManager.getTemplate(
104                            templateResource, errorTemplateResource, templateContextType);
105            }
106    
107            public static TemplateManager getTemplateManager(
108                    String templateManagerName) {
109    
110                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
111    
112                    return templateManagers.get(templateManagerName);
113            }
114    
115            public static Set<String> getTemplateManagerNames() {
116                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
117    
118                    return templateManagers.keySet();
119            }
120    
121            public static Map<String, TemplateManager> getTemplateManagers() {
122                    return Collections.unmodifiableMap(_getTemplateManagers());
123            }
124    
125            public static boolean hasTemplateManager(String templateManagerName) {
126                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
127    
128                    return templateManagers.containsKey(templateManagerName);
129            }
130    
131            public static void init() throws TemplateException {
132                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
133    
134                    for (TemplateManager templateManager : templateManagers.values()) {
135                            templateManager.init();
136                    }
137            }
138    
139            public static void registerTemplateManager(TemplateManager templateManager)
140                    throws TemplateException {
141    
142                    templateManager.init();
143    
144                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
145    
146                    templateManagers.put(templateManager.getName(), templateManager);
147            }
148    
149            public static void unregisterTemplateManager(String templateManagerName) {
150                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
151    
152                    TemplateManager templateManager = templateManagers.remove(
153                            templateManagerName);
154    
155                    if (templateManager != null) {
156                            templateManager.destroy();
157                    }
158            }
159    
160            public void setTemplateManagers(List<TemplateManager> templateManagers) {
161                    PortalRuntimePermission.checkSetBeanProperty(getClass());
162    
163                    Map<String, TemplateManager> templateManagersMap =
164                            _getTemplateManagers();
165    
166                    for (TemplateManager templateManager : templateManagers) {
167                            templateManagersMap.put(templateManager.getName(), templateManager);
168                    }
169            }
170    
171            private static TemplateManager _getTemplateManager(
172                            String templateManagerName)
173                    throws TemplateException {
174    
175                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
176    
177                    TemplateManager templateManager = templateManagers.get(
178                            templateManagerName);
179    
180                    if (templateManager == null) {
181                            throw new TemplateException(
182                                    "Unsupported template manager " + templateManagerName);
183                    }
184    
185                    return templateManager;
186            }
187    
188            private static Map<String, TemplateManager> _getTemplateManagers() {
189                    PortalRuntimePermission.checkGetBeanProperty(TemplateManagerUtil.class);
190    
191                    return _templateManagers;
192            }
193    
194            private static Map<String, Set<String>> _supportedLanguageTypes =
195                    new ConcurrentHashMap<String, Set<String>>();
196            private static Map<String, TemplateManager> _templateManagers =
197                    new ConcurrentHashMap<String, TemplateManager>();
198    
199    }