KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > CvsRootRewriter


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 the CVSROOT Selector (RFE #65366).
16  * The Initial Developer of the Original Software is Michael Nascimento Santos.
17  * Portions created by Michael Nascimento Santos are Copyright (C) 2005.
18  * All Rights Reserved.
19  */

20 package org.netbeans.modules.versioning.system.cvss.ui.actions;
21
22 import java.io.*;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import org.netbeans.api.progress.ProgressHandle;
28 import org.netbeans.api.progress.ProgressHandleFactory;
29 import org.openide.ErrorManager;
30 import org.openide.util.NbBundle;
31 import org.openide.awt.StatusDisplayer;
32
33 /**
34  * Rewrites CVS/Root files with new content. Acts on one root folder recursively.
35  *
36  * @author Michael Nascimento Santos
37  */

38 public class CvsRootRewriter {
39     private static final String JavaDoc CVS_FOLDER = "CVS"; // NOI18N
40
private static final String JavaDoc ROOT_FILE = "Root"; // NOI18N
41

42     private final File file;
43     private final String JavaDoc newRoot;
44     private ProgressHandle handle;
45     
46     public CvsRootRewriter(File file, String JavaDoc newRoot) {
47         this.file = file;
48         this.newRoot = newRoot;
49     }
50     
51     public void rewrite() {
52         StatusDisplayer.getDefault().setStatusText(""); // NOI18N
53

54         try {
55             final Collection JavaDoc toRewrite = scanForFiles();
56             rewriteFolders(toRewrite);
57             
58             StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Success")); // NOI18N
59
} catch (IOException ex) {
60             StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Failure", ex.getMessage())); // NOI18N
61
};
62     }
63     
64     private Collection JavaDoc scanForFiles() throws IOException {
65         Collection JavaDoc<File> toRewrite = new HashSet JavaDoc<File>();
66         Collection JavaDoc<File> toScan = new HashSet JavaDoc<File>();
67         toScan.add(file);
68         
69         handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Progress")); // NOI18N
70

71         int i = 0;
72         int totalSize = 1;
73         
74         handle.start(100);
75         int lastValue = 0;
76         
77         do {
78             Collection JavaDoc<File> toAdd = new HashSet JavaDoc<File>();
79             
80             for (Iterator JavaDoc it = toScan.iterator(); it.hasNext(); i++) {
81                 File folder = (File)it.next();
82                 it.remove();
83                 
84                 File rootFile = getCvsRootFile(folder);
85                 
86                 if (rootFile == null) {
87                     continue;
88                 }
89                 
90                 handle.progress(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Scanning", folder.getPath())); // NOI18N
91

92                 toRewrite.add(rootFile);
93                 toAdd.addAll(Arrays.asList(folder.listFiles(new FileFilter() {
94                     public boolean accept(File file) {
95                         return file.isDirectory();
96                     }
97                 })));
98             }
99             
100             toScan.addAll(toAdd);
101             totalSize += toScan.size();
102             
103             lastValue = Math.max(lastValue, (int)((i / (double)totalSize) * 50));
104             handle.progress(lastValue);
105         } while (!toScan.isEmpty());
106         
107         return toRewrite;
108     }
109     
110     private void rewriteFolders(final Collection JavaDoc toRewrite) {
111         int i = 0;
112         
113         for (Iterator JavaDoc it = toRewrite.iterator(); it.hasNext();) {
114             File rootFile = (File)it.next();
115             
116             if (rootFile.exists() && rootFile.canWrite()) {
117                 PrintStream ps = null;
118                 
119                 try {
120                     ps = new PrintStream(new BufferedOutputStream(
121                             new FileOutputStream(rootFile)));
122                     ps.println(newRoot);
123                 } catch (IOException ioe) {
124                     notify(ioe);
125                 } finally {
126                     if (ps != null) {
127                         ps.close();
128                     }
129                 }
130                 
131             }
132             
133             handle.progress(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Rewriting", rootFile.getPath()),
134                             ((int)((++i / (double)toRewrite.size()) * 50)) + 50);
135         }
136         
137         handle.finish();
138     }
139     
140     static File getCvsRootFile(File folder) throws IOException {
141         if (folder == null || !folder.isDirectory()) {
142             return null;
143         }
144         
145         File rootFile = new File(folder, CVS_FOLDER + File.separator + ROOT_FILE);
146         
147         // TODO In light of issue #68881 the code should be improved to recalculate
148
// CVS/Repository files (using absolute path format) too.
149

150         File repositoryFile = new File(folder, CVS_FOLDER + File.separator + "Repository");
151         
152         if (repositoryFile.canRead()) {
153             InputStream in = null;
154             try {
155                 in = new FileInputStream(repositoryFile);
156                 if (in.read() == '/') {
157                     throw new IOException("#68881 Absolute CVS/Repository paths are unsupported.");
158                 }
159             } finally {
160                 if (in != null) {
161                     try {
162                         in.close();
163                     } catch (IOException alreadyClosed) {
164                     }
165                 }
166             }
167         }
168         
169         return (rootFile.exists() && !rootFile.isDirectory()) ? rootFile : null;
170     }
171     
172     static String JavaDoc getCvsRoot(File folder) throws IOException {
173         File rootFile = getCvsRootFile(folder);
174         
175         if (rootFile == null) {
176             return null;
177         }
178         
179         try {
180             BufferedReader reader = new BufferedReader(new FileReader(rootFile));
181             
182             try {
183                 try {
184                     return reader.readLine();
185                 } catch (IOException ex) {
186                     notify(ex);
187                     return null;
188                 }
189             } finally {
190                 try {
191                     reader.close();
192                 } catch (IOException ex) {
193                     notify(ex);
194                 }
195             }
196         } catch (FileNotFoundException ex) {
197             notify(ex);
198             
199             return null;
200         }
201     }
202     
203     private static void notify(Throwable JavaDoc t) {
204         ErrorManager.getDefault().notify(ErrorManager.ERROR, t);
205     }
206 }
Popular Tags