1
22
23 package com.liferay.portal.velocity;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
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.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.velocity.exception.ResourceNotFoundException;
34 import org.apache.velocity.runtime.resource.Resource;
35 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
36
37
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 PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
67 int modificationCheckInterval = GetterUtil.getInteger(PropsUtil.get(
68 PropsUtil.
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 _log.error("Failed to get " + source);
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 = LogFactory.getLog(LiferayResourceLoader.class);
122
123 }