Search in sources :

Example 16 with Role

use of org.apache.catalina.Role in project tomcat by apache.

the class GlobalResourcesLifecycleListener method createMBeans.

/**
 * 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 (log.isDebugEnabled()) {
        log.debug("Creating UserDatabase MBeans for resource " + name);
        log.debug("Database=" + database);
    }
    try {
        MBeanUtils.createMBean(database);
    } catch (Exception e) {
        throw new IllegalArgumentException(sm.getString("globalResources.createError.userDatabase", name), e);
    }
    if (database.isSparse()) {
        // Avoid loading all the database as mbeans
        return;
    }
    // Create the MBeans for each defined Role
    Iterator<Role> roles = database.getRoles();
    while (roles.hasNext()) {
        Role role = roles.next();
        if (log.isDebugEnabled()) {
            log.debug("  Creating Role MBean for role " + role);
        }
        try {
            MBeanUtils.createMBean(role);
        } catch (Exception e) {
            throw new IllegalArgumentException(sm.getString("globalResources.createError.userDatabase.role", role), e);
        }
    }
    // Create the MBeans for each defined Group
    Iterator<Group> groups = database.getGroups();
    while (groups.hasNext()) {
        Group group = groups.next();
        if (log.isDebugEnabled()) {
            log.debug("  Creating Group MBean for group " + group);
        }
        try {
            MBeanUtils.createMBean(group);
        } catch (Exception e) {
            throw new IllegalArgumentException(sm.getString("globalResources.createError.userDatabase.group", group), e);
        }
    }
    // Create the MBeans for each defined User
    Iterator<User> users = database.getUsers();
    while (users.hasNext()) {
        User user = users.next();
        if (log.isDebugEnabled()) {
            log.debug("  Creating User MBean for user " + user);
        }
        try {
            MBeanUtils.createMBean(user);
        } catch (Exception e) {
            throw new IllegalArgumentException(sm.getString("globalResources.createError.userDatabase.user", user), e);
        }
    }
}
Also used : Role(org.apache.catalina.Role) Group(org.apache.catalina.Group) User(org.apache.catalina.User) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException)

Example 17 with Role

use of org.apache.catalina.Role in project tomcat by apache.

the class UserMBean method getRoles.

/**
 * @return the MBean Names of all roles assigned to this user.
 */
public String[] getRoles() {
    User user = (User) this.resource;
    List<String> results = new ArrayList<>();
    Iterator<Role> roles = user.getRoles();
    while (roles.hasNext()) {
        Role role = null;
        try {
            role = roles.next();
            ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), role);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException(sm.getString("userMBean.createError.role", role), e);
        }
    }
    return results.toArray(new String[0]);
}
Also used : Role(org.apache.catalina.Role) MalformedObjectNameException(javax.management.MalformedObjectNameException) User(org.apache.catalina.User) ArrayList(java.util.ArrayList) ObjectName(javax.management.ObjectName)

Example 18 with Role

use of org.apache.catalina.Role in project tomcat by apache.

the class UserMBean method addRole.

/**
 * Add a new {@link Role} to those this user belongs to.
 *
 * @param rolename Role name of the new role
 */
public void addRole(String rolename) {
    User user = (User) this.resource;
    if (user == null) {
        return;
    }
    Role role = user.getUserDatabase().findRole(rolename);
    if (role == null) {
        throw new IllegalArgumentException(sm.getString("userMBean.invalidRole", rolename));
    }
    user.addRole(role);
}
Also used : Role(org.apache.catalina.Role) User(org.apache.catalina.User)

Example 19 with Role

use of org.apache.catalina.Role in project tomcat by apache.

the class SparseUserDatabaseMBean method getRoles.

/**
 * @return the MBean Names of all roles defined in this database.
 */
public String[] getRoles() {
    UserDatabase database = (UserDatabase) this.resource;
    List<String> results = new ArrayList<>();
    Iterator<Role> roles = database.getRoles();
    while (roles.hasNext()) {
        Role role = roles.next();
        results.add(findRole(role.getRolename()));
    }
    return results.toArray(new String[0]);
}
Also used : Role(org.apache.catalina.Role) UserDatabase(org.apache.catalina.UserDatabase) ArrayList(java.util.ArrayList)

Example 20 with Role

use of org.apache.catalina.Role in project tomcat by apache.

the class SparseUserDatabaseMBean method findRole.

/**
 * Return the MBean Name for the specified role name (if any);
 * otherwise return <code>null</code>.
 *
 * @param rolename Role name to look up
 * @return the role object name
 */
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);
        if (database.isSparse() && !mserver.isRegistered(oname)) {
            MBeanUtils.createMBean(role);
        }
        return oname.toString();
    } catch (Exception e) {
        throw new IllegalArgumentException(sm.getString("userMBean.createError.role", rolename), e);
    }
}
Also used : Role(org.apache.catalina.Role) UserDatabase(org.apache.catalina.UserDatabase) ObjectName(javax.management.ObjectName)

Aggregations

Role (org.apache.catalina.Role)50 UserDatabase (org.apache.catalina.UserDatabase)21 Group (org.apache.catalina.Group)20 User (org.apache.catalina.User)18 ArrayList (java.util.ArrayList)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 ObjectName (javax.management.ObjectName)7 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 MBeanException (javax.management.MBeanException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 Statement (java.sql.Statement)2 NamingException (javax.naming.NamingException)2 OperationNotSupportedException (javax.naming.OperationNotSupportedException)2 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1