org.apache.catalina.UserDatabase Java Examples

The following examples show how to use org.apache.catalina.UserDatabase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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));

}
 
Example #2
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #3
Source File: MemoryUserDatabaseMBean.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #4
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #5
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #6
Source File: MemoryUserDatabaseMBean.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #7
Source File: MemoryUserDatabaseMBean.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Return the MBean Name for the specified user name (if any);
 * otherwise return <code>null</code>.
 *
 * @param username User name to look up
 * @return the user object name
 */
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;
    }
}
 
Example #8
Source File: MemoryUserDatabaseMBean.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * 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);
        return oname.toString();
    } catch (MalformedObjectNameException e) {
        IllegalArgumentException iae = new IllegalArgumentException(
                "Cannot create object name for role [" + rolename + "]");
        iae.initCause(e);
        throw iae;
    }

}
 
Example #9
Source File: MemoryUserDatabaseMBean.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Return the MBean Name for the specified group name (if any);
 * otherwise return <code>null</code>.
 *
 * @param groupname Group name to look up
 * @return the group object name
 */
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;
    }
}
 
Example #10
Source File: MBeanUtils.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Deregister the MBean for this
 * <code>UserDatabase</code> object.
 *
 * @param userDatabase The UserDatabase to be managed
 *
 * @exception Exception if an MBean cannot be deregistered
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static void destroyMBean(UserDatabase userDatabase)
    throws Exception {

    String mname = createManagedName(userDatabase);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        return;
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    ObjectName oname = createObjectName(domain, userDatabase);
    if( mserver.isRegistered(oname) )
        mserver.unregisterMBean(oname);

}
 
Example #11
Source File: UserDatabaseRealm.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare for the beginning of active use of the public methods of this
 * component and implement the requirements of
 * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {

    try {
        Context context = getServer().getGlobalNamingContext();
        database = (UserDatabase) context.lookup(resourceName);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        containerLog.error(sm.getString("userDatabaseRealm.lookup",
                                        resourceName),
                           e);
        database = null;
    }
    if (database == null) {
        throw new LifecycleException
            (sm.getString("userDatabaseRealm.noDatabase", resourceName));
    }

    super.startInternal();
}
 
Example #12
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
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
        }
    }
}
 
Example #13
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #14
Source File: UserDatabaseRealm.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Prepare for the beginning of active use of the public methods of this
 * component and implement the requirements of
 * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *                that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {

    try {
        Context context = getServer().getGlobalNamingContext();
        database = (UserDatabase) context.lookup(resourceName);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        containerLog.error(sm.getString("userDatabaseRealm.lookup", resourceName), e);
        database = null;
    }
    if (database == null) {
        throw new LifecycleException(
                sm.getString("userDatabaseRealm.noDatabase", resourceName));
    }

    super.startInternal();
}
 
Example #15
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #16
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #17
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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));

}
 
Example #18
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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));

}
 
Example #19
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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));

}
 
Example #20
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #21
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #22
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #23
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #24
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #25
Source File: MemoryUserDatabaseMBean.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #26
Source File: MBeanUtils.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Create, register, and return an MBean for this
 * <code>UserDatabase</code> object.
 *
 * @param userDatabase The UserDatabase to be managed
 *
 * @exception Exception if an MBean cannot be created or registered
 */
static DynamicMBean createMBean(UserDatabase userDatabase)
    throws Exception {

    String mname = createManagedName(userDatabase);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        Exception e = new Exception("ManagedBean is not found with "+mname);
        throw new MBeanException(e);
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    DynamicMBean mbean = managed.createMBean(userDatabase);
    ObjectName oname = createObjectName(domain, userDatabase);
    if( mserver.isRegistered( oname ))  {
        mserver.unregisterMBean(oname);
    }
    mserver.registerMBean(mbean, oname);
    return (mbean);

}
 
Example #27
Source File: MBeanUtils.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Deregister the MBean for this
 * <code>UserDatabase</code> object.
 *
 * @param userDatabase The UserDatabase to be managed
 *
 * @exception Exception if an MBean cannot be deregistered
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static void destroyMBean(UserDatabase userDatabase)
    throws Exception {

    String mname = createManagedName(userDatabase);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        return;
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    ObjectName oname = createObjectName(domain, userDatabase);
    if( mserver.isRegistered(oname) )
        mserver.unregisterMBean(oname);

}
 
Example #28
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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));

}
 
Example #29
Source File: MemoryUserDatabaseMBean.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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));

}
 
Example #30
Source File: UserDatabaseRealm.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare for the beginning of active use of the public methods of this
 * component and implement the requirements of
 * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {

    try {
        Context context = getServer().getGlobalNamingContext();
        database = (UserDatabase) context.lookup(resourceName);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        containerLog.error(sm.getString("userDatabaseRealm.lookup",
                                        resourceName),
                           e);
        database = null;
    }
    if (database == null) {
        throw new LifecycleException
            (sm.getString("userDatabaseRealm.noDatabase", resourceName));
    }

    super.startInternal();
}