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.app.VelocityEngine;
033    import org.apache.velocity.runtime.RuntimeConstants;
034    import org.apache.velocity.util.introspection.SecureUberspector;
035    
036    /**
037     * @author Raymond Aug??
038     */
039    @DoPrivileged
040    public class VelocityManager extends BaseTemplateManager {
041    
042            @Override
043            public void destroy() {
044                    if (_velocityEngine == null) {
045                            return;
046                    }
047    
048                    _velocityEngine = null;
049    
050                    templateContextHelper.removeAllHelperUtilities();
051    
052                    templateContextHelper = null;
053            }
054    
055            @Override
056            public void destroy(ClassLoader classLoader) {
057                    templateContextHelper.removeHelperUtilities(classLoader);
058            }
059    
060            @Override
061            public String getName() {
062                    return TemplateConstants.LANG_TYPE_VM;
063            }
064    
065            @Override
066            public void init() throws TemplateException {
067                    if (_velocityEngine != null) {
068                            return;
069                    }
070    
071                    _velocityEngine = new VelocityEngine();
072    
073                    ExtendedProperties extendedProperties = new FastExtendedProperties();
074    
075                    extendedProperties.setProperty(
076                            VelocityEngine.DIRECTIVE_IF_TOSTRING_NULLCHECK,
077                            String.valueOf(
078                                    PropsValues.VELOCITY_ENGINE_DIRECTIVE_IF_TO_STRING_NULL_CHECK));
079    
080                    extendedProperties.setProperty(
081                            VelocityEngine.EVENTHANDLER_METHODEXCEPTION,
082                            LiferayMethodExceptionEventHandler.class.getName());
083    
084                    extendedProperties.setProperty(
085                            RuntimeConstants.INTROSPECTOR_RESTRICT_CLASSES,
086                            StringUtil.merge(PropsValues.VELOCITY_ENGINE_RESTRICTED_CLASSES));
087    
088                    extendedProperties.setProperty(
089                            RuntimeConstants.INTROSPECTOR_RESTRICT_PACKAGES,
090                            StringUtil.merge(PropsValues.VELOCITY_ENGINE_RESTRICTED_PACKAGES));
091    
092                    extendedProperties.setProperty(
093                            VelocityEngine.RESOURCE_LOADER, "liferay");
094    
095                    boolean cacheEnabled = false;
096    
097                    if (PropsValues.VELOCITY_ENGINE_RESOURCE_MODIFICATION_CHECK_INTERVAL !=
098                                    0) {
099    
100                            cacheEnabled = true;
101                    }
102    
103                    extendedProperties.setProperty(
104                            "liferay." + VelocityEngine.RESOURCE_LOADER + ".cache",
105                            String.valueOf(cacheEnabled));
106    
107                    extendedProperties.setProperty(
108                            "liferay." + VelocityEngine.RESOURCE_LOADER + ".class",
109                            LiferayResourceLoader.class.getName());
110    
111                    extendedProperties.setProperty(
112                            VelocityEngine.RESOURCE_MANAGER_CLASS,
113                            LiferayResourceManager.class.getName());
114    
115                    extendedProperties.setProperty(
116                            VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
117                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
118    
119                    extendedProperties.setProperty(
120                            VelocityEngine.RUNTIME_LOG_LOGSYSTEM + ".log4j.category",
121                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
122    
123                    extendedProperties.setProperty(
124                            RuntimeConstants.UBERSPECT_CLASSNAME,
125                            SecureUberspector.class.getName());
126    
127                    extendedProperties.setProperty(
128                            VelocityEngine.VM_LIBRARY,
129                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_VELOCIMACRO_LIBRARY));
130    
131                    extendedProperties.setProperty(
132                            VelocityEngine.VM_LIBRARY_AUTORELOAD,
133                            String.valueOf(!cacheEnabled));
134    
135                    extendedProperties.setProperty(
136                            VelocityEngine.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
137                            String.valueOf(!cacheEnabled));
138    
139                    _velocityEngine.setExtendedProperties(extendedProperties);
140    
141                    try {
142                            _velocityEngine.init();
143                    }
144                    catch (Exception e) {
145                            throw new TemplateException(e);
146                    }
147            }
148    
149            @Override
150            protected Template doGetTemplate(
151                    TemplateResource templateResource,
152                    TemplateResource errorTemplateResource, boolean restricted,
153                    Map<String, Object> helperUtilities, boolean privileged) {
154    
155                    Template template = new VelocityTemplate(
156                            templateResource, errorTemplateResource, helperUtilities,
157                            _velocityEngine, templateContextHelper, privileged);
158    
159                    if (restricted) {
160                            template = new RestrictedTemplate(
161                                    template, templateContextHelper.getRestrictedVariables());
162                    }
163    
164                    return template;
165            }
166    
167            private VelocityEngine _velocityEngine;
168    
169    }