001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.velocity;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.velocity.VelocityContext;
023    import com.liferay.portal.kernel.velocity.VelocityEngine;
024    import com.liferay.portal.util.PropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.io.Writer;
028    
029    import org.apache.commons.collections.ExtendedProperties;
030    import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
031    import org.apache.velocity.runtime.resource.util.StringResourceRepository;
032    
033    /**
034     * @author Raymond Augé
035     */
036    public class VelocityEngineImpl implements VelocityEngine {
037    
038            public VelocityEngineImpl() {
039            }
040    
041            public void flushTemplate(String resource) {
042                    StringResourceRepository stringResourceRepository =
043                            StringResourceLoader.getRepository();
044    
045                    if (stringResourceRepository != null) {
046                            stringResourceRepository.removeStringResource(resource);
047                    }
048            }
049    
050            public VelocityContext getEmptyContext() {
051                    return new VelocityContextImpl();
052            }
053    
054            public VelocityContext getRestrictedToolsContext() {
055                    return _restrictedToolsContext;
056            }
057    
058            public VelocityContext getStandardToolsContext() {
059                    return _standardToolsContext;
060            }
061    
062            public VelocityContext getWrappedRestrictedToolsContext() {
063                    return new VelocityContextImpl(
064                            _restrictedToolsContext.getWrappedVelocityContext());
065            }
066    
067            public VelocityContext getWrappedStandardToolsContext() {
068                    return new VelocityContextImpl(
069                            _standardToolsContext.getWrappedVelocityContext());
070            }
071    
072            public void init() throws Exception {
073                    _velocityEngine = new org.apache.velocity.app.VelocityEngine();
074    
075                    LiferayResourceLoader.setListeners(
076                            PropsValues.VELOCITY_ENGINE_RESOURCE_LISTENERS);
077    
078                    ExtendedProperties extendedProperties = new ExtendedProperties();
079    
080                    extendedProperties.setProperty(_RESOURCE_LOADER, "string,servlet");
081    
082                    extendedProperties.setProperty(
083                            "string." + _RESOURCE_LOADER + ".class",
084                            StringResourceLoader.class.getName());
085    
086                    extendedProperties.setProperty(
087                            "string." + _RESOURCE_LOADER + ".repository.class",
088                            StringResourceRepositoryImpl.class.getName());
089    
090                    extendedProperties.setProperty(
091                            "servlet." + _RESOURCE_LOADER + ".class",
092                            LiferayResourceLoader.class.getName());
093    
094                    extendedProperties.setProperty(
095                            org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CLASS,
096                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER));
097    
098                    extendedProperties.setProperty(
099                            org.apache.velocity.app.VelocityEngine.RESOURCE_MANAGER_CACHE_CLASS,
100                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE));
101    
102                    extendedProperties.setProperty(
103                            org.apache.velocity.app.VelocityEngine.VM_LIBRARY,
104                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
105    
106                    extendedProperties.setProperty(
107                            org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
108                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
109    
110                    extendedProperties.setProperty(
111                            org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM +
112                                    ".log4j.category",
113                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
114    
115                    _velocityEngine.setExtendedProperties(extendedProperties);
116    
117                    _velocityEngine.init();
118    
119                    _restrictedToolsContext = new VelocityContextImpl();
120    
121                    VelocityVariables.insertHelperUtilities(
122                            _restrictedToolsContext,
123                            PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
124    
125                    _standardToolsContext = new VelocityContextImpl();
126    
127                    VelocityVariables.insertHelperUtilities(_standardToolsContext, null);
128            }
129    
130            public boolean mergeTemplate(
131                            String velocityTemplateId, String velocityTemplateContent,
132                            VelocityContext velocityContext, Writer writer)
133                    throws Exception {
134    
135                    if ((Validator.isNotNull(velocityTemplateContent)) &&
136                            (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED ||
137                             !resourceExists(velocityTemplateId))) {
138    
139                            StringResourceRepository stringResourceRepository =
140                                    StringResourceLoader.getRepository();
141    
142                            stringResourceRepository.putStringResource(
143                                    velocityTemplateId, velocityTemplateContent);
144    
145                            if (_log.isDebugEnabled()) {
146                                    _log.debug(
147                                            "Added " + velocityTemplateId +
148                                                    " to the Velocity template repository");
149                            }
150                    }
151    
152                    VelocityContextImpl velocityContextImpl =
153                            (VelocityContextImpl)velocityContext;
154    
155                    return _velocityEngine.mergeTemplate(
156                            velocityTemplateId, StringPool.UTF8,
157                            velocityContextImpl.getWrappedVelocityContext(), writer);
158            }
159    
160            public boolean mergeTemplate(
161                            String velocityTemplateId, VelocityContext velocityContext,
162                            Writer writer)
163                    throws Exception {
164    
165                    return mergeTemplate(velocityTemplateId, null, velocityContext, writer);
166            }
167    
168            public boolean resourceExists(String resource) {
169                    return _velocityEngine.resourceExists(resource);
170            }
171    
172            private static final String _RESOURCE_LOADER =
173                    org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER;
174    
175            private static Log _log = LogFactoryUtil.getLog(VelocityEngineImpl.class);
176    
177            private VelocityContextImpl _restrictedToolsContext;
178            private VelocityContextImpl _standardToolsContext;
179            private org.apache.velocity.app.VelocityEngine _velocityEngine;
180    
181    }