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.deploy.sandbox.SandboxDeployListener;
018    
019    import org.apache.velocity.runtime.resource.Resource;
020    import org.apache.velocity.runtime.resource.ResourceManagerImpl;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class LiferayResourceManager extends ResourceManagerImpl {
026    
027            public String getLoaderNameForResource(String source) {
028    
029                    // Velocity's default implementation makes its cache useless because
030                    // getResourceStream is called to test the availability of a template
031    
032                    if (globalCache.get(source) != null) {
033                            return LiferayResourceLoader.class.getName();
034                    }
035                    else {
036                            return super.getLoaderNameForResource(source);
037                    }
038            }
039    
040            public Resource getResource(
041                            String resourceName, int resourceType, String encoding)
042                    throws Exception {
043    
044                    if (resourceName.contains(SandboxDeployListener.SANDBOX_MARKER)) {
045                            return loadResource(resourceName, resourceType, encoding);
046                    }
047                    else {
048                            return super.getResource(resourceName, resourceType, encoding);
049                    }
050            }
051    
052    }