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