1 11 12 package org.jivesoftware.messenger.user; 13 14 import org.jivesoftware.messenger.event.UserEventDispatcher; 15 import org.jivesoftware.stringprep.Stringprep; 16 import org.jivesoftware.stringprep.StringprepException; 17 import org.jivesoftware.util.*; 18 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Set ; 22 23 29 public class UserManager { 30 31 private static Cache userCache; 32 private static UserProvider provider; 33 private static UserManager instance = new UserManager(); 34 35 static { 36 CacheManager.initializeCache("userCache", 512 * 1024); 38 CacheManager.initializeCache("username2roster", 512 * 1024); 39 userCache = CacheManager.getCache("userCache"); 40 String className = JiveGlobals.getXMLProperty("provider.user.className", 42 "org.jivesoftware.messenger.user.DefaultUserProvider"); 43 try { 44 Class c = ClassUtils.forName(className); 45 provider = (UserProvider)c.newInstance(); 46 } 47 catch (Exception e) { 48 Log.error("Error loading user provider: " + className, e); 49 provider = new DefaultUserProvider(); 50 } 51 } 52 53 61 public static UserProvider getUserProvider() { 62 return provider; 63 } 64 65 70 public static UserManager getInstance() { 71 return instance; 72 } 73 74 private UserManager() { 75 76 } 77 78 91 public User createUser(String username, String password, String name, String email) 92 throws UserAlreadyExistsException 93 { 94 if (provider.isReadOnly()) { 95 throw new UnsupportedOperationException ("User provider is read-only."); 96 } 97 try { 99 username = Stringprep.nodeprep(username); 100 } 101 catch (StringprepException se) { 102 throw new IllegalArgumentException ("Invalid username: " + username, se); 103 } 104 User user = provider.createUser(username, password, name, email); 105 userCache.put(username, user); 106 107 UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created, 109 Collections.emptyMap()); 110 111 return user; 112 } 113 114 119 public void deleteUser(User user) { 120 if (provider.isReadOnly()) { 121 throw new UnsupportedOperationException ("User provider is read-only."); 122 } 123 124 String username = user.getUsername(); 125 try { 127 username = Stringprep.nodeprep(username); 128 } 129 catch (StringprepException se) { 130 throw new IllegalArgumentException ("Invalid username: " + username, se); 131 } 132 133 UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting, 135 Collections.emptyMap()); 136 137 provider.deleteUser(user.getUsername()); 138 userCache.remove(user.getUsername()); 140 } 141 142 149 public User getUser(String username) throws UserNotFoundException { 150 username = username.trim().toLowerCase(); 152 User user = (User) userCache.get(username); 153 if (user == null) { 154 synchronized(username.intern()) { 155 user = (User) userCache.get(username); 156 if (user == null) { 157 user = provider.loadUser(username); 158 userCache.put(username, user); 159 } 160 } 161 } 162 return user; 163 } 164 165 170 public int getUserCount() { 171 return provider.getUserCount(); 172 } 173 174 179 public Collection <User> getUsers() { 180 return provider.getUsers(); 181 } 182 183 195 public Collection <User> getUsers(int startIndex, int numResults) { 196 return provider.getUsers(startIndex, numResults); 197 } 198 199 213 public Set <String > getSearchFields() throws UnsupportedOperationException { 214 return provider.getSearchFields(); 215 } 216 217 232 public Collection <User> findUsers(Set <String > fields, String query) 233 throws UnsupportedOperationException 234 { 235 return provider.findUsers(fields, query); 236 } 237 } | Popular Tags |