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.RUNTIME_LOG_LOGSYSTEM_CLASS,
110 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
111
112 extendedProperties.setProperty(
113 org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
114 ".log4j.category",
115 PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
116
117 _velocityEngine.setExtendedProperties(extendedProperties);
118
119 _velocityEngine.init();
120
121 _restrictedToolsContext = new VelocityContextImpl();
122
123 VelocityVariables.insertHelperUtilities(
124 _restrictedToolsContext,
125 PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
126
127 _standardToolsContext = new VelocityContextImpl();
128
129 VelocityVariables.insertHelperUtilities(_standardToolsContext, null);
130 }
131
132 public boolean mergeTemplate(
133 String velocityTemplateId, String velocityTemplateContent,
134 VelocityContext velocityContext, Writer writer)
135 throws Exception {
136
137 if ((Validator.isNotNull(velocityTemplateContent)) &&
138 (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED ||
139 !resourceExists(velocityTemplateId))) {
140
141 StringResourceRepository stringResourceRepository =
142 StringResourceLoader.getRepository();
143
144 stringResourceRepository.putStringResource(
145 velocityTemplateId, velocityTemplateContent);
146
147 if (_log.isDebugEnabled()) {
148 _log.debug(
149 "Added " + velocityTemplateId +
150 " to the Velocity template repository");
151 }
152 }
153
154 VelocityContextImpl velocityContextImpl =
155 (VelocityContextImpl)velocityContext;
156
157 return _velocityEngine.mergeTemplate(
158 velocityTemplateId, StringPool.UTF8,
159 velocityContextImpl.getWrappedVelocityContext(), writer);
160 }
161
162 public boolean mergeTemplate(
163 String velocityTemplateId, VelocityContext velocityContext,
164 Writer writer)
165 throws Exception {
166
167 return mergeTemplate(velocityTemplateId, null, velocityContext, writer);
168 }
169
170 public boolean resourceExists(String resource) {
171 return _velocityEngine.resourceExists(resource);
172 }
173
174 private static final String _RESOURCE_LOADER =
175 org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER;
176
177 private static Log _log = LogFactoryUtil.getLog(VelocityEngineImpl.class);
178
179 private VelocityContextImpl _restrictedToolsContext;
180 private VelocityContextImpl _standardToolsContext;
181 private org.apache.velocity.app.VelocityEngine _velocityEngine;
182
183 }