KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > mbeans > GroupMBean


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.catalina.mbeans;
19
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.RuntimeOperationsException JavaDoc;
29
30 import org.apache.catalina.Group;
31 import org.apache.catalina.Role;
32 import org.apache.catalina.User;
33 import org.apache.tomcat.util.modeler.BaseModelMBean;
34 import org.apache.tomcat.util.modeler.ManagedBean;
35 import org.apache.tomcat.util.modeler.Registry;
36
37 /**
38  * <p>A <strong>ModelMBean</strong> implementation for the
39  * <code>org.apache.catalina.Group</code> component.</p>
40  *
41  * @author Craig R. McClanahan
42  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
43  */

44
45 public class GroupMBean extends BaseModelMBean {
46
47
48     // ----------------------------------------------------------- Constructors
49

50
51     /**
52      * Construct a <code>ModelMBean</code> with default
53      * <code>ModelMBeanInfo</code> information.
54      *
55      * @exception MBeanException if the initializer of an object
56      * throws an exception
57      * @exception RuntimeOperationsException if an IllegalArgumentException
58      * occurs
59      */

60     public GroupMBean()
61         throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
62
63         super();
64
65     }
66
67
68     // ----------------------------------------------------- Instance Variables
69

70
71     /**
72      * The configuration information registry for our managed beans.
73      */

74     protected Registry registry = MBeanUtils.createRegistry();
75
76
77     /**
78      * The <code>MBeanServer</code> in which we are registered.
79      */

80     protected MBeanServer JavaDoc mserver = MBeanUtils.createServer();
81
82
83     /**
84      * The <code>ManagedBean</code> information describing this MBean.
85      */

86     protected ManagedBean managed =
87         registry.findManagedBean("Group");
88
89
90     // ------------------------------------------------------------- Attributes
91

92
93     /**
94      * Return the MBean Names of all authorized roles for this group.
95      */

96     public String JavaDoc[] getRoles() {
97
98         Group group = (Group) this.resource;
99         ArrayList JavaDoc results = new ArrayList JavaDoc();
100         Iterator JavaDoc roles = group.getRoles();
101         while (roles.hasNext()) {
102             Role role = null;
103             try {
104                 role = (Role) roles.next();
105                 ObjectName JavaDoc oname =
106                     MBeanUtils.createObjectName(managed.getDomain(), role);
107                 results.add(oname.toString());
108             } catch (MalformedObjectNameException JavaDoc e) {
109                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
110                     ("Cannot create object name for role " + role);
111                 iae.initCause(e);
112                 throw iae;
113             }
114         }
115         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
116
117     }
118
119
120     /**
121      * Return the MBean Names of all users that are members of this group.
122      */

123     public String JavaDoc[] getUsers() {
124
125         Group group = (Group) this.resource;
126         ArrayList JavaDoc results = new ArrayList JavaDoc();
127         Iterator JavaDoc users = group.getUsers();
128         while (users.hasNext()) {
129             User user = null;
130             try {
131                 user = (User) users.next();
132                 ObjectName JavaDoc oname =
133                     MBeanUtils.createObjectName(managed.getDomain(), user);
134                 results.add(oname.toString());
135             } catch (MalformedObjectNameException JavaDoc e) {
136                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
137                     ("Cannot create object name for user " + user);
138                 iae.initCause(e);
139                 throw iae;
140             }
141         }
142         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
143
144     }
145
146
147     // ------------------------------------------------------------- Operations
148

149
150     /**
151      * Add a new {@link Role} to those this group belongs to.
152      *
153      * @param rolename Role name of the new role
154      */

155     public void addRole(String JavaDoc rolename) {
156
157         Group group = (Group) this.resource;
158         if (group == null) {
159             return;
160         }
161         Role role = group.getUserDatabase().findRole(rolename);
162         if (role == null) {
163             throw new IllegalArgumentException JavaDoc
164                 ("Invalid role name '" + rolename + "'");
165         }
166         group.addRole(role);
167
168     }
169
170
171     /**
172      * Remove a {@link Role} from those this group belongs to.
173      *
174      * @param rolename Role name of the old role
175      */

176     public void removeRole(String JavaDoc rolename) {
177
178         Group group = (Group) this.resource;
179         if (group == null) {
180             return;
181         }
182         Role role = group.getUserDatabase().findRole(rolename);
183         if (role == null) {
184             throw new IllegalArgumentException JavaDoc
185                 ("Invalid role name '" + rolename + "'");
186         }
187         group.removeRole(role);
188
189     }
190
191
192 }
193
Popular Tags