1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }