001 package com.croftsoft.apps.chat.user.seri;
002
003 import java.io.*;
004 import java.util.*;
005
006 import com.croftsoft.core.lang.NullArgumentException;
007 import com.croftsoft.core.security.Authentication;
008
009 import com.croftsoft.apps.chat.user.User;
010 import com.croftsoft.apps.chat.user.UserId;
011 import com.croftsoft.apps.chat.user.UserStore;
012
013 /*********************************************************************
014 * Serializable UserStore implementation.
015 *
016 * @version
017 * 2003-06-11
018 * @since
019 * 2003-06-07
020 * @author
021 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
022 *********************************************************************/
023
024 public final class SeriUserStore
025 implements Serializable, UserStore
026 //////////////////////////////////////////////////////////////////////
027 //////////////////////////////////////////////////////////////////////
028 {
029
030 private static final long serialVersionUID = 0L;
031
032 //
033
034 private final Map userIdToUserMap;
035
036 private final Map usernameToUserIdMap;
037
038 private final Random random;
039
040 //
041
042 private UserId [ ] userIds;
043
044 //////////////////////////////////////////////////////////////////////
045 //////////////////////////////////////////////////////////////////////
046
047 public SeriUserStore ( )
048 //////////////////////////////////////////////////////////////////////
049 {
050 userIdToUserMap = new HashMap ( );
051
052 usernameToUserIdMap = new HashMap ( );
053
054 random = new Random ( );
055
056 userIds = new UserId [ 0 ];
057 }
058
059 //////////////////////////////////////////////////////////////////////
060 //////////////////////////////////////////////////////////////////////
061
062 public User getUser ( Authentication authentication )
063 //////////////////////////////////////////////////////////////////////
064 {
065 NullArgumentException.check ( authentication );
066
067 UserId userId = getUserId ( authentication.getUsername ( ) );
068
069 if ( userId == null )
070 {
071 return null;
072 }
073
074 User user = getUser ( userId );
075
076 if ( user == null )
077 {
078 return null;
079 }
080
081 if ( !user.getPassword ( ).equals (
082 authentication.getPassword ( ) ) )
083 {
084 return null;
085 }
086
087 return user;
088 }
089
090 public User getUser ( UserId userId )
091 //////////////////////////////////////////////////////////////////////
092 {
093 NullArgumentException.check ( userId );
094
095 return ( User ) userIdToUserMap.get ( userId );
096 }
097
098 public UserId getUserId ( String username )
099 //////////////////////////////////////////////////////////////////////
100 {
101 return ( UserId ) usernameToUserIdMap.get ( username );
102 }
103
104 public UserId [ ] getUserIds ( )
105 //////////////////////////////////////////////////////////////////////
106 {
107 return userIds;
108 }
109
110 //////////////////////////////////////////////////////////////////////
111 //////////////////////////////////////////////////////////////////////
112
113 public synchronized UserId createUser (
114 Authentication authentication )
115 //////////////////////////////////////////////////////////////////////
116 {
117 String username = authentication.getUsername ( );
118
119 String password = authentication.getPassword ( );
120
121 NullArgumentException.check ( username );
122
123 if ( usernameToUserIdMap.containsKey ( username ) )
124 {
125 throw new IllegalArgumentException ( );
126 }
127
128 NullArgumentException.check ( password );
129
130 UserId userId = new SeriUserId ( random.nextLong ( ) );
131
132 User user = new SeriUser ( userId, username, password );
133
134 userIdToUserMap.put ( userId, user );
135
136 usernameToUserIdMap.put ( username, userId );
137
138 userIds = ( UserId [ ] )
139 userIdToUserMap.keySet ( ).toArray ( new UserId [ 0 ] );
140
141 return userId;
142 }
143
144 public synchronized boolean removeUser ( UserId userId )
145 //////////////////////////////////////////////////////////////////////
146 {
147 User user = getUser ( userId );
148
149 if ( user == null )
150 {
151 return false;
152 }
153
154 usernameToUserIdMap.remove ( user.getUsername ( ) );
155
156 userIdToUserMap.remove ( userId );
157
158 userIds = ( UserId [ ] )
159 userIdToUserMap.keySet ( ).toArray ( new UserId [ 0 ] );
160
161 return true;
162 }
163
164 //////////////////////////////////////////////////////////////////////
165 //////////////////////////////////////////////////////////////////////
166 }