1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.velocity;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.util.PropsUtil;
27  
28  import java.io.InputStream;
29  
30  import org.apache.commons.collections.ExtendedProperties;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.velocity.exception.ResourceNotFoundException;
34  import org.apache.velocity.runtime.resource.Resource;
35  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
36  
37  /**
38   * <a href="LiferayResourceLoader.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class LiferayResourceLoader extends ResourceLoader {
44  
45      private static VelocityResourceListener[] _listeners =
46          new VelocityResourceListener[0];
47  
48      public static void setListeners(String[] listeners) {
49          _listeners = new VelocityResourceListener[listeners.length];
50  
51          for (int i = 0; i < listeners.length; i++) {
52              try {
53                  _listeners[i] = (VelocityResourceListener)Class.forName(
54                      listeners[i]).newInstance();
55              }
56              catch (Exception ex) {
57                  _log.error(ex);
58  
59                  _listeners[i] = null;
60              }
61          }
62      }
63  
64      public void init(ExtendedProperties props) {
65          boolean cachingOn = GetterUtil.getBoolean(PropsUtil.get(
66              PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
67  
68          setCachingOn(cachingOn);
69          setModificationCheckInterval(0);
70      }
71  
72      public InputStream getResourceStream(String source)
73          throws ResourceNotFoundException {
74  
75          if (_log.isDebugEnabled()) {
76              _log.debug("Get resource for " + source);
77          }
78  
79          InputStream is = null;
80  
81          for (int i = 0; (is == null) && (i < _listeners.length); i++) {
82              if (_listeners[i] != null) {
83                  if (is == null) {
84                      is = _listeners[i].getResourceStream(source);
85                  }
86              }
87          }
88  
89          if (is == null) {
90              _log.error("Failed to get " + source);
91  
92              throw new ResourceNotFoundException(source);
93          }
94  
95          if (_log.isDebugEnabled()) {
96              _log.debug("Successfully got " + source);
97          }
98  
99          return is;
100     }
101 
102     public long getLastModified(Resource resource) {
103         if (_log.isDebugEnabled()) {
104             _log.debug("Get last modified for " + resource.getName());
105         }
106 
107         return 0;
108     }
109 
110     public boolean isSourceModified(Resource resource) {
111         if (_log.isDebugEnabled()) {
112             _log.debug("Check modified status for " + resource.getName());
113         }
114 
115         return false;
116     }
117 
118     private static Log _log = LogFactory.getLog(LiferayResourceLoader.class);
119 
120 }