001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.servlet.ServletContextPool;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.util.PortalUtil;
023
024 import java.io.InputStream;
025
026 import javax.servlet.ServletContext;
027
028 import org.apache.velocity.exception.ResourceNotFoundException;
029
030
033 public class ServletVelocityResourceListener extends VelocityResourceListener {
034
035 public InputStream getResourceStream(String source)
036 throws ResourceNotFoundException {
037
038 InputStream is = null;
039
040 int pos = source.indexOf(SERVLET_SEPARATOR);
041
042 if (pos != -1) {
043 String servletContextName = source.substring(0, pos);
044
045 if (Validator.isNull(servletContextName)) {
046 servletContextName = PortalUtil.getPathContext();
047 }
048
049 ServletContext servletContext = ServletContextPool.get(
050 servletContextName);
051
052 if (servletContext != null) {
053 String name =
054 source.substring(pos + SERVLET_SEPARATOR.length());
055
056 if (_log.isDebugEnabled()) {
057 _log.debug(
058 name + " is associated with the servlet context " +
059 servletContextName + " " + servletContext);
060 }
061
062 is = servletContext.getResourceAsStream(name);
063
064 if ((is == null) && (name.endsWith("/init_custom.vm"))) {
065 if (_log.isWarnEnabled()) {
066 _log.warn(
067 "The template " + name + " should be created");
068 }
069
070 is = new UnsyncByteArrayInputStream(new byte[0]);
071 }
072 }
073 else {
074 _log.error(
075 source + " is not valid because " + servletContextName +
076 " does not map to a servlet context");
077 }
078 }
079
080 return is;
081 }
082
083 private static Log _log = LogFactoryUtil.getLog(
084 ServletVelocityResourceListener.class);
085
086 }