1
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.kernel.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
43 public class LiferayResourceLoader extends ResourceLoader {
44
45 public static void setListeners(String[] listeners) {
46 _listeners = new VelocityResourceListener[listeners.length];
47
48 for (int i = 0; i < listeners.length; i++) {
49 try {
50 _listeners[i] = (VelocityResourceListener)Class.forName(
51 listeners[i]).newInstance();
52 }
53 catch (Exception ex) {
54 _log.error(ex);
55
56 _listeners[i] = null;
57 }
58 }
59 }
60
61 public long getLastModified(Resource resource) {
62 if (_log.isDebugEnabled()) {
63 _log.debug("Get last modified for " + resource.getName());
64 }
65
66 return 0;
67 }
68
69 public InputStream getResourceStream(String source)
70 throws ResourceNotFoundException {
71
72 if (_log.isDebugEnabled()) {
73 _log.debug("Get resource for " + source);
74 }
75
76 InputStream is = null;
77
78 for (int i = 0; (is == null) && (i < _listeners.length); i++) {
79 if (_listeners[i] != null) {
80 if (is == null) {
81 is = _listeners[i].getResourceStream(source);
82 }
83 }
84 }
85
86 if (is == null) {
87 if (_log.isDebugEnabled()) {
88 _log.debug("Could not find " + source);
89 }
90
91 throw new ResourceNotFoundException(source);
92 }
93
94 if (_log.isDebugEnabled()) {
95 _log.debug("Successfully got " + source);
96 }
97
98 return is;
99 }
100
101 public void init(ExtendedProperties props) {
102 boolean cachingOn = GetterUtil.getBoolean(PropsUtil.get(
103 PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
104 int modificationCheckInterval = GetterUtil.getInteger(PropsUtil.get(
105 PropsKeys.
106 VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL));
107
108 setCachingOn(cachingOn);
109 setModificationCheckInterval(modificationCheckInterval);
110 }
111
112 public boolean isSourceModified(Resource resource) {
113 if (_log.isDebugEnabled()) {
114 _log.debug("Check modified status for " + resource.getName());
115 }
116
117 return false;
118 }
119
120 private static Log _log =
121 LogFactoryUtil.getLog(LiferayResourceLoader.class);
122
123 private static VelocityResourceListener[] _listeners =
124 new VelocityResourceListener[0];
125
126 }