1 48 49 package org.jpublish.view.velocity; 50 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 54 import com.anthonyeden.lib.util.ClassUtilities; 55 import org.apache.commons.collections.ExtendedProperties; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.apache.velocity.exception.ResourceNotFoundException; 59 import org.apache.velocity.runtime.RuntimeServices; 60 import org.apache.velocity.runtime.resource.Resource; 61 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 62 import org.jpublish.SiteContext; 63 import org.jpublish.view.ContentSource; 64 65 71 72 public class JPublishResourceLoader extends ResourceLoader { 73 74 private static final String VM_GLOBAL_LIBRARY = "VM_global_library.vm"; 75 private Log log = 76 LogFactory.getLog(JPublishResourceLoader.class); 77 private SiteContext siteContext; 78 79 84 85 public void setSiteContext(SiteContext siteContext) { 86 this.siteContext = siteContext; 87 } 88 89 94 95 public void init(ExtendedProperties configuration) { 96 log.debug("init()"); 97 } 98 99 105 106 public void commonInit(RuntimeServices rs, 107 ExtendedProperties configuration) { 108 log.debug("commonInit()"); 109 110 super.commonInit(rs, configuration); 111 112 Object siteContext = rs.getProperty("jpublish.resource.loader.siteContext"); 113 if (log.isDebugEnabled()) { 114 log.debug("SiteContext from rs: " + siteContext); 115 } 116 if (siteContext == null) { 117 siteContext = configuration.get("jpublish.resource.loader.siteContext"); 118 if (log.isDebugEnabled()) { 119 log.debug("SiteContext from configuration: " + siteContext); 120 } 121 } 122 123 setSiteContext((SiteContext) siteContext); 124 125 } 126 127 134 public InputStream getResourceStream(String name) 135 throws ResourceNotFoundException { 136 if (VM_GLOBAL_LIBRARY.equals(name)) { 137 log.debug("Loading global library"); 138 return ClassUtilities.getResourceAsStream(name); 139 } 140 141 if (log.isDebugEnabled()) { 142 log.debug("getResourceStream(" + name + ")"); 143 } 144 try { 145 return getContentSource(name).getInputStream(); 146 } catch (IOException e) { 147 log.error("IO error getting resource stream"); 148 if (log.isDebugEnabled()) { 149 e.printStackTrace(); 150 } 151 throw new ResourceNotFoundException("An IO error occured:" + e); 152 } 153 } 154 155 161 public boolean isSourceModified(Resource resource) { 162 boolean isModified = 163 resource.getLastModified() != getLastModified(resource); 164 if (log.isDebugEnabled()) { 165 log.debug("isSourceModified=" + isModified); 166 } 167 return isModified; 168 } 169 170 176 public long getLastModified(Resource resource) { 177 try { 178 if (VM_GLOBAL_LIBRARY.equals(resource.getName())) { 179 return -1; 180 } 181 182 if (log.isDebugEnabled()) { 183 log.debug("getLastModified(" + resource.getName() + ")"); 184 } 185 ContentSource contentSource = getContentSource(resource.getName()); 186 long lastModified = contentSource.getLastModified(); 187 return lastModified; 188 } catch (IOException e) { 189 log.error("An IO exception occured while retrieving the last " + 190 "modified time"); 191 return -1; 192 } 193 } 194 195 201 202 protected ContentSource getContentSource(String name) { 203 if (log.isDebugEnabled()) { 204 log.debug("getContent(" + name + ")"); 205 } 206 return siteContext.getContentSource(name); 207 } 208 209 } 210 | Popular Tags |