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