01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.components;
19:
20: import java.io.BufferedReader;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.io.InputStreamReader;
24: import java.util.HashMap;
25: import java.util.StringTokenizer;
26:
27: public class UserManager {
28:
29: static UserManager instance;
30:
31: HashMap passwords = new HashMap();
32: HashMap names = new HashMap();
33:
34: protected UserManager(InputStream stream) throws IOException {
35: BufferedReader input = new BufferedReader(
36: new InputStreamReader(stream));
37: while (true) {
38: String line = input.readLine();
39: if (line != null) {
40: if (!line.startsWith("#") && !line.equals("")) {
41: StringTokenizer st = new StringTokenizer(line, ":");
42: String name = st.nextToken();
43: String password = st.nextToken();
44: passwords.put(name, password);
45: String fullname = st.nextToken();
46: names.put(name, fullname);
47: }
48: } else {
49: break;
50: }
51: }
52: }
53:
54: public static UserManager getInstance(InputStream stream)
55: throws IOException {
56: if (instance == null) {
57: instance = new UserManager(stream);
58: }
59: return instance;
60: }
61:
62: public boolean isValidName(String name) {
63: return passwords.containsKey(name);
64: }
65:
66: public boolean isValidPassword(String name, String password) {
67: String storedPassword = (String) passwords.get(name);
68: return (storedPassword != null)
69: && (storedPassword.equals(password));
70: }
71:
72: public String getFullName(String name) {
73: return (String) names.get(name);
74: }
75: }
|