1
14
15 package com.liferay.portal.freemarker;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.servlet.ServletContextPool;
20 import com.liferay.portal.kernel.util.Validator;
21 import com.liferay.portal.util.PortalUtil;
22
23 import java.io.IOException;
24
25 import java.net.URL;
26
27 import javax.servlet.ServletContext;
28
29
34 public class ServletTemplateLoader extends URLTemplateLoader {
35
36 public URL getURL(String name) throws IOException {
37 URL url = null;
38
39 int pos = name.indexOf(SERVLET_SEPARATOR);
40
41 if (pos != -1) {
42 String servletContextName = name.substring(0, pos);
43
44 if (Validator.isNull(servletContextName)) {
45 servletContextName = PortalUtil.getPathContext();
46 }
47
48 ServletContext servletContext = ServletContextPool.get(
49 servletContextName);
50
51 if (servletContext != null) {
52 String templateName =
53 name.substring(pos + SERVLET_SEPARATOR.length());
54
55 if (_log.isDebugEnabled()) {
56 _log.debug(
57 name + " is associated with the servlet context " +
58 servletContextName + " " + servletContext);
59 }
60
61 url = servletContext.getResource(templateName);
62
63 if ((url == null) &&
64 (templateName.endsWith("/init_custom.ftl"))) {
65
66 if (_log.isWarnEnabled()) {
67 _log.warn(
68 "The template " + name + " should be created");
69 }
70
71 String portalServletContextName =
72 PortalUtil.getPathContext();
73
74 ServletContext portalServletContext =
75 ServletContextPool.get(portalServletContextName);
76
77 url = portalServletContext.getResource(
78 "/html/themes/_unstyled/template/init_custom.ftl");
79 }
80 }
81 else {
82 _log.error(
83 name + " is not valid because " + servletContextName +
84 " does not map to a servlet context");
85 }
86 }
87
88 return url;
89 }
90
91 private static Log _log = LogFactoryUtil.getLog(
92 ServletTemplateLoader.class);
93
94 }