1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
31   * <a href="ServletVelocityResourceListener.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Alexander Chow
35   */
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  }