1
22
23 package com.liferay.portal.velocity;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.util.PropsKeys;
27 import com.liferay.portal.util.PropsUtil;
28
29 import java.io.InputStream;
30
31 import org.apache.commons.collections.ExtendedProperties;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
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 _log.error("Failed to get " + source);
95
96 throw new ResourceNotFoundException(source);
97 }
98
99 if (_log.isDebugEnabled()) {
100 _log.debug("Successfully got " + source);
101 }
102
103 return is;
104 }
105
106 public long getLastModified(Resource resource) {
107 if (_log.isDebugEnabled()) {
108 _log.debug("Get last modified for " + resource.getName());
109 }
110
111 return 0;
112 }
113
114 public boolean isSourceModified(Resource resource) {
115 if (_log.isDebugEnabled()) {
116 _log.debug("Check modified status for " + resource.getName());
117 }
118
119 return false;
120 }
121
122 private static Log _log = LogFactory.getLog(LiferayResourceLoader.class);
123
124 }