KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > CvsLiteFileHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32
33 /**
34  * Cvs client library FileHandler that performs
35  * operations using openide filesystems.
36  *
37  * <p>It writes user's data files. Folders, temporary
38  * files and metadata files are written directly by
39  * the cvsclient library.
40  *
41  * <p>It supresses FilesystemHandler event propagating
42  * to cache to avoid
43  *
44  * @author Petr Kuzel
45  */

46 class CvsLiteFileHandler extends DefaultFileHandler {
47
48     protected boolean createNewFile(File JavaDoc file) throws IOException JavaDoc {
49         boolean exists = file.isFile();
50         if (exists) {
51             return false;
52         } else {
53             File JavaDoc 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 JavaDoc e) {
60                     // #69639: Try File I/O instead
61
return file.createNewFile();
62                 }
63             } finally {
64                 FilesystemHandler.ignoreEvents(false);
65             }
66             return true;
67         }
68     }
69
70     protected OutputStream JavaDoc createOutputStream(File JavaDoc file) throws IOException JavaDoc {
71         FileObject fo = FileUtil.toFileObject(file);
72         if (fo == null) {
73             // #69639: Try File I/O instead
74
return new FileOutputStream JavaDoc(file);
75         }
76         FileLock lock = fo.lock();
77         OutputStream JavaDoc stream = null;
78         try {
79             stream = fo.getOutputStream(lock);
80         } catch (IOException JavaDoc e) {
81             lock.releaseLock();
82             throw e;
83         }
84         return new LockedOutputStream(lock, stream);
85     }
86
87     public void removeLocalFile(String JavaDoc pathname) throws IOException JavaDoc {
88         File JavaDoc fileToDelete = new File JavaDoc(pathname);
89         fileToDelete = FileUtil.normalizeFile(fileToDelete);
90         FileObject fo = FileUtil.toFileObject(fileToDelete);
91         if (fo == null) {
92             // #69639: Try File I/O instead
93
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 JavaDoc pathname, String JavaDoc newName) throws IOException JavaDoc {
105         File JavaDoc sourceFile = new File JavaDoc(pathname);
106         FileObject fo = FileUtil.toFileObject(sourceFile);
107         if (fo == null) {
108             // #69639: Try File I/O instead
109
sourceFile.renameTo(new File JavaDoc(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 JavaDoc {
129
130         private final OutputStream JavaDoc peer;
131         private final FileLock lock;
132
133         public LockedOutputStream(FileLock lock, OutputStream JavaDoc peer) {
134             this.lock = lock;
135             this.peer = peer;
136         }
137
138         public void close() throws IOException JavaDoc {
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 JavaDoc {
149             peer.flush();
150         }
151
152         public void write(byte b[]) throws IOException JavaDoc {
153             peer.write(b);
154         }
155
156         public void write(byte b[], int off, int len) throws IOException JavaDoc {
157             peer.write(b, off, len);
158         }
159
160         public void write(int b) throws IOException JavaDoc {
161             peer.write(b);
162         }
163     }
164 }
165
Popular Tags