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.kernel.servlet.PortalWebResourceConstants;
020 import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
021 import com.liferay.portal.kernel.servlet.ServletContextPool;
022 import com.liferay.portal.kernel.template.TemplateConstants;
023 import com.liferay.portal.template.URLResourceParser;
024 import com.liferay.portal.util.PortalUtil;
025
026 import java.io.IOException;
027
028 import java.net.URL;
029
030 import javax.servlet.ServletContext;
031
032
036 public class VelocityServletResourceParser extends URLResourceParser {
037
038 @Override
039 public URL getURL(String source) throws IOException {
040 int pos = source.indexOf(TemplateConstants.SERVLET_SEPARATOR);
041
042 if (pos == -1) {
043 return null;
044 }
045
046 String servletContextName = source.substring(0, pos);
047
048 if (servletContextName.equals(PortalUtil.getPathContext())) {
049 servletContextName = PortalUtil.getServletContextName();
050 }
051
052 ServletContext servletContext = ServletContextPool.get(
053 servletContextName);
054
055 if (servletContext == null) {
056 _log.error(
057 source + " is not valid because " + servletContextName +
058 " does not map to a servlet context");
059
060 return null;
061 }
062
063 String name = source.substring(
064 pos + TemplateConstants.SERVLET_SEPARATOR.length());
065
066 if (_log.isDebugEnabled()) {
067 _log.debug(
068 name + " is associated with the servlet context " +
069 servletContextName + " " + servletContext);
070 }
071
072 URL url = servletContext.getResource(name);
073
074 if ((url == null) && name.endsWith("/init_custom.vm")) {
075 if (_log.isWarnEnabled()) {
076 _log.warn("The template " + name + " should be created");
077 }
078
079 ServletContext themeClassicServletContext =
080 PortalWebResourcesUtil.getServletContext(
081 PortalWebResourceConstants.RESOURCE_TYPE_THEME_CLASSIC);
082
083 url = themeClassicServletContext.getResource(
084 "/classic/templates/init_custom.vm");
085 }
086
087 return url;
088 }
089
090 private static final Log _log = LogFactoryUtil.getLog(
091 VelocityServletResourceParser.class);
092
093 }