1 package org.roller.presentation.velocity; 2 3 import org.apache.commons.collections.ExtendedProperties; 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.velocity.exception.ResourceNotFoundException; 7 import org.apache.velocity.runtime.resource.Resource; 8 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 9 import org.roller.RollerException; 10 import org.roller.model.Roller; 11 import org.roller.pojos.PageData; 12 import org.roller.presentation.RollerContext; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.io.UnsupportedEncodingException ; 17 18 27 public class RollerResourceLoader extends ResourceLoader 28 { 29 private static Log mLogger = 30 LogFactory.getFactory().getInstance(RollerResourceLoader.class); 31 32 public void init( ExtendedProperties configuration) 33 { 34 if (mLogger.isDebugEnabled()) 35 { 36 mLogger.debug(configuration); 37 } 38 } 39 40 47 private Roller getRoller() 48 { 49 return RollerContext.getRoller( null ); 50 } 51 52 public boolean isSourceModified(Resource resource) 53 { 54 return (resource.getLastModified() != 55 readLastModified(resource, "checking timestamp")); 56 } 57 58 public long getLastModified(Resource resource) 59 { 60 return readLastModified(resource, "getting timestamp"); 61 } 62 63 70 public InputStream getResourceStream( String name ) 71 throws ResourceNotFoundException 72 { 73 if (name == null || name.length() == 0) 74 { 75 throw new ResourceNotFoundException ("Need to specify a template name!"); 76 } 77 78 try 79 { 80 PageData page = getPage( name ); 81 if (page == null) 82 { 83 throw new ResourceNotFoundException( 84 "RollerResourceLoader: page \"" + 85 name + "\" not found"); 86 } 87 return new ByteArrayInputStream ( page.getTemplate().getBytes("UTF-8") ); 88 } 89 catch (UnsupportedEncodingException uex) 90 { 91 mLogger.error(uex); 94 throw new RuntimeException (uex); 95 } 96 catch (RollerException re) 97 { 98 String msg = "RollerResourceLoader Error: " + 99 "database problem trying to load resource " + name; 100 mLogger.error( msg, re ); 101 throw new ResourceNotFoundException (msg); 102 } 103 } 104 105 113 private long readLastModified(Resource resource, String i_operation) 114 { 115 118 String name = resource.getName(); 119 try 120 { 121 PageData page = getPage( name ); 122 123 if (mLogger.isDebugEnabled()) 124 { 125 mLogger.debug(name + ": resource=" + resource.getLastModified() + 126 " vs. page=" + page.getUpdateTime().getTime()); 127 } 128 return page.getUpdateTime().getTime(); 129 } 130 catch (RollerException re) 131 { 132 mLogger.error( "Error " + i_operation, re ); 133 } 134 return 0; 135 } 136 137 public PageData getPage(String id) throws RollerException 138 { 139 if (getRoller() == null) throw new RollerException( 140 "RollerResourceLoader.getRoller() returned NULL"); 141 return getRoller().getUserManager().retrievePage(id); } 143 144 } 145 | Popular Tags |