1 18 package org.apache.roller.ui.rendering.velocity; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.InputStream ; 22 import java.io.UnsupportedEncodingException ; 23 import org.apache.commons.collections.ExtendedProperties; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.velocity.exception.ResourceNotFoundException; 27 import org.apache.velocity.runtime.resource.Resource; 28 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.model.RollerFactory; 31 import org.apache.roller.pojos.WeblogTemplate; 32 33 34 43 public class RollerResourceLoader extends ResourceLoader { 44 45 private static Log mLogger = LogFactory.getLog(RollerResourceLoader.class); 46 47 48 public void init(ExtendedProperties configuration) { 49 if (mLogger.isDebugEnabled()) { 50 mLogger.debug(configuration); 51 } 52 } 53 54 55 public boolean isSourceModified(Resource resource) { 56 return (resource.getLastModified() != 57 readLastModified(resource, "checking timestamp")); 58 } 59 60 61 public long getLastModified(Resource resource) { 62 return readLastModified(resource, "getting timestamp"); 63 } 64 65 72 public InputStream getResourceStream(String name) 73 throws ResourceNotFoundException { 74 75 if (name == null || name.length() == 0) { 76 throw new ResourceNotFoundException("Need to specify a template name!"); 77 } 78 79 try { 80 WeblogTemplate page = 81 RollerFactory.getRoller().getUserManager().getPage(name); 82 83 if (page == null) { 84 throw new ResourceNotFoundException( 85 "RollerResourceLoader: page \"" + 86 name + "\" not found"); 87 } 88 return new ByteArrayInputStream ( page.getContents().getBytes("UTF-8") ); 89 } catch (UnsupportedEncodingException uex) { 90 mLogger.error(uex); 93 throw new RuntimeException (uex); 94 } catch (RollerException re) { 95 String msg = "RollerResourceLoader Error: " + 96 "database problem trying to load resource " + name; 97 mLogger.error( msg, re ); 98 throw new ResourceNotFoundException(msg); 99 } 100 } 101 102 103 111 private long readLastModified(Resource resource, String i_operation) { 112 113 116 String name = resource.getName(); 117 try { 118 WeblogTemplate page = 119 RollerFactory.getRoller().getUserManager().getPage(name); 120 121 if (mLogger.isDebugEnabled()) { 122 mLogger.debug(name + ": resource=" + resource.getLastModified() + 123 " vs. page=" + page.getLastModified().getTime()); 124 } 125 return page.getLastModified().getTime(); 126 } catch (RollerException re) { 127 mLogger.error( "Error " + i_operation, re ); 128 } 129 return 0; 130 } 131 132 } 133 | Popular Tags |