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