/**
   * Removes a given role from this manager.
   *
   * @param role the role to remove, cannot be <code>null</code>.
   * @return <code>true</code> if the role was removed (i.e., it was managed by this manager), or
   *     <code>false</code> if it was not found.
   */
  public boolean removeRole(String name) {
    if (name == null) {
      throw new IllegalArgumentException("Name cannot be null!");
    }

    checkPermissions();

    // Cannot remove predefined roles...
    if (isPredefinedRole(name)) {
      return false;
    }

    try {
      Role result = m_store.removeRole(name);
      if (result != null) {
        // FELIX-3755: Remove the role as (required)member from all groups...
        removeRoleFromAllGroups(result);

        unwireChangeListener(result);
        m_roleChangeReflector.roleRemoved(result);

        return true;
      }

      return false;
    } catch (Exception e) {
      throw new BackendException("Failed to remove role " + name + "!", e);
    }
  }
  /**
   * Returns a collection with all roles matching a given key-value pair.
   *
   * @param key the key to search for;
   * @param value the value to search for.
   * @return a list with all matching roles, can be empty, but never <code>null</code>.
   */
  public List getRoles(String key, String value) {
    if (key == null) {
      throw new IllegalArgumentException("Key cannot be null!");
    }
    if (value == null) {
      throw new IllegalArgumentException("Value cannot be null!");
    }

    List matchingRoles = new ArrayList();

    try {
      String criteria = "(".concat(key).concat("=").concat(value).concat(")");

      Role[] roles = m_store.getRoles(criteria);
      for (int i = 0; i < roles.length; i++) {
        Role role = roles[i];
        if (!isPredefinedRole(role.getName())) {
          matchingRoles.add(wireChangeListener(role));
        }
      }
    } catch (Exception e) {
      throw new BackendException("Failed to get roles!", e);
    }

    return matchingRoles;
  }
 /**
  * Returns the by its given name.
  *
  * @param roleName the name of the role to return, cannot be <code>null</code>.
  * @return the role matching the given name, or <code>null</code> if no role matched the given
  *     name.
  */
 public Role getRoleByName(String roleName) {
   try {
     Role result;
     if (isPredefinedRole(roleName)) {
       result = getPredefinedRole(roleName);
     } else {
       result = m_store.getRoleByName(roleName);
     }
     return wireChangeListener(result);
   } catch (Exception e) {
     throw new BackendException("Failed to get role by name: " + roleName + "!", e);
   }
 }
 /**
  * Removes a given role as (required)member from any groups it is member of.
  *
  * @param removedRole the role that is removed from the store already, cannot be <code>null</code>
  *     .
  * @throws BackendException in case of problems accessing the store.
  */
 private void removeRoleFromAllGroups(Role removedRole) {
   try {
     Role[] roles = m_store.getRoles(null);
     for (int i = 0; i < roles.length; i++) {
       if (roles[i].getType() == Role.GROUP) {
         Group group = (Group) roles[i];
         // Don't check whether the given role is actually a member
         // of the group, but let the group itself figure this out...
         group.removeMember(removedRole);
       }
     }
   } catch (Exception e) {
     throw new BackendException("Failed to get all roles!", e);
   }
 }
  /**
   * Returns a collection with all roles matching a given filter.
   *
   * @param filter the filter to match the individual roles against, can be <code>null</code> if all
   *     roles should be returned.
   * @return a list with all matching roles, can be empty, but never <code>null</code>.
   */
  public List getRoles(String filter) {
    List matchingRoles = new ArrayList();

    try {
      Role[] roles = m_store.getRoles(sanitizeFilter(filter));
      for (int i = 0; i < roles.length; i++) {
        Role role = roles[i];
        if (!isPredefinedRole(role.getName())) {
          matchingRoles.add(wireChangeListener(role));
        }
      }
    } catch (Exception e) {
      throw new BackendException("Failed to get roles!", e);
    }

    return matchingRoles;
  }
  /**
   * Adds a given role to this manager.
   *
   * @param role the role to add, cannot be <code>null</code>. If it is already contained by this
   *     manager, this method will not do anything.
   * @return the given role if added, <code>null</code> otherwise.
   */
  public Role addRole(String name, int type) {
    if ((name == null) || "".equals(name.trim())) {
      throw new IllegalArgumentException("Name cannot be null or empty!");
    }
    if (type != Role.GROUP && type != Role.USER) {
      throw new IllegalArgumentException("Invalid role type!");
    }

    checkPermissions();

    try {
      Role result = m_store.addRole(name, type);
      if (result != null) {
        result = wireChangeListener(result);
        m_roleChangeReflector.roleAdded(result);
      }

      return result;
    } catch (Exception e) {
      throw new BackendException("Adding role " + name + " failed!", e);
    }
  }