Search in sources :

Example 11 with UserDatabase

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

the class MemoryUserDatabaseMBean method removeRole.

/**
     * 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;
    }
}
Also used : Role(org.apache.catalina.Role) UserDatabase(org.apache.catalina.UserDatabase) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 12 with UserDatabase

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

the class MemoryUserDatabaseMBean method createRole.

/**
     * 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
     * @return the new role object name
     */
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);
}
Also used : Role(org.apache.catalina.Role) UserDatabase(org.apache.catalina.UserDatabase) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 13 with UserDatabase

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

the class GlobalResourcesLifecycleListener method createMBeans.

/**
     * Create the MBeans for the interesting global JNDI resources in
     * the specified naming context.
     *
     * @param prefix Prefix for complete object name paths
     * @param context Context to be scanned
     *
     * @exception NamingException if a JNDI exception occurs
     */
protected void createMBeans(String prefix, Context context) throws NamingException {
    if (log.isDebugEnabled()) {
        log.debug("Creating MBeans for Global JNDI Resources in Context '" + prefix + "'");
    }
    try {
        NamingEnumeration<Binding> bindings = context.listBindings("");
        while (bindings.hasMore()) {
            Binding binding = bindings.next();
            String name = prefix + binding.getName();
            Object value = context.lookup(binding.getName());
            if (log.isDebugEnabled()) {
                log.debug("Checking resource " + name);
            }
            if (value instanceof Context) {
                createMBeans(name + "/", (Context) value);
            } else if (value instanceof UserDatabase) {
                try {
                    createMBeans(name, (UserDatabase) value);
                } catch (Exception e) {
                    log.error("Exception creating UserDatabase MBeans for " + name, e);
                }
            }
        }
    } catch (RuntimeException ex) {
        log.error("RuntimeException " + ex);
    } catch (OperationNotSupportedException ex) {
        log.error("Operation not supported " + ex);
    }
}
Also used : Binding(javax.naming.Binding) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) OperationNotSupportedException(javax.naming.OperationNotSupportedException) UserDatabase(org.apache.catalina.UserDatabase) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException)

Example 14 with UserDatabase

use of org.apache.catalina.UserDatabase in project tomee by apache.

the class TomcatWebAppBuilder method start.

public void start(final StandardServer server) {
    if (SystemInstance.get().isDefaultProfile()) {
        // add user tomee is no user are specified
        try {
            final NamingResourcesImpl resources = server.getGlobalNamingResources();
            final ContextResource userDataBaseResource = resources.findResource("UserDatabase");
            final UserDatabase db = (UserDatabase) server.getGlobalNamingContext().lookup(userDataBaseResource.getName());
            if (!db.getUsers().hasNext() && db instanceof MemoryUserDatabase) {
                final MemoryUserDatabase mudb = (MemoryUserDatabase) db;
                final boolean oldRo = mudb.getReadonly();
                try {
                    ((MemoryUserDatabase) db).setReadonly(false);
                    db.createRole("tomee-admin", "tomee admin role");
                    db.createUser("tomee", "tomee", "TomEE");
                    db.findUser("tomee").addRole(db.findRole("tomee-admin"));
                } finally {
                    mudb.setReadonly(oldRo);
                }
            }
        } catch (final Throwable t) {
        // no-op
        }
    }
}
Also used : MemoryUserDatabase(org.apache.catalina.users.MemoryUserDatabase) UserDatabase(org.apache.catalina.UserDatabase) MemoryUserDatabase(org.apache.catalina.users.MemoryUserDatabase) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Aggregations

UserDatabase (org.apache.catalina.UserDatabase)14 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 MBeanException (javax.management.MBeanException)6 RuntimeOperationsException (javax.management.RuntimeOperationsException)6 Group (org.apache.catalina.Group)4 Role (org.apache.catalina.Role)4 User (org.apache.catalina.User)4 ArrayList (java.util.ArrayList)3 ObjectName (javax.management.ObjectName)3 Binding (javax.naming.Binding)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 OperationNotSupportedException (javax.naming.OperationNotSupportedException)1 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)1 MemoryUserDatabase (org.apache.catalina.users.MemoryUserDatabase)1 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)1