1 16 package org.apache.commons.vfs.impl; 17 18 import org.apache.commons.vfs.Capability; 19 import org.apache.commons.vfs.FileName; 20 import org.apache.commons.vfs.FileObject; 21 import org.apache.commons.vfs.FileSystemException; 22 import org.apache.commons.vfs.FileSystemOptions; 23 import org.apache.commons.vfs.FileType; 24 import org.apache.commons.vfs.NameScope; 25 import org.apache.commons.vfs.provider.AbstractFileSystem; 26 import org.apache.commons.vfs.provider.DelegateFileObject; 27 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 40 public class VirtualFileSystem 41 extends AbstractFileSystem 42 { 43 private final Map junctions = new HashMap (); 44 45 public VirtualFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions) 46 { 47 super(rootName, null, fileSystemOptions); 48 } 49 50 53 protected void addCapabilities(final Collection caps) 54 { 55 caps.add(Capability.ATTRIBUTES); 57 caps.add(Capability.CREATE); 58 caps.add(Capability.DELETE); 59 caps.add(Capability.GET_TYPE); 60 caps.add(Capability.JUNCTIONS); 61 caps.add(Capability.GET_LAST_MODIFIED); 62 caps.add(Capability.SET_LAST_MODIFIED_FILE); 63 caps.add(Capability.SET_LAST_MODIFIED_FOLDER); 64 caps.add(Capability.LIST_CHILDREN); 65 caps.add(Capability.READ_CONTENT); 66 caps.add(Capability.SIGNING); 67 caps.add(Capability.WRITE_CONTENT); 68 caps.add(Capability.APPEND_CONTENT); 69 } 70 71 75 protected FileObject createFile(final FileName name) 76 throws Exception 77 { 78 final FileName junctionPoint = getJunctionForFile(name); 80 final FileObject file; 81 if (junctionPoint != null) 82 { 83 final FileObject junctionFile = (FileObject) junctions.get(junctionPoint); 85 final String relName = junctionPoint.getRelativeName(name); 86 file = junctionFile.resolveFile(relName, NameScope.DESCENDENT_OR_SELF); 87 } 88 else 89 { 90 file = null; 91 } 92 93 return new DelegateFileObject(name, this, file); 95 } 96 97 100 public void addJunction(final String junctionPoint, 101 final FileObject targetFile) 102 throws FileSystemException 103 { 104 final FileName junctionName = getFileSystemManager().resolveName(getRootName(), junctionPoint); 105 106 if (getJunctionForFile(junctionName) != null) 108 { 109 throw new FileSystemException("vfs.impl/nested-junction.error", junctionName); 110 } 111 112 try 113 { 114 junctions.put(junctionName, targetFile); 116 117 final DelegateFileObject junctionFile = (DelegateFileObject) getFileFromCache(junctionName); 119 if (junctionFile != null) 120 { 121 junctionFile.setFile(targetFile); 122 } 123 124 FileName childName = junctionName; 126 boolean done = false; 127 for (FileName parentName = childName.getParent(); 128 !done && parentName != null; 129 childName = parentName, parentName = parentName.getParent()) 130 { 131 DelegateFileObject file = (DelegateFileObject) getFileFromCache(parentName); 132 if (file == null) 133 { 134 file = new DelegateFileObject(parentName, this, null); 135 putFileToCache(file); 136 } 137 else 138 { 139 done = file.exists(); 140 } 141 142 file.attachChild(childName, FileType.FOLDER); 144 } 145 146 } 148 catch (final Exception e) 149 { 150 throw new FileSystemException("vfs.impl/create-junction.error", junctionName, e); 151 } 152 } 153 154 157 public void removeJunction(final String junctionPoint) 158 throws FileSystemException 159 { 160 final FileName junctionName = getFileSystemManager().resolveName(getRootName(), junctionPoint); 161 junctions.remove(junctionName); 162 163 } 166 167 170 private FileName getJunctionForFile(final FileName name) 171 { 172 if (junctions.containsKey(name)) 173 { 174 return name; 176 } 177 178 for (Iterator iterator = junctions.keySet().iterator(); iterator.hasNext();) 180 { 181 final FileName junctionPoint = (FileName) iterator.next(); 182 if (junctionPoint.isDescendent(name)) 183 { 184 return junctionPoint; 185 } 186 } 187 188 return null; 190 } 191 } 192 | Popular Tags |