| Home >> All >> org >> roller >> presentation >> [ velocity Javadoc ] |
Source code: org/roller/presentation/velocity/WebappResourceLoader.java
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.presentation.RollerContext; 10 11 import java.io.InputStream; 12 13 import javax.servlet.ServletContext; 14 15 /** 16 * Tries to load Velocity resources from the Webapp. 17 * This class borrows heavily from 18 * org.apache.velocity.tools.view.servlet.WebappLoader 19 * http://cvs.apache.org/viewcvs/jakarta-velocity- 20 * tools/view/src/java/org/apache/velocity/tools/view/servlet/WebappLoader.java? 21 * rev=1.1.1.1&content-type=text/vnd.viewcvs-markup 22 * 23 * @author Lance Lavandowska 24 */ 25 public class WebappResourceLoader extends ResourceLoader 26 { 27 private static Log mLogger = 28 LogFactory.getFactory().getInstance(WebappResourceLoader.class); 29 30 private static ServletContext mContext = null; 31 32 /** 33 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties) 34 */ 35 public void init(ExtendedProperties arg0) 36 { 37 rsvc.info("WebappResourceLoader : initialization starting."); 38 39 this.getContext(); 40 if (mContext == null) 41 { 42 mLogger.warn("WebappResourceLoader : Unable to find ServletContext!"); 43 } 44 45 rsvc.info("WebappResourceLoader : initialization complete."); 46 } 47 48 private ServletContext getContext() 49 { 50 if (mContext == null) 51 { 52 mContext = RollerContext.getServletContext(); 53 } 54 return mContext; 55 } 56 57 public static void setServletContext(ServletContext context) 58 { 59 mContext = context; 60 } 61 62 /** 63 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getResourceStream(java.lang.String) 64 */ 65 public InputStream getResourceStream(String name) 66 throws ResourceNotFoundException 67 { 68 InputStream result = null; 69 70 if (name == null || name.length() == 0) 71 { 72 throw new ResourceNotFoundException ("No template name provided"); 73 } 74 75 try 76 { 77 if (!name.startsWith("/")) 78 name = "/" + name; 79 80 result = getContext().getResourceAsStream( name ); 81 } 82 catch( NullPointerException npe) 83 { 84 String msg = "WebappResourceLoader.getResourceStream(): " + name; 85 if (mContext == null) 86 { 87 mLogger.info("WebappResourceLoader("+name+"): ServletContext is null"); 88 msg += "\n\tServletContext is null"; 89 } 90 throw new ResourceNotFoundException(msg); 91 } 92 catch( Exception fnfe ) 93 { 94 /* 95 * log and convert to a general Velocity ResourceNotFoundException 96 */ 97 throw new ResourceNotFoundException( fnfe.getMessage() ); 98 } 99 100 return result; 101 } 102 103 /** 104 * Defaults to return false. 105 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource) 106 */ 107 public boolean isSourceModified(Resource arg0) 108 { 109 return false; 110 } 111 112 /** 113 * Defaults to return 0. 114 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource) 115 */ 116 public long getLastModified(Resource arg0) 117 { 118 return 0; 119 } 120 }