001
014
015 package com.liferay.portal.velocity;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.util.PropsValues;
020
021 import java.io.IOException;
022 import java.io.InputStream;
023
024 import org.apache.commons.collections.ExtendedProperties;
025 import org.apache.velocity.exception.ResourceNotFoundException;
026 import org.apache.velocity.runtime.resource.Resource;
027 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
028
029
033 public class LiferayResourceLoader extends ResourceLoader {
034
035 public static void setVelocityResourceListeners(
036 String[] velocityResourceListeners) {
037
038 _velocityResourceListeners = new VelocityResourceListener[
039 velocityResourceListeners.length];
040
041 for (int i = 0; i < velocityResourceListeners.length; i++) {
042 try {
043 Class<?> clazz = Class.forName(velocityResourceListeners[i]);
044
045 _velocityResourceListeners[i] = (VelocityResourceListener)
046 clazz.newInstance();
047 }
048 catch (Exception e) {
049 _log.error(e);
050
051 _velocityResourceListeners[i] = null;
052 }
053 }
054 }
055
056 @Override
057 public long getLastModified(Resource resource) {
058 if (_log.isDebugEnabled()) {
059 _log.debug("Get last modified for " + resource.getName());
060 }
061
062 return 0;
063 }
064
065 @Override
066 public InputStream getResourceStream(String source)
067 throws ResourceNotFoundException {
068
069 InputStream is = doGetResourceStream(source);
070
071 if (is == null) {
072 if (_log.isDebugEnabled()) {
073 _log.debug("Could not find " + source);
074 }
075
076 throw new ResourceNotFoundException(source);
077 }
078
079 if (_log.isDebugEnabled()) {
080 _log.debug("Successfully got " + source);
081 }
082
083 return is;
084 }
085
086 @Override
087 public void init(ExtendedProperties props) {
088 setModificationCheckInterval(
089 PropsValues.
090 VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL);
091 }
092
093 @Override
094 public boolean isSourceModified(Resource resource) {
095 if (_log.isDebugEnabled()) {
096 _log.debug("Check modified status for " + resource.getName());
097 }
098
099 return false;
100 }
101
102 @Override
103 public boolean resourceExists(String resourceName) {
104 InputStream is = doGetResourceStream(resourceName);
105
106 try {
107 if (is != null) {
108 is.close();
109 }
110 }
111 catch (IOException ioe) {
112 }
113
114 if (is != null) {
115 return true;
116 }
117 else {
118 return false;
119 }
120 }
121
122 protected InputStream doGetResourceStream(String source)
123 throws ResourceNotFoundException {
124
125 if (_log.isDebugEnabled()) {
126 _log.debug("Get resource for " + source);
127 }
128
129 InputStream is = null;
130
131 for (int i = 0; (is == null) && (i < _velocityResourceListeners.length);
132 i++) {
133
134 VelocityResourceListener velocityResourceListener =
135 _velocityResourceListeners[i];
136
137 if (velocityResourceListener != null) {
138 is = velocityResourceListener.getResourceStream(source);
139 }
140 }
141
142 return is;
143 }
144
145 private static Log _log = LogFactoryUtil.getLog(
146 LiferayResourceLoader.class);
147
148 private static VelocityResourceListener[] _velocityResourceListeners =
149 new VelocityResourceListener[0];
150
151 }