1 19 20 package org.netbeans.upgrade.systemoptions; 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.Iterator ; 30 import java.util.List ; 31 import java.util.Properties ; 32 import org.openide.filesystems.FileLock; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.filesystems.Repository; 36 37 40 class PropertiesStorage { 41 private static final String USERROOT_PREFIX = "/Preferences"; private final static FileObject SFS_ROOT = 43 Repository.getDefault().getDefaultFileSystem().getRoot(); 44 45 private final String folderPath; 46 private String filePath; 47 48 static PropertiesStorage instance(final String absolutePath) { 49 return new PropertiesStorage(absolutePath); 50 } 51 52 FileObject preferencesRoot() throws IOException { 53 return FileUtil.createFolder(SFS_ROOT, USERROOT_PREFIX); 54 } 55 56 57 58 private PropertiesStorage(final String absolutePath) { 59 StringBuffer sb = new StringBuffer (); 60 sb.append(USERROOT_PREFIX).append(absolutePath); 61 folderPath = sb.toString(); 62 } 63 64 65 public Properties load() throws IOException { 66 try { 67 Properties retval = new Properties (); 68 InputStream is = inputStream(); 69 if (is != null) { 70 try { 71 retval.load(is); 72 } finally { 73 if (is != null) is.close(); 74 } 75 } 76 return retval; 77 } finally { 78 } 79 } 80 81 public void save(final Properties properties) throws IOException { 82 if (!properties.isEmpty()) { 83 OutputStream os = null; 84 try { 85 os = outputStream(); 86 properties.store(os,new Date ().toString()); } finally { 88 if (os != null) os.close(); 89 } 90 } else { 91 FileObject file = toPropertiesFile(); 92 if (file != null) { 93 file.delete(); 94 } 95 FileObject folder = toFolder(); 96 while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) { 97 folder.delete(); 98 folder = folder.getParent(); 99 } 100 } 101 } 102 103 private InputStream inputStream() throws IOException { 104 FileObject file = toPropertiesFile(false); 105 return (file == null) ? null : file.getInputStream(); 106 } 107 108 private OutputStream outputStream() throws IOException { 109 FileObject fo = toPropertiesFile(true); 110 final FileLock lock = fo.lock(); 111 final OutputStream os = fo.getOutputStream(lock); 112 return new FilterOutputStream (os) { 113 public void close() throws IOException { 114 super.close(); 115 lock.releaseLock(); 116 } 117 }; 118 } 119 120 private String folderPath() { 121 return folderPath; 122 } 123 124 private String filePath() { 125 if (filePath == null) { 126 String [] all = folderPath().split("/"); StringBuilder sb = new StringBuilder (); 128 for (int i = 0; i < all.length-1; i++) { 129 sb.append(all[i]).append("/"); } 131 if (all.length > 0) { 132 sb.append(all[all.length-1]).append(".properties"); } else { 134 sb.append("root.properties"); } 136 filePath = sb.toString(); 137 } 138 return filePath; 139 } 140 141 protected FileObject toFolder() { 142 return SFS_ROOT.getFileObject(folderPath); 143 } 144 145 protected FileObject toPropertiesFile() { 146 return SFS_ROOT.getFileObject(filePath()); 147 } 148 149 protected FileObject toFolder(boolean create) throws IOException { 150 FileObject retval = toFolder(); 151 if (retval == null && create) { 152 retval = FileUtil.createFolder(SFS_ROOT, folderPath); 153 } 154 assert (retval == null && !create) || (retval != null && retval.isFolder()); 155 return retval; 156 } 157 158 protected FileObject toPropertiesFile(boolean create) throws IOException { 159 FileObject retval = toPropertiesFile(); 160 if (retval == null && create) { 161 retval = FileUtil.createData(SFS_ROOT,filePath()); } 163 assert (retval == null && !create) || (retval != null && retval.isData()); 164 return retval; 165 } 166 } | Popular Tags |