1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You 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 implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
36  * <p>A <strong>ModelMBean</strong> implementation for the
37  * <code>org.apache.catalina.users.MemoryUserDatabase</code> component.</p>
38  *
39  * @author Craig R. McClanahan
40  */

41 public class MemoryUserDatabaseMBean extends BaseModelMBean {
42
43     private static final StringManager sm = StringManager.getManager(MemoryUserDatabaseMBean.class);
44
45     // ----------------------------------------------------- Instance Variables
46
47     /**
48      * The configuration information registry for our managed beans.
49      */

50     protected final Registry registry = MBeanUtils.createRegistry();
51
52
53     /**
54      * The <code>ManagedBean</code> information describing this MBean.
55      */

56     protected final ManagedBean managed = registry.findManagedBean("MemoryUserDatabase");
57
58
59     /**
60      * The <code>ManagedBean</code> information describing Group MBeans.
61      */

62     protected final ManagedBean managedGroup = registry.findManagedBean("Group");
63
64
65     /**
66      * The <code>ManagedBean</code> information describing Group MBeans.
67      */

68     protected final ManagedBean managedRole = registry.findManagedBean("Role");
69
70
71     /**
72      * The <code>ManagedBean</code> information describing User MBeans.
73      */

74     protected final ManagedBean managedUser = registry.findManagedBean("User");
75
76
77     // ------------------------------------------------------------- Attributes
78
79     /**
80      * @return the MBean Names of all groups defined in this database.
81      */

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     /**
95      * @return the MBean Names of all roles defined in this database.
96      */

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     /**
110      * @return the MBean Names of all users defined in this database.
111      */

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     // ------------------------------------------------------------- Operations
125
126     /**
127      * Create a new Group and return the corresponding MBean Name.
128      *
129      * @param groupname Group name of the new group
130      * @param description Description of the new group
131      * @return the new group object name
132      */

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     /**
148      * Create a new Role and return the corresponding MBean Name.
149      *
150      * @param rolename Group name of the new group
151      * @param description Description of the new group
152      * @return the new role object name
153      */

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     /**
169      * Create a new User and return the corresponding MBean Name.
170      *
171      * @param username User name of the new user
172      * @param password Password for the new user
173      * @param fullName Full name for the new user
174      * @return the new user object name
175      */

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     /**
191      * Return the MBean Name for the specified group name (if any);
192      * otherwise return <code>null</code>.
193      *
194      * @param groupname Group name to look up
195      * @return the group object name
196      */

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     /**
215      * Return the MBean Name for the specified role name (if any);
216      * otherwise return <code>null</code>.
217      *
218      * @param rolename Role name to look up
219      * @return the role object name
220      */

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     /**
240      * Return the MBean Name for the specified user name (if any);
241      * otherwise return <code>null</code>.
242      *
243      * @param username User name to look up
244      * @return the user object name
245      */

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     /**
264      * Remove an existing group and destroy the corresponding MBean.
265      *
266      * @param groupname Group name to remove
267      */

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     /**
286      * Remove an existing role and destroy the corresponding MBean.
287      *
288      * @param rolename Role name to remove
289      */

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     /**
308      * Remove an existing user and destroy the corresponding MBean.
309      *
310      * @param username User name to remove
311      */

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