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: UserRegistry.java
19:
20: Maintains a list of registered users.
21:
22: Author: Ovidiu Predescu <ovidiu@apache.org>
23: Date: August 28, 2002
24:
25: */
26:
27: package org.apache.cocoon.samples.flow.prefs;
28:
29: import java.util.HashMap;
30: import java.util.Map;
31:
32: /**
33: * Maintains a list of registered users. This is a very simple class,
34: * there is no persistence of the users, but such thing should be easy
35: * to add.
36: *
37: * @author <a href="mailto:ovidiu@apache.org">Ovidiu Predescu</a>
38: * @since August 28, 2002
39: * @version CVS $Id: UserRegistry.java 433543 2006-08-22 06:22:54Z crossley $
40: */
41: public class UserRegistry {
42: static UserRegistry userRegistry = new UserRegistry();
43:
44: Map registeredUsers = new HashMap();
45:
46: public static UserRegistry getUserRegistry() {
47: return userRegistry;
48: }
49:
50: protected UserRegistry() {
51: }
52:
53: public synchronized boolean addUser(User user) {
54: if (registeredUsers.containsKey(user.getLogin()))
55: return false;
56:
57: registeredUsers.put(user.getLogin(), user);
58: return true;
59: }
60:
61: public boolean removeUser(User user) {
62: return registeredUsers.remove(user) != null;
63: }
64:
65: /**
66: * Checks is a particular login name is taken or not.
67: *
68: * @param loginName a <code>String</code> value
69: * @return true if <code>loginName</code> is taken, false otherwise
70: */
71: public boolean isLoginNameTaken(String loginName) {
72: return registeredUsers.get(loginName) != null;
73: }
74:
75: /**
76: * Returns the {@link User} object which represents an user. Note that
77: * we require a password to be present, to avoid presenting private
78: * information to anyone.
79: *
80: * @param loginName a <code>String</code> value
81: * @param password a <code>String</code> value
82: * @return an <code>User</code> value
83: */
84: public User getUserWithLogin(String loginName, String password) {
85: User user = (User) registeredUsers.get(loginName);
86:
87: if (user == null)
88: return null;
89:
90: return password.equals(user.getPassword()) ? user : null;
91: }
92: }
|