1
19
20 package com.liferay.portal.velocity;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.util.PortalUtil;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.InputStream;
29
30 import javax.servlet.ServletContext;
31
32 import org.apache.velocity.exception.ResourceNotFoundException;
33
34
41 public class ServletVelocityResourceListener extends VelocityResourceListener {
42
43 public InputStream getResourceStream(String source)
44 throws ResourceNotFoundException {
45
46 InputStream is = null;
47
48 int pos = source.indexOf(SERVLET_SEPARATOR);
49
50 if (pos != -1) {
51 String servletContextName = source.substring(0, pos);
52
53 if (Validator.isNull(servletContextName)) {
54 servletContextName = PortalUtil.getPathContext();
55 }
56
57 ServletContext servletContext = VelocityContextPool.get(
58 servletContextName);
59
60 if (servletContext != null) {
61 String name =
62 source.substring(pos + SERVLET_SEPARATOR.length());
63
64 if (_log.isDebugEnabled()) {
65 _log.debug(
66 name + " is associated with the servlet context " +
67 servletContextName + " " + servletContext);
68 }
69
70 is = servletContext.getResourceAsStream(name);
71
72 if ((is == null) && (name.endsWith("/init_custom.vm"))) {
73 if (_log.isWarnEnabled()) {
74 _log.warn(
75 "The template " + name + " should be created");
76 }
77
78 is = new ByteArrayInputStream(new byte[0]);
79 }
80 }
81 else {
82 _log.error(
83 source + " is not valid because " + servletContextName +
84 " does not map to a servlet context");
85 }
86 }
87
88 return is;
89 }
90
91 private static Log _log =
92 LogFactoryUtil.getLog(ServletVelocityResourceListener.class);
93
94 }