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.Group</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 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: // ----------------------------------------------------- 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("Group");
080:
081: // ------------------------------------------------------------- Attributes
082:
083: /**
084: * Return the MBean Names of all authorized roles for this group.
085: */
086: public String[] getRoles() {
087:
088: Group group = (Group) this .resource;
089: ArrayList results = new ArrayList();
090: Iterator roles = group.getRoles();
091: while (roles.hasNext()) {
092: Role role = null;
093: try {
094: role = (Role) roles.next();
095: ObjectName oname = MBeanUtils.createObjectName(managed
096: .getDomain(), role);
097: results.add(oname.toString());
098: } catch (MalformedObjectNameException e) {
099: IllegalArgumentException iae = new IllegalArgumentException(
100: "Cannot create object name for role " + role);
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 users that are members of this group.
111: */
112: public String[] getUsers() {
113:
114: Group group = (Group) this .resource;
115: ArrayList results = new ArrayList();
116: Iterator users = group.getUsers();
117: while (users.hasNext()) {
118: User user = null;
119: try {
120: user = (User) users.next();
121: ObjectName oname = MBeanUtils.createObjectName(managed
122: .getDomain(), user);
123: results.add(oname.toString());
124: } catch (MalformedObjectNameException e) {
125: IllegalArgumentException iae = new IllegalArgumentException(
126: "Cannot create object name for user " + user);
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 Role} to those this group belongs to.
139: *
140: * @param rolename Role name of the new role
141: */
142: public void addRole(String rolename) {
143:
144: Group group = (Group) this .resource;
145: if (group == null) {
146: return;
147: }
148: Role role = group.getUserDatabase().findRole(rolename);
149: if (role == null) {
150: throw new IllegalArgumentException("Invalid role name '"
151: + rolename + "'");
152: }
153: group.addRole(role);
154:
155: }
156:
157: /**
158: * Remove a {@link Role} from those this group belongs to.
159: *
160: * @param rolename Role name of the old role
161: */
162: public void removeRole(String rolename) {
163:
164: Group group = (Group) this .resource;
165: if (group == null) {
166: return;
167: }
168: Role role = group.getUserDatabase().findRole(rolename);
169: if (role == null) {
170: throw new IllegalArgumentException("Invalid role name '"
171: + rolename + "'");
172: }
173: group.removeRole(role);
174:
175: }
176:
177: }
|