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