1
14
15 package com.liferay.portal.velocity;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.util.PropsKeys;
20 import com.liferay.portal.kernel.util.StringPool;
21 import com.liferay.portal.kernel.util.Validator;
22 import com.liferay.portal.kernel.velocity.VelocityContext;
23 import com.liferay.portal.kernel.velocity.VelocityEngine;
24 import com.liferay.portal.util.PropsUtil;
25 import com.liferay.portal.util.PropsValues;
26
27 import java.io.Writer;
28
29 import org.apache.commons.collections.ExtendedProperties;
30 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
31 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
32
33
38 public class VelocityEngineImpl implements VelocityEngine {
39
40 public VelocityEngineImpl() {
41 }
42
43 public void flushTemplate(String resource) {
44 StringResourceRepository stringResourceRepository =
45 StringResourceLoader.getRepository();
46
47 if (stringResourceRepository != null) {
48 stringResourceRepository.removeStringResource(resource);
49 }
50 }
51
52 public VelocityContext getEmptyContext() {
53 return new VelocityContextImpl();
54 }
55
56 public VelocityContext getRestrictedToolsContext() {
57 return _restrictedToolsContext;
58 }
59
60 public VelocityContext getStandardToolsContext() {
61 return _standardToolsContext;
62 }
63
64 public VelocityContext getWrappedRestrictedToolsContext() {
65 return new VelocityContextImpl(
66 _restrictedToolsContext.getWrappedVelocityContext());
67 }
68
69 public VelocityContext getWrappedStandardToolsContext() {
70 return new VelocityContextImpl(
71 _standardToolsContext.getWrappedVelocityContext());
72 }
73
74 public void init() throws Exception {
75 _velocityEngine = new org.apache.velocity.app.VelocityEngine();
76
77 LiferayResourceLoader.setListeners(
78 PropsValues.VELOCITY_ENGINE_RESOURCE_LISTENERS);
79
80 ExtendedProperties extendedProperties = new ExtendedProperties();
81
82 extendedProperties.setProperty(_RESOURCE_LOADER, "string,servlet");
83
84 extendedProperties.setProperty(
85 "string." + _RESOURCE_LOADER + ".class",
86 StringResourceLoader.class.getName());
87
88 extendedProperties.setProperty(
89 "string." + _RESOURCE_LOADER + ".repository.class",
90 StringResourceRepositoryImpl.class.getName());
91
92 extendedProperties.setProperty(
93 "servlet." + _RESOURCE_LOADER + ".class",
94 LiferayResourceLoader.class.getName());
95
96 extendedProperties.setProperty(
97 org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS,
98 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER));
99
100 extendedProperties.setProperty(
101 org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS,
102 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
103
104 extendedProperties.setProperty(
105 org.apache.velocity.app.VelocityEngine.VM_LIBRARY,
106 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
107
108 extendedProperties.setProperty(
109 org.apache.velocity.app.VelocityEngine.VM_LIBRARY_AUTORELOAD,
110 String.valueOf(
111 !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
112
113 extendedProperties.setProperty(
114 org.apache.velocity.app.VelocityEngine.
115 VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
116 String.valueOf(
117 !PropsValues.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
118
119 extendedProperties.setProperty(
120 org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
121 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
122
123 extendedProperties.setProperty(
124 org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
125 ".log4j.category",
126 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
127
128 _velocityEngine.setExtendedProperties(extendedProperties);
129
130 _velocityEngine.init();
131
132 _restrictedToolsContext = new VelocityContextImpl();
133
134 VelocityVariables.insertHelperUtilities(
135 _restrictedToolsContext,
136 PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
137
138 _standardToolsContext = new VelocityContextImpl();
139
140 VelocityVariables.insertHelperUtilities(_standardToolsContext, null);
141 }
142
143 public boolean mergeTemplate(
144 String velocityTemplateId, String velocityTemplateContent,
145 VelocityContext velocityContext, Writer writer)
146 throws Exception {
147
148 if ((Validator.isNotNull(velocityTemplateContent)) &&
149 (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED ||
150 !resourceExists(velocityTemplateId))) {
151
152 StringResourceRepository stringResourceRepository =
153 StringResourceLoader.getRepository();
154
155 stringResourceRepository.putStringResource(
156 velocityTemplateId, velocityTemplateContent);
157
158 if (_log.isDebugEnabled()) {
159 _log.debug(
160 "Added " + velocityTemplateId +
161 " to the Velocity template repository");
162 }
163 }
164
165 VelocityContextImpl velocityContextImpl =
166 (VelocityContextImpl)velocityContext;
167
168 return _velocityEngine.mergeTemplate(
169 velocityTemplateId, StringPool.UTF8,
170 velocityContextImpl.getWrappedVelocityContext(), writer);
171 }
172
173 public boolean mergeTemplate(
174 String velocityTemplateId, VelocityContext velocityContext,
175 Writer writer)
176 throws Exception {
177
178 return mergeTemplate(velocityTemplateId, null, velocityContext, writer);
179 }
180
181 public boolean resourceExists(String resource) {
182 return _velocityEngine.resourceExists(resource);
183 }
184
185 private static final String _RESOURCE_LOADER =
186 org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER;
187
188 private static Log _log = LogFactoryUtil.getLog(VelocityEngineImpl.class);
189
190 private VelocityContextImpl _restrictedToolsContext;
191 private VelocityContextImpl _standardToolsContext;
192 private org.apache.velocity.app.VelocityEngine _velocityEngine;
193
194 }