1 19 20 package org.netbeans.core.startup.preferences; 21 22 import java.io.FilterOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Date ; 29 import java.util.List ; 30 import java.util.Properties ; 31 import org.openide.filesystems.FileLock; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.Repository; 35 36 41 class PropertiesStorage implements NbPreferences.FileStorage { 42 private static final String USERROOT_PREFIX = "/Preferences"; private static final String SYSTEMROOT_PREFIX = "/SystemPreferences"; private final static FileObject SFS_ROOT = 45 Repository.getDefault().getDefaultFileSystem().getRoot(); 46 47 private final String folderPath; 48 private String filePath; 49 private boolean isModified; 50 51 52 static NbPreferences.FileStorage instance(final String absolutePath) { 53 return new PropertiesStorage(absolutePath, true); 54 } 55 56 FileObject preferencesRoot() throws IOException { 57 return FileUtil.createFolder(SFS_ROOT, USERROOT_PREFIX); 58 } 59 60 static NbPreferences.FileStorage instanceReadOnly(final String absolutePath) { 61 return new PropertiesStorage(absolutePath, false) { 62 public boolean isReadOnly() { 63 return true; 64 } 65 66 public final String [] childrenNames() { 67 return new String [0]; 68 } 69 70 public final Properties load() throws IOException { 71 return new Properties (); 72 } 73 74 protected FileObject toPropertiesFile(boolean create) throws IOException { 75 if (create) { 76 throw new IOException (); 77 } 78 return null; 79 } 80 81 protected FileObject toFolder(boolean create) throws IOException { 82 if (create) { 83 throw new IOException (); 84 } 85 return null; 86 } 87 88 protected FileObject toPropertiesFile() { 89 return null; 90 } 91 92 protected FileObject toFolder() { 93 return null; 94 } 95 96 FileObject preferencesRoot() throws IOException { 97 return FileUtil.createFolder(SFS_ROOT, SYSTEMROOT_PREFIX); 98 } 99 100 }; 101 } 102 103 104 private PropertiesStorage(final String absolutePath, boolean userRoot) { 105 StringBuffer sb = new StringBuffer (); 106 String prefix = (userRoot) ? USERROOT_PREFIX : SYSTEMROOT_PREFIX; 107 sb.append(prefix).append(absolutePath); 108 folderPath = sb.toString(); 109 } 110 111 public boolean isReadOnly() { 112 return false; 113 } 114 115 public void markModified() { 116 isModified = true; 117 } 118 119 public final boolean existsNode() { 120 return (toPropertiesFile() != null) || (toFolder() != null); 121 } 122 123 public String [] childrenNames() { 124 Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.CHILDREN_NAMES, true); 125 try { 126 FileObject folder = toFolder(); 127 List <String > folderNames = new ArrayList <String >(); 128 129 if (folder != null) { 130 for (FileObject fo : Collections.list(folder.getFolders(false))) { 131 folderNames.add(fo.getNameExt()); 132 } 133 for (FileObject fo : Collections.list(folder.getData(false))) { 134 if (fo.hasExt("properties")) { folderNames.add(fo.getName()); 136 } 137 } 138 } 139 140 return folderNames.toArray(new String [folderNames.size()]); 141 } finally { 142 sw.stop(); 143 } 144 } 145 146 public final void removeNode() throws IOException { 147 Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.REMOVE_NODE, true); 148 try { 149 FileObject propertiesFile = toPropertiesFile(); 150 if (propertiesFile != null && propertiesFile.isValid()) { 151 propertiesFile.delete(); 152 FileObject folder = propertiesFile.getParent(); 153 while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) { 154 folder.delete(); 155 folder = folder.getParent(); 156 } 157 } 158 } finally { 159 sw.stop(); 160 } 161 } 162 163 public Properties load() throws IOException { 164 Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.LOAD, true); 165 try { 166 Properties retval = new Properties (); 167 InputStream is = inputStream(); 168 if (is != null) { 169 try { 170 retval.load(is); 171 } finally { 172 if (is != null) is.close(); 173 } 174 } 175 return retval; 176 } finally { 177 sw.stop(); 178 } 179 } 180 181 public void save(final Properties properties) throws IOException { 182 if (isModified) { 183 Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.FLUSH, true); 184 try { 185 isModified = false; 186 if (!properties.isEmpty()) { 187 OutputStream os = null; 188 try { 189 os = outputStream(); 190 properties.store(os, null); 191 } finally { 192 if (os != null) os.close(); 193 } 194 } else { 195 FileObject file = toPropertiesFile(); 196 if (file != null) { 197 file.delete(); 198 } 199 FileObject folder = toFolder(); 200 while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) { 201 folder.delete(); 202 folder = folder.getParent(); 203 } 204 } 205 } finally { 206 sw.stop(); 207 } 208 } 209 } 210 211 private InputStream inputStream() throws IOException { 212 FileObject file = toPropertiesFile(false); 213 return (file == null) ? null : file.getInputStream(); 214 } 215 216 private OutputStream outputStream() throws IOException { 217 FileObject fo = toPropertiesFile(true); 218 final FileLock lock = fo.lock(); 219 final OutputStream os = fo.getOutputStream(lock); 220 return new FilterOutputStream (os) { 221 public void close() throws IOException { 222 super.close(); 223 lock.releaseLock(); 224 } 225 }; 226 } 227 228 private String folderPath() { 229 return folderPath; 230 } 231 232 private String filePath() { 233 if (filePath == null) { 234 String [] all = folderPath().split("/"); StringBuilder sb = new StringBuilder (); 236 for (int i = 0; i < all.length-1; i++) { 237 sb.append(all[i]).append("/"); } 239 if (all.length > 0) { 240 sb.append(all[all.length-1]).append(".properties"); } else { 242 sb.append("root.properties"); } 244 filePath = sb.toString(); 245 } 246 return filePath; 247 } 248 249 protected FileObject toFolder() { 250 return SFS_ROOT.getFileObject(folderPath()); 251 } 252 253 protected FileObject toPropertiesFile() { 254 return SFS_ROOT.getFileObject(filePath()); 255 } 256 257 protected FileObject toFolder(boolean create) throws IOException { 258 FileObject retval = toFolder(); 259 if (retval == null && create) { 260 retval = FileUtil.createFolder(SFS_ROOT, folderPath); 261 } 262 assert (retval == null && !create) || (retval != null && retval.isFolder()); 263 return retval; 264 } 265 266 protected FileObject toPropertiesFile(boolean create) throws IOException { 267 FileObject retval = toPropertiesFile(); 268 if (retval == null && create) { 269 retval = FileUtil.createData(SFS_ROOT,filePath()); } 271 assert (retval == null && !create) || (retval != null && retval.isData()); 272 return retval; 273 } 274 } 275 | Popular Tags |