001    /**
002     * Copyright (c) 2000-2012 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.deploy.sandbox.SandboxHandler;
018    import com.liferay.portal.kernel.util.ReflectionUtil;
019    
020    import java.lang.reflect.Field;
021    
022    import org.apache.commons.collections.ExtendedProperties;
023    import org.apache.velocity.runtime.RuntimeInstance;
024    import org.apache.velocity.runtime.RuntimeServices;
025    import org.apache.velocity.runtime.resource.Resource;
026    import org.apache.velocity.runtime.resource.ResourceManager;
027    import org.apache.velocity.runtime.resource.ResourceManagerImpl;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class LiferayResourceManager extends ResourceManagerImpl {
034    
035            @Override
036            public String getLoaderNameForResource(String source) {
037    
038                    // Velocity's default implementation makes its cache useless because
039                    // getResourceStream is called to test the availability of a template
040    
041                    if ((globalCache.get(ResourceManager.RESOURCE_CONTENT + source) !=
042                                    null) ||
043                            (globalCache.get(ResourceManager.RESOURCE_TEMPLATE + source) !=
044                                    null)) {
045    
046                            return LiferayResourceLoader.class.getName();
047                    }
048                    else {
049                            return super.getLoaderNameForResource(source);
050                    }
051            }
052    
053            @Override
054            public Resource getResource(
055                            String resourceName, int resourceType, String encoding)
056                    throws Exception {
057    
058                    if (resourceName.contains(SandboxHandler.SANDBOX_MARKER)) {
059                            return loadResource(resourceName, resourceType, encoding);
060                    }
061                    else {
062                            return super.getResource(resourceName, resourceType, encoding);
063                    }
064            }
065    
066            @Override
067            public synchronized void initialize(RuntimeServices runtimeServices)
068                    throws Exception {
069    
070                    ExtendedProperties extendedProperties =
071                            runtimeServices.getConfiguration();
072    
073                    Field field = ReflectionUtil.getDeclaredField(
074                            RuntimeInstance.class, "configuration");
075    
076                    field.set(
077                            runtimeServices, new FastExtendedProperties(extendedProperties));
078    
079                    super.initialize(runtimeServices);
080            }
081    
082    }