1 17 18 package org.apache.catalina.mbeans; 19 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 24 import javax.management.MBeanException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 import javax.management.RuntimeOperationsException ; 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 45 46 public class MemoryUserDatabaseMBean extends BaseModelMBean { 47 48 49 51 52 61 public MemoryUserDatabaseMBean() 62 throws MBeanException , RuntimeOperationsException { 63 64 super(); 65 66 } 67 68 69 71 72 75 protected Registry registry = MBeanUtils.createRegistry(); 76 77 78 81 protected MBeanServer mserver = MBeanUtils.createServer(); 82 83 84 87 protected ManagedBean managed = 88 registry.findManagedBean("MemoryUserDatabase"); 89 90 91 94 protected ManagedBean managedGroup = 95 registry.findManagedBean("Group"); 96 97 98 101 protected ManagedBean managedRole = 102 registry.findManagedBean("Role"); 103 104 105 108 protected ManagedBean managedUser = 109 registry.findManagedBean("User"); 110 111 112 114 115 118 public String [] getGroups() { 119 120 UserDatabase database = (UserDatabase) this.resource; 121 ArrayList results = new ArrayList (); 122 Iterator groups = database.getGroups(); 123 while (groups.hasNext()) { 124 Group group = (Group) groups.next(); 125 results.add(findGroup(group.getGroupname())); 126 } 127 return ((String []) results.toArray(new String [results.size()])); 128 129 } 130 131 132 135 public String [] getRoles() { 136 137 UserDatabase database = (UserDatabase) this.resource; 138 ArrayList results = new ArrayList (); 139 Iterator roles = database.getRoles(); 140 while (roles.hasNext()) { 141 Role role = (Role) roles.next(); 142 results.add(findRole(role.getRolename())); 143 } 144 return ((String []) results.toArray(new String [results.size()])); 145 146 } 147 148 149 152 public String [] getUsers() { 153 154 UserDatabase database = (UserDatabase) this.resource; 155 ArrayList results = new ArrayList (); 156 Iterator users = database.getUsers(); 157 while (users.hasNext()) { 158 User user = (User) users.next(); 159 results.add(findUser(user.getUsername())); 160 } 161 return ((String []) results.toArray(new String [results.size()])); 162 163 } 164 165 166 168 169 175 public String createGroup(String groupname, String description) { 176 177 UserDatabase database = (UserDatabase) this.resource; 178 Group group = database.createGroup(groupname, description); 179 try { 180 MBeanUtils.createMBean(group); 181 } catch (Exception e) { 182 IllegalArgumentException iae = new IllegalArgumentException 183 ("Exception creating group " + group + " MBean"); 184 iae.initCause(e); 185 throw iae; 186 } 187 return (findGroup(groupname)); 188 189 } 190 191 192 198 public String createRole(String rolename, String description) { 199 200 UserDatabase database = (UserDatabase) this.resource; 201 Role role = database.createRole(rolename, description); 202 try { 203 MBeanUtils.createMBean(role); 204 } catch (Exception e) { 205 IllegalArgumentException iae = new IllegalArgumentException 206 ("Exception creating role " + role + " MBean"); 207 iae.initCause(e); 208 throw iae; 209 } 210 return (findRole(rolename)); 211 212 } 213 214 215 222 public String createUser(String username, String password, 223 String 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 e) { 230 IllegalArgumentException iae = new IllegalArgumentException 231 ("Exception creating user " + user + " MBean"); 232 iae.initCause(e); 233 throw iae; 234 } 235 return (findUser(username)); 236 237 } 238 239 240 246 public String findGroup(String 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 oname = 255 MBeanUtils.createObjectName(managedGroup.getDomain(), group); 256 return (oname.toString()); 257 } catch (MalformedObjectNameException e) { 258 IllegalArgumentException iae = new IllegalArgumentException 259 ("Cannot create object name for group " + group); 260 iae.initCause(e); 261 throw iae; 262 } 263 264 } 265 266 267 273 public String findRole(String 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 oname = 282 MBeanUtils.createObjectName(managedRole.getDomain(), role); 283 return (oname.toString()); 284 } catch (MalformedObjectNameException e) { 285 IllegalArgumentException iae = new IllegalArgumentException 286 ("Cannot create object name for role " + role); 287 iae.initCause(e); 288 throw iae; 289 } 290 291 } 292 293 294 300 public String findUser(String 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 oname = 309 MBeanUtils.createObjectName(managedUser.getDomain(), user); 310 return (oname.toString()); 311 } catch (MalformedObjectNameException e) { 312 IllegalArgumentException iae = new IllegalArgumentException 313 ("Cannot create object name for user " + user); 314 iae.initCause(e); 315 throw iae; 316 } 317 318 } 319 320 321 326 public void removeGroup(String 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 e) { 337 IllegalArgumentException iae = new IllegalArgumentException 338 ("Exception destroying group " + group + " MBean"); 339 iae.initCause(e); 340 throw iae; 341 } 342 343 } 344 345 346 351 public void removeRole(String 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 e) { 362 IllegalArgumentException iae = new IllegalArgumentException 363 ("Exception destroying role " + role + " MBean"); 364 iae.initCause(e); 365 throw iae; 366 } 367 368 } 369 370 371 376 public void removeUser(String 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 e) { 387 IllegalArgumentException iae = new IllegalArgumentException 388 ("Exception destroying user " + user + " MBean"); 389 iae.initCause(e); 390 throw iae; 391 } 392 393 } 394 395 396 } 397 | Popular Tags |