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