1 12 13 package fr.jayasoft.ivy.repository.vfs; 14 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.IOException ; 18 import java.util.Arrays ; 19 import java.util.List ; 20 import java.util.ArrayList ; 21 22 import org.apache.commons.vfs.*; 23 import org.apache.commons.vfs.impl.StandardFileSystemManager; 24 25 import fr.jayasoft.ivy.repository.AbstractRepository; 26 import fr.jayasoft.ivy.repository.RepositoryCopyProgressListener; 27 import fr.jayasoft.ivy.repository.Resource; 28 import fr.jayasoft.ivy.repository.TransferEvent; 29 import fr.jayasoft.ivy.util.CopyProgressListener; 30 import fr.jayasoft.ivy.util.FileUtil; 31 import fr.jayasoft.ivy.util.Message; 32 33 34 public class VfsRepository extends AbstractRepository { 35 38 private static final String IVY_VFS_CONFIG = "ivy_vfs.xml"; 39 private StandardFileSystemManager _manager = null; 40 private final CopyProgressListener _progress = new RepositoryCopyProgressListener(this); 41 42 43 47 public VfsRepository() { 48 } 49 50 private FileSystemManager getVFSManager() throws IOException { 51 synchronized (this) { 52 if (_manager == null) { 53 _manager = createVFSManager(); 54 } 55 } 56 return _manager; 57 } 58 59 private StandardFileSystemManager createVFSManager() throws IOException { 60 StandardFileSystemManager result = null; 61 try { 62 68 result = new StandardFileSystemManager(); 69 result.setConfiguration(getClass().getResource(IVY_VFS_CONFIG)); 70 result.init(); 71 72 Message.verbose("Available VFS schemes..."); 74 String [] schemes = result.getSchemes(); 75 Arrays.sort(schemes); 76 for (int i = 0; i < schemes.length; i++) { 77 Message.verbose("VFS Supported Scheme: " + schemes[i]); 78 } 79 } catch (FileSystemException e) { 80 86 Message.error("Unable to initialize VFS repository manager!"); 87 Message.error(e.getLocalizedMessage()); 88 IOException error = new IOException (e.getLocalizedMessage()); 89 error.initCause(e); 90 throw error; 91 } 92 93 return result; 94 } 95 96 97 protected void finalize() { 98 if (_manager != null) { 99 _manager.close(); 100 _manager = null; 101 } 102 } 103 104 111 public Resource getResource(String vfsURI) throws IOException { 112 return new VfsResource(vfsURI, getVFSManager()); 113 } 114 115 123 public void get(String srcVfsURI, File destination) throws IOException { 124 VfsResource src = new VfsResource(srcVfsURI, getVFSManager()); 125 fireTransferInitiated(src, TransferEvent.REQUEST_GET); 126 try { 127 FileContent content = src.getContent(); 128 if (content == null) { 129 throw new IllegalArgumentException ("invalid vfs uri "+srcVfsURI+": no content found"); 130 } 131 FileUtil.copy(content.getInputStream(), destination, _progress); 132 } catch (IOException ex) { 133 fireTransferError(ex); 134 throw ex; 135 } catch (RuntimeException ex) { 136 fireTransferError(ex); 137 throw ex; 138 } 139 } 140 141 149 public List list(String vfsURI) throws IOException { 150 ArrayList list = new ArrayList (); 151 Message.debug("list called for URI" + vfsURI); 152 FileObject resourceImpl = getVFSManager().resolveFile(vfsURI); 153 Message.debug("resourceImpl=" + resourceImpl.toString()); 154 Message.debug("resourceImpl.exists()" + resourceImpl.exists()); 155 Message.debug("resourceImpl.getType()" + resourceImpl.getType()); 156 Message.debug("FileType.FOLDER" + FileType.FOLDER); 157 if ((resourceImpl != null) && resourceImpl.exists() && (resourceImpl.getType() == FileType.FOLDER)) { 158 FileObject[] children = resourceImpl.getChildren(); 159 for (int i = 0; i < children.length; i++) { 160 FileObject child = children[i]; 161 Message.debug("child " + i + child.getName().getURI()); 162 list.add(VfsResource.normalize(child.getName().getURI())); 163 } 164 } 165 return list; 166 } 167 168 169 170 180 public void put(File source, String vfsURI, boolean overwrite) throws IOException { 181 VfsResource dest = new VfsResource(vfsURI, getVFSManager()); 182 fireTransferInitiated(dest, TransferEvent.REQUEST_PUT); 183 if (dest.physicallyExists() && ! overwrite) { 184 throw new IOException ("Cannot copy. Destination file: " + dest.getName() + " exists and overwrite not set."); 185 } 186 if (dest.getContent() == null) { 187 throw new IllegalArgumentException ("invalid vfs uri "+vfsURI+" to put data to: resource has no content"); 188 } 189 190 FileUtil.copy(new FileInputStream (source), 191 dest.getContent().getOutputStream(), 192 _progress); 193 } 194 195 196 } 197 | Popular Tags |