1 12 package fr.jayasoft.ivy.repository.vfs; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 19 import org.apache.commons.vfs.FileContent; 20 import org.apache.commons.vfs.FileObject; 21 import org.apache.commons.vfs.FileSystemException; 22 import org.apache.commons.vfs.FileSystemManager; 23 import org.apache.commons.vfs.FileType; 24 25 import fr.jayasoft.ivy.repository.Resource; 26 import fr.jayasoft.ivy.util.Message; 27 import fr.jayasoft.ivy.resolver.VfsResolver; 28 29 public class VfsResource implements Resource { 30 private String _vfsURI; 31 private FileSystemManager _fsManager; 32 33 private transient boolean _init = false; 34 private transient boolean _exists; 35 private transient long _lastModified; 36 private transient long _contentLength; 37 38 private transient FileContent _content = null; 39 private transient FileObject _resourceImpl; 40 41 public VfsResource(String vfsURI, FileSystemManager fsManager) { 43 this._vfsURI = vfsURI; 44 this._fsManager = fsManager; 45 this._init = false; 46 } 47 48 private void init() { 49 if (!_init) { 50 try { 51 _resourceImpl = _fsManager.resolveFile(_vfsURI); 52 _content = _resourceImpl.getContent(); 53 54 _exists = _resourceImpl.exists(); 55 _lastModified = _content.getLastModifiedTime(); 56 _contentLength = _content.getSize(); 57 } catch (FileSystemException e) { 58 Message.verbose(e.getLocalizedMessage()); 59 _exists = false; 60 _lastModified = 0; 61 _contentLength = 0; 62 } 63 64 _init = true; 65 } 66 } 67 68 76 public List getChildren() { 77 init(); 78 ArrayList list = new ArrayList (); 79 try { 80 if ((_resourceImpl != null) && _resourceImpl.exists() && (_resourceImpl.getType() == FileType.FOLDER)) { 81 FileObject[] children = _resourceImpl.getChildren(); 82 for (int i = 0; i < children.length; i++) { 83 FileObject child = children[i]; 84 list.add(normalize(child.getName().getURI())); 85 } 86 } 87 } catch (IOException e) { 88 Message.verbose(e.getLocalizedMessage()); 89 } 90 return list; 91 } 92 93 public FileContent getContent() { 94 init(); 95 return _content; 96 } 97 98 103 public String getName() { 104 return normalize(_vfsURI); 105 } 106 107 public Resource clone(String cloneName) { 108 return new VfsResource(cloneName, _fsManager); 109 } 110 111 118 public static String normalize(String vfsURI) { 119 if (vfsURI == null) { 120 return ""; 121 } 122 123 if (vfsURI.startsWith("file:////")) { 124 vfsURI = vfsURI.replaceFirst("////", "///"); 125 } 126 return vfsURI; 127 } 128 129 134 public long getLastModified() { 135 init(); 136 return _lastModified; 137 } 138 139 144 public long getContentLength() { 145 init(); 146 return _contentLength; 147 } 148 149 155 public boolean exists() { 156 init(); 157 return _exists; 158 } 159 160 166 public boolean physicallyExists() { 167 init(); 169 170 try { 171 return _resourceImpl.exists(); 172 } catch (Exception e) { 176 Message.verbose(e.getLocalizedMessage()); 177 return false; 178 } 179 } 180 181 public String toString() { 182 return VfsResolver.prepareForDisplay(getName()); 183 } 184 185 public boolean isLocal() { 186 return getName().startsWith("file:"); 187 } 188 189 public InputStream openStream() throws IOException { 190 return getContent().getInputStream(); 191 } 192 } 193 | Popular Tags |