1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
62: public class PasswordFile
63: {
64: private static String DEFAULT_FILE;
65: static
66: {
67: DEFAULT_FILE = System.getProperty(CramMD5Registry.PASSWORD_FILE,
68: CramMD5Registry.DEFAULT_PASSWORD_FILE);
69: }
70: private HashMap entries;
71: private File passwdFile;
72: private long lastmod;
73:
74: public PasswordFile() throws IOException
75: {
76: this(DEFAULT_FILE);
77: }
78:
79: public PasswordFile(final File pwFile) throws IOException
80: {
81: this(pwFile.getAbsolutePath());
82: }
83:
84: public PasswordFile(final String fileName) throws IOException
85: {
86: passwdFile = new File(fileName);
87: update();
88: }
89:
90: public synchronized void add(final String user, final String passwd,
91: final String[] attributes) throws IOException
92: {
93: checkCurrent();
94: if (entries.containsKey(user))
95: throw new UserAlreadyExistsException(user);
96: if (attributes.length != 5)
97: throw new IllegalArgumentException("Wrong number of attributes");
98: final String[] fields = new String[7];
99: fields[0] = user;
100: fields[1] = passwd;
101: System.arraycopy(attributes, 0, fields, 2, 5);
102: entries.put(user, fields);
103: savePasswd();
104: }
105:
106: public synchronized void changePasswd(final String user, final String passwd)
107: throws IOException
108: {
109: checkCurrent();
110: if (! entries.containsKey(user))
111: throw new NoSuchUserException(user);
112: final String[] fields = (String[]) entries.get(user);
113: fields[1] = passwd;
114: entries.remove(user);
115: entries.put(user, fields);
116: savePasswd();
117: }
118:
119: public synchronized String[] lookup(final String user) throws IOException
120: {
121: checkCurrent();
122: if (! entries.containsKey(user))
123: throw new NoSuchUserException(user);
124: return (String[]) entries.get(user);
125: }
126:
127: public synchronized boolean contains(final String s) throws IOException
128: {
129: checkCurrent();
130: return entries.containsKey(s);
131: }
132:
133: private synchronized void update() throws IOException
134: {
135: lastmod = passwdFile.lastModified();
136: readPasswd(new FileInputStream(passwdFile));
137: }
138:
139: private void checkCurrent() throws IOException
140: {
141: if (passwdFile.lastModified() > lastmod)
142: update();
143: }
144:
145: private synchronized void readPasswd(final InputStream in) throws IOException
146: {
147: final BufferedReader din = new BufferedReader(new InputStreamReader(in));
148: String line;
149: entries = new HashMap();
150: while ((line = din.readLine()) != null)
151: {
152: final String[] fields = new String[7];
153: final StringTokenizer st = new StringTokenizer(line, ":", true);
154: try
155: {
156: fields[0] = st.nextToken();
157: st.nextToken();
158: fields[1] = st.nextToken();
159: if (fields[1].equals(":"))
160: fields[1] = "";
161: else
162: st.nextToken();
163: fields[2] = st.nextToken();
164: if (fields[2].equals(":"))
165: fields[2] = "";
166: else
167: st.nextToken();
168: fields[3] = st.nextToken();
169: if (fields[3].equals(":"))
170: fields[3] = "";
171: else
172: st.nextToken();
173: fields[4] = st.nextToken();
174: if (fields[4].equals(":"))
175: fields[4] = "";
176: else
177: st.nextToken();
178: fields[5] = st.nextToken();
179: if (fields[5].equals(":"))
180: fields[5] = "";
181: else
182: st.nextToken();
183: fields[6] = st.nextToken();
184: if (fields[6].equals(":"))
185: fields[6] = "";
186: }
187: catch (NoSuchElementException x)
188: {
189: continue;
190: }
191: entries.put(fields[0], fields);
192: }
193: }
194:
195: private synchronized void savePasswd() throws IOException
196: {
197: if (passwdFile != null)
198: {
199: final FileOutputStream fos = new FileOutputStream(passwdFile);
200: PrintWriter pw = null;
201: try
202: {
203: pw = new PrintWriter(fos);
204: String key;
205: String[] fields;
206: CPStringBuilder sb;
207: int i;
208: for (Iterator it = entries.keySet().iterator(); it.hasNext();)
209: {
210: key = (String) it.next();
211: fields = (String[]) entries.get(key);
212: sb = new CPStringBuilder(fields[0]);
213: for (i = 1; i < fields.length; i++)
214: sb.append(":").append(fields[i]);
215: pw.println(sb.toString());
216: }
217: }
218: finally
219: {
220: if (pw != null)
221: try
222: {
223: pw.flush();
224: }
225: finally
226: {
227: pw.close();
228: }
229: try
230: {
231: fos.close();
232: }
233: catch (IOException ignored)
234: {
235: }
236: lastmod = passwdFile.lastModified();
237: }
238: }
239: }
240: }