KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > remotemanager > UserManagerImpl


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * 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 *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.remotemanager;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.rmi.RemoteException JavaDoc;
24 import javax.mail.internet.ParseException JavaDoc;
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 /**
44  * This class do all the hard work to manage the user inside James.
45  *
46  */

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 JavaDoc {
72     }
73
74
75
76     public ArrayList JavaDoc getRepositoryNames()
77             throws RemoteException JavaDoc {
78         Iterator JavaDoc iterator = usersStore.getRepositoryNames();
79         ArrayList JavaDoc list = new ArrayList JavaDoc();
80         while (iterator.hasNext()) {
81             list.add(iterator.next());
82         }
83         return list;
84     }
85
86     public boolean setRepository(String JavaDoc name)
87             throws RemoteException JavaDoc {
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 JavaDoc username,
99                            String JavaDoc password)
100             throws RemoteException JavaDoc {
101         if (users.contains(username)) {
102             getLogger().error("User " + username + " already exist!");
103             throw new RemoteException JavaDoc("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 JavaDoc username)
114             throws RemoteException JavaDoc {
115         try {
116             users.removeUser(username);
117             getLogger().info("User " + username + " successful deleted.");
118             return true;
119         } catch (Exception JavaDoc e) {
120             getLogger().error("Error deleting user " + username + " - " + e.getMessage());
121         }
122         return false;
123     }
124
125     public boolean verifyUser(String JavaDoc username)
126             throws RemoteException JavaDoc {
127         return users.contains(username);
128     }
129
130     public int getCountUsers()
131             throws RemoteException JavaDoc {
132         return users.countUsers();
133     }
134
135     public ArrayList JavaDoc getUserList()
136             throws RemoteException JavaDoc {
137         Iterator JavaDoc iterator = users.list();
138         ArrayList JavaDoc list = new ArrayList JavaDoc();
139         while (iterator.hasNext()) {
140             list.add(iterator.next());
141         }
142         return list;
143     }
144
145     public boolean setPassword(String JavaDoc username,
146                                String JavaDoc password)
147             throws RemoteException JavaDoc {
148         JamesUser user = (JamesUser)users.getUserByName(username);
149         if (user == null) {
150             getLogger().error("No such user " + username + " found!");
151             throw new RemoteException JavaDoc("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 JavaDoc username,
164                             String JavaDoc alias)
165             throws RemoteException JavaDoc {
166         JamesUser user = (JamesUser)users.getUserByName(username);
167         if (user == null) {
168             getLogger().error("No such user " + username + " found!");
169             throw new RemoteException JavaDoc("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 JavaDoc("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 JavaDoc username)
188             throws RemoteException JavaDoc {
189         JamesUser user = (JamesUser)users.getUserByName(username);
190         if (user == null) {
191             getLogger().error("No such user " + username + " found!");
192             throw new RemoteException JavaDoc("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 JavaDoc checkAlias(String JavaDoc username)
205             throws RemoteException JavaDoc {
206         JamesUser user = (JamesUser)users.getUserByName(username);
207         if (user == null) {
208             getLogger().error("No such user " + username + " found!");
209             throw new RemoteException JavaDoc("No such user " + username + " found!");
210         }
211         if (user.getAliasing()) {
212             String JavaDoc 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 JavaDoc username,
222                               String JavaDoc forward)
223             throws RemoteException JavaDoc {
224         JamesUser user = (JamesUser)users.getUserByName(username);
225         if (user == null) {
226             getLogger().error("No such user " + username + " found!");
227             throw new RemoteException JavaDoc("No such user " + username + " found!");
228         }
229         MailAddress forwardAddress;
230         try {
231             forwardAddress = new MailAddress(forward);
232         } catch (ParseException JavaDoc pe) {
233             getLogger().error("Parse exception with that email address: " + pe.getMessage());
234             throw new RemoteException JavaDoc("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 JavaDoc username)
247             throws RemoteException JavaDoc {
248         JamesUser user = (JamesUser)users.getUserByName(username);
249         if (user == null) {
250             getLogger().error("No such user " + username + " found!");
251             throw new RemoteException JavaDoc("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 JavaDoc checkForward(String JavaDoc username)
264             throws RemoteException JavaDoc {
265         JamesUser user = (JamesUser)users.getUserByName(username);
266         if (user == null) {
267             getLogger().error("No such user " + username + " found!");
268             throw new RemoteException JavaDoc("No such user " + username + " found!");
269         }
270         if (user.getForwarding()) {
271             String JavaDoc 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