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:
022: import javax.management.MBeanException;
023: import javax.management.MBeanServer;
024: import javax.management.MalformedObjectNameException;
025: import javax.management.ObjectName;
026: import javax.management.RuntimeOperationsException;
027:
028: import org.apache.catalina.Group;
029: import org.apache.catalina.Role;
030: import org.apache.catalina.User;
031: import org.apache.commons.modeler.BaseModelMBean;
032: import org.apache.commons.modeler.ManagedBean;
033: import org.apache.commons.modeler.Registry;
034: import org.apache.tomcat.util.compat.JdkCompat;
035:
036: /**
037: * <p>A <strong>ModelMBean</strong> implementation for the
038: * <code>org.apache.catalina.User</code> component.</p>
039: *
040: * @author Craig R. McClanahan
041: * @version $Revision: 1.5 $ $Date: 2004/04/15 01:44:09 $
042: */
043:
044: public class UserMBean extends BaseModelMBean {
045:
046: // ----------------------------------------------------------- Constructors
047:
048: /**
049: * Construct a <code>ModelMBean</code> with default
050: * <code>ModelMBeanInfo</code> information.
051: *
052: * @exception MBeanException if the initializer of an object
053: * throws an exception
054: * @exception RuntimeOperationsException if an IllegalArgumentException
055: * occurs
056: */
057: public UserMBean() throws MBeanException,
058: RuntimeOperationsException {
059:
060: super ();
061:
062: }
063:
064: // ----------------------------------------------------- Class Variables
065:
066: /**
067: * JDK compatibility support
068: */
069: private static final JdkCompat jdkCompat = JdkCompat.getJdkCompat();
070:
071: // ----------------------------------------------------- Instance Variables
072:
073: /**
074: * The configuration information registry for our managed beans.
075: */
076: protected Registry registry = MBeanUtils.createRegistry();
077:
078: /**
079: * The <code>MBeanServer</code> in which we are registered.
080: */
081: protected MBeanServer mserver = MBeanUtils.createServer();
082:
083: /**
084: * The <code>ManagedBean</code> information describing this MBean.
085: */
086: protected ManagedBean managed = registry.findManagedBean("User");
087:
088: // ------------------------------------------------------------- Attributes
089:
090: /**
091: * Return the MBean Names of all groups this user is a member of.
092: */
093: public String[] getGroups() {
094:
095: User user = (User) this .resource;
096: ArrayList results = new ArrayList();
097: Iterator groups = user.getGroups();
098: while (groups.hasNext()) {
099: Group group = null;
100: try {
101: group = (Group) groups.next();
102: ObjectName oname = MBeanUtils.createObjectName(managed
103: .getDomain(), group);
104: results.add(oname.toString());
105: } catch (MalformedObjectNameException e) {
106: IllegalArgumentException iae = new IllegalArgumentException(
107: "Cannot create object name for group " + group);
108: jdkCompat.chainException(iae, e);
109: throw iae;
110: }
111: }
112: return ((String[]) results.toArray(new String[results.size()]));
113:
114: }
115:
116: /**
117: * Return the MBean Names of all roles assigned to this user.
118: */
119: public String[] getRoles() {
120:
121: User user = (User) this .resource;
122: ArrayList results = new ArrayList();
123: Iterator roles = user.getRoles();
124: while (roles.hasNext()) {
125: Role role = null;
126: try {
127: role = (Role) roles.next();
128: ObjectName oname = MBeanUtils.createObjectName(managed
129: .getDomain(), role);
130: results.add(oname.toString());
131: } catch (MalformedObjectNameException e) {
132: IllegalArgumentException iae = new IllegalArgumentException(
133: "Cannot create object name for role " + role);
134: jdkCompat.chainException(iae, e);
135: throw iae;
136: }
137: }
138: return ((String[]) results.toArray(new String[results.size()]));
139:
140: }
141:
142: // ------------------------------------------------------------- Operations
143:
144: /**
145: * Add a new {@link Group} to those this user belongs to.
146: *
147: * @param groupname Group name of the new group
148: */
149: public void addGroup(String groupname) {
150:
151: User user = (User) this .resource;
152: if (user == null) {
153: return;
154: }
155: Group group = user.getUserDatabase().findGroup(groupname);
156: if (group == null) {
157: throw new IllegalArgumentException("Invalid group name '"
158: + groupname + "'");
159: }
160: user.addGroup(group);
161:
162: }
163:
164: /**
165: * Add a new {@link Role} to those this user belongs to.
166: *
167: * @param rolename Role name of the new role
168: */
169: public void addRole(String rolename) {
170:
171: User user = (User) this .resource;
172: if (user == null) {
173: return;
174: }
175: Role role = user.getUserDatabase().findRole(rolename);
176: if (role == null) {
177: throw new IllegalArgumentException("Invalid role name '"
178: + rolename + "'");
179: }
180: user.addRole(role);
181:
182: }
183:
184: /**
185: * Remove a {@link Group} from those this user belongs to.
186: *
187: * @param groupname Group name of the old group
188: */
189: public void removeGroup(String groupname) {
190:
191: User user = (User) this .resource;
192: if (user == null) {
193: return;
194: }
195: Group group = user.getUserDatabase().findGroup(groupname);
196: if (group == null) {
197: throw new IllegalArgumentException("Invalid group name '"
198: + groupname + "'");
199: }
200: user.removeGroup(group);
201:
202: }
203:
204: /**
205: * Remove a {@link Role} from those this user belongs to.
206: *
207: * @param rolename Role name of the old role
208: */
209: public void removeRole(String rolename) {
210:
211: User user = (User) this .resource;
212: if (user == null) {
213: return;
214: }
215: Role role = user.getUserDatabase().findRole(rolename);
216: if (role == null) {
217: throw new IllegalArgumentException("Invalid role name '"
218: + rolename + "'");
219: }
220: user.removeRole(role);
221:
222: }
223:
224: }
|