1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.velocity;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.PropsKeys;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.kernel.velocity.VelocityContext;
31  import com.liferay.portal.kernel.velocity.VelocityEngine;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portal.util.PropsValues;
34  
35  import java.io.Writer;
36  
37  import org.apache.commons.collections.ExtendedProperties;
38  import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
39  import org.apache.velocity.runtime.resource.util.StringResourceRepository;
40  
41  /**
42   * <a href="VelocityEngineImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Raymond Augé
45   */
46  public class VelocityEngineImpl implements VelocityEngine {
47  
48      public VelocityEngineImpl() {
49      }
50  
51      public void flushTemplate(String resource) {
52          StringResourceRepository stringResourceRepository =
53              StringResourceLoader.getRepository();
54  
55          if (stringResourceRepository != null) {
56              stringResourceRepository.removeStringResource(resource);
57          }
58      }
59  
60      public VelocityContext getEmptyContext() {
61          return new VelocityContextImpl();
62      }
63  
64      public VelocityContext getRestrictedToolsContext() {
65          return _restrictedToolsContext;
66      }
67  
68      public VelocityContext getStandardToolsContext() {
69          return _standardToolsContext;
70      }
71  
72      public VelocityContext getWrappedRestrictedToolsContext() {
73          return new VelocityContextImpl(
74              _restrictedToolsContext.getWrappedVelocityContext());
75      }
76  
77      public VelocityContext getWrappedStandardToolsContext() {
78          return new VelocityContextImpl(
79              _standardToolsContext.getWrappedVelocityContext());
80      }
81  
82      public void init() throws Exception {
83          _velocityEngine = new org.apache.velocity.app.VelocityEngine();
84  
85          LiferayResourceLoader.setListeners(
86              PropsValues.VELOCITY_ENGINE_RESOURCE_LISTENERS);
87  
88          ExtendedProperties extendedProperties = new ExtendedProperties();
89  
90          extendedProperties.setProperty(_RESOURCE_LOADER, "string,servlet");
91  
92          extendedProperties.setProperty(
93              "string." + _RESOURCE_LOADER + ".class",
94              StringResourceLoader.class.getName());
95  
96          extendedProperties.setProperty(
97              "servlet." + _RESOURCE_LOADER + ".class",
98              LiferayResourceLoader.class.getName());
99  
100         extendedProperties.setProperty(
101             org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS,
102             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER));
103 
104         extendedProperties.setProperty(
105             org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS,
106             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
107 
108         extendedProperties.setProperty(
109             org.apache.velocity.app.VelocityEngine.VM_LIBRARY,
110             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
111 
112         extendedProperties.setProperty(
113             org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
114             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
115 
116         extendedProperties.setProperty(
117             org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
118                 ".log4j.category",
119             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
120 
121         _velocityEngine.setExtendedProperties(extendedProperties);
122 
123         _velocityEngine.init();
124 
125         _restrictedToolsContext = new VelocityContextImpl();
126 
127         VelocityVariables.insertHelperUtilities(
128             _restrictedToolsContext,
129             PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
130 
131         _standardToolsContext = new VelocityContextImpl();
132 
133         VelocityVariables.insertHelperUtilities(_standardToolsContext, null);
134     }
135 
136     public boolean mergeTemplate(
137             String velocityTemplateId, String velocityTemplateContent,
138             VelocityContext velocityContext, Writer writer)
139         throws Exception {
140 
141         if ((Validator.isNotNull(velocityTemplateContent)) &&
142             (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED ||
143              !resourceExists(velocityTemplateId))) {
144 
145             StringResourceRepository stringResourceRepository =
146                 StringResourceLoader.getRepository();
147 
148             stringResourceRepository.putStringResource(
149                 velocityTemplateId, velocityTemplateContent);
150 
151             if (_log.isDebugEnabled()) {
152                 _log.debug(
153                     "Added " + velocityTemplateId +
154                         " to the Velocity template repository");
155             }
156         }
157 
158         VelocityContextImpl velocityContextImpl =
159             (VelocityContextImpl)velocityContext;
160 
161         return _velocityEngine.mergeTemplate(
162             velocityTemplateId, StringPool.UTF8,
163             velocityContextImpl.getWrappedVelocityContext(), writer);
164     }
165 
166     public boolean mergeTemplate(
167             String velocityTemplateId, VelocityContext velocityContext,
168             Writer writer)
169         throws Exception {
170 
171         return mergeTemplate(velocityTemplateId, null, velocityContext, writer);
172     }
173 
174     public boolean resourceExists(String resource) {
175         return _velocityEngine.resourceExists(resource);
176     }
177 
178     private static final String _RESOURCE_LOADER =
179         org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER;
180 
181     private static Log _log = LogFactoryUtil.getLog(VelocityEngineImpl.class);
182 
183     private VelocityContextImpl _restrictedToolsContext;
184     private VelocityContextImpl _standardToolsContext;
185     private org.apache.velocity.app.VelocityEngine _velocityEngine;
186 
187 }