KickJava   Java API By Example, From Geeks To Geeks.

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


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.User</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 UserMBean 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 UserMBean()
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("User");
88
89
90     // ------------------------------------------------------------- Attributes
91

92
93     /**
94      * Return the MBean Names of all groups this user is a member of.
95      */

96     public String JavaDoc[] getGroups() {
97
98         User user = (User) this.resource;
99         ArrayList JavaDoc results = new ArrayList JavaDoc();
100         Iterator JavaDoc groups = user.getGroups();
101         while (groups.hasNext()) {
102             Group group = null;
103             try {
104                 group = (Group) groups.next();
105                 ObjectName JavaDoc oname =
106                     MBeanUtils.createObjectName(managed.getDomain(), group);
107                 results.add(oname.toString());
108             } catch (MalformedObjectNameException JavaDoc e) {
109                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
110                     ("Cannot create object name for group " + group);
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 roles assigned to this user.
122      */

123     public String JavaDoc[] getRoles() {
124
125         User user = (User) this.resource;
126         ArrayList JavaDoc results = new ArrayList JavaDoc();
127         Iterator JavaDoc roles = user.getRoles();
128         while (roles.hasNext()) {
129             Role role = null;
130             try {
131                 role = (Role) roles.next();
132                 ObjectName JavaDoc oname =
133                     MBeanUtils.createObjectName(managed.getDomain(), role);
134                 results.add(oname.toString());
135             } catch (MalformedObjectNameException JavaDoc e) {
136                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
137                     ("Cannot create object name for role " + role);
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 Group} to those this user belongs to.
152      *
153      * @param groupname Group name of the new group
154      */

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

176     public void addRole(String JavaDoc rolename) {
177
178         User user = (User) this.resource;
179         if (user == null) {
180             return;
181         }
182         Role role = user.getUserDatabase().findRole(rolename);
183         if (role == null) {
184             throw new IllegalArgumentException JavaDoc
185                 ("Invalid role name '" + rolename + "'");
186         }
187         user.addRole(role);
188
189     }
190
191
192     /**
193      * Remove a {@link Group} from those this user belongs to.
194      *
195      * @param groupname Group name of the old group
196      */

197     public void removeGroup(String JavaDoc groupname) {
198
199         User user = (User) this.resource;
200         if (user == null) {
201             return;
202         }
203         Group group = user.getUserDatabase().findGroup(groupname);
204         if (group == null) {
205             throw new IllegalArgumentException JavaDoc
206                 ("Invalid group name '" + groupname + "'");
207         }
208         user.removeGroup(group);
209
210     }
211
212
213     /**
214      * Remove a {@link Role} from those this user belongs to.
215      *
216      * @param rolename Role name of the old role
217      */

218     public void removeRole(String JavaDoc rolename) {
219
220         User user = (User) this.resource;
221         if (user == null) {
222             return;
223         }
224         Role role = user.getUserDatabase().findRole(rolename);
225         if (role == null) {
226             throw new IllegalArgumentException JavaDoc
227                 ("Invalid role name '" + rolename + "'");
228         }
229         user.removeRole(role);
230
231     }
232
233
234 }
235
Popular Tags