KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > security > management > DefaultUserManagerJmxAdapterImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.security.management;
19
20 import java.security.Principal JavaDoc;
21 import java.security.acl.Group JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.sape.carbon.core.component.ComponentConfiguration;
29 import org.sape.carbon.core.component.lifecycle.Configurable;
30 import org.sape.carbon.core.config.InvalidConfigurationException;
31 import org.sape.carbon.core.exception.InvalidParameterException;
32
33 /**
34  * Service used to provide JMX management of the user store.
35  *
36  * <p>
37  * Since the standard UserManager service has strongly typed parameters on
38  * all of its method, a default JMX console cannot effectively administer
39  * the user store. This interface provides methods to administer the
40  * UserStore that users only simple java parameters. All returned values
41  * can have their <code>toString</code> method called to get back useful
42  * information.
43  * </p>
44  *
45  * @author $Author: dvoet $ $Date: 2003/10/28 19:02:00 $
46  * @version $Revision: 1.8 $
47  *
48  * @stereotype interface
49  * @since carbon 1.2
50  */

51 public class DefaultUserManagerJmxAdapterImpl
52     implements UserManagerJmxAdapter, Configurable {
53     /** Holds a reference to the configuration. */
54     protected UserManagerJmxAdapterConfiguration config;
55
56     /**
57      * Creates a new user in the datastore given the name and password.
58      * Since the adapter only takes simple java types, only the simple
59      * plaintext password is accepted. This interface does not support
60      * certificates, bio-metric information, or any other type of
61      * complicated credentail.
62      *
63      * @param userName name of the new user to add
64      * @param password the password to associated with the user
65      *
66      * @throws DuplicatePrincipalException indicates the user already
67      * exists
68      * @throws InvalidParameterException indicates a null parameter was
69      * passed in
70      */

71     public void createUser(String JavaDoc userName, Map JavaDoc userInfo)
72         throws DuplicatePrincipalException, SecurityManagementDataStoreException {
73         if (userName == null) {
74             throw new InvalidParameterException(
75                 this.getClass(),
76                 "userName was an empty String");
77         }
78
79         UserManager userManager = config.getUserManager();
80
81         userManager.createUser(userName, userInfo);
82     }
83
84     /**
85      * Removes a user from the datastore with the given username.
86      *
87      * @param userName name of the new user to remove
88      *
89      * @throws UnknownPrincipalException indicates the principalName given
90      * does not represent a user
91      * @throws InvalidParameterException indicates a null parameter was
92      * passed in
93      */

94     public void removeUser(String JavaDoc userName)
95         throws UnknownPrincipalException, SecurityManagementDataStoreException {
96         if (userName == null) {
97             throw new InvalidParameterException(
98                 this.getClass(),
99                 "userName was an empty String");
100         }
101
102         UserManager userManager = config.getUserManager();
103
104         Principal JavaDoc user = userManager.retreiveUser(userName);
105
106         if (user == null) {
107             throw new UnknownPrincipalException(
108                 this.getClass(), userName);
109         }
110
111         userManager.removeUser(user);
112     }
113
114     /**
115      * Updates the password for an existing user. Since the adapter only
116      * takes simple java types, only the simple plaintext password is
117      * accepted. This interface does not support certificates,
118      * bio-metric information, or any other type of complicated
119      * credentail.
120      *
121      * @param userName name of the new user to add
122      * @param password the password to associated with the user
123      *
124      * @throws UnknownPrincipalException indicates the principalName given
125      * does not represent a user
126      * @throws InvalidParameterException indicates a null parameter was
127      * passed in
128      */

129     public void updatePassword(String JavaDoc userName, String JavaDoc password)
130         throws UnknownPrincipalException, SecurityManagementDataStoreException {
131         if (userName == null) {
132             throw new InvalidParameterException(
133                 this.getClass(),
134                 "userName was an empty String");
135         }
136
137         UserManager userManager = config.getUserManager();
138
139         Principal JavaDoc user = userManager.retreiveUser(userName);
140
141         if (user == null) {
142             throw new UnknownPrincipalException(
143                 this.getClass(),
144                 user.getName());
145         }
146
147         userManager.updateCredential(user, password);
148     }
149
150     /**
151      * Creates a new group with the given user name.
152      *
153      * @param groupName the name of the new group to create
154      *
155      * @throws DuplicateGroupException indicates a group with the given
156      * principal already exists in this user store.
157      * @throws InvalidParameterException indicates a null parameter was
158      * passed in
159      */

160     public void createGroup(String JavaDoc groupName)
161         throws DuplicateGroupException, SecurityManagementDataStoreException {
162         if (groupName == null) {
163             throw new InvalidParameterException(
164                 this.getClass(),
165                 "groupName was an empty String");
166         }
167
168         UserManager userManager = config.getUserManager();
169
170         userManager.createGroup(groupName);
171     }
172
173     /**
174      * Removes a group with the given name.
175      *
176      * @param groupName the name of the group to remove
177      *
178      * @throws UnknownGroupException indicates the group does not exist in
179      * the user store.
180      * @throws InvalidParameterException indicates a null parameter was
181      * passed in
182      */

183     public void removeGroup(String JavaDoc groupName)
184         throws UnknownGroupException, SecurityManagementDataStoreException {
185         if (groupName == null) {
186             throw new InvalidParameterException(
187                 this.getClass(),
188                 "groupName was an empty String");
189         }
190
191         UserManager userManager = config.getUserManager();
192
193         Group JavaDoc group = userManager.retreiveGroup(groupName);
194
195         if (group == null) {
196             throw new UnknownGroupException(
197                 this.getClass(), group.getName());
198         }
199
200         userManager.removeGroup(group);
201     }
202
203     /**
204      * Adds a user to a group.
205      *
206      * @param userName name of the new user to add to the group
207      * @param groupName the name of the group to add to
208      *
209      * @return if use the user store has changed
210      *
211      * @throws UnknownPrincipalException indicates the principalName given
212      * does not represent a user
213      * @throws InvalidParameterException indicates a null parameter was
214      * passed in
215      * @throws UnknownGroupException indicates the group does not exist in
216      * the user store.
217      */

218     public boolean addUserToGroup(String JavaDoc userName, String JavaDoc groupName)
219         throws UnknownPrincipalException, SecurityManagementDataStoreException {
220         if (userName == null) {
221             throw new InvalidParameterException(
222                 this.getClass(),
223                 "userName was an empty String");
224         }
225
226         if (groupName == null) {
227             throw new InvalidParameterException(
228                 this.getClass(),
229                 "groupName was an empty String");
230         }
231
232         boolean result = false;
233         UserManager userManager = config.getUserManager();
234
235         Principal JavaDoc user = userManager.retreiveUser(userName);
236
237         if (user == null) {
238             throw new UnknownPrincipalException(
239                 this.getClass(), user.getName());
240         }
241
242         Group JavaDoc group = userManager.retreiveGroup(groupName);
243
244         if (group == null) {
245             throw new UnknownGroupException(
246                 this.getClass(), group.getName());
247         }
248
249         result = userManager.addPrincipalToGroup(user, group);
250
251         return result;
252     }
253
254     /**
255      * Adds a group to a group.
256      *
257      * @param childGroupName name of the child group to add to parent
258      * @param parentGroupName the name of the group to add to
259      *
260      * @return if use the user store has changed
261      *
262      * @throws UnknownPrincipalException indicates the principalName given
263      * does not represent a user
264      * @throws UnknownGroupException indicates the group does not exist in
265      * the user store.
266      * @throws InvalidParameterException indicates a null parameter was
267      * passed in
268      */

269     public boolean addGroupToGroup(
270         String JavaDoc childGroupName, String JavaDoc parentGroupName)
271         throws UnknownPrincipalException, UnknownGroupException, SecurityManagementDataStoreException {
272         if (childGroupName == null) {
273             throw new InvalidParameterException(
274                 this.getClass(),
275                 "childGroupName was an empty String");
276         }
277
278         if (parentGroupName == null) {
279             throw new InvalidParameterException(
280                 this.getClass(),
281                 "parentGroupName was an empty String");
282         }
283
284         boolean result = false;
285         UserManager userManager = config.getUserManager();
286
287         Group JavaDoc childGroup = userManager.retreiveGroup(childGroupName);
288
289         if (childGroup == null) {
290             throw new UnknownGroupException(
291                 this.getClass(),
292                 childGroup.getName());
293         }
294
295         Group JavaDoc parentGroup = userManager.retreiveGroup(parentGroupName);
296
297         if (parentGroup == null) {
298             throw new UnknownGroupException(
299                 this.getClass(),
300                 parentGroup.getName());
301         }
302
303         result = userManager.addPrincipalToGroup(childGroup, parentGroup);
304
305         return result;
306     }
307
308     /**
309      * Removes a user from a group.
310      *
311      * @param userName name of the new user to remove from the group
312      * @param groupName the name of the group to remove from
313      *
314      * @return if use the user store has changed
315      *
316      * @throws UnknownPrincipalException indicates the principalName given
317      * does not represent a user
318      * @throws UnknownGroupException indicates the group does not exist in
319      * the user store.
320      * @throws InvalidParameterException indicates a null parameter was
321      * passed in
322      */

323     public boolean removeUserFromGroup(String JavaDoc userName, String JavaDoc groupName)
324         throws UnknownPrincipalException, UnknownGroupException, SecurityManagementDataStoreException {
325         if (userName == null) {
326             throw new InvalidParameterException(
327                 this.getClass(),
328                 "userName was an empty String");
329         }
330
331         if (groupName == null) {
332             throw new InvalidParameterException(
333                 this.getClass(),
334                 "groupName was an empty String");
335         }
336
337         boolean result = false;
338         UserManager userManager = config.getUserManager();
339
340         Principal JavaDoc user = userManager.retreiveUser(userName);
341
342         if (user == null) {
343             throw new UnknownPrincipalException(
344                 this.getClass(),
345                 user.getName());
346         }
347
348         Group JavaDoc group = userManager.retreiveGroup(groupName);
349
350         if (group == null) {
351             throw new UnknownGroupException(
352                 this.getClass(),
353                 group.getName());
354         }
355
356         result = userManager.removePrincipalFromGroup(user, group);
357
358         return result;
359     }
360
361     /**
362      * Removes a group from a group.
363      *
364      * @param childGroupName name of the child group to remove from parent
365      * @param parentGroupName the name of the group to from from
366      *
367      * @return if use the user store has changed
368      *
369      * @throws UnknownGroupException indicates the group does not exist in
370      * the user store.
371      * @throws UnknownPrincipalException indicates the principalName given
372      * does not represent a user
373      * @throws InvalidParameterException indicates a null parameter was
374      * passed in
375      */

376     public boolean removeGroupFromGroup(
377         String JavaDoc childGroupName, String JavaDoc parentGroupName)
378         throws UnknownGroupException, UnknownPrincipalException, SecurityManagementDataStoreException {
379         if (childGroupName == null) {
380             throw new InvalidParameterException(
381                 this.getClass(),
382                 "childGroupName was an empty String");
383         }
384
385         if (parentGroupName == null) {
386             throw new InvalidParameterException(
387                 this.getClass(),
388                 "parentGroupName was an empty String");
389         }
390
391         boolean result = false;
392         UserManager userManager = config.getUserManager();
393
394         Group JavaDoc childGroup = userManager.retreiveGroup(childGroupName);
395
396         if (childGroup == null) {
397             throw new UnknownGroupException(
398                 this.getClass(),
399                 childGroup.getName());
400         }
401
402         Group JavaDoc parentGroup = userManager.retreiveGroup(parentGroupName);
403
404         if (parentGroup == null) {
405             throw new UnknownGroupException(
406                 this.getClass(),
407                 parentGroup.getName());
408         }
409
410         result =
411             userManager.removePrincipalFromGroup(childGroup, parentGroup);
412
413         return result;
414     }
415
416     /**
417      * Retreives all names of the members (users and groups) of a group.
418      *
419      * @return all member names as Strings
420      */

421     public Set JavaDoc retreiveAllUserNames() throws SecurityManagementDataStoreException {
422         UserManager userManager = config.getUserManager();
423
424         return userManager.retreiveAllUserNames();
425     }
426
427     /**
428      * Retreives all user names in the system.
429      *
430      * @return all user names in the system as Strings
431      */

432     public Set JavaDoc retreiveAllGroupNames() throws SecurityManagementDataStoreException {
433         UserManager userManager = config.getUserManager();
434
435         return userManager.retreiveAllGroupNames();
436     }
437
438     /**
439      * Retreives all names of the members (users and groups) of a group.
440      *
441      * @param groupName name of the group to return
442      *
443      * @return all member names as Strings
444      *
445      * @throws UnknownGroupException indicates the group does not exist in
446      * the user store.
447      * @throws InvalidParameterException indicates a null parameter was
448      * passed in
449      */

450     public Collection JavaDoc retreiveAllMemberNames(String JavaDoc groupName)
451         throws UnknownGroupException, SecurityManagementDataStoreException {
452         if (groupName == null) {
453             throw new InvalidParameterException(
454                 this.getClass(),
455                 "groupName was an empty String");
456         }
457
458         UserManager userManager = config.getUserManager();
459
460         Group JavaDoc group = userManager.retreiveGroup(groupName);
461
462         if (group == null) {
463             throw new UnknownGroupException(
464                 this.getClass(),
465                 group.getName());
466         }
467
468         Enumeration JavaDoc membersEnumeration = group.members();
469         Set JavaDoc membersSet = new HashSet JavaDoc();
470
471         while (membersEnumeration.hasMoreElements()) {
472             Principal JavaDoc member =
473                 (Principal JavaDoc) membersEnumeration.nextElement();
474
475             membersSet.add(member.getName());
476         }
477
478         return membersSet;
479     }
480
481     /**
482      * Configures the service.
483      *
484      * @param configuration
485      * <code>UserManagerJmxAdapterConfiguration</code>
486      * configuration object for the service
487      *
488      * @throws InvalidConfigurationException indicates a null parameter
489      * was passed in
490      */

491     public void configure(ComponentConfiguration configuration) {
492
493         try {
494             this.config =
495                 (UserManagerJmxAdapterConfiguration) configuration;
496         } catch (ClassCastException JavaDoc cce) {
497             throw new InvalidConfigurationException(
498                 this.getClass(), configuration.getConfigurationName(),
499                 "ComponentConfiguration",
500                 "Specifed UserManagerJmxAdapterConfiguration does not "
501                     + "implement correct interface.",
502                 cce);
503         }
504     }
505 }
506
Popular Tags