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.Group</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 GroupMBean 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 GroupMBean() 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("Group");
087:
088: // ------------------------------------------------------------- Attributes
089:
090: /**
091: * Return the MBean Names of all authorized roles for this group.
092: */
093: public String[] getRoles() {
094:
095: Group group = (Group) this .resource;
096: ArrayList results = new ArrayList();
097: Iterator roles = group.getRoles();
098: while (roles.hasNext()) {
099: Role role = null;
100: try {
101: role = (Role) roles.next();
102: ObjectName oname = MBeanUtils.createObjectName(managed
103: .getDomain(), role);
104: results.add(oname.toString());
105: } catch (MalformedObjectNameException e) {
106: IllegalArgumentException iae = new IllegalArgumentException(
107: "Cannot create object name for role " + role);
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 users that are members of this group.
118: */
119: public String[] getUsers() {
120:
121: Group group = (Group) this .resource;
122: ArrayList results = new ArrayList();
123: Iterator users = group.getUsers();
124: while (users.hasNext()) {
125: User user = null;
126: try {
127: user = (User) users.next();
128: ObjectName oname = MBeanUtils.createObjectName(managed
129: .getDomain(), user);
130: results.add(oname.toString());
131: } catch (MalformedObjectNameException e) {
132: IllegalArgumentException iae = new IllegalArgumentException(
133: "Cannot create object name for user " + user);
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 Role} to those this group belongs to.
146: *
147: * @param rolename Role name of the new role
148: */
149: public void addRole(String rolename) {
150:
151: Group group = (Group) this .resource;
152: if (group == null) {
153: return;
154: }
155: Role role = group.getUserDatabase().findRole(rolename);
156: if (role == null) {
157: throw new IllegalArgumentException("Invalid role name '"
158: + rolename + "'");
159: }
160: group.addRole(role);
161:
162: }
163:
164: /**
165: * Remove a {@link Role} from those this group belongs to.
166: *
167: * @param rolename Role name of the old role
168: */
169: public void removeRole(String rolename) {
170:
171: Group group = (Group) this .resource;
172: if (group == null) {
173: return;
174: }
175: Role role = group.getUserDatabase().findRole(rolename);
176: if (role == null) {
177: throw new IllegalArgumentException("Invalid role name '"
178: + rolename + "'");
179: }
180: group.removeRole(role);
181:
182: }
183:
184: }
|