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