001    /**
002     * Copyright (c) 2000-2012 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.freemarker;
016    
017    import com.liferay.portal.kernel.template.Template;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.template.TemplateContextType;
020    import com.liferay.portal.kernel.template.TemplateException;
021    import com.liferay.portal.kernel.template.TemplateManager;
022    import com.liferay.portal.kernel.template.TemplateResource;
023    import com.liferay.portal.kernel.util.ReflectionUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.template.PACLTemplateWrapper;
026    import com.liferay.portal.template.RestrictedTemplate;
027    import com.liferay.portal.template.TemplateContextHelper;
028    import com.liferay.portal.util.PropsValues;
029    
030    import freemarker.cache.TemplateCache;
031    
032    import freemarker.template.Configuration;
033    
034    import java.lang.reflect.Field;
035    
036    import java.util.Map;
037    
038    /**
039     * @author Mika Koivisto
040     * @author Tina Tina
041     */
042    public class FreeMarkerManager implements TemplateManager {
043    
044            public void destroy() {
045                    if (_configuration == null) {
046                            return;
047                    }
048    
049                    _configuration.clearEncodingMap();
050                    _configuration.clearSharedVariables();
051                    _configuration.clearTemplateCache();
052    
053                    _configuration = null;
054    
055                    _templateContextHelper.removeAllHelperUtilities();
056    
057                    _templateContextHelper = null;
058            }
059    
060            public void destroy(ClassLoader classLoader) {
061                    _templateContextHelper.removeHelperUtilities(classLoader);
062            }
063    
064            public String getName() {
065                    return TemplateConstants.LANG_TYPE_FTL;
066            }
067    
068            public Template getTemplate(
069                    TemplateResource templateResource,
070                    TemplateContextType templateContextType) {
071    
072                    return getTemplate(templateResource, null, templateContextType);
073            }
074    
075            public Template getTemplate(
076                    TemplateResource templateResource,
077                    TemplateResource errorTemplateResource,
078                    TemplateContextType templateContextType) {
079    
080                    Template template = null;
081    
082                    Map<String, Object> context = _templateContextHelper.getHelperUtilities(
083                            templateContextType);
084    
085                    if (templateContextType.equals(TemplateContextType.EMPTY)) {
086                            template = new FreeMarkerTemplate(
087                                    templateResource, errorTemplateResource, null, _configuration,
088                                    _templateContextHelper);
089                    }
090                    else if (templateContextType.equals(TemplateContextType.RESTRICTED)) {
091                            template = new RestrictedTemplate(
092                                    new FreeMarkerTemplate(
093                                            templateResource, errorTemplateResource, context,
094                                            _configuration, _templateContextHelper),
095                                    _templateContextHelper.getRestrictedVariables());
096                    }
097                    else if (templateContextType.equals(TemplateContextType.STANDARD)) {
098                            template = new FreeMarkerTemplate(
099                                    templateResource, errorTemplateResource, context,
100                                    _configuration, _templateContextHelper);
101                    }
102    
103                    return PACLTemplateWrapper.getTemplate(template);
104            }
105    
106            public void init() throws TemplateException {
107                    if (_configuration != null) {
108                            return;
109                    }
110    
111                    _configuration = new Configuration();
112    
113                    try {
114                            Field field = ReflectionUtil.getDeclaredField(
115                                    Configuration.class, "cache");
116    
117                            TemplateCache templateCache = new LiferayTemplateCache(
118                                    _configuration);
119    
120                            field.set(_configuration, templateCache);
121                    }
122                    catch (Exception e) {
123                            throw new TemplateException(
124                                    "Unable to Initialize Freemarker manager");
125                    }
126    
127                    _configuration.setDefaultEncoding(StringPool.UTF8);
128                    _configuration.setLocalizedLookup(
129                            PropsValues.FREEMARKER_ENGINE_LOCALIZED_LOOKUP);
130                    _configuration.setNewBuiltinClassResolver(
131                            new LiferayTemplateClassResolver());
132                    _configuration.setObjectWrapper(new LiferayObjectWrapper());
133    
134                    try {
135                            _configuration.setSetting(
136                                    "auto_import", PropsValues.FREEMARKER_ENGINE_MACRO_LIBRARY);
137                            _configuration.setSetting(
138                                    "template_exception_handler",
139                                    PropsValues.FREEMARKER_ENGINE_TEMPLATE_EXCEPTION_HANDLER);
140                    }
141                    catch (Exception e) {
142                            throw new TemplateException("Unable to init freemarker manager", e);
143                    }
144            }
145    
146            public void setTemplateContextHelper(
147                    TemplateContextHelper templateContextHelper) {
148    
149                    _templateContextHelper = templateContextHelper;
150            }
151    
152            private Configuration _configuration;
153            private TemplateContextHelper _templateContextHelper;
154    
155    }