KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > UserManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.components;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 public class UserManager {
27
28     static UserManager instance;
29     
30     HashMap JavaDoc passwords = new HashMap JavaDoc();
31     HashMap JavaDoc names = new HashMap JavaDoc();
32     
33     protected UserManager(InputStream JavaDoc stream) throws IOException JavaDoc {
34         BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stream));
35         while (true) {
36             String JavaDoc line = input.readLine();
37             if (line != null) {
38                 if (!line.startsWith("#") && !line.equals("")) {
39                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line,":");
40                     String JavaDoc name = st.nextToken();
41                     String JavaDoc password = st.nextToken();
42                     passwords.put(name,password);
43                     String JavaDoc fullname = st.nextToken();
44                     names.put(name,fullname);
45                 }
46             } else {
47                 break;
48             }
49         }
50     }
51     
52     public static UserManager getInstance(InputStream JavaDoc stream) throws IOException JavaDoc {
53         if (instance == null) {
54             instance = new UserManager(stream);
55         }
56         return instance;
57     }
58
59     public boolean isValidName(String JavaDoc name) {
60         return passwords.containsKey(name);
61     }
62     
63     public boolean isValidPassword(String JavaDoc name, String JavaDoc password) {
64         String JavaDoc storedPassword = (String JavaDoc) passwords.get(name);
65         return (storedPassword != null) && (storedPassword.equals(password));
66     }
67     
68     public String JavaDoc getFullName(String JavaDoc name) {
69         return (String JavaDoc) names.get(name);
70     }
71 }
72
Popular Tags