1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.util.PropsKeys;
29  import com.liferay.portal.util.PropsUtil;
30  
31  import java.io.InputStream;
32  
33  import org.apache.commons.collections.ExtendedProperties;
34  import org.apache.velocity.exception.ResourceNotFoundException;
35  import org.apache.velocity.runtime.resource.Resource;
36  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
37  
38  /**
39   * <a href="LiferayResourceLoader.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class LiferayResourceLoader extends ResourceLoader {
45  
46      private static VelocityResourceListener[] _listeners =
47          new VelocityResourceListener[0];
48  
49      public static void setListeners(String[] listeners) {
50          _listeners = new VelocityResourceListener[listeners.length];
51  
52          for (int i = 0; i < listeners.length; i++) {
53              try {
54                  _listeners[i] = (VelocityResourceListener)Class.forName(
55                      listeners[i]).newInstance();
56              }
57              catch (Exception ex) {
58                  _log.error(ex);
59  
60                  _listeners[i] = null;
61              }
62          }
63      }
64  
65      public void init(ExtendedProperties props) {
66          boolean cachingOn = GetterUtil.getBoolean(PropsUtil.get(
67              PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
68          int modificationCheckInterval = GetterUtil.getInteger(PropsUtil.get(
69              PropsKeys.
70                  VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL));
71  
72          setCachingOn(cachingOn);
73          setModificationCheckInterval(modificationCheckInterval);
74      }
75  
76      public InputStream getResourceStream(String source)
77          throws ResourceNotFoundException {
78  
79          if (_log.isDebugEnabled()) {
80              _log.debug("Get resource for " + source);
81          }
82  
83          InputStream is = null;
84  
85          for (int i = 0; (is == null) && (i < _listeners.length); i++) {
86              if (_listeners[i] != null) {
87                  if (is == null) {
88                      is = _listeners[i].getResourceStream(source);
89                  }
90              }
91          }
92  
93          if (is == null) {
94              if (_log.isDebugEnabled()) {
95                  _log.debug("Could not find " + source);
96              }
97  
98              throw new ResourceNotFoundException(source);
99          }
100 
101         if (_log.isDebugEnabled()) {
102             _log.debug("Successfully got " + source);
103         }
104 
105         return is;
106     }
107 
108     public long getLastModified(Resource resource) {
109         if (_log.isDebugEnabled()) {
110             _log.debug("Get last modified for " + resource.getName());
111         }
112 
113         return 0;
114     }
115 
116     public boolean isSourceModified(Resource resource) {
117         if (_log.isDebugEnabled()) {
118             _log.debug("Check modified status for " + resource.getName());
119         }
120 
121         return false;
122     }
123 
124     private static Log _log =
125          LogFactoryUtil.getLog(LiferayResourceLoader.class);
126 
127 }