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