001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.PropsKeys;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.kernel.velocity.VelocityContext;
023 import com.liferay.portal.kernel.velocity.VelocityEngine;
024 import com.liferay.portal.kernel.velocity.VelocityVariablesUtil;
025 import com.liferay.portal.util.PropsUtil;
026 import com.liferay.portal.util.PropsValues;
027
028 import java.io.Writer;
029
030 import org.apache.commons.collections.ExtendedProperties;
031 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
032 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
033
034
037 public class VelocityEngineImpl implements VelocityEngine {
038
039 public VelocityEngineImpl() {
040 }
041
042 public void flushTemplate(String velocityTemplateId) {
043 StringResourceRepository stringResourceRepository =
044 StringResourceLoader.getRepository();
045
046 if (stringResourceRepository != null) {
047 stringResourceRepository.removeStringResource(velocityTemplateId);
048 }
049
050 LiferayResourceCacheUtil.remove(velocityTemplateId);
051 }
052
053 public VelocityContext getEmptyContext() {
054 return new VelocityContextImpl();
055 }
056
057 public VelocityContext getRestrictedToolsContext() {
058 return _restrictedToolsContext;
059 }
060
061 public VelocityContext getStandardToolsContext() {
062 return _standardToolsContext;
063 }
064
065 public VelocityContext getWrappedRestrictedToolsContext() {
066 return new VelocityContextImpl(
067 _restrictedToolsContext.getWrappedVelocityContext());
068 }
069
070 public VelocityContext getWrappedStandardToolsContext() {
071 return new VelocityContextImpl(
072 _standardToolsContext.getWrappedVelocityContext());
073 }
074
075 public void init() throws Exception {
076 if (_velocityEngine != null) {
077 return;
078 }
079
080 _velocityEngine = new org.apache.velocity.app.VelocityEngine();
081
082 LiferayResourceLoader.setVelocityResourceListeners(
083 PropsValues.VELOCITY_ENGINE_RESOURCE_LISTENERS);
084
085 ExtendedProperties extendedProperties = new FastExtendedProperties();
086
087 extendedProperties.setProperty(_RESOURCE_LOADER, "string,servlet");
088
089 extendedProperties.setProperty(
090 "string." + _RESOURCE_LOADER + ".cache",
091 String.valueOf(
092 PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
093
094 extendedProperties.setProperty(
095 "string." + _RESOURCE_LOADER + ".class",
096 StringResourceLoader.class.getName());
097
098 extendedProperties.setProperty(
099 "string." + _RESOURCE_LOADER + ".repository.class",
100 StringResourceRepositoryImpl.class.getName());
101
102 extendedProperties.setProperty(
103 "servlet." + _RESOURCE_LOADER + ".cache",
104 String.valueOf(
105 PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
106
107 extendedProperties.setProperty(
108 "servlet." + _RESOURCE_LOADER + ".class",
109 LiferayResourceLoader.class.getName());
110
111 extendedProperties.setProperty(
112 org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS,
113 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER));
114
115 extendedProperties.setProperty(
116 org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS,
117 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
118
119 extendedProperties.setProperty(
120 org.apache.velocity.app.VelocityEngine.VM_LIBRARY,
121 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
122
123 extendedProperties.setProperty(
124 org.apache.velocity.app.VelocityEngine.VM_LIBRARY_AUTORELOAD,
125 String.valueOf(
126 !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
127
128 extendedProperties.setProperty(
129 org.apache.velocity.app.VelocityEngine.
130 VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
131 String.valueOf(
132 !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
133
134 extendedProperties.setProperty(
135 org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
136 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
137
138 extendedProperties.setProperty(
139 org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
140 ".log4j.category",
141 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
142
143 _velocityEngine.setExtendedProperties(extendedProperties);
144
145 _velocityEngine.init();
146
147 _restrictedToolsContext = new VelocityContextImpl();
148
149 VelocityVariablesUtil.insertHelperUtilities(
150 _restrictedToolsContext,
151 PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
152
153 _standardToolsContext = new VelocityContextImpl();
154
155 VelocityVariablesUtil.insertHelperUtilities(
156 _standardToolsContext, null);
157 }
158
159 public boolean mergeTemplate(
160 String velocityTemplateId, String velocityTemplateContent,
161 VelocityContext velocityContext, Writer writer)
162 throws Exception {
163
164 if ((Validator.isNotNull(velocityTemplateContent)) &&
165 (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED ||
166 !resourceExists(velocityTemplateId))) {
167
168 StringResourceRepository stringResourceRepository =
169 StringResourceLoader.getRepository();
170
171 stringResourceRepository.putStringResource(
172 velocityTemplateId, velocityTemplateContent);
173
174 if (_log.isDebugEnabled()) {
175 _log.debug(
176 "Added " + velocityTemplateId +
177 " to the Velocity template repository");
178 }
179 }
180
181 VelocityContextImpl velocityContextImpl =
182 (VelocityContextImpl)velocityContext;
183
184 return _velocityEngine.mergeTemplate(
185 velocityTemplateId, StringPool.UTF8,
186 velocityContextImpl.getWrappedVelocityContext(), writer);
187 }
188
189 public boolean mergeTemplate(
190 String velocityTemplateId, VelocityContext velocityContext,
191 Writer writer)
192 throws Exception {
193
194 return mergeTemplate(velocityTemplateId, null, velocityContext, writer);
195 }
196
197 public boolean resourceExists(String resource) {
198 return _velocityEngine.resourceExists(resource);
199 }
200
201 private static final String _RESOURCE_LOADER =
202 org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER;
203
204 private static Log _log = LogFactoryUtil.getLog(VelocityEngineImpl.class);
205
206 private VelocityContextImpl _restrictedToolsContext;
207 private VelocityContextImpl _standardToolsContext;
208 private org.apache.velocity.app.VelocityEngine _velocityEngine;
209
210 }