1 19 20 package org.netbeans.modules.refactoring.spi; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.util.Lookup; 32 33 69 public abstract class BackupFacility { 70 71 private BackupFacility() { 72 } 73 74 private static BackupFacility defaultInstance; 75 76 82 public abstract Handle backup(FileObject... file) throws IOException ; 83 84 90 public final Handle backup(Collection <? extends FileObject> fileObjects) throws IOException { 91 return backup(fileObjects.toArray(new FileObject[fileObjects.size()])); 92 } 93 94 100 public abstract void clear(); 101 102 107 public static BackupFacility getDefault() { 108 BackupFacility instance = Lookup.getDefault().lookup(BackupFacility.class); 109 return (instance != null) ? instance : getDefaultInstance(); 110 } 111 112 private static synchronized BackupFacility getDefaultInstance() { 113 if (defaultInstance == null) { 114 defaultInstance = new DefaultImpl(); 115 } 116 117 return defaultInstance; 118 } 119 120 125 public interface Handle { 126 130 void restore() throws IOException ; 131 } 132 133 private static class DefaultHandle implements Handle { 134 ArrayList <Long > handle; 135 DefaultImpl instance; 136 private DefaultHandle(DefaultImpl instance, ArrayList <Long > handles) { 137 this.handle = handles; 138 this.instance = instance; 139 } 140 public void restore() throws IOException { 141 for (long l:handle) { 142 instance.restore(l); 143 } 144 } 145 } 146 147 private static class DefaultImpl extends BackupFacility { 148 149 private long currentId = 0; 150 private HashMap <Long , BackupEntry> map = new HashMap (); 151 152 private class BackupEntry { 153 private File file; 154 private String path; 155 } 156 157 158 private DefaultImpl() { 159 } 160 161 public Handle backup(FileObject ... file) throws IOException { 162 ArrayList <Long > list = new ArrayList (); 163 for (FileObject f:file) { 164 list.add(backup(f)); 165 } 166 return new DefaultHandle(this, list); 167 } 168 174 public long backup(FileObject file) throws IOException { 175 BackupEntry entry = new BackupEntry(); 176 entry.file = File.createTempFile("nbbackup", null); copy(FileUtil.toFile(file), entry.file); 178 entry.path = file.getPath(); 179 map.put(currentId, entry); 180 entry.file.deleteOnExit(); 181 return currentId++; 182 } 183 188 void restore(long id) throws IOException { 189 BackupEntry entry = map.get(id); 190 if(entry==null) { 191 throw new IllegalArgumentException ("Backup with id " + id + "does not exist"); 192 } 193 File backup = File.createTempFile("nbbackup", null); backup.deleteOnExit(); 195 File f = new File (entry.path); 196 if (createNewFile(f)) { 197 backup.createNewFile(); 198 copy(f,backup); 199 } 200 copy(entry.file,f); 201 FileUtil.toFileObject(f).refresh(true); 202 entry.file.delete(); 203 if (backup.exists()) { 204 entry.file = backup; 205 } else { 206 map.remove(id); 207 } 208 } 209 210 213 private boolean createNewFile(File f) throws IOException { 214 if (f.exists()) 215 return true; 216 createNewFile(f.getParentFile()); 217 f.createNewFile(); 218 return false; 219 220 } 221 222 private void copy(File a, File b) throws IOException { 223 FileInputStream fs = new FileInputStream (a); 224 FileOutputStream fo = new FileOutputStream (b); 225 try { 226 FileUtil.copy(fs, fo); 227 } finally { 228 fs.close(); 229 fo.close(); 230 } 231 } 232 233 public void clear() { 234 for(BackupEntry entry: map.values()) { 235 entry.file.delete(); 236 } 237 map.clear(); 238 } 239 } 240 } | Popular Tags |