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