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.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  /**
30   * <a href="ServletVelocityResourceListener.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * @author Alexander Chow
34   */
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  }