KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > connection > PasswordsFile


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
28  * Represents .cvspass passwords file.
29  *
30  * @author Petr Kuzel
31  */

32 public final class PasswordsFile {
33
34     /**
35      * Locates scrambled password for given CVS Root.
36      *
37      * @param cvsRootString identifies repository session [:method:][[user][:password]@][hostname[:[port]]]/path/to/repository
38      * @return scrambled password or <code>null</code>
39      */

40     public static String JavaDoc findPassword(String JavaDoc cvsRootString) {
41         File passFile = new File(System.getProperty("cvs.passfile", System.getProperty("user.home") + "/.cvspass"));
42         BufferedReader reader = null;
43         String JavaDoc password = null;
44
45         try {
46             reader = new BufferedReader(new FileReader(passFile));
47             String JavaDoc 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     /**
68      * List roots matching given prefix e.g. <tt>":pserver:"</tt>.
69      */

70     public static Collection listRoots(String JavaDoc 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 JavaDoc line;
79             while ((line = reader.readLine()) != null) {
80                 line = normalize(line);
81                 String JavaDoc elements[] = line.split(" "); // NOI18N
82
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     /**
98      * Writes scrambled password for given CVS root.
99      * Eliminates all previous values and possible duplicities.
100      *
101      * @param cvsRootString identifies repository session [:method:][[user][:password]@][hostname[:[port]]]/path/to/repository
102      * @param encodedPassword
103      * @throws IOException on write failure
104      */

105     public static void storePassword(String JavaDoc cvsRootString, String JavaDoc 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 JavaDoc LF = System.getProperty("line.separator"); // NOI18N
113
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 JavaDoc 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                 // copyFile needs less permissions than File.renameTo
143
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 JavaDoc e) {
157                 // ignore
158
}
159         }
160     }
161
162     /**
163      * Normalizes several possible line formats into
164      * 'normal' one that allows to apply dumb string operations.
165      */

166     private static String JavaDoc normalize(String JavaDoc line) {
167         if (line.startsWith("/1 ")) { // NOI18N
168
line = line.substring("/1 ".length()); // NOI18N
169
}
170         return line;
171     }
172 }
173
Popular Tags