1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.velocity;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.util.PropsKeys;
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.velocity.exception.ResourceNotFoundException;
32  import org.apache.velocity.runtime.resource.Resource;
33  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
34  
35  /**
36   * <a href="LiferayResourceLoader.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class LiferayResourceLoader extends ResourceLoader {
42  
43      private static VelocityResourceListener[] _listeners =
44          new VelocityResourceListener[0];
45  
46      public static void setListeners(String[] listeners) {
47          _listeners = new VelocityResourceListener[listeners.length];
48  
49          for (int i = 0; i < listeners.length; i++) {
50              try {
51                  _listeners[i] = (VelocityResourceListener)Class.forName(
52                      listeners[i]).newInstance();
53              }
54              catch (Exception ex) {
55                  _log.error(ex);
56  
57                  _listeners[i] = null;
58              }
59          }
60      }
61  
62      public void init(ExtendedProperties props) {
63          boolean cachingOn = GetterUtil.getBoolean(PropsUtil.get(
64              PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
65          int modificationCheckInterval = GetterUtil.getInteger(PropsUtil.get(
66              PropsKeys.
67                  VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL));
68  
69          setCachingOn(cachingOn);
70          setModificationCheckInterval(modificationCheckInterval);
71      }
72  
73      public InputStream getResourceStream(String source)
74          throws ResourceNotFoundException {
75  
76          if (_log.isDebugEnabled()) {
77              _log.debug("Get resource for " + source);
78          }
79  
80          InputStream is = null;
81  
82          for (int i = 0; (is == null) && (i < _listeners.length); i++) {
83              if (_listeners[i] != null) {
84                  if (is == null) {
85                      is = _listeners[i].getResourceStream(source);
86                  }
87              }
88          }
89  
90          if (is == null) {
91              if (_log.isDebugEnabled()) {
92                  _log.debug("Could not find " + source);
93              }
94  
95              throw new ResourceNotFoundException(source);
96          }
97  
98          if (_log.isDebugEnabled()) {
99              _log.debug("Successfully got " + source);
100         }
101 
102         return is;
103     }
104 
105     public long getLastModified(Resource resource) {
106         if (_log.isDebugEnabled()) {
107             _log.debug("Get last modified for " + resource.getName());
108         }
109 
110         return 0;
111     }
112 
113     public boolean isSourceModified(Resource resource) {
114         if (_log.isDebugEnabled()) {
115             _log.debug("Check modified status for " + resource.getName());
116         }
117 
118         return false;
119     }
120 
121     private static Log _log =
122         LogFactoryUtil.getLog(LiferayResourceLoader.class);
123 
124 }