1 package org.jicengine.io; 2 3 import java.io.*; 4 import javax.servlet.ServletContext ; 5 6 22 23 public class WebApplicationResource extends AbstractResource implements UrlReadable { 24 25 private ServletContext webApplication; 26 private String resourceName; 27 28 33 public WebApplicationResource(ServletContext webApplication, String resourceName) 34 { 35 super("web-app://" + resourceName); 36 this.webApplication = webApplication; 37 this.resourceName = resourceName; 38 } 39 40 public ServletContext getServletContext() 41 { 42 return this.webApplication; 43 } 44 45 public String getResourceName() 46 { 47 return this.resourceName; 48 } 49 50 public java.net.URL getUrl() throws IOException 51 { 52 java.net.URL url = this.webApplication.getResource(getResourceName()); 53 if( url != null ){ 54 return url; 55 } 56 else { 57 throw new IOException("Can't find web-application resource '" + getResourceName() + "'"); 58 } 59 } 60 61 public boolean isAvailable() 62 { 63 try { 64 return (this.webApplication.getResource(getResourceName()) != null); 65 } catch (java.net.MalformedURLException e){ 66 return false; 67 } 68 } 69 70 public InputStream getInputStream() throws java.io.IOException 71 { 72 InputStream stream = this.webApplication.getResourceAsStream(getResourceName()); 73 74 if( stream != null ){ 75 return stream; 76 } 77 else { 78 throw new IOException("Can't find web-application resource '" + getResourceName() + "'"); 79 } 80 } 81 82 public Resource getResource(String relativePath) throws java.io.IOException 83 { 84 return new WebApplicationResource(getServletContext(), PathResolver.getRealPath(getResourceName(), relativePath)); 85 } 86 } 87 | Popular Tags |