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                            boolean restricted)
085                    throws TemplateException {
086    
087                    TemplateManager templateManager = _getTemplateManager(
088                            templateManagerName);
089    
090                    return templateManager.getTemplate(templateResource, restricted);
091            }
092    
093            public static Template getTemplate(
094                            String templateManagerName, TemplateResource templateResource,
095                            TemplateResource errorTemplateResource, boolean restricted)
096                    throws TemplateException {
097    
098                    TemplateManager templateManager = _getTemplateManager(
099                            templateManagerName);
100    
101                    return templateManager.getTemplate(
102                            templateResource, errorTemplateResource, restricted);
103            }
104    
105            public static TemplateManager getTemplateManager(
106                    String templateManagerName) {
107    
108                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
109    
110                    return templateManagers.get(templateManagerName);
111            }
112    
113            public static Set<String> getTemplateManagerNames() {
114                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
115    
116                    return templateManagers.keySet();
117            }
118    
119            public static Map<String, TemplateManager> getTemplateManagers() {
120                    return Collections.unmodifiableMap(_getTemplateManagers());
121            }
122    
123            public static boolean hasTemplateManager(String templateManagerName) {
124                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
125    
126                    return templateManagers.containsKey(templateManagerName);
127            }
128    
129            public static void init() throws TemplateException {
130                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
131    
132                    for (TemplateManager templateManager : templateManagers.values()) {
133                            templateManager.init();
134                    }
135            }
136    
137            public static void registerTemplateManager(TemplateManager templateManager)
138                    throws TemplateException {
139    
140                    templateManager.init();
141    
142                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
143    
144                    templateManagers.put(templateManager.getName(), templateManager);
145            }
146    
147            public static void unregisterTemplateManager(String templateManagerName) {
148                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
149    
150                    TemplateManager templateManager = templateManagers.remove(
151                            templateManagerName);
152    
153                    if (templateManager != null) {
154                            templateManager.destroy();
155                    }
156            }
157    
158            public void setTemplateManagers(List<TemplateManager> templateManagers) {
159                    PortalRuntimePermission.checkSetBeanProperty(getClass());
160    
161                    Map<String, TemplateManager> templateManagersMap =
162                            _getTemplateManagers();
163    
164                    for (TemplateManager templateManager : templateManagers) {
165                            templateManagersMap.put(templateManager.getName(), templateManager);
166                    }
167            }
168    
169            private static TemplateManager _getTemplateManager(
170                            String templateManagerName)
171                    throws TemplateException {
172    
173                    Map<String, TemplateManager> templateManagers = _getTemplateManagers();
174    
175                    TemplateManager templateManager = templateManagers.get(
176                            templateManagerName);
177    
178                    if (templateManager == null) {
179                            throw new TemplateException(
180                                    "Unsupported template manager " + templateManagerName);
181                    }
182    
183                    return templateManager;
184            }
185    
186            private static Map<String, TemplateManager> _getTemplateManagers() {
187                    PortalRuntimePermission.checkGetBeanProperty(TemplateManagerUtil.class);
188    
189                    return _templateManagers;
190            }
191    
192            private static Map<String, Set<String>> _supportedLanguageTypes =
193                    new ConcurrentHashMap<String, Set<String>>();
194            private static Map<String, TemplateManager> _templateManagers =
195                    new ConcurrentHashMap<String, TemplateManager>();
196    
197    }