1 19 20 package org.netbeans.modules.masterfs.filebasedfs; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.logging.Logger ; 29 import org.netbeans.modules.masterfs.filebasedfs.fileobjects.FileObjectFactory; 30 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.actions.SystemAction; 35 36 39 public final class FileBasedFileSystem extends FileSystem { 40 private static Map allInstances = new HashMap (); 41 private final FileObjectFactory factory; 42 43 public static void reinitForTests() { 45 FileBasedFileSystem.allInstances = new HashMap (); 46 } 47 48 public static FileBasedFileSystem getInstance(final File file) { 49 FileBasedFileSystem retVal; 50 final FileInfo fInfo = new FileInfo(file); 51 final FileInfo rootInfo = fInfo.getRoot(); 52 53 synchronized (FileBasedFileSystem.allInstances) { 54 final File rootFile = rootInfo.getFile(); 55 retVal = (FileBasedFileSystem) FileBasedFileSystem.allInstances.get(rootFile); 56 if (retVal == null) { 57 if (rootInfo.isConvertibleToFileObject()) { 58 retVal = new FileBasedFileSystem(rootFile); 59 FileBasedFileSystem.allInstances.put(rootFile, retVal); 60 } 61 } 62 } 63 return retVal; 64 } 65 66 public static final FileObject getFileObject(final File file) { 67 FileBasedFileSystem fs = getInstance(file); 68 return (fs != null) ? fs.findFileObject(file) : null; 69 } 70 71 72 static Collection getInstances() { 73 synchronized (FileBasedFileSystem.allInstances) { 74 return new ArrayList (allInstances.values()); 75 } 76 } 77 78 static int getSize () { 79 synchronized (FileBasedFileSystem.allInstances) { 80 return allInstances.size(); 81 } 82 } 83 84 private FileBasedFileSystem(final File rootFile) { 85 this.factory = FileObjectFactory.getInstance(new FileInfo(rootFile)); 86 } 87 88 public final org.openide.filesystems.FileObject findResource(final String name) { 89 File f = new File (name); 90 assert f.getAbsolutePath().replace('\\', '/').equals(name.replace('\\', '/')) : name + " versus " + f.getAbsolutePath(); 91 return findFileObject(f); 92 } 93 94 public final FileObject findFileObject(final File f) { 95 return findFileObject(new FileInfo (f)); 96 } 97 98 public final FileObject findFileObject(final FileInfo fInfo) { 99 File f = fInfo.getFile(); 100 boolean issue45485 = fInfo.isWindows() && f.getName().endsWith("."); if (issue45485) { 102 File f2 = FileUtil.normalizeFile(f); 103 issue45485 = !f2.getName().endsWith("."); 104 if (issue45485) return null; 105 } 106 final FileObject retVal = (getFactory().findFileObject(fInfo)); 107 return (retVal != null && retVal.isValid()) ? retVal : null; 108 } 109 110 public final org.openide.filesystems.FileObject getRoot() { 111 return getFactory().getRoot(); 112 } 113 114 public final String getDisplayName() { 115 return getFactory().getRoot().getRealRoot().getPath(); 116 } 117 118 public final SystemAction[] getActions() { 119 return new SystemAction[] {}; 120 } 121 122 public final SystemAction[] getActions(final Set foSet) { 123 return new SystemAction[] {}; 124 125 } 126 127 public final void refresh(final boolean expected) { 128 Statistics.StopWatch stopWatch = Statistics.getStopWatch(Statistics.REFRESH_FS); 129 stopWatch.start(); 130 131 getFactory().refreshAll(expected); 132 stopWatch.stop(); 133 134 Logger.getLogger("org.netbeans.modules.masterfs.REFRESH").fine( 136 "FS.refresh statistics (" + Statistics.fileObjects() + "FileObjects):\n " + 137 Statistics.REFRESH_FS.toString() + "\n " + 138 Statistics.LISTENERS_CALLS.toString() + "\n " + 139 Statistics.REFRESH_FOLDER.toString() + "\n " + 140 Statistics.REFRESH_FILE.toString() + "\n" 141 ); 142 143 Statistics.REFRESH_FS.reset(); 144 Statistics.LISTENERS_CALLS.reset(); 145 Statistics.REFRESH_FOLDER.reset(); 146 Statistics.REFRESH_FILE.reset(); 147 } 148 149 public final boolean isReadOnly() { 150 return false; 151 } 152 153 public final String toString() { 154 return getDisplayName(); 155 } 156 157 public final FileObjectFactory getFactory() { 158 return factory; 159 } 160 } 161 | Popular Tags |