001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/MemoryUserDatabaseMBean.java,v 1.4 2002/02/10 03:20:17 craigmcc Exp $
003: * $Revision: 1.4 $
004: * $Date: 2002/02/10 03:20:17 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 2002 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.mbeans;
065:
066: import java.util.ArrayList;
067: import java.util.Iterator;
068: import javax.management.MalformedObjectNameException;
069: import javax.management.MBeanException;
070: import javax.management.MBeanServer;
071: import javax.management.ObjectName;
072: import javax.management.RuntimeOperationsException;
073: import org.apache.catalina.Group;
074: import org.apache.catalina.Role;
075: import org.apache.catalina.User;
076: import org.apache.catalina.UserDatabase;
077: import org.apache.commons.modeler.BaseModelMBean;
078: import org.apache.commons.modeler.ManagedBean;
079: import org.apache.commons.modeler.Registry;
080:
081: /**
082: * <p>A <strong>ModelMBean</strong> implementation for the
083: * <code>org.apache.catalina.users.MemoryUserDatabase</code> component.</p>
084: *
085: * @author Craig R. McClanahan
086: * @version $Revision: 1.4 $ $Date: 2002/02/10 03:20:17 $
087: */
088:
089: public class MemoryUserDatabaseMBean extends BaseModelMBean {
090:
091: // ----------------------------------------------------------- Constructors
092:
093: /**
094: * Construct a <code>ModelMBean</code> with default
095: * <code>ModelMBeanInfo</code> information.
096: *
097: * @exception MBeanException if the initializer of an object
098: * throws an exception
099: * @exception RuntimeOperationsException if an IllegalArgumentException
100: * occurs
101: */
102: public MemoryUserDatabaseMBean() throws MBeanException,
103: RuntimeOperationsException {
104:
105: super ();
106:
107: }
108:
109: // ----------------------------------------------------- Instance Variables
110:
111: /**
112: * The configuration information registry for our managed beans.
113: */
114: protected Registry registry = MBeanUtils.createRegistry();
115:
116: /**
117: * The <code>MBeanServer</code> in which we are registered.
118: */
119: protected MBeanServer mserver = MBeanUtils.createServer();
120:
121: /**
122: * The <code>ManagedBean</code> information describing this MBean.
123: */
124: protected ManagedBean managed = registry
125: .findManagedBean("MemoryUserDatabase");
126:
127: /**
128: * The <code>ManagedBean</code> information describing Group MBeans.
129: */
130: protected ManagedBean managedGroup = registry
131: .findManagedBean("Group");
132:
133: /**
134: * The <code>ManagedBean</code> information describing Group MBeans.
135: */
136: protected ManagedBean managedRole = registry
137: .findManagedBean("Role");
138:
139: /**
140: * The <code>ManagedBean</code> information describing User MBeans.
141: */
142: protected ManagedBean managedUser = registry
143: .findManagedBean("User");
144:
145: // ------------------------------------------------------------- Attributes
146:
147: /**
148: * Return the MBean Names of all groups defined in this database.
149: */
150: public String[] getGroups() {
151:
152: UserDatabase database = (UserDatabase) this .resource;
153: ArrayList results = new ArrayList();
154: Iterator groups = database.getGroups();
155: while (groups.hasNext()) {
156: Group group = (Group) groups.next();
157: results.add(findGroup(group.getGroupname()));
158: }
159: return ((String[]) results.toArray(new String[results.size()]));
160:
161: }
162:
163: /**
164: * Return the MBean Names of all roles defined in this database.
165: */
166: public String[] getRoles() {
167:
168: UserDatabase database = (UserDatabase) this .resource;
169: ArrayList results = new ArrayList();
170: Iterator roles = database.getRoles();
171: while (roles.hasNext()) {
172: Role role = (Role) roles.next();
173: results.add(findRole(role.getRolename()));
174: }
175: return ((String[]) results.toArray(new String[results.size()]));
176:
177: }
178:
179: /**
180: * Return the MBean Names of all users defined in this database.
181: */
182: public String[] getUsers() {
183:
184: UserDatabase database = (UserDatabase) this .resource;
185: ArrayList results = new ArrayList();
186: Iterator users = database.getUsers();
187: while (users.hasNext()) {
188: User user = (User) users.next();
189: results.add(findUser(user.getUsername()));
190: }
191: return ((String[]) results.toArray(new String[results.size()]));
192:
193: }
194:
195: // ------------------------------------------------------------- Operations
196:
197: /**
198: * Create a new Group and return the corresponding MBean Name.
199: *
200: * @param groupname Group name of the new group
201: * @param description Description of the new group
202: */
203: public String createGroup(String groupname, String description) {
204:
205: UserDatabase database = (UserDatabase) this .resource;
206: Group group = database.createGroup(groupname, description);
207: /*
208: if (roles != null) {
209: for (int i = 0; i < roles.length; i++) {
210: Role role = database.findRole(roles[i]);
211: if (role == null) {
212: createRole(roles[i], null);
213: role = database.findRole(roles[i]);
214: }
215: group.addRole(role);
216: }
217: }
218: */
219: try {
220: MBeanUtils.createMBean(group);
221: } catch (Exception e) {
222: throw new IllegalArgumentException(
223: "Exception creating group " + group + " MBean: "
224: + e);
225: }
226: return (findGroup(groupname));
227:
228: }
229:
230: /**
231: * Create a new Role and return the corresponding MBean Name.
232: *
233: * @param rolename Group name of the new group
234: * @param description Description of the new group
235: */
236: public String createRole(String rolename, String description) {
237:
238: UserDatabase database = (UserDatabase) this .resource;
239: Role role = database.createRole(rolename, description);
240: try {
241: MBeanUtils.createMBean(role);
242: } catch (Exception e) {
243: throw new IllegalArgumentException(
244: "Exception creating role " + role + " MBean: " + e);
245: }
246: return (findRole(rolename));
247:
248: }
249:
250: /**
251: * Create a new User and return the corresponding MBean Name.
252: *
253: * @param username User name of the new user
254: * @param password Password for the new user
255: * @param fullName Full name for the new user
256: */
257: public String createUser(String username, String password,
258: String fullName) {
259:
260: UserDatabase database = (UserDatabase) this .resource;
261: User user = database.createUser(username, password, fullName);
262: /*
263: if (roles != null) {
264: for (int i = 0; i < roles.length; i++) {
265: Role role = database.findRole(roles[i]);
266: if (role == null) {
267: createRole(roles[i], null);
268: role = database.findRole(roles[i]);
269: }
270: user.addRole(role);
271: }
272: }
273: */
274: try {
275: MBeanUtils.createMBean(user);
276: } catch (Exception e) {
277: throw new IllegalArgumentException(
278: "Exception creating user " + user + " MBean: " + e);
279: }
280: return (findUser(username));
281:
282: }
283:
284: /**
285: * Return the MBean Name for the specified group name (if any);
286: * otherwise return <code>null</code>.
287: *
288: * @param groupname Group name to look up
289: */
290: public String findGroup(String groupname) {
291:
292: UserDatabase database = (UserDatabase) this .resource;
293: Group group = database.findGroup(groupname);
294: if (group == null) {
295: return (null);
296: }
297: try {
298: ObjectName oname = MBeanUtils.createObjectName(managedGroup
299: .getDomain(), group);
300: return (oname.toString());
301: } catch (MalformedObjectNameException e) {
302: throw new IllegalArgumentException(
303: "Cannot create object name for group " + group);
304: }
305:
306: }
307:
308: /**
309: * Return the MBean Name for the specified role name (if any);
310: * otherwise return <code>null</code>.
311: *
312: * @param rolename Role name to look up
313: */
314: public String findRole(String rolename) {
315:
316: UserDatabase database = (UserDatabase) this .resource;
317: Role role = database.findRole(rolename);
318: if (role == null) {
319: return (null);
320: }
321: try {
322: ObjectName oname = MBeanUtils.createObjectName(managedRole
323: .getDomain(), role);
324: return (oname.toString());
325: } catch (MalformedObjectNameException e) {
326: throw new IllegalArgumentException(
327: "Cannot create object name for role " + role);
328: }
329:
330: }
331:
332: /**
333: * Return the MBean Name for the specified user name (if any);
334: * otherwise return <code>null</code>.
335: *
336: * @param username User name to look up
337: */
338: public String findUser(String username) {
339:
340: UserDatabase database = (UserDatabase) this .resource;
341: User user = database.findUser(username);
342: if (user == null) {
343: return (null);
344: }
345: try {
346: ObjectName oname = MBeanUtils.createObjectName(managedUser
347: .getDomain(), user);
348: return (oname.toString());
349: } catch (MalformedObjectNameException e) {
350: throw new IllegalArgumentException(
351: "Cannot create object name for user " + user);
352: }
353:
354: }
355:
356: /**
357: * Remove an existing group and destroy the corresponding MBean.
358: *
359: * @param groupname Group name to remove
360: */
361: public void removeGroup(String groupname) {
362:
363: UserDatabase database = (UserDatabase) this .resource;
364: Group group = database.findGroup(groupname);
365: if (group == null) {
366: return;
367: }
368: try {
369: MBeanUtils.destroyMBean(group);
370: database.removeGroup(group);
371: } catch (Exception e) {
372: throw new IllegalArgumentException(
373: "Exception destroying group " + group + " MBean: "
374: + e);
375: }
376:
377: }
378:
379: /**
380: * Remove an existing role and destroy the corresponding MBean.
381: *
382: * @param rolename Role name to remove
383: */
384: public void removeRole(String rolename) {
385:
386: UserDatabase database = (UserDatabase) this .resource;
387: Role role = database.findRole(rolename);
388: if (role == null) {
389: return;
390: }
391: try {
392: MBeanUtils.destroyMBean(role);
393: database.removeRole(role);
394: } catch (Exception e) {
395: throw new IllegalArgumentException(
396: "Exception destroying role " + role + " MBean: "
397: + e);
398: }
399:
400: }
401:
402: /**
403: * Remove an existing user and destroy the corresponding MBean.
404: *
405: * @param username User name to remove
406: */
407: public void removeUser(String username) {
408:
409: UserDatabase database = (UserDatabase) this .resource;
410: User user = database.findUser(username);
411: if (user == null) {
412: return;
413: }
414: try {
415: MBeanUtils.destroyMBean(user);
416: database.removeUser(user);
417: } catch (Exception e) {
418: throw new IllegalArgumentException(
419: "Exception destroying user " + user + " MBean: "
420: + e);
421: }
422:
423: }
424:
425: }
|