001
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
033 public class LiferayResourceManager extends ResourceManagerImpl {
034
035 @Override
036 public String getLoaderNameForResource(String source) {
037
038
039
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 }