1 17 package org.apache.excalibur.source.impl; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.net.URLConnection ; 24 25 import org.apache.excalibur.source.Source; 26 import org.apache.excalibur.source.SourceException; 27 import org.apache.excalibur.source.SourceNotFoundException; 28 import org.apache.excalibur.source.SourceUtil; 29 import org.apache.excalibur.source.SourceValidity; 30 import org.apache.excalibur.source.impl.validity.NOPValidity; 31 32 39 public final class ResourceSource 40 extends AbstractSource 41 implements Source 42 { 43 44 private URL m_location; 45 private String m_mimeType; 46 47 public ResourceSource( final String systemId ) throws MalformedURLException 48 { 49 final int pos = SourceUtil.indexOfSchemeColon(systemId); 50 if (pos == -1 || ! systemId.startsWith("://", pos)) 51 { 52 throw new MalformedURLException ("Invalid format for ResourceSource : " + systemId); 53 } 54 55 setSystemId(systemId); 56 m_location = getClassLoader().getResource(systemId.substring( pos + 3 )); 57 setScheme(systemId.substring(0, pos)); 58 } 59 60 public boolean exists() 61 { 62 return m_location != null; 63 } 64 65 protected void checkInfos() 66 { 67 super.checkInfos(); 68 69 URLConnection connection = null; 70 try 71 { 72 connection = m_location.openConnection(); 73 setLastModified(connection.getLastModified()); 74 setContentLength(connection.getContentLength()); 75 m_mimeType = connection.getContentType(); 76 } 77 catch(IOException ioe) 78 { 79 setLastModified(0); 80 setContentLength(-1); 81 m_mimeType = null; 82 } 83 } 84 85 public String getMimeType() 86 { 87 return m_mimeType; 88 } 89 90 93 public InputStream getInputStream() 94 throws IOException , SourceException 95 { 96 97 InputStream in = m_location.openStream(); 98 if ( in == null ) 99 throw new SourceNotFoundException( "Source '"+m_location+"' was not found" ); 100 return in; 101 } 102 103 107 public SourceValidity getValidity() 108 { 109 return NOPValidity.SHARED_INSTANCE; 111 } 112 113 protected ClassLoader getClassLoader() { 114 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 115 if( loader == null ) 116 { 117 loader = getClass().getClassLoader(); 118 } 119 120 return loader; 121 } 122 } 123 | Popular Tags |