1 20 package org.netbeans.modules.versioning.system.cvss.ui.actions; 21 22 import java.io.*; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 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 38 public class CvsRootRewriter { 39 private static final String CVS_FOLDER = "CVS"; private static final String ROOT_FILE = "Root"; 42 private final File file; 43 private final String newRoot; 44 private ProgressHandle handle; 45 46 public CvsRootRewriter(File file, String newRoot) { 47 this.file = file; 48 this.newRoot = newRoot; 49 } 50 51 public void rewrite() { 52 StatusDisplayer.getDefault().setStatusText(""); 54 try { 55 final Collection toRewrite = scanForFiles(); 56 rewriteFolders(toRewrite); 57 58 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Success")); } catch (IOException ex) { 60 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Failure", ex.getMessage())); }; 62 } 63 64 private Collection scanForFiles() throws IOException { 65 Collection <File> toRewrite = new HashSet <File>(); 66 Collection <File> toScan = new HashSet <File>(); 67 toScan.add(file); 68 69 handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(CvsRootRewriter.class, "MSG_CVSRootRewriter_Progress")); 71 int i = 0; 72 int totalSize = 1; 73 74 handle.start(100); 75 int lastValue = 0; 76 77 do { 78 Collection <File> toAdd = new HashSet <File>(); 79 80 for (Iterator 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())); 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 toRewrite) { 111 int i = 0; 112 113 for (Iterator 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 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 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 t) { 204 ErrorManager.getDefault().notify(ErrorManager.ERROR, t); 205 } 206 } | Popular Tags |