KickJava   Java API By Example, From Geeks To Geeks.

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


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.catalina.UserDatabase;
34 import org.apache.tomcat.util.modeler.BaseModelMBean;
35 import org.apache.tomcat.util.modeler.ManagedBean;
36 import org.apache.tomcat.util.modeler.Registry;
37
38 /**
39  * <p>A <strong>ModelMBean</strong> implementation for the
40  * <code>org.apache.catalina.users.MemoryUserDatabase</code> component.</p>
41  *
42  * @author Craig R. McClanahan
43  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
44  */

45
46 public class MemoryUserDatabaseMBean extends BaseModelMBean {
47
48
49     // ----------------------------------------------------------- Constructors
50

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

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

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

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

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

87     protected ManagedBean managed =
88         registry.findManagedBean("MemoryUserDatabase");
89
90
91     /**
92      * The <code>ManagedBean</code> information describing Group MBeans.
93      */

94     protected ManagedBean managedGroup =
95         registry.findManagedBean("Group");
96
97
98     /**
99      * The <code>ManagedBean</code> information describing Group MBeans.
100      */

101     protected ManagedBean managedRole =
102         registry.findManagedBean("Role");
103
104
105     /**
106      * The <code>ManagedBean</code> information describing User MBeans.
107      */

108     protected ManagedBean managedUser =
109         registry.findManagedBean("User");
110
111
112     // ------------------------------------------------------------- Attributes
113

114
115     /**
116      * Return the MBean Names of all groups defined in this database.
117      */

118     public String JavaDoc[] getGroups() {
119
120         UserDatabase database = (UserDatabase) this.resource;
121         ArrayList JavaDoc results = new ArrayList JavaDoc();
122         Iterator JavaDoc groups = database.getGroups();
123         while (groups.hasNext()) {
124             Group group = (Group) groups.next();
125             results.add(findGroup(group.getGroupname()));
126         }
127         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
128
129     }
130
131
132     /**
133      * Return the MBean Names of all roles defined in this database.
134      */

135     public String JavaDoc[] getRoles() {
136
137         UserDatabase database = (UserDatabase) this.resource;
138         ArrayList JavaDoc results = new ArrayList JavaDoc();
139         Iterator JavaDoc roles = database.getRoles();
140         while (roles.hasNext()) {
141             Role role = (Role) roles.next();
142             results.add(findRole(role.getRolename()));
143         }
144         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
145
146     }
147
148
149     /**
150      * Return the MBean Names of all users defined in this database.
151      */

152     public String JavaDoc[] getUsers() {
153
154         UserDatabase database = (UserDatabase) this.resource;
155         ArrayList JavaDoc results = new ArrayList JavaDoc();
156         Iterator JavaDoc users = database.getUsers();
157         while (users.hasNext()) {
158             User user = (User) users.next();
159             results.add(findUser(user.getUsername()));
160         }
161         return ((String JavaDoc[]) results.toArray(new String JavaDoc[results.size()]));
162
163     }
164
165
166     // ------------------------------------------------------------- Operations
167

168
169     /**
170      * Create a new Group and return the corresponding MBean Name.
171      *
172      * @param groupname Group name of the new group
173      * @param description Description of the new group
174      */

175     public String JavaDoc createGroup(String JavaDoc groupname, String JavaDoc description) {
176
177         UserDatabase database = (UserDatabase) this.resource;
178         Group group = database.createGroup(groupname, description);
179         try {
180             MBeanUtils.createMBean(group);
181         } catch (Exception JavaDoc e) {
182             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
183                 ("Exception creating group " + group + " MBean");
184             iae.initCause(e);
185             throw iae;
186         }
187         return (findGroup(groupname));
188
189     }
190
191
192     /**
193      * Create a new Role and return the corresponding MBean Name.
194      *
195      * @param rolename Group name of the new group
196      * @param description Description of the new group
197      */

198     public String JavaDoc createRole(String JavaDoc rolename, String JavaDoc description) {
199
200         UserDatabase database = (UserDatabase) this.resource;
201         Role role = database.createRole(rolename, description);
202         try {
203             MBeanUtils.createMBean(role);
204         } catch (Exception JavaDoc e) {
205             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
206                 ("Exception creating role " + role + " MBean");
207             iae.initCause(e);
208             throw iae;
209         }
210         return (findRole(rolename));
211
212     }
213
214
215     /**
216      * Create a new User and return the corresponding MBean Name.
217      *
218      * @param username User name of the new user
219      * @param password Password for the new user
220      * @param fullName Full name for the new user
221      */

222     public String JavaDoc createUser(String JavaDoc username, String JavaDoc password,
223                              String JavaDoc fullName) {
224
225         UserDatabase database = (UserDatabase) this.resource;
226         User user = database.createUser(username, password, fullName);
227         try {
228             MBeanUtils.createMBean(user);
229         } catch (Exception JavaDoc e) {
230             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
231                 ("Exception creating user " + user + " MBean");
232             iae.initCause(e);
233             throw iae;
234         }
235         return (findUser(username));
236
237     }
238
239
240     /**
241      * Return the MBean Name for the specified group name (if any);
242      * otherwise return <code>null</code>.
243      *
244      * @param groupname Group name to look up
245      */

246     public String JavaDoc findGroup(String JavaDoc groupname) {
247
248         UserDatabase database = (UserDatabase) this.resource;
249         Group group = database.findGroup(groupname);
250         if (group == null) {
251             return (null);
252         }
253         try {
254             ObjectName JavaDoc oname =
255                 MBeanUtils.createObjectName(managedGroup.getDomain(), group);
256             return (oname.toString());
257         } catch (MalformedObjectNameException JavaDoc e) {
258             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
259                 ("Cannot create object name for group " + group);
260             iae.initCause(e);
261             throw iae;
262         }
263
264     }
265
266
267     /**
268      * Return the MBean Name for the specified role name (if any);
269      * otherwise return <code>null</code>.
270      *
271      * @param rolename Role name to look up
272      */

273     public String JavaDoc findRole(String JavaDoc rolename) {
274
275         UserDatabase database = (UserDatabase) this.resource;
276         Role role = database.findRole(rolename);
277         if (role == null) {
278             return (null);
279         }
280         try {
281             ObjectName JavaDoc oname =
282                 MBeanUtils.createObjectName(managedRole.getDomain(), role);
283             return (oname.toString());
284         } catch (MalformedObjectNameException JavaDoc e) {
285             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
286                 ("Cannot create object name for role " + role);
287             iae.initCause(e);
288             throw iae;
289         }
290
291     }
292
293
294     /**
295      * Return the MBean Name for the specified user name (if any);
296      * otherwise return <code>null</code>.
297      *
298      * @param username User name to look up
299      */

300     public String JavaDoc findUser(String JavaDoc username) {
301
302         UserDatabase database = (UserDatabase) this.resource;
303         User user = database.findUser(username);
304         if (user == null) {
305             return (null);
306         }
307         try {
308             ObjectName JavaDoc oname =
309                 MBeanUtils.createObjectName(managedUser.getDomain(), user);
310             return (oname.toString());
311         } catch (MalformedObjectNameException JavaDoc e) {
312             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
313                 ("Cannot create object name for user " + user);
314             iae.initCause(e);
315             throw iae;
316         }
317
318     }
319
320
321     /**
322      * Remove an existing group and destroy the corresponding MBean.
323      *
324      * @param groupname Group name to remove
325      */

326     public void removeGroup(String JavaDoc groupname) {
327
328         UserDatabase database = (UserDatabase) this.resource;
329         Group group = database.findGroup(groupname);
330         if (group == null) {
331             return;
332         }
333         try {
334             MBeanUtils.destroyMBean(group);
335             database.removeGroup(group);
336         } catch (Exception JavaDoc e) {
337             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
338                 ("Exception destroying group " + group + " MBean");
339             iae.initCause(e);
340             throw iae;
341         }
342
343     }
344
345
346     /**
347      * Remove an existing role and destroy the corresponding MBean.
348      *
349      * @param rolename Role name to remove
350      */

351     public void removeRole(String JavaDoc rolename) {
352
353         UserDatabase database = (UserDatabase) this.resource;
354         Role role = database.findRole(rolename);
355         if (role == null) {
356             return;
357         }
358         try {
359             MBeanUtils.destroyMBean(role);
360             database.removeRole(role);
361         } catch (Exception JavaDoc e) {
362             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
363                 ("Exception destroying role " + role + " MBean");
364             iae.initCause(e);
365             throw iae;
366         }
367
368     }
369
370
371     /**
372      * Remove an existing user and destroy the corresponding MBean.
373      *
374      * @param username User name to remove
375      */

376     public void removeUser(String JavaDoc username) {
377
378         UserDatabase database = (UserDatabase) this.resource;
379         User user = database.findUser(username);
380         if (user == null) {
381             return;
382         }
383         try {
384             MBeanUtils.destroyMBean(user);
385             database.removeUser(user);
386         } catch (Exception JavaDoc e) {
387             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc
388                 ("Exception destroying user " + user + " MBean");
389             iae.initCause(e);
390             throw iae;
391         }
392
393     }
394
395
396 }
397
Popular Tags