1 17 package org.apache.excalibur.source.impl; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.logger.LogEnabled; 25 import org.apache.avalon.framework.logger.Logger; 26 import org.apache.commons.vfs.FileContent; 27 import org.apache.commons.vfs.FileObject; 28 import org.apache.commons.vfs.FileSystemException; 29 import org.apache.commons.vfs.FileSystemManager; 30 import org.apache.commons.vfs.VFS; 31 import org.apache.excalibur.source.ModifiableSource; 32 import org.apache.excalibur.source.SourceException; 33 import org.apache.excalibur.source.SourceUtil; 34 35 43 public class CommonsVFSSource extends AbstractSource 44 implements LogEnabled, ModifiableSource { 45 46 53 public CommonsVFSSource(final String location, final Map parameters) 54 throws FileSystemException { 55 m_location = location; 56 m_manager = VFS.getManager(); 57 m_fileObject = m_manager.resolveFile(location); m_fileContent = m_fileObject.getContent(); 59 } 60 61 66 protected void getInfos() { 67 try { 68 setContentLength(m_fileContent.getSize()); 69 } catch (final FileSystemException e) { 70 71 if (getLogger().isWarnEnabled()) { 72 getLogger().warn( 73 "Unable to determine content length for " + m_location, e 74 ); 75 } 76 setContentLength(-1); } 78 79 try { 80 setLastModified(m_fileContent.getLastModifiedTime()); 81 } catch (final FileSystemException e) { 82 83 if (getLogger().isWarnEnabled()) { 84 getLogger().warn( 85 "Unable to determine last modified date for " + m_location, e 86 ); 87 } 88 setLastModified(0); } 90 91 setSystemId(m_location); 92 setScheme(SourceUtil.getScheme(m_location)); 93 } 94 95 103 public InputStream getInputStream() throws IOException , SourceException { 104 return m_fileContent.getInputStream(); 105 } 106 107 113 public boolean exists() { 114 try { 115 return m_fileObject.exists(); 116 } catch (final FileSystemException e) { 117 118 if (getLogger().isWarnEnabled()) { 119 getLogger().warn("Unable to determine existence for " + m_location, e); 120 } 121 return false; 122 } 123 } 124 125 127 134 public boolean canCancel(final OutputStream stream) { 135 return false; 137 } 138 139 146 public void cancel(final OutputStream stream) throws IOException { 147 throw new IOException ("Cancel() not implemented"); 148 } 149 150 156 public void delete() throws SourceException { 157 try { 158 m_fileObject.delete(); 159 } catch (final FileSystemException e) { 160 throw new SourceException("Unable to delete resource: " + m_location, e); 161 } 162 } 163 164 170 public OutputStream getOutputStream() throws IOException { 171 return m_fileContent.getOutputStream(); 172 } 173 174 181 public void enableLogging(final Logger logger) { 182 m_logger = logger; 183 } 184 185 190 private Logger getLogger() { 191 return m_logger; 192 } 193 194 195 private final String m_location; 196 197 198 private final FileSystemManager m_manager; 199 200 201 private final FileObject m_fileObject; 202 203 204 private final FileContent m_fileContent; 205 206 207 private Logger m_logger; 208 } 209 | Popular Tags |