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