1   /**
2    * Copyright (c) 2000-2008 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.PropsKeys;
27  import com.liferay.portal.util.PropsUtil;
28  
29  import java.io.InputStream;
30  
31  import org.apache.commons.collections.ExtendedProperties;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
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              _log.error("Failed to get " + source);
95  
96              throw new ResourceNotFoundException(source);
97          }
98  
99          if (_log.isDebugEnabled()) {
100             _log.debug("Successfully got " + source);
101         }
102 
103         return is;
104     }
105 
106     public long getLastModified(Resource resource) {
107         if (_log.isDebugEnabled()) {
108             _log.debug("Get last modified for " + resource.getName());
109         }
110 
111         return 0;
112     }
113 
114     public boolean isSourceModified(Resource resource) {
115         if (_log.isDebugEnabled()) {
116             _log.debug("Check modified status for " + resource.getName());
117         }
118 
119         return false;
120     }
121 
122     private static Log _log = LogFactory.getLog(LiferayResourceLoader.class);
123 
124 }