1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
34   * <a href="VelocityEngineImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Raymond Augé
37   */
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              "servlet." + _RESOURCE_LOADER + ".class",
90              LiferayResourceLoader.class.getName());
91  
92          extendedProperties.setProperty(
93              org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS,
94              PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER));
95  
96          extendedProperties.setProperty(
97              org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS,
98              PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
99  
100         extendedProperties.setProperty(
101             org.apache.velocity.app.VelocityEngine.VM_LIBRARY,
102             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
103 
104         extendedProperties.setProperty(
105             org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
106             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
107 
108         extendedProperties.setProperty(
109             org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
110                 ".log4j.category",
111             PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
112 
113         _velocityEngine.setExtendedProperties(extendedProperties);
114 
115         _velocityEngine.init();
116 
117         _restrictedToolsContext = new VelocityContextImpl();
118 
119         VelocityVariables.insertHelperUtilities(
120             _restrictedToolsContext,
121             PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
122 
123         _standardToolsContext = new VelocityContextImpl();
124 
125         VelocityVariables.insertHelperUtilities(_standardToolsContext, null);
126     }
127 
128     public boolean mergeTemplate(
129             String velocityTemplateId, String velocityTemplateContent,
130             VelocityContext velocityContext, Writer writer)
131         throws Exception {
132 
133         if (Validator.isNotNull(velocityTemplateContent) &&
134             !resourceExists(velocityTemplateId)) {
135 
136             StringResourceRepository stringResourceRepository =
137                 StringResourceLoader.getRepository();
138 
139             stringResourceRepository.putStringResource(
140                 velocityTemplateId, velocityTemplateContent);
141 
142             if (_log.isDebugEnabled()) {
143                 _log.debug(
144                     "Added " + velocityTemplateId +
145                         " to the Velocity template repository");
146             }
147         }
148 
149         VelocityContextImpl velocityContextImpl =
150             (VelocityContextImpl)velocityContext;
151 
152         return _velocityEngine.mergeTemplate(
153             velocityTemplateId, StringPool.UTF8,
154             velocityContextImpl.getWrappedVelocityContext(), writer);
155     }
156 
157     public boolean mergeTemplate(
158             String velocityTemplateId, VelocityContext velocityContext,
159             Writer writer)
160         throws Exception {
161 
162         return mergeTemplate(velocityTemplateId, null, velocityContext, writer);
163     }
164 
165     public boolean resourceExists(String resource) {
166         return _velocityEngine.resourceExists(resource);
167     }
168 
169     private static final String _RESOURCE_LOADER =
170         org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER;
171 
172     private static final Log _log =
173         LogFactoryUtil.getLog(VelocityEngineImpl.class);
174 
175     private VelocityContextImpl _restrictedToolsContext;
176     private VelocityContextImpl _standardToolsContext;
177     private org.apache.velocity.app.VelocityEngine _velocityEngine;
178 
179 }