1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  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              PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
67          int modificationCheckInterval = GetterUtil.getInteger(PropsUtil.get(
68              PropsKeys.
69                  VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL));
70  
71          setCachingOn(cachingOn);
72          setModificationCheckInterval(modificationCheckInterval);
73      }
74  
75      public InputStream getResourceStream(String source)
76          throws ResourceNotFoundException {
77  
78          if (_log.isDebugEnabled()) {
79              _log.debug("Get resource for " + source);
80          }
81  
82          InputStream is = null;
83  
84          for (int i = 0; (is == null) && (i < _listeners.length); i++) {
85              if (_listeners[i] != null) {
86                  if (is == null) {
87                      is = _listeners[i].getResourceStream(source);
88                  }
89              }
90          }
91  
92          if (is == null) {
93              if (_log.isDebugEnabled()) {
94                  _log.debug("Could not find " + source);
95              }
96  
97              throw new ResourceNotFoundException(source);
98          }
99  
100         if (_log.isDebugEnabled()) {
101             _log.debug("Successfully got " + source);
102         }
103 
104         return is;
105     }
106 
107     public long getLastModified(Resource resource) {
108         if (_log.isDebugEnabled()) {
109             _log.debug("Get last modified for " + resource.getName());
110         }
111 
112         return 0;
113     }
114 
115     public boolean isSourceModified(Resource resource) {
116         if (_log.isDebugEnabled()) {
117             _log.debug("Check modified status for " + resource.getName());
118         }
119 
120         return false;
121     }
122 
123     private static Log _log =
124         LogFactoryUtil.getLog(LiferayResourceLoader.class);
125 
126 }