001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.theme.ThemeLoader;
020 import com.liferay.portal.theme.ThemeLoaderFactory;
021
022 import java.io.File;
023 import java.io.FileInputStream;
024 import java.io.InputStream;
025
026 import org.apache.velocity.exception.ResourceNotFoundException;
027
028
031 public class ThemeLoaderVelocityResourceListener
032 extends VelocityResourceListener {
033
034 @Override
035 public InputStream getResourceStream(String source)
036 throws ResourceNotFoundException {
037
038 try {
039 return doGetResourceStream(source);
040 }
041 catch (Exception e) {
042 throw new ResourceNotFoundException(source);
043 }
044 }
045
046 protected InputStream doGetResourceStream(String source) throws Exception {
047 int pos = source.indexOf(THEME_LOADER_SEPARATOR);
048
049 if (pos == -1) {
050 return null;
051 }
052
053 String servletContextName = source.substring(0, pos);
054
055 ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(
056 servletContextName);
057
058 if (themeLoader == null) {
059 _log.error(
060 source + " is not valid because " + servletContextName +
061 " does not map to a theme loader");
062
063 return null;
064 }
065
066 String name = source.substring(pos + THEME_LOADER_SEPARATOR.length());
067
068 String themesPath = themeLoader.getThemesPath();
069
070 if (name.startsWith(themesPath)) {
071 name = name.substring(themesPath.length(), name.length());
072 }
073
074 if (_log.isDebugEnabled()) {
075 _log.debug(
076 name + " is associated with the theme loader " +
077 servletContextName + " " + themeLoader);
078 }
079
080 File fileStorage = themeLoader.getFileStorage();
081
082 return new FileInputStream(fileStorage.getPath() + name);
083 }
084
085 private static Log _log = LogFactoryUtil.getLog(
086 ThemeLoaderVelocityResourceListener.class);
087
088 }