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