KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > flow > prefs > UserRegistry


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     UserRegistry.java
18
19     Maintains a list of registered users.
20
21     Author: Ovidiu Predescu <ovidiu@apache.org>
22     Date: August 28, 2002
23
24  */

25
26 package org.apache.cocoon.samples.flow.prefs;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Maintains a list of registered users. This is a very simple class,
33  * there is no persistence of the users, but such thing should be easy
34  * to add.
35  *
36  * @author <a HREF="mailto:ovidiu@apache.org">Ovidiu Predescu</a>
37  * @since August 28, 2002
38  * @version CVS $Id: UserRegistry.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class UserRegistry
41 {
42   static UserRegistry userRegistry = new UserRegistry();
43
44   Map JavaDoc registeredUsers = new HashMap JavaDoc();
45
46   public static UserRegistry getUserRegistry()
47   {
48     return userRegistry;
49   }
50
51   protected UserRegistry()
52   {
53   }
54
55   public synchronized boolean addUser(User user)
56   {
57     if (registeredUsers.containsKey(user.getLogin()))
58       return false;
59
60     registeredUsers.put(user.getLogin(), user);
61     return true;
62   }
63
64   public boolean removeUser(User user)
65   {
66     return registeredUsers.remove(user) != null;
67   }
68
69   /**
70    * Checks is a particular login name is taken or not.
71    *
72    * @param loginName a <code>String</code> value
73    * @return true if <code>loginName</code> is taken, false otherwise
74    */

75   public boolean isLoginNameTaken(String JavaDoc loginName)
76   {
77     return registeredUsers.get(loginName) != null;
78   }
79
80   /**
81    * Returns the {@link User} object which represents an user. Note that
82    * we require a password to be present, to avoid presenting private
83    * information to anyone.
84    *
85    * @param loginName a <code>String</code> value
86    * @param password a <code>String</code> value
87    * @return an <code>User</code> value
88    */

89   public User getUserWithLogin(String JavaDoc loginName, String JavaDoc password)
90   {
91     User user = (User)registeredUsers.get(loginName);
92
93     if (user == null)
94       return null;
95
96     return password.equals(user.getPassword()) ? user : null;
97   }
98 }
99
Popular Tags