1 19 20 package org.netbeans.modules.versioning.system.cvss; 21 22 import org.netbeans.lib.cvsclient.file.DefaultFileHandler; 23 import org.netbeans.modules.versioning.system.cvss.util.Utils; 24 import org.openide.filesystems.FileUtil; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileLock; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 import java.io.FileOutputStream ; 32 33 46 class CvsLiteFileHandler extends DefaultFileHandler { 47 48 protected boolean createNewFile(File file) throws IOException { 49 boolean exists = file.isFile(); 50 if (exists) { 51 return false; 52 } else { 53 File parent = file.getParentFile(); 54 FileObject fo = Utils.mkfolders(parent); 55 try { 56 FilesystemHandler.ignoreEvents(true); 57 try { 58 fo.createData(file.getName()); 59 } catch (IOException e) { 60 return file.createNewFile(); 62 } 63 } finally { 64 FilesystemHandler.ignoreEvents(false); 65 } 66 return true; 67 } 68 } 69 70 protected OutputStream createOutputStream(File file) throws IOException { 71 FileObject fo = FileUtil.toFileObject(file); 72 if (fo == null) { 73 return new FileOutputStream (file); 75 } 76 FileLock lock = fo.lock(); 77 OutputStream stream = null; 78 try { 79 stream = fo.getOutputStream(lock); 80 } catch (IOException e) { 81 lock.releaseLock(); 82 throw e; 83 } 84 return new LockedOutputStream(lock, stream); 85 } 86 87 public void removeLocalFile(String pathname) throws IOException { 88 File fileToDelete = new File (pathname); 89 fileToDelete = FileUtil.normalizeFile(fileToDelete); 90 FileObject fo = FileUtil.toFileObject(fileToDelete); 91 if (fo == null) { 92 fileToDelete.delete(); 94 return; 95 } 96 try { 97 FilesystemHandler.ignoreEvents(true); 98 fo.delete(); 99 } finally { 100 FilesystemHandler.ignoreEvents(false); 101 } 102 } 103 104 public void renameLocalFile(String pathname, String newName) throws IOException { 105 File sourceFile = new File (pathname); 106 FileObject fo = FileUtil.toFileObject(sourceFile); 107 if (fo == null) { 108 sourceFile.renameTo(new File (sourceFile.getParentFile(), newName)); 110 return; 111 } 112 FileLock lock = null; 113 try { 114 lock = fo.lock(); 115 try { 116 FilesystemHandler.ignoreEvents(true); 117 fo.rename(lock, newName, null); 118 } finally { 119 FilesystemHandler.ignoreEvents(false); 120 } 121 } finally { 122 if (lock != null) { 123 lock.releaseLock(); 124 } 125 } 126 } 127 128 private static class LockedOutputStream extends OutputStream { 129 130 private final OutputStream peer; 131 private final FileLock lock; 132 133 public LockedOutputStream(FileLock lock, OutputStream peer) { 134 this.lock = lock; 135 this.peer = peer; 136 } 137 138 public void close() throws IOException { 139 lock.releaseLock(); 140 try { 141 FilesystemHandler.ignoreEvents(true); 142 peer.close(); 143 } finally { 144 FilesystemHandler.ignoreEvents(false); 145 } 146 } 147 148 public void flush() throws IOException { 149 peer.flush(); 150 } 151 152 public void write(byte b[]) throws IOException { 153 peer.write(b); 154 } 155 156 public void write(byte b[], int off, int len) throws IOException { 157 peer.write(b, off, len); 158 } 159 160 public void write(int b) throws IOException { 161 peer.write(b); 162 } 163 } 164 } 165 | Popular Tags |