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.util.Validator;
20 import com.liferay.portal.util.PortalUtil;
21 import com.liferay.portal.velocity.VelocityContextPool;
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 = VelocityContextPool.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 portalContextName = PortalUtil.getPathContext();
72
73 ServletContext portalContext =
74 VelocityContextPool.get(portalContextName);
75
76 url = portalContext.getResource(
77 "/html/themes/_unstyled/template/init_custom.ftl");
78 }
79 }
80 else {
81 _log.error(
82 name + " is not valid because " + servletContextName +
83 " does not map to a servlet context");
84 }
85 }
86
87 return url;
88 }
89
90 private static Log _log = LogFactoryUtil.getLog(
91 ServletTemplateLoader.class);
92
93 }