1 19 20 package org.netbeans.lib.cvsclient.connection; 21 22 import org.netbeans.lib.cvsclient.file.FileUtils; 23 24 import java.io.*; 25 import java.util.*; 26 27 32 public final class PasswordsFile { 33 34 40 public static String findPassword(String cvsRootString) { 41 File passFile = new File(System.getProperty("cvs.passfile", System.getProperty("user.home") + "/.cvspass")); 42 BufferedReader reader = null; 43 String password = null; 44 45 try { 46 reader = new BufferedReader(new FileReader(passFile)); 47 String line; 48 while ((line = reader.readLine()) != null) { 49 line = normalize(line); 50 if (line.startsWith(cvsRootString+" ")) { 51 password = line.substring(cvsRootString.length() + 1); 52 break; 53 } 54 } 55 } catch (IOException e) { 56 return null; 57 } 58 finally { 59 if (reader != null) { 60 try { reader.close(); } catch (IOException e) {} 61 } 62 } 63 return password; 64 65 } 66 67 70 public static Collection listRoots(String prefix) { 71 72 List roots = new ArrayList(); 73 74 File passFile = new File(System.getProperty("cvs.passfile", System.getProperty("user.home") + "/.cvspass")); 75 BufferedReader reader = null; 76 try { 77 reader = new BufferedReader(new FileReader(passFile)); 78 String line; 79 while ((line = reader.readLine()) != null) { 80 line = normalize(line); 81 String elements[] = line.split(" "); if (elements[0].startsWith(prefix)) { 83 roots.add(elements[0]); 84 } 85 } 86 } catch (IOException e) { 87 return Collections.EMPTY_SET; 88 } 89 finally { 90 if (reader != null) { 91 try { reader.close(); } catch (IOException e) {} 92 } 93 } 94 return roots; 95 } 96 97 105 public static void storePassword(String cvsRootString, String encodedPassword) throws IOException { 106 File passFile = new File(System.getProperty("cvs.passfile", 107 System.getProperty("user.home") + File.separatorChar + 108 ".cvspass")); 109 BufferedWriter writer = null; 110 BufferedReader reader = null; 111 try { 112 final String LF = System.getProperty("line.separator"); if (passFile.createNewFile()) { 114 writer = new BufferedWriter(new FileWriter(passFile)); 115 writer.write(cvsRootString + " " + encodedPassword + LF); 116 writer.close(); 117 } 118 else { 119 File tempFile = File.createTempFile("cvs", "tmp"); 120 reader = new BufferedReader(new FileReader(passFile)); 121 writer = new BufferedWriter(new FileWriter(tempFile)); 122 String line; 123 boolean stored = false; 124 while ((line = reader.readLine()) != null) { 125 if (normalize(line).startsWith(cvsRootString + " ")) { 126 if (stored == false) { 127 writer.write(cvsRootString + " " + encodedPassword + LF); 128 stored = true; 129 } 130 } 131 else { 132 writer.write(line + LF); 133 } 134 } 135 if (stored == false) { 136 writer.write(cvsRootString + " " + encodedPassword + LF); 137 } 138 reader.close(); 139 writer.flush(); 140 writer.close(); 141 142 FileUtils.copyFile(tempFile, passFile); 144 tempFile.delete(); 145 } 146 } 147 finally { 148 try { 149 if (writer != null) { 150 writer.close(); 151 } 152 if (reader != null) { 153 reader.close(); 154 } 155 } 156 catch (Exception e) { 157 } 159 } 160 } 161 162 166 private static String normalize(String line) { 167 if (line.startsWith("/1 ")) { line = line.substring("/1 ".length()); } 170 return line; 171 } 172 } 173 | Popular Tags |