KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > java > dev > cvsrootselector > 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
21 package net.java.dev.cvsrootselector;
22
23 import java.io.*;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import org.netbeans.api.progress.ProgressHandle;
29 import org.netbeans.api.progress.ProgressHandleFactory;
30 import org.openide.ErrorManager;
31 import org.openide.awt.StatusDisplayer;
32
33 public class CvsRootRewriter {
34     private static final String JavaDoc CVS_FOLDER = "CVS";
35     private static final String JavaDoc ROOT_FILE = "Root";
36
37     private final File file;
38     private final String JavaDoc newRoot;
39     private ProgressHandle handle;
40     
41     public CvsRootRewriter(File file, String JavaDoc newRoot) {
42         this.file = file;
43         this.newRoot = newRoot;
44     }
45     
46     public void rewrite() {
47         StatusDisplayer.getDefault().setStatusText("");
48         
49         try {
50             final Collection JavaDoc toRewrite = scanForFiles();
51             rewriteFolders(toRewrite);
52             
53             StatusDisplayer.getDefault().setStatusText("CVS Root rewritten for " +
54                     toRewrite.size() + " folders");
55         } catch (IOException ex) {
56             StatusDisplayer.getDefault().setStatusText("CVS Root failed with: " + ex.getMessage());
57         };
58     }
59     
60     private Collection JavaDoc scanForFiles() throws IOException {
61         Collection JavaDoc toRewrite = new HashSet JavaDoc();
62         Collection JavaDoc toScan = new HashSet JavaDoc();
63         toScan.add(file);
64         
65         handle = ProgressHandleFactory.createHandle("Rewriting roots...");
66         
67         int i = 0;
68         int totalSize = 1;
69         
70         handle.start(100);
71         int lastValue = 0;
72         
73         do {
74             Collection JavaDoc toAdd = new HashSet JavaDoc();
75             
76             for (Iterator JavaDoc it = toScan.iterator(); it.hasNext(); i++) {
77                 File folder = (File)it.next();
78                 it.remove();
79                 
80                 File rootFile = getCvsRootFile(folder);
81                 
82                 if (rootFile == null) {
83                     continue;
84                 }
85                 
86                 handle.progress("Scanning " + folder.getPath() + " ...");
87                 
88                 toRewrite.add(rootFile);
89                 toAdd.addAll(Arrays.asList(folder.listFiles(new FileFilter() {
90                     public boolean accept(File file) {
91                         return file.isDirectory();
92                     }
93                 })));
94             }
95             
96             toScan.addAll(toAdd);
97             totalSize += toScan.size();
98             
99             lastValue = Math.max(lastValue, (int)((i / (double)totalSize) * 50));
100             handle.progress(lastValue);
101         } while (!toScan.isEmpty());
102         
103         return toRewrite;
104     }
105     
106     private void rewriteFolders(final Collection JavaDoc toRewrite) {
107         int i = 0;
108         
109         for (Iterator JavaDoc it = toRewrite.iterator(); it.hasNext();) {
110             File rootFile = (File)it.next();
111             
112             if (rootFile.exists() && rootFile.canWrite()) {
113                 PrintStream ps = null;
114                 
115                 try {
116                     ps = new PrintStream(new BufferedOutputStream(
117                             new FileOutputStream(rootFile)));
118                     ps.println(newRoot);
119                 } catch (IOException ioe) {
120                     notify(ioe);
121                 } finally {
122                     if (ps != null) {
123                         ps.close();
124                     }
125                 }
126                 
127             }
128             
129             handle.progress("Rewriting " + rootFile.getPath() + "...",
130                     ((int)((++i / (double)toRewrite.size()) * 50)) + 50);
131         }
132         
133         handle.finish();
134     }
135     
136     static File getCvsRootFile(File folder) throws IOException {
137         if (folder == null || !folder.isDirectory()) {
138             return null;
139         }
140         
141         File rootFile = new File(folder, CVS_FOLDER + File.separator + ROOT_FILE);
142         
143         // TODO In light of issue #68881 the code should be improved to recalculate
144
// CVS/Repository files (using absolute path format) too.
145

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