1 19 20 package org.netbeans.core.startup; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.StringTokenizer ; 31 import org.netbeans.Util; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.modules.InstalledFileLocator; 34 35 41 public final class InstalledFileLocatorImpl extends InstalledFileLocator { 42 43 44 public InstalledFileLocatorImpl() {} 45 46 private static final File [] dirs; 47 static { 48 List <File > _dirs = new ArrayList <File >(); 49 addDir(_dirs, System.getProperty("netbeans.user")); 50 String nbdirs = System.getProperty("netbeans.dirs"); if (nbdirs != null) { 52 StringTokenizer tok = new StringTokenizer (nbdirs, File.pathSeparator); 53 while (tok.hasMoreTokens()) { 54 addDir(_dirs, tok.nextToken()); 55 } 56 } 57 addDir(_dirs, System.getProperty("netbeans.home")); 58 dirs = _dirs.toArray(new File [_dirs.size()]); 59 } 60 61 private static void addDir(List <File > _dirs, String d) { 62 if (d != null) { 63 File f = new File (d).getAbsoluteFile(); 64 if (f.isDirectory()) { 65 _dirs.add(f); 66 } 67 } 68 } 69 70 77 private static Map <String ,Map <File ,Set <String >>> fileCache = null; 78 79 85 public static synchronized void prepareCache() { 86 assert fileCache == null; 87 fileCache = new HashMap <String ,Map <File ,Set <String >>>(); 88 } 89 90 97 public static synchronized void discardCache() { 98 assert fileCache != null; 99 fileCache = null; 100 } 101 102 106 public File locate(String relativePath, String codeNameBase, boolean localized) { 107 if (relativePath.length() == 0) { 108 throw new IllegalArgumentException ("Cannot look up \"\" in InstalledFileLocator.locate"); } 110 if (relativePath.charAt(0) == '/') { 111 throw new IllegalArgumentException ("Paths passed to InstalledFileLocator.locate should not start with '/': " + relativePath); } 113 int slashIdx = relativePath.lastIndexOf('/'); 114 if (slashIdx == relativePath.length() - 1) { 115 throw new IllegalArgumentException ("Paths passed to InstalledFileLocator.locate should not end in '/': " + relativePath); } 117 118 String prefix, name; 119 if (slashIdx != -1) { 120 prefix = relativePath.substring(0, slashIdx + 1); 121 name = relativePath.substring(slashIdx + 1); 122 assert name.length() > 0; 123 } else { 124 prefix = ""; 125 name = relativePath; 126 } 127 synchronized (InstalledFileLocatorImpl.class) { 128 if (localized) { 129 int i = name.lastIndexOf('.'); 130 String baseName, ext; 131 if (i == -1) { 132 baseName = name; 133 ext = ""; 134 } else { 135 baseName = name.substring(0, i); 136 ext = name.substring(i); 137 } 138 String [] suffixes = org.netbeans.Util.getLocalizingSuffixesFast(); 139 for (int j = 0; j < suffixes.length; j++) { 140 String locName = baseName + suffixes[j] + ext; 141 File f = locateExactPath(prefix, locName); 142 if (f != null) { 143 return f; 144 } 145 } 146 return null; 147 } else { 148 return locateExactPath(prefix, name); 149 } 150 } 151 } 152 153 154 private static File locateExactPath(String prefix, String name) { 155 assert Thread.holdsLock(InstalledFileLocatorImpl.class); 156 if (fileCache != null) { 157 Map <File ,Set <String >> fileCachePerPrefix = fileCache.get(prefix); 158 if (fileCachePerPrefix == null) { 159 fileCachePerPrefix = new HashMap <File ,Set <String >>(dirs.length * 2); 160 for (int i = 0; i < dirs.length; i++) { 161 File root = dirs[i]; 162 File d; 163 if (prefix.length() > 0) { 164 assert prefix.charAt(prefix.length() - 1) == '/'; 165 d = new File (root, prefix.substring(0, prefix.length() - 1).replace('/', File.separatorChar)); 166 } else { 167 d = root; 168 } 169 if (d.isDirectory()) { 170 String [] kids = d.list(); 171 if (kids != null) { 172 fileCachePerPrefix.put(root, new HashSet <String >(Arrays.asList(kids))); 173 } else { 174 Util.err.warning("could not read files in " + d); 175 } 176 } 177 } 178 fileCache.put(prefix, fileCachePerPrefix); 179 } 180 for (int i = 0; i < dirs.length; i++) { 181 Set <String > names = fileCachePerPrefix.get(dirs[i]); 182 if (names != null && names.contains(name)) { 183 return makeFile(dirs[i], prefix, name); 184 } 185 } 186 } else { 187 for (int i = 0; i < dirs.length; i++) { 188 File f = makeFile(dirs[i], prefix, name); 189 if (f.exists()) { 190 return f; 191 } 192 } 193 } 194 return null; 195 } 196 197 private static File makeFile(File dir, String prefix, String name) { 198 return FileUtil.normalizeFile(new File (dir, prefix.replace('/', File.separatorChar) + name)); 199 } 200 201 } 202 | Popular Tags |