~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to javacvs/cvsmodule/src/org/netbeans/modules/versioning/system/cvss/CvsLiteFileHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.versioning.system.cvss;
 
43
 
 
44
import org.netbeans.lib.cvsclient.file.DefaultFileHandler;
 
45
import org.netbeans.lib.cvsclient.file.FileReadOnlyHandler;
 
46
import org.netbeans.modules.versioning.system.cvss.util.Utils;
 
47
import org.openide.filesystems.FileUtil;
 
48
import org.openide.filesystems.FileObject;
 
49
import org.openide.filesystems.FileLock;
 
50
import org.openide.util.Utilities;
 
51
 
 
52
import java.io.File;
 
53
import java.io.IOException;
 
54
import java.io.OutputStream;
 
55
import java.io.FileOutputStream;
 
56
 
 
57
/**
 
58
 * Cvs client library FileHandler that performs
 
59
 * operations using openide filesystems.
 
60
 *
 
61
 * <p>It writes user's data files. Folders, temporary
 
62
 * files and metadata files are written directly by
 
63
 * the cvsclient library.
 
64
 *
 
65
 * <p>It supresses FilesystemHandler event propagating
 
66
 * to cache to avoid
 
67
 *
 
68
 * @author Petr Kuzel
 
69
 */
 
70
class CvsLiteFileHandler extends DefaultFileHandler implements FileReadOnlyHandler {
 
71
 
 
72
    protected boolean createNewFile(File file) throws IOException {
 
73
        boolean exists = file.isFile();
 
74
        if (exists) {
 
75
            return false;
 
76
        } else {
 
77
            File parent = file.getParentFile();
 
78
            FileObject fo = Utils.mkfolders(parent);
 
79
            try {
 
80
                FilesystemHandler.ignoreEvents(true);
 
81
                try {
 
82
                    fo.createData(file.getName());
 
83
                } catch (IOException e) {
 
84
                    // #69639: Try File I/O instead
 
85
                    return file.createNewFile();
 
86
                }
 
87
            } finally {
 
88
                FilesystemHandler.ignoreEvents(false);
 
89
            }
 
90
            return true;
 
91
        }
 
92
    }
 
93
 
 
94
    protected OutputStream createOutputStream(File file) throws IOException {
 
95
        file = FileUtil.normalizeFile(file);
 
96
        FileObject fo = FileUtil.toFileObject(file);
 
97
        if (fo == null) {
 
98
            // #69639: Try File I/O instead
 
99
            return new FileOutputStream(file);
 
100
        }
 
101
        FileLock lock = fo.lock();
 
102
        OutputStream stream = null;
 
103
        try {
 
104
            stream = fo.getOutputStream(lock);
 
105
        } catch (IOException e) {
 
106
            lock.releaseLock();
 
107
            throw e;
 
108
        }
 
109
        return new LockedOutputStream(lock, stream);
 
110
    }
 
111
 
 
112
    public void removeLocalFile(String pathname) throws IOException {
 
113
        File fileToDelete = new File(pathname);
 
114
        fileToDelete = FileUtil.normalizeFile(fileToDelete);
 
115
        FileObject fo = FileUtil.toFileObject(fileToDelete);
 
116
        if (fo == null) {
 
117
            // #69639: Try File I/O instead
 
118
            fileToDelete.delete();
 
119
            return;
 
120
        }
 
121
        try {
 
122
            FilesystemHandler.ignoreEvents(true);
 
123
            fo.delete();
 
124
        } finally {
 
125
            FilesystemHandler.ignoreEvents(false);
 
126
        }
 
127
    }
 
128
 
 
129
    public void setFileReadOnly(File file, boolean readOnly) throws IOException {
 
130
        String [] command = new String[3];
 
131
        // TODO: update for JDK 6
 
132
        if (Utilities.isWindows()) {
 
133
            command[0] = "attrib";
 
134
            command[1] = readOnly ? "+R" : "-R";
 
135
        } else {
 
136
            command[0] = "chmod";
 
137
            command[1] = readOnly ? "u-w" : "u+w";
 
138
        }
 
139
        command[2] = file.getAbsolutePath();
 
140
        try {
 
141
            Runtime.getRuntime().exec(command);
 
142
        } catch (Exception e) {
 
143
            // probably does not work, ignore
 
144
        }
 
145
    }
 
146
        
 
147
    private static class LockedOutputStream extends OutputStream {
 
148
 
 
149
        private final OutputStream peer;
 
150
        private final FileLock lock;
 
151
 
 
152
        public LockedOutputStream(FileLock lock, OutputStream peer) {
 
153
            this.lock = lock;
 
154
            this.peer = peer;
 
155
        }
 
156
 
 
157
        public void close() throws IOException {
 
158
            lock.releaseLock();
 
159
            try {
 
160
                FilesystemHandler.ignoreEvents(true);
 
161
                peer.close();
 
162
            } finally {
 
163
                FilesystemHandler.ignoreEvents(false);
 
164
            }
 
165
        }
 
166
 
 
167
        public void flush() throws IOException {
 
168
            peer.flush();
 
169
        }
 
170
 
 
171
        public void write(byte b[]) throws IOException {
 
172
            peer.write(b);
 
173
        }
 
174
 
 
175
        public void write(byte b[], int off, int len) throws IOException {
 
176
            peer.write(b, off, len);
 
177
        }
 
178
 
 
179
        public void write(int b) throws IOException {
 
180
            peer.write(b);
 
181
        }
 
182
    }
 
183
}