/** Return the MBean Names of all roles defined in this database. */
  public String[] getRoles() {

    UserDatabase database = (UserDatabase) this.resource;
    ArrayList<String> results = new ArrayList<String>();
    Iterator<Role> roles = database.getRoles();
    while (roles.hasNext()) {
      Role role = roles.next();
      results.add(findRole(role.getRolename()));
    }
    return results.toArray(new String[results.size()]);
  }
  /** Return the MBean Names of all users defined in this database. */
  public String[] getUsers() {

    UserDatabase database = (UserDatabase) this.resource;
    ArrayList<String> results = new ArrayList<String>();
    Iterator<User> users = database.getUsers();
    while (users.hasNext()) {
      User user = users.next();
      results.add(findUser(user.getUsername()));
    }
    return results.toArray(new String[results.size()]);
  }
  /** Return the MBean Names of all groups defined in this database. */
  public String[] getGroups() {

    UserDatabase database = (UserDatabase) this.resource;
    ArrayList<String> results = new ArrayList<String>();
    Iterator<Group> groups = database.getGroups();
    while (groups.hasNext()) {
      Group group = groups.next();
      results.add(findGroup(group.getGroupname()));
    }
    return results.toArray(new String[results.size()]);
  }
  /**
   * Create a new User and return the corresponding MBean Name.
   *
   * @param username User name of the new user
   * @param password Password for the new user
   * @param fullName Full name for the new user
   */
  public String createUser(String username, String password, String fullName) {

    UserDatabase database = (UserDatabase) this.resource;
    User user = database.createUser(username, password, fullName);
    try {
      MBeanUtils.createMBean(user);
    } catch (Exception e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Exception creating user [" + username + "] MBean");
      iae.initCause(e);
      throw iae;
    }
    return (findUser(username));
  }
  /**
   * Create a new Role and return the corresponding MBean Name.
   *
   * @param rolename Group name of the new group
   * @param description Description of the new group
   */
  public String createRole(String rolename, String description) {

    UserDatabase database = (UserDatabase) this.resource;
    Role role = database.createRole(rolename, description);
    try {
      MBeanUtils.createMBean(role);
    } catch (Exception e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Exception creating role [" + rolename + "] MBean");
      iae.initCause(e);
      throw iae;
    }
    return (findRole(rolename));
  }
  /**
   * Create a new Group and return the corresponding MBean Name.
   *
   * @param groupname Group name of the new group
   * @param description Description of the new group
   */
  public String createGroup(String groupname, String description) {

    UserDatabase database = (UserDatabase) this.resource;
    Group group = database.createGroup(groupname, description);
    try {
      MBeanUtils.createMBean(group);
    } catch (Exception e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Exception creating group [" + groupname + "] MBean");
      iae.initCause(e);
      throw iae;
    }
    return (findGroup(groupname));
  }
  /**
   * Create the MBeans for the specified UserDatabase and its contents.
   *
   * @param name Complete resource name of this UserDatabase
   * @param database The UserDatabase to be processed
   * @exception Exception if an exception occurs while creating MBeans
   */
  protected void createMBeans(String name, UserDatabase database) throws Exception {

    // Create the MBean for the UserDatabase itself
    if (debug >= 2) {
      log("Creating UserDatabase MBeans for resource " + name);
      log("Database=" + database);
    }
    if (org.apache.catalina.mbeans.MBeanUtils.createMBean(database) == null) {
      throw new IllegalArgumentException("Cannot create UserDatabase MBean for resource " + name);
    }

    // Create the MBeans for each defined Role
    Iterator roles = database.getRoles();
    while (roles.hasNext()) {
      Role role = (Role) roles.next();
      if (debug >= 3) {
        log("  Creating Role MBean for role " + role);
      }
      if (org.apache.catalina.mbeans.MBeanUtils.createMBean(role) == null) {
        throw new IllegalArgumentException("Cannot create Role MBean for role " + role);
      }
    }

    // Create the MBeans for each defined Group
    Iterator groups = database.getGroups();
    while (groups.hasNext()) {
      Group group = (Group) groups.next();
      if (debug >= 3) {
        log("  Creating Group MBean for group " + group);
      }
      if (org.apache.catalina.mbeans.MBeanUtils.createMBean(group) == null) {
        throw new IllegalArgumentException("Cannot create Group MBean for group " + group);
      }
    }

    // Create the MBeans for each defined User
    Iterator users = database.getUsers();
    while (users.hasNext()) {
      User user = (User) users.next();
      if (debug >= 3) {
        log("  Creating User MBean for user " + user);
      }
      if (org.apache.catalina.mbeans.MBeanUtils.createMBean(user) == null) {
        throw new IllegalArgumentException("Cannot create User MBean for user " + user);
      }
    }
  }
  /**
   * Remove an existing user and destroy the corresponding MBean.
   *
   * @param username User name to remove
   */
  public void removeUser(String username) {

    UserDatabase database = (UserDatabase) this.resource;
    User user = database.findUser(username);
    if (user == null) {
      return;
    }
    try {
      MBeanUtils.destroyMBean(user);
      database.removeUser(user);
    } catch (Exception e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Exception destroying user [" + username + "] MBean");
      iae.initCause(e);
      throw iae;
    }
  }
  /**
   * Remove an existing role and destroy the corresponding MBean.
   *
   * @param rolename Role name to remove
   */
  public void removeRole(String rolename) {

    UserDatabase database = (UserDatabase) this.resource;
    Role role = database.findRole(rolename);
    if (role == null) {
      return;
    }
    try {
      MBeanUtils.destroyMBean(role);
      database.removeRole(role);
    } catch (Exception e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Exception destroying role [" + rolename + "] MBean");
      iae.initCause(e);
      throw iae;
    }
  }
  /**
   * Remove an existing group and destroy the corresponding MBean.
   *
   * @param groupname Group name to remove
   */
  public void removeGroup(String groupname) {

    UserDatabase database = (UserDatabase) this.resource;
    Group group = database.findGroup(groupname);
    if (group == null) {
      return;
    }
    try {
      MBeanUtils.destroyMBean(group);
      database.removeGroup(group);
    } catch (Exception e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Exception destroying group [" + groupname + "] MBean");
      iae.initCause(e);
      throw iae;
    }
  }
  /**
   * Return the MBean Name for the specified user name (if any); otherwise return <code>null</code>.
   *
   * @param username User name to look up
   */
  public String findUser(String username) {

    UserDatabase database = (UserDatabase) this.resource;
    User user = database.findUser(username);
    if (user == null) {
      return (null);
    }
    try {
      ObjectName oname = MBeanUtils.createObjectName(managedUser.getDomain(), user);
      return (oname.toString());
    } catch (MalformedObjectNameException e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Cannot create object name for user [" + username + "]");
      iae.initCause(e);
      throw iae;
    }
  }
  /**
   * Return the MBean Name for the specified role name (if any); otherwise return <code>null</code>.
   *
   * @param rolename Role name to look up
   */
  public String findRole(String rolename) {

    UserDatabase database = (UserDatabase) this.resource;
    Role role = database.findRole(rolename);
    if (role == null) {
      return (null);
    }
    try {
      ObjectName oname = MBeanUtils.createObjectName(managedRole.getDomain(), role);
      return (oname.toString());
    } catch (MalformedObjectNameException e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Cannot create object name for role [" + rolename + "]");
      iae.initCause(e);
      throw iae;
    }
  }
  /**
   * Return the MBean Name for the specified group name (if any); otherwise return <code>null</code>
   * .
   *
   * @param groupname Group name to look up
   */
  public String findGroup(String groupname) {

    UserDatabase database = (UserDatabase) this.resource;
    Group group = database.findGroup(groupname);
    if (group == null) {
      return (null);
    }
    try {
      ObjectName oname = MBeanUtils.createObjectName(managedGroup.getDomain(), group);
      return (oname.toString());
    } catch (MalformedObjectNameException e) {
      IllegalArgumentException iae =
          new IllegalArgumentException("Cannot create object name for group [" + groupname + "]");
      iae.initCause(e);
      throw iae;
    }
  }