1 50 package org.apache.avalon.excalibur.monitor; 51 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.io.InputStreamReader ; 55 import java.io.OutputStream ; 56 import java.io.Reader ; 57 import java.io.Writer ; 58 59 import org.apache.excalibur.source.Source; 60 import org.apache.excalibur.source.SourceException; 61 import org.apache.excalibur.source.SourceValidity; 62 63 67 public final class SourceResource 68 extends StreamResource 69 { 70 71 private final Source m_source; 72 73 74 private SourceValidity m_validity; 75 76 79 public SourceResource( final Source source ) 80 throws Exception 81 { 82 super( source.getURI() ); 83 84 m_source = source; 85 setPreviousModified( System.currentTimeMillis() ); 86 m_validity = source.getValidity(); 87 } 88 89 92 public long lastModified() 93 { 94 if( null == m_validity ) 95 { 96 return System.currentTimeMillis(); 97 } 98 else 99 { 100 int valid = m_validity.isValid(); 101 boolean isValid = false; 102 SourceValidity newVal = null; 103 if ( valid == 0 ) { 104 m_source.refresh(); 105 newVal = m_source.getValidity(); 106 if( newVal != null) 107 { 108 valid = m_validity.isValid( newVal ); 109 isValid = (valid == 1); 110 } 111 } else { 112 isValid = (valid == 1); 113 } 114 if ( isValid ) { 115 return getPreviousModified(); 116 } else { 117 if ( null == newVal ) { 118 m_source.refresh(); 119 m_validity = m_source.getValidity(); 120 } 121 return System.currentTimeMillis(); 122 } 123 } 124 } 125 126 129 public InputStream getResourceAsStream() 130 throws IOException 131 { 132 try 133 { 134 return m_source.getInputStream(); 135 } 136 catch( SourceException se ) 137 { 138 throw new IOException ( "SourceException: " + se.getMessage() ); 139 } 140 } 141 142 145 public Reader getResourceAsReader() 146 throws IOException 147 { 148 return new InputStreamReader ( getResourceAsStream() ); 149 } 150 151 154 public OutputStream setResourceAsStream() 155 throws IOException 156 { 157 throw new IOException ( "setResourceAsStream() not supported for URLResource" ); 158 } 159 160 163 public Writer setResourceAsWriter() 164 throws IOException 165 { 166 throw new IOException ( "setResourceAsWriter() not supported for URLResource" ); 167 } 168 169 public Source getSource() 170 { 171 return m_source; 172 } 173 } 174 | Popular Tags |