001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.velocity;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.template.Template;
019    import com.liferay.portal.kernel.template.TemplateConstants;
020    import com.liferay.portal.kernel.template.TemplateException;
021    import com.liferay.portal.kernel.template.TemplateResource;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.template.BaseTemplateManager;
025    import com.liferay.portal.template.RestrictedTemplate;
026    import com.liferay.portal.util.PropsUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import java.util.Map;
030    
031    import org.apache.commons.collections.ExtendedProperties;
032    import org.apache.velocity.VelocityContext;
033    import org.apache.velocity.app.VelocityEngine;
034    import org.apache.velocity.runtime.RuntimeConstants;
035    import org.apache.velocity.util.introspection.SecureUberspector;
036    
037    /**
038     * @author Raymond Aug??
039     */
040    @DoPrivileged
041    public class VelocityManager extends BaseTemplateManager {
042    
043            @Override
044            public void destroy() {
045                    if (_velocityEngine == null) {
046                            return;
047                    }
048    
049                    _velocityEngine = null;
050    
051                    templateContextHelper.removeAllHelperUtilities();
052    
053                    templateContextHelper = null;
054            }
055    
056            @Override
057            public void destroy(ClassLoader classLoader) {
058                    templateContextHelper.removeHelperUtilities(classLoader);
059            }
060    
061            @Override
062            public String getName() {
063                    return TemplateConstants.LANG_TYPE_VM;
064            }
065    
066            @Override
067            public void init() throws TemplateException {
068                    if (_velocityEngine != null) {
069                            return;
070                    }
071    
072                    _velocityEngine = new VelocityEngine();
073    
074                    ExtendedProperties extendedProperties = new FastExtendedProperties();
075    
076                    extendedProperties.setProperty(
077                            VelocityEngine.DIRECTIVE_IF_TOSTRING_NULLCHECK,
078                            String.valueOf(
079                                    PropsValues.VELOCITY_ENGINE_DIRECTIVE_IF_TO_STRING_NULL_CHECK));
080    
081                    extendedProperties.setProperty(
082                            VelocityEngine.EVENTHANDLER_METHODEXCEPTION,
083                            LiferayMethodExceptionEventHandler.class.getName());
084    
085                    extendedProperties.setProperty(
086                            RuntimeConstants.INTROSPECTOR_RESTRICT_CLASSES,
087                            StringUtil.merge(PropsValues.VELOCITY_ENGINE_RESTRICTED_CLASSES));
088    
089                    extendedProperties.setProperty(
090                            RuntimeConstants.INTROSPECTOR_RESTRICT_PACKAGES,
091                            StringUtil.merge(PropsValues.VELOCITY_ENGINE_RESTRICTED_PACKAGES));
092    
093                    extendedProperties.setProperty(
094                            VelocityEngine.RESOURCE_LOADER, "liferay");
095    
096                    boolean cacheEnabled = false;
097    
098                    if (PropsValues.VELOCITY_ENGINE_RESOURCE_MODIFICATION_CHECK_INTERVAL !=
099                                    0) {
100    
101                            cacheEnabled = true;
102                    }
103    
104                    extendedProperties.setProperty(
105                            "liferay." + VelocityEngine.RESOURCE_LOADER + ".cache",
106                            String.valueOf(cacheEnabled));
107    
108                    extendedProperties.setProperty(
109                            "liferay." + VelocityEngine.RESOURCE_LOADER + ".class",
110                            LiferayResourceLoader.class.getName());
111    
112                    extendedProperties.setProperty(
113                            VelocityEngine.RESOURCE_MANAGER_CLASS,
114                            LiferayResourceManager.class.getName());
115    
116                    extendedProperties.setProperty(
117                            VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
118                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
119    
120                    extendedProperties.setProperty(
121                            VelocityEngine.RUNTIME_LOG_LOGSYSTEM + ".log4j.category",
122                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
123    
124                    extendedProperties.setProperty(
125                            RuntimeConstants.UBERSPECT_CLASSNAME,
126                            SecureUberspector.class.getName());
127    
128                    extendedProperties.setProperty(
129                            VelocityEngine.VM_LIBRARY,
130                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
131    
132                    extendedProperties.setProperty(
133                            VelocityEngine.VM_LIBRARY_AUTORELOAD,
134                            String.valueOf(!cacheEnabled));
135    
136                    extendedProperties.setProperty(
137                            VelocityEngine.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
138                            String.valueOf(!cacheEnabled));
139    
140                    _velocityEngine.setExtendedProperties(extendedProperties);
141    
142                    try {
143                            _velocityEngine.init();
144                    }
145                    catch (Exception e) {
146                            throw new TemplateException(e);
147                    }
148            }
149    
150            @Override
151            protected Template doGetTemplate(
152                    TemplateResource templateResource,
153                    TemplateResource errorTemplateResource, boolean restricted,
154                    Map<String, Object> helperUtilities, boolean privileged) {
155    
156                    VelocityContext velocityContext = getVelocityContext(helperUtilities);
157    
158                    Template template = new VelocityTemplate(
159                            templateResource, errorTemplateResource, velocityContext,
160                            _velocityEngine, templateContextHelper, privileged);
161    
162                    if (restricted) {
163                            template = new RestrictedTemplate(
164                                    template, templateContextHelper.getRestrictedVariables());
165                    }
166    
167                    return template;
168            }
169    
170            protected VelocityContext getVelocityContext(
171                    Map<String, Object> helperUtilities) {
172    
173                    VelocityContext velocityContext = new VelocityContext();
174    
175                    for (Map.Entry<String, Object> entry : helperUtilities.entrySet()) {
176                            velocityContext.put(entry.getKey(), entry.getValue());
177                    }
178    
179                    return velocityContext;
180            }
181    
182            private VelocityEngine _velocityEngine;
183    
184    }