Source code: org/apache/webapp/admin/users/UserUtils.java
1 /*
2 * Copyright 2002,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 package org.apache.webapp.admin.users;
19
20
21 import java.util.Arrays;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
24
25
26 /**
27 * <p>Shared utility methods for the user database administration module.</p>
28 *
29 * @author Craig R. McClanahan
30 * @version $Revision: 302726 $ $Date: 2004-02-27 09:59:07 -0500 (Fri, 27 Feb 2004) $
31 * @since 4.1
32 */
33
34 public class UserUtils {
35
36
37 // --------------------------------------------------------- Public Methods
38
39
40 /**
41 * Construct and return a GroupsForm identifying all currently defined
42 * groups in the specified user database.
43 *
44 * @param mserver MBeanServer to be consulted
45 * @param databaseName MBean Name of the user database to be consulted
46 *
47 * @exception Exception if an error occurs
48 */
49 public static GroupsForm getGroupsForm(MBeanServer mserver,
50 String databaseName)
51 throws Exception {
52
53 ObjectName dname = new ObjectName(databaseName);
54 String results[] =
55 (String[]) mserver.getAttribute(dname, "groups");
56 if (results == null) {
57 results = new String[0];
58 }
59 Arrays.sort(results);
60
61 GroupsForm groupsForm = new GroupsForm();
62 groupsForm.setDatabaseName(databaseName);
63 groupsForm.setGroups(results);
64 return (groupsForm);
65
66 }
67
68
69 /**
70 * Construct and return a RolesForm identifying all currently defined
71 * roles in the specified user database.
72 *
73 * @param mserver MBeanServer to be consulted
74 * @param databaseName MBean Name of the user database to be consulted
75 *
76 * @exception Exception if an error occurs
77 */
78 public static RolesForm getRolesForm(MBeanServer mserver,
79 String databaseName)
80 throws Exception {
81
82 ObjectName dname = new ObjectName(databaseName);
83 String results[] =
84 (String[]) mserver.getAttribute(dname, "roles");
85 if (results == null) {
86 results = new String[0];
87 }
88 Arrays.sort(results);
89
90 RolesForm rolesForm = new RolesForm();
91 rolesForm.setDatabaseName(databaseName);
92 rolesForm.setRoles(results);
93 return (rolesForm);
94
95 }
96
97
98 /**
99 * Construct and return a UsersForm identifying all currently defined
100 * users in the specified user database.
101 *
102 * @param mserver MBeanServer to be consulted
103 * @param databaseName MBean Name of the user database to be consulted
104 *
105 * @exception Exception if an error occurs
106 */
107 public static UsersForm getUsersForm(MBeanServer mserver,
108 String databaseName)
109 throws Exception {
110
111 ObjectName dname = new ObjectName(databaseName);
112 String results[] =
113 (String[]) mserver.getAttribute(dname, "users");
114 if (results == null) {
115 results = new String[0];
116 }
117 Arrays.sort(results);
118
119 UsersForm usersForm = new UsersForm();
120 usersForm.setDatabaseName(databaseName);
121 usersForm.setUsers(results);
122 return (usersForm);
123
124 }
125
126
127 }