1
17 package org.apache.catalina.mbeans;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import javax.management.MalformedObjectNameException;
24 import javax.management.ObjectName;
25
26 import org.apache.catalina.Group;
27 import org.apache.catalina.Role;
28 import org.apache.catalina.User;
29 import org.apache.catalina.UserDatabase;
30 import org.apache.tomcat.util.modeler.BaseModelMBean;
31 import org.apache.tomcat.util.modeler.ManagedBean;
32 import org.apache.tomcat.util.modeler.Registry;
33 import org.apache.tomcat.util.res.StringManager;
34
35
41 public class MemoryUserDatabaseMBean extends BaseModelMBean {
42
43 private static final StringManager sm = StringManager.getManager(MemoryUserDatabaseMBean.class);
44
45
46
47
50 protected final Registry registry = MBeanUtils.createRegistry();
51
52
53
56 protected final ManagedBean managed = registry.findManagedBean("MemoryUserDatabase");
57
58
59
62 protected final ManagedBean managedGroup = registry.findManagedBean("Group");
63
64
65
68 protected final ManagedBean managedRole = registry.findManagedBean("Role");
69
70
71
74 protected final ManagedBean managedUser = registry.findManagedBean("User");
75
76
77
78
79
82 public String[] getGroups() {
83 UserDatabase database = (UserDatabase) this.resource;
84 List<String> results = new ArrayList<>();
85 Iterator<Group> groups = database.getGroups();
86 while (groups.hasNext()) {
87 Group group = groups.next();
88 results.add(findGroup(group.getGroupname()));
89 }
90 return results.toArray(new String[results.size()]);
91 }
92
93
94
97 public String[] getRoles() {
98 UserDatabase database = (UserDatabase) this.resource;
99 List<String> results = new ArrayList<>();
100 Iterator<Role> roles = database.getRoles();
101 while (roles.hasNext()) {
102 Role role = roles.next();
103 results.add(findRole(role.getRolename()));
104 }
105 return results.toArray(new String[results.size()]);
106 }
107
108
109
112 public String[] getUsers() {
113 UserDatabase database = (UserDatabase) this.resource;
114 List<String> results = new ArrayList<>();
115 Iterator<User> users = database.getUsers();
116 while (users.hasNext()) {
117 User user = users.next();
118 results.add(findUser(user.getUsername()));
119 }
120 return results.toArray(new String[results.size()]);
121 }
122
123
124
125
126
133 public String createGroup(String groupname, String description) {
134 UserDatabase database = (UserDatabase) this.resource;
135 Group group = database.createGroup(groupname, description);
136 try {
137 MBeanUtils.createMBean(group);
138 } catch (Exception e) {
139 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.createMBeanError.group", groupname));
140 iae.initCause(e);
141 throw iae;
142 }
143 return findGroup(groupname);
144 }
145
146
147
154 public String createRole(String rolename, String description) {
155 UserDatabase database = (UserDatabase) this.resource;
156 Role role = database.createRole(rolename, description);
157 try {
158 MBeanUtils.createMBean(role);
159 } catch (Exception e) {
160 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.createMBeanError.role", rolename));
161 iae.initCause(e);
162 throw iae;
163 }
164 return findRole(rolename);
165 }
166
167
168
176 public String createUser(String username, String password, String fullName) {
177 UserDatabase database = (UserDatabase) this.resource;
178 User user = database.createUser(username, password, fullName);
179 try {
180 MBeanUtils.createMBean(user);
181 } catch (Exception e) {
182 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.createMBeanError.user", username));
183 iae.initCause(e);
184 throw iae;
185 }
186 return findUser(username);
187 }
188
189
190
197 public String findGroup(String groupname) {
198 UserDatabase database = (UserDatabase) this.resource;
199 Group group = database.findGroup(groupname);
200 if (group == null) {
201 return null;
202 }
203 try {
204 ObjectName oname = MBeanUtils.createObjectName(managedGroup.getDomain(), group);
205 return oname.toString();
206 } catch (MalformedObjectNameException e) {
207 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.createError.group", groupname));
208 iae.initCause(e);
209 throw iae;
210 }
211 }
212
213
214
221 public String findRole(String rolename) {
222 UserDatabase database = (UserDatabase) this.resource;
223 Role role = database.findRole(rolename);
224 if (role == null) {
225 return null;
226 }
227 try {
228 ObjectName oname = MBeanUtils.createObjectName(managedRole.getDomain(), role);
229 return oname.toString();
230 } catch (MalformedObjectNameException e) {
231 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.createError.role", rolename));
232 iae.initCause(e);
233 throw iae;
234 }
235
236 }
237
238
239
246 public String findUser(String username) {
247 UserDatabase database = (UserDatabase) this.resource;
248 User user = database.findUser(username);
249 if (user == null) {
250 return null;
251 }
252 try {
253 ObjectName oname = MBeanUtils.createObjectName(managedUser.getDomain(), user);
254 return oname.toString();
255 } catch (MalformedObjectNameException e) {
256 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.createError.user", username));
257 iae.initCause(e);
258 throw iae;
259 }
260 }
261
262
263
268 public void removeGroup(String groupname) {
269 UserDatabase database = (UserDatabase) this.resource;
270 Group group = database.findGroup(groupname);
271 if (group == null) {
272 return;
273 }
274 try {
275 MBeanUtils.destroyMBean(group);
276 database.removeGroup(group);
277 } catch (Exception e) {
278 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.destroyError.group", groupname));
279 iae.initCause(e);
280 throw iae;
281 }
282 }
283
284
285
290 public void removeRole(String rolename) {
291 UserDatabase database = (UserDatabase) this.resource;
292 Role role = database.findRole(rolename);
293 if (role == null) {
294 return;
295 }
296 try {
297 MBeanUtils.destroyMBean(role);
298 database.removeRole(role);
299 } catch (Exception e) {
300 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.destroyError.role", rolename));
301 iae.initCause(e);
302 throw iae;
303 }
304 }
305
306
307
312 public void removeUser(String username) {
313 UserDatabase database = (UserDatabase) this.resource;
314 User user = database.findUser(username);
315 if (user == null) {
316 return;
317 }
318 try {
319 MBeanUtils.destroyMBean(user);
320 database.removeUser(user);
321 } catch (Exception e) {
322 IllegalArgumentException iae = new IllegalArgumentException(sm.getString("userMBean.destroyError.user", username));
323 iae.initCause(e);
324 throw iae;
325 }
326 }
327 }
328