Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portal.velocity; import com.liferay.portal.deploy.sandbox.SandboxHandler; import com.liferay.portal.kernel.util.ReflectionUtil; import org.apache.commons.collections.ExtendedProperties; import org.apache.velocity.runtime.RuntimeInstance; import org.apache.velocity.runtime.RuntimeServices; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.runtime.resource.ResourceManager; import org.apache.velocity.runtime.resource.ResourceManagerImpl; import org.apache.velocity.runtime.resource.loader.ResourceLoader; import org.apache.velocity.runtime.resource.loader.StringResourceLoader; import java.lang.reflect.Field; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; /** * Loads velocity resources * * Performs better than default LiferayResourceManager avoiding resource checks on disk */ public class LiferayResourceManager extends ResourceManagerImpl { private ConcurrentHashMap<String, String> loaderNameMap = new ConcurrentHashMap<String, String>(); private static final String LIFERAY_RESOURCE_LOADER = LiferayResourceLoader.class.getName(); private StringResourceLoader stringResourceLoader; @Override public String getLoaderNameForResource(String source) { // Velocity's default implementation makes its cache useless because // getResourceStream is called to test the availability of a template if ((globalCache.get(ResourceManager.RESOURCE_CONTENT + source) != null) || (globalCache.get(ResourceManager.RESOURCE_TEMPLATE + source) != null)) { return LIFERAY_RESOURCE_LOADER; } else { // ======= Start of change ============== String name = loaderNameMap.get(source); if (name != null) { if (StringResourceLoader.class.toString().equals(name)) { if (stringResourceLoader == null) { for (Iterator<ResourceLoader> i = resourceLoaders.iterator(); i.hasNext();) { ResourceLoader loader = i.next(); if (loader instanceof StringResourceLoader) { stringResourceLoader = (StringResourceLoader) loader; break; } } } if (stringResourceLoader.resourceExists(source)) { return name; } else { return null; } } else { return name; } } name = super.getLoaderNameForResource(source); if (name != null) { loaderNameMap.put(source, name); } return name; } // =======end of change================ } @Override public Resource getResource(String resourceName, int resourceType, String encoding) throws Exception { if (resourceName.contains(SandboxHandler.SANDBOX_MARKER)) { return loadResource(resourceName, resourceType, encoding); } else { return super.getResource(resourceName, resourceType, encoding); } } @Override public synchronized void initialize(RuntimeServices runtimeServices) throws Exception { ExtendedProperties extendedProperties = runtimeServices.getConfiguration(); Field field = ReflectionUtil.getDeclaredField(RuntimeInstance.class, "configuration"); field.set(runtimeServices, new FastExtendedProperties(extendedProperties)); super.initialize(runtimeServices); } }