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 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 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 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 97 throw new ResourceNotFoundException( fnfe.getMessage() ); 98 } 99 100 return result; 101 } 102 103 107 public boolean isSourceModified(Resource arg0) 108 { 109 return false; 110 } 111 112 116 public long getLastModified(Resource arg0) 117 { 118 return 0; 119 } 120 } 121 | Popular Tags |