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.tomcat.util.modeler.BaseModelMBean;
033: import org.apache.tomcat.util.modeler.ManagedBean;
034: import org.apache.tomcat.util.modeler.Registry;
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: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
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: // ----------------------------------------------------- Instance Variables
065:
066: /**
067: * The configuration information registry for our managed beans.
068: */
069: protected Registry registry = MBeanUtils.createRegistry();
070:
071: /**
072: * The <code>MBeanServer</code> in which we are registered.
073: */
074: protected MBeanServer mserver = MBeanUtils.createServer();
075:
076: /**
077: * The <code>ManagedBean</code> information describing this MBean.
078: */
079: protected ManagedBean managed = registry.findManagedBean("User");
080:
081: // ------------------------------------------------------------- Attributes
082:
083: /**
084: * Return the MBean Names of all groups this user is a member of.
085: */
086: public String[] getGroups() {
087:
088: User user = (User) this .resource;
089: ArrayList results = new ArrayList();
090: Iterator groups = user.getGroups();
091: while (groups.hasNext()) {
092: Group group = null;
093: try {
094: group = (Group) groups.next();
095: ObjectName oname = MBeanUtils.createObjectName(managed
096: .getDomain(), group);
097: results.add(oname.toString());
098: } catch (MalformedObjectNameException e) {
099: IllegalArgumentException iae = new IllegalArgumentException(
100: "Cannot create object name for group " + group);
101: iae.initCause(e);
102: throw iae;
103: }
104: }
105: return ((String[]) results.toArray(new String[results.size()]));
106:
107: }
108:
109: /**
110: * Return the MBean Names of all roles assigned to this user.
111: */
112: public String[] getRoles() {
113:
114: User user = (User) this .resource;
115: ArrayList results = new ArrayList();
116: Iterator roles = user.getRoles();
117: while (roles.hasNext()) {
118: Role role = null;
119: try {
120: role = (Role) roles.next();
121: ObjectName oname = MBeanUtils.createObjectName(managed
122: .getDomain(), role);
123: results.add(oname.toString());
124: } catch (MalformedObjectNameException e) {
125: IllegalArgumentException iae = new IllegalArgumentException(
126: "Cannot create object name for role " + role);
127: iae.initCause(e);
128: throw iae;
129: }
130: }
131: return ((String[]) results.toArray(new String[results.size()]));
132:
133: }
134:
135: // ------------------------------------------------------------- Operations
136:
137: /**
138: * Add a new {@link Group} to those this user belongs to.
139: *
140: * @param groupname Group name of the new group
141: */
142: public void addGroup(String groupname) {
143:
144: User user = (User) this .resource;
145: if (user == null) {
146: return;
147: }
148: Group group = user.getUserDatabase().findGroup(groupname);
149: if (group == null) {
150: throw new IllegalArgumentException("Invalid group name '"
151: + groupname + "'");
152: }
153: user.addGroup(group);
154:
155: }
156:
157: /**
158: * Add a new {@link Role} to those this user belongs to.
159: *
160: * @param rolename Role name of the new role
161: */
162: public void addRole(String rolename) {
163:
164: User user = (User) this .resource;
165: if (user == null) {
166: return;
167: }
168: Role role = user.getUserDatabase().findRole(rolename);
169: if (role == null) {
170: throw new IllegalArgumentException("Invalid role name '"
171: + rolename + "'");
172: }
173: user.addRole(role);
174:
175: }
176:
177: /**
178: * Remove a {@link Group} from those this user belongs to.
179: *
180: * @param groupname Group name of the old group
181: */
182: public void removeGroup(String groupname) {
183:
184: User user = (User) this .resource;
185: if (user == null) {
186: return;
187: }
188: Group group = user.getUserDatabase().findGroup(groupname);
189: if (group == null) {
190: throw new IllegalArgumentException("Invalid group name '"
191: + groupname + "'");
192: }
193: user.removeGroup(group);
194:
195: }
196:
197: /**
198: * Remove a {@link Role} from those this user belongs to.
199: *
200: * @param rolename Role name of the old role
201: */
202: public void removeRole(String rolename) {
203:
204: User user = (User) this .resource;
205: if (user == null) {
206: return;
207: }
208: Role role = user.getUserDatabase().findRole(rolename);
209: if (role == null) {
210: throw new IllegalArgumentException("Invalid role name '"
211: + rolename + "'");
212: }
213: user.removeRole(role);
214:
215: }
216:
217: }
|