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