001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.mbeans;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import javax.management.MalformedObjectNameException;
022: import javax.management.MBeanException;
023: import javax.management.MBeanServer;
024: import javax.management.ObjectName;
025: import javax.management.RuntimeOperationsException;
026: import org.apache.catalina.Group;
027: import org.apache.catalina.Role;
028: import org.apache.catalina.User;
029: import org.apache.catalina.UserDatabase;
030: import org.apache.commons.modeler.BaseModelMBean;
031: import org.apache.commons.modeler.ManagedBean;
032: import org.apache.commons.modeler.Registry;
033: import org.apache.tomcat.util.compat.JdkCompat;
034:
035: /**
036: * <p>A <strong>ModelMBean</strong> implementation for the
037: * <code>org.apache.catalina.users.MemoryUserDatabase</code> component.</p>
038: *
039: * @author Craig R. McClanahan
040: * @version $Revision: 1.4 $ $Date: 2004/04/15 01:44:09 $
041: */
042:
043: public class MemoryUserDatabaseMBean extends BaseModelMBean {
044:
045: // ----------------------------------------------------------- Constructors
046:
047: /**
048: * Construct a <code>ModelMBean</code> with default
049: * <code>ModelMBeanInfo</code> information.
050: *
051: * @exception MBeanException if the initializer of an object
052: * throws an exception
053: * @exception RuntimeOperationsException if an IllegalArgumentException
054: * occurs
055: */
056: public MemoryUserDatabaseMBean() throws MBeanException,
057: RuntimeOperationsException {
058:
059: super ();
060:
061: }
062:
063: // ----------------------------------------------------- Class Variables
064:
065: /**
066: * JDK compatibility support
067: */
068: private static final JdkCompat jdkCompat = JdkCompat.getJdkCompat();
069:
070: // ----------------------------------------------------- Instance Variables
071:
072: /**
073: * The configuration information registry for our managed beans.
074: */
075: protected Registry registry = MBeanUtils.createRegistry();
076:
077: /**
078: * The <code>MBeanServer</code> in which we are registered.
079: */
080: protected MBeanServer mserver = MBeanUtils.createServer();
081:
082: /**
083: * The <code>ManagedBean</code> information describing this MBean.
084: */
085: protected ManagedBean managed = registry
086: .findManagedBean("MemoryUserDatabase");
087:
088: /**
089: * The <code>ManagedBean</code> information describing Group MBeans.
090: */
091: protected ManagedBean managedGroup = registry
092: .findManagedBean("Group");
093:
094: /**
095: * The <code>ManagedBean</code> information describing Group MBeans.
096: */
097: protected ManagedBean managedRole = registry
098: .findManagedBean("Role");
099:
100: /**
101: * The <code>ManagedBean</code> information describing User MBeans.
102: */
103: protected ManagedBean managedUser = registry
104: .findManagedBean("User");
105:
106: // ------------------------------------------------------------- Attributes
107:
108: /**
109: * Return the MBean Names of all groups defined in this database.
110: */
111: public String[] getGroups() {
112:
113: UserDatabase database = (UserDatabase) this .resource;
114: ArrayList results = new ArrayList();
115: Iterator groups = database.getGroups();
116: while (groups.hasNext()) {
117: Group group = (Group) groups.next();
118: results.add(findGroup(group.getGroupname()));
119: }
120: return ((String[]) results.toArray(new String[results.size()]));
121:
122: }
123:
124: /**
125: * Return the MBean Names of all roles defined in this database.
126: */
127: public String[] getRoles() {
128:
129: UserDatabase database = (UserDatabase) this .resource;
130: ArrayList results = new ArrayList();
131: Iterator roles = database.getRoles();
132: while (roles.hasNext()) {
133: Role role = (Role) roles.next();
134: results.add(findRole(role.getRolename()));
135: }
136: return ((String[]) results.toArray(new String[results.size()]));
137:
138: }
139:
140: /**
141: * Return the MBean Names of all users defined in this database.
142: */
143: public String[] getUsers() {
144:
145: UserDatabase database = (UserDatabase) this .resource;
146: ArrayList results = new ArrayList();
147: Iterator users = database.getUsers();
148: while (users.hasNext()) {
149: User user = (User) users.next();
150: results.add(findUser(user.getUsername()));
151: }
152: return ((String[]) results.toArray(new String[results.size()]));
153:
154: }
155:
156: // ------------------------------------------------------------- Operations
157:
158: /**
159: * Create a new Group and return the corresponding MBean Name.
160: *
161: * @param groupname Group name of the new group
162: * @param description Description of the new group
163: */
164: public String createGroup(String groupname, String description) {
165:
166: UserDatabase database = (UserDatabase) this .resource;
167: Group group = database.createGroup(groupname, description);
168: try {
169: MBeanUtils.createMBean(group);
170: } catch (Exception e) {
171: IllegalArgumentException iae = new IllegalArgumentException(
172: "Exception creating group " + group + " MBean");
173: jdkCompat.chainException(iae, e);
174: throw iae;
175: }
176: return (findGroup(groupname));
177:
178: }
179:
180: /**
181: * Create a new Role and return the corresponding MBean Name.
182: *
183: * @param rolename Group name of the new group
184: * @param description Description of the new group
185: */
186: public String createRole(String rolename, String description) {
187:
188: UserDatabase database = (UserDatabase) this .resource;
189: Role role = database.createRole(rolename, description);
190: try {
191: MBeanUtils.createMBean(role);
192: } catch (Exception e) {
193: IllegalArgumentException iae = new IllegalArgumentException(
194: "Exception creating role " + role + " MBean");
195: jdkCompat.chainException(iae, e);
196: throw iae;
197: }
198: return (findRole(rolename));
199:
200: }
201:
202: /**
203: * Create a new User and return the corresponding MBean Name.
204: *
205: * @param username User name of the new user
206: * @param password Password for the new user
207: * @param fullName Full name for the new user
208: */
209: public String createUser(String username, String password,
210: String fullName) {
211:
212: UserDatabase database = (UserDatabase) this .resource;
213: User user = database.createUser(username, password, fullName);
214: try {
215: MBeanUtils.createMBean(user);
216: } catch (Exception e) {
217: IllegalArgumentException iae = new IllegalArgumentException(
218: "Exception creating user " + user + " MBean");
219: jdkCompat.chainException(iae, e);
220: throw iae;
221: }
222: return (findUser(username));
223:
224: }
225:
226: /**
227: * Return the MBean Name for the specified group name (if any);
228: * otherwise return <code>null</code>.
229: *
230: * @param groupname Group name to look up
231: */
232: public String findGroup(String groupname) {
233:
234: UserDatabase database = (UserDatabase) this .resource;
235: Group group = database.findGroup(groupname);
236: if (group == null) {
237: return (null);
238: }
239: try {
240: ObjectName oname = MBeanUtils.createObjectName(managedGroup
241: .getDomain(), group);
242: return (oname.toString());
243: } catch (MalformedObjectNameException e) {
244: IllegalArgumentException iae = new IllegalArgumentException(
245: "Cannot create object name for group " + group);
246: jdkCompat.chainException(iae, e);
247: throw iae;
248: }
249:
250: }
251:
252: /**
253: * Return the MBean Name for the specified role name (if any);
254: * otherwise return <code>null</code>.
255: *
256: * @param rolename Role name to look up
257: */
258: public String findRole(String rolename) {
259:
260: UserDatabase database = (UserDatabase) this .resource;
261: Role role = database.findRole(rolename);
262: if (role == null) {
263: return (null);
264: }
265: try {
266: ObjectName oname = MBeanUtils.createObjectName(managedRole
267: .getDomain(), role);
268: return (oname.toString());
269: } catch (MalformedObjectNameException e) {
270: IllegalArgumentException iae = new IllegalArgumentException(
271: "Cannot create object name for role " + role);
272: jdkCompat.chainException(iae, e);
273: throw iae;
274: }
275:
276: }
277:
278: /**
279: * Return the MBean Name for the specified user name (if any);
280: * otherwise return <code>null</code>.
281: *
282: * @param username User name to look up
283: */
284: public String findUser(String username) {
285:
286: UserDatabase database = (UserDatabase) this .resource;
287: User user = database.findUser(username);
288: if (user == null) {
289: return (null);
290: }
291: try {
292: ObjectName oname = MBeanUtils.createObjectName(managedUser
293: .getDomain(), user);
294: return (oname.toString());
295: } catch (MalformedObjectNameException e) {
296: IllegalArgumentException iae = new IllegalArgumentException(
297: "Cannot create object name for user " + user);
298: jdkCompat.chainException(iae, e);
299: throw iae;
300: }
301:
302: }
303:
304: /**
305: * Remove an existing group and destroy the corresponding MBean.
306: *
307: * @param groupname Group name to remove
308: */
309: public void removeGroup(String groupname) {
310:
311: UserDatabase database = (UserDatabase) this .resource;
312: Group group = database.findGroup(groupname);
313: if (group == null) {
314: return;
315: }
316: try {
317: MBeanUtils.destroyMBean(group);
318: database.removeGroup(group);
319: } catch (Exception e) {
320: IllegalArgumentException iae = new IllegalArgumentException(
321: "Exception destroying group " + group + " MBean");
322: jdkCompat.chainException(iae, e);
323: throw iae;
324: }
325:
326: }
327:
328: /**
329: * Remove an existing role and destroy the corresponding MBean.
330: *
331: * @param rolename Role name to remove
332: */
333: public void removeRole(String rolename) {
334:
335: UserDatabase database = (UserDatabase) this .resource;
336: Role role = database.findRole(rolename);
337: if (role == null) {
338: return;
339: }
340: try {
341: MBeanUtils.destroyMBean(role);
342: database.removeRole(role);
343: } catch (Exception e) {
344: IllegalArgumentException iae = new IllegalArgumentException(
345: "Exception destroying role " + role + " MBean");
346: jdkCompat.chainException(iae, e);
347: throw iae;
348: }
349:
350: }
351:
352: /**
353: * Remove an existing user and destroy the corresponding MBean.
354: *
355: * @param username User name to remove
356: */
357: public void removeUser(String username) {
358:
359: UserDatabase database = (UserDatabase) this .resource;
360: User user = database.findUser(username);
361: if (user == null) {
362: return;
363: }
364: try {
365: MBeanUtils.destroyMBean(user);
366: database.removeUser(user);
367: } catch (Exception e) {
368: IllegalArgumentException iae = new IllegalArgumentException(
369: "Exception destroying user " + user + " MBean");
370: jdkCompat.chainException(iae, e);
371: throw iae;
372: }
373:
374: }
375:
376: }
|