1 20 21 package net.java.dev.cvsrootselector; 22 23 import java.io.*; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 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 CVS_FOLDER = "CVS"; 35 private static final String ROOT_FILE = "Root"; 36 37 private final File file; 38 private final String newRoot; 39 private ProgressHandle handle; 40 41 public CvsRootRewriter(File file, String 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 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 scanForFiles() throws IOException { 61 Collection toRewrite = new HashSet (); 62 Collection toScan = new HashSet (); 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 toAdd = new HashSet (); 75 76 for (Iterator 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 toRewrite) { 107 int i = 0; 108 109 for (Iterator 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 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 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 t) { 200 ErrorManager.getDefault().notify(ErrorManager.ERROR, t); 201 } 202 } | Popular Tags |