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