1 17 18 package org.apache.james.remotemanager; 19 20 import java.util.Iterator ; 21 import java.util.HashMap ; 22 import java.util.ArrayList ; 23 import java.rmi.RemoteException ; 24 import javax.mail.internet.ParseException ; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.context.Contextualizable; 27 import org.apache.avalon.framework.context.Context; 28 import org.apache.avalon.framework.context.ContextException; 29 import org.apache.avalon.framework.component.Composable; 30 import org.apache.avalon.framework.component.ComponentManager; 31 import org.apache.avalon.framework.component.ComponentException; 32 import org.apache.avalon.framework.configuration.Configurable; 33 import org.apache.avalon.framework.configuration.Configuration; 34 import org.apache.avalon.framework.configuration.ConfigurationException; 35 import org.apache.avalon.framework.activity.Initializable; 36 import org.apache.james.services.MailServer; 37 import org.apache.james.services.UsersRepository; 38 import org.apache.james.services.UsersStore; 39 import org.apache.james.services.JamesUser; 40 import org.apache.mailet.MailAddress; 41 42 43 47 public class UserManagerImpl extends AbstractLogEnabled 48 implements UserManager, Contextualizable, Composable, Configurable, Initializable { 49 50 private MailServer mailServer; 51 private UsersStore usersStore; 52 private UsersRepository users; 53 54 55 public void contextualize(Context context) 56 throws ContextException { 57 } 58 59 public void compose(ComponentManager componentManager) 60 throws ComponentException { 61 this.mailServer = (MailServer)componentManager.lookup( "org.apache.james.services.MailServer" ); 62 this.usersStore = (UsersStore)componentManager.lookup( "org.apache.james.services.UsersStore" ); 63 this.users = usersStore.getRepository("LocalUsers"); 64 } 65 66 public void configure(Configuration configuration) 67 throws ConfigurationException { 68 } 69 70 public void initialize() 71 throws Exception { 72 } 73 74 75 76 public ArrayList getRepositoryNames() 77 throws RemoteException { 78 Iterator iterator = usersStore.getRepositoryNames(); 79 ArrayList list = new ArrayList (); 80 while (iterator.hasNext()) { 81 list.add(iterator.next()); 82 } 83 return list; 84 } 85 86 public boolean setRepository(String name) 87 throws RemoteException { 88 UsersRepository repos = usersStore.getRepository(name); 89 if (repos == null) { 90 getLogger().warn("No user repository found with name " + name); 91 return false; 92 } 93 this.users = repos; 94 getLogger().info("Set to new user repository " + name); 95 return true; 96 } 97 98 public boolean addUser(String username, 99 String password) 100 throws RemoteException { 101 if (users.contains(username)) { 102 getLogger().error("User " + username + " already exist!"); 103 throw new RemoteException ("User " + username + " already exist!"); 104 } 105 if (mailServer.addUser(username, password)) { 106 getLogger().info("User " + username + " successful added."); 107 return true; 108 } 109 getLogger().error("Error adding user " + username); 110 return false; 111 } 112 113 public boolean deleteUser(String username) 114 throws RemoteException { 115 try { 116 users.removeUser(username); 117 getLogger().info("User " + username + " successful deleted."); 118 return true; 119 } catch (Exception e) { 120 getLogger().error("Error deleting user " + username + " - " + e.getMessage()); 121 } 122 return false; 123 } 124 125 public boolean verifyUser(String username) 126 throws RemoteException { 127 return users.contains(username); 128 } 129 130 public int getCountUsers() 131 throws RemoteException { 132 return users.countUsers(); 133 } 134 135 public ArrayList getUserList() 136 throws RemoteException { 137 Iterator iterator = users.list(); 138 ArrayList list = new ArrayList (); 139 while (iterator.hasNext()) { 140 list.add(iterator.next()); 141 } 142 return list; 143 } 144 145 public boolean setPassword(String username, 146 String password) 147 throws RemoteException { 148 JamesUser user = (JamesUser)users.getUserByName(username); 149 if (user == null) { 150 getLogger().error("No such user " + username + " found!"); 151 throw new RemoteException ("No such user " + username + " found!"); 152 } 153 if (user.setPassword(password)) { 154 users.updateUser(user); 155 getLogger().info("Password for user " + username + " reset."); 156 return true; 157 } 158 getLogger().error("Error resetting password for user " + username); 159 return false; 160 } 161 162 163 public boolean setAlias(String username, 164 String alias) 165 throws RemoteException { 166 JamesUser user = (JamesUser)users.getUserByName(username); 167 if (user == null) { 168 getLogger().error("No such user " + username + " found!"); 169 throw new RemoteException ("No such user " + username + " found!"); 170 } 171 JamesUser aliasUser = (JamesUser)users.getUserByName(alias); 172 if (aliasUser == null) { 173 getLogger().error("Alias unknown to server - create that user " + alias + " first!"); 174 throw new RemoteException ("Alias unknown to server - create that user " + alias + " first!"); 175 } 176 if (user.setAlias(alias)) { 177 user.setAliasing(true); 178 users.updateUser(user); 179 getLogger().info("Alias for user " + username + " set to: " + alias); 180 return true; 181 } 182 getLogger().error("Error setting alias " + alias + " for user " + username); 183 return false; 184 } 185 186 187 public boolean unsetAlias(String username) 188 throws RemoteException { 189 JamesUser user = (JamesUser)users.getUserByName(username); 190 if (user == null) { 191 getLogger().error("No such user " + username + " found!"); 192 throw new RemoteException ("No such user " + username + " found!"); 193 } 194 if (user.getAliasing()) { 195 user.setAliasing(false); 196 users.updateUser(user); 197 getLogger().info("Alias for user " + username + " unset."); 198 return true; 199 } 200 getLogger().info("Aliasing not active for user " + username); 201 return false; 202 } 203 204 public String checkAlias(String username) 205 throws RemoteException { 206 JamesUser user = (JamesUser)users.getUserByName(username); 207 if (user == null) { 208 getLogger().error("No such user " + username + " found!"); 209 throw new RemoteException ("No such user " + username + " found!"); 210 } 211 if (user.getAliasing()) { 212 String alias = user.getAlias(); 213 getLogger().info("Alias is set to " + alias + " for user " + username); 214 return alias; 215 } 216 getLogger().info("No alias is set for this user " + username); 217 return null; 218 } 219 220 221 public boolean setForward(String username, 222 String forward) 223 throws RemoteException { 224 JamesUser user = (JamesUser)users.getUserByName(username); 225 if (user == null) { 226 getLogger().error("No such user " + username + " found!"); 227 throw new RemoteException ("No such user " + username + " found!"); 228 } 229 MailAddress forwardAddress; 230 try { 231 forwardAddress = new MailAddress(forward); 232 } catch (ParseException pe) { 233 getLogger().error("Parse exception with that email address: " + pe.getMessage()); 234 throw new RemoteException ("Parse exception with that email address: " + pe.getMessage()); 235 } 236 if (user.setForwardingDestination(forwardAddress)) { 237 user.setForwarding(true); 238 users.updateUser(user); 239 getLogger().info("Forwarding destination for " + username + " set to: " + forwardAddress.toString()); 240 return true; 241 } 242 getLogger().error("Error setting forward for user " + username); 243 return false; 244 } 245 246 public boolean unsetForward(String username) 247 throws RemoteException { 248 JamesUser user = (JamesUser)users.getUserByName(username); 249 if (user == null) { 250 getLogger().error("No such user " + username + " found!"); 251 throw new RemoteException ("No such user " + username + " found!"); 252 } 253 if (user.getForwarding()) { 254 user.setForwarding(false); 255 users.updateUser(user); 256 getLogger().info("Forward for user " + username + " unset."); 257 return true; 258 } 259 getLogger().info("Forwarding not active for user " + username); 260 return false; 261 } 262 263 public String checkForward(String username) 264 throws RemoteException { 265 JamesUser user = (JamesUser)users.getUserByName(username); 266 if (user == null) { 267 getLogger().error("No such user " + username + " found!"); 268 throw new RemoteException ("No such user " + username + " found!"); 269 } 270 if (user.getForwarding()) { 271 String forward = user.getForwardingDestination().toString(); 272 getLogger().info("Forward is set to " + forward + " for user " + username); 273 return forward; 274 } 275 getLogger().info("No forward set for user " + username); 276 return null; 277 } 278 279 } 280 | Popular Tags |