1
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
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 }