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.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.io.InputStream;
21  
22  import org.apache.velocity.exception.ResourceNotFoundException;
23  
24  /**
25   * <a href="ClassLoaderVelocityResourceListener.java.html"><b><i>View Source</i>
26   * </b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class ClassLoaderVelocityResourceListener
31      extends VelocityResourceListener {
32  
33      public InputStream getResourceStream(String source)
34          throws ResourceNotFoundException {
35  
36          InputStream is = null;
37  
38          int pos = source.indexOf(JOURNAL_SEPARATOR);
39  
40          if (pos == -1) {
41              pos = source.indexOf(SERVLET_SEPARATOR);
42          }
43  
44          if (pos == -1) {
45              pos = source.indexOf(THEME_LOADER_SEPARATOR);
46          }
47  
48          if (pos == -1) {
49              ClassLoader classLoader = getClass().getClassLoader();
50  
51              if (_log.isDebugEnabled()) {
52                  _log.debug("Loading " + source);
53              }
54  
55              is = classLoader.getResourceAsStream(source);
56          }
57  
58          return is;
59      }
60  
61      private static Log _log = LogFactoryUtil.getLog(
62          ClassLoaderVelocityResourceListener.class);
63  
64  }