epugh       2003/08/23 14:09:52

  Modified:    security/src/java/org/apache/fulcrum/security/spi/hibernate/simple
                        HibernateGroupManagerImpl.java
                        HibernateRoleManagerImpl.java
                        HibernateUserManagerImpl.java
                        HibernatePermissionManagerImpl.java
  Log:
  add in the hibernate spi
  
  Revision  Changes    Path
  1.2       +91 -60    
jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernateGroupManagerImpl.java
  
  Index: HibernateGroupManagerImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernateGroupManagerImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HibernateGroupManagerImpl.java    23 Aug 2003 15:30:12 -0000      1.1
  +++ HibernateGroupManagerImpl.java    23 Aug 2003 21:09:52 -0000      1.2
  @@ -52,10 +52,15 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -import java.util.ArrayList;
  -import java.util.Iterator;
   import java.util.List;
   
  +import net.sf.hibernate.Hibernate;
  +import net.sf.hibernate.HibernateException;
  +import net.sf.hibernate.Session;
  +import net.sf.hibernate.Transaction;
  +import net.sf.hibernate.avalon.HibernateService;
  +
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.component.Composable;
  @@ -67,7 +72,7 @@
   import org.apache.fulcrum.security.entity.Group;
   import org.apache.fulcrum.security.entity.Role;
   import org.apache.fulcrum.security.model.simple.entity.SimpleGroup;
  -import org.apache.fulcrum.security.model.simple.manager.*;
  +import org.apache.fulcrum.security.model.simple.manager.SimpleGroupManager;
   import org.apache.fulcrum.security.util.DataBackendException;
   import org.apache.fulcrum.security.util.EntityExistsException;
   import org.apache.fulcrum.security.util.GroupSet;
  @@ -78,13 +83,14 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
    * @version $Id$
    */
  -public class HibernateGroupManagerImpl extends AbstractLogEnabled implements 
SimpleGroupManager, Composable
  +public class HibernateGroupManagerImpl extends AbstractLogEnabled implements 
SimpleGroupManager, Composable, Disposable
   {
       /** Logging */
       private static Log log = LogFactory.getLog(HibernateGroupManagerImpl.class);
  -    private static List groups = new ArrayList();
  -    /** Our Unique ID counter */
  -    private static int uniqueId = 0;
  +    /** Hibernate components */
  +    private HibernateService hibernateService;
  +    private Session session;
  +    private Transaction transaction;
       private ComponentManager manager = null;
       /** Our role Manager **/
       private RoleManager roleManager;
  @@ -201,7 +207,18 @@
         */
       public GroupSet getAllGroups() throws DataBackendException
       {
  -        return new GroupSet(groups);
  +        GroupSet groupSet = new GroupSet();
  +        try
  +        {
  +            session = hibernateService.openSession();
  +            List groups = session.find("from SimpleGroup");
  +            groupSet.add(groups);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving group information", e);
  +        }
  +        return groupSet;
       }
       /**
        * Removes a Group from the system.
  @@ -219,8 +236,10 @@
               groupExists = checkExists(group);
               if (groupExists)
               {
  -                groups.remove(group);
  -                return;
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.delete(group);
  +                transaction.commit();
               }
               else
               {
  @@ -233,9 +252,7 @@
               log.error(e);
               throw new DataBackendException("removeGroup(Group) failed", e);
           }
  -        finally
  -        {
  -        }
  +
       }
       /**
        * Renames an existing Group.
  @@ -254,9 +271,8 @@
               groupExists = checkExists(group);
               if (groupExists)
               {
  -                groups.remove(group);
                   group.setName(name);
  -                groups.add(group);
  +                saveGroup(group);
               }
               else
               {
  @@ -267,9 +283,6 @@
           {
               throw new DataBackendException("renameGroup(Group,String)", e);
           }
  -        finally
  -        {
  -        }
       }
       /**
        * Stores Group's attributes. The Groups is required to exist in the system.
  @@ -287,8 +300,10 @@
               groupExists = checkExists(group);
               if (groupExists)
               {
  -                groups.remove(group);
  -                groups.add(group);
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.update(group);
  +                transaction.commit();
               }
               else
               {
  @@ -311,34 +326,31 @@
        */
       public boolean checkExists(Group group) throws DataBackendException
       {
  +        List groups;
           try
           {
  -            boolean exists = false;
  -            for (Iterator i = groups.iterator(); i.hasNext();)
  -            {
  -                Group g = (Group) i.next();
  -                if (g.getName().equalsIgnoreCase(group.getName()) | g.getId() == 
group.getId())
  -                {
  -                    exists = true;
  -                }
  -            }
  -            return exists;
  -            //return groups.contains(group);
  +            session = hibernateService.openSession();
  +            groups = session.find("from SimpleGroup sg where sg.name=?", 
group.getName(), Hibernate.STRING);
           }
  -        catch (Exception e)
  +        catch (HibernateException e)
           {
  -            throw new DataBackendException("Problem checking if groups exists", e);
  +            throw new DataBackendException("Error retriving user information", e);
           }
  +        if (groups.size() > 1)
  +        {
  +            throw new DataBackendException("Multiple groups with same name '" + 
group.getName() + "'");
  +        }
  +        return (groups.size() == 1);
       }
       /**
  -     * Creates a new group with specified attributes.
  -     *
  -     * @param group the object describing the group to be created.
  -     * @return a new Group object that has id set up properly.
  -     * @throws DataBackendException if there was an error accessing the data
  -     *         backend.
  -     * @throws EntityExistsException if the group already exists.
  -     */
  +     * Creates a new group with specified attributes.
  +     *
  +     * @param group the object describing the group to be created.
  +     * @return a new Group object that has id set up properly.
  +     * @throws DataBackendException if there was an error accessing the data
  +     *         backend.
  +     * @throws EntityExistsException if the group already exists.
  +     */
       public synchronized Group addGroup(Group group) throws DataBackendException, 
EntityExistsException
       {
           boolean groupExists = false;
  @@ -350,28 +362,40 @@
           {
               throw new DataBackendException("Could not create a group with an id!");
           }
  -        groupExists = checkExists(group);
  -        if (!groupExists)
  +        if (checkExists(group))
  +        {
  +            throw new EntityExistsException("The group '" + group.getName() + "' 
already exists");
  +        }
  +        try
           {
  -            group.setId(getUniqueId());
  -            groups.add(group);
  -            // return the object with correct id
  -            return group;
  +            session = hibernateService.openSession();
  +            transaction = session.beginTransaction();
  +            session.save(group);
  +            transaction.commit();
           }
  -        else
  +        catch (HibernateException e)
           {
  -            throw new EntityExistsException("Group '" + group + "' already exists");
  +            log.error("Error adding group", e);
  +            try
  +            {
  +                transaction.rollback();
  +            }
  +            catch (HibernateException he)
  +            {
  +            }
  +            throw new DataBackendException("Failed to create group '" + 
group.getName() + "'", e);
           }
  +        return group;
       }
       /**
  -       * Grants a Group a Role
  -       *
  -       * @param group the Group.
  -       * @param role the Role.
  -       * @throws DataBackendException if there was an error accessing the data
  -       *         backend.
  -       * @throws UnknownEntityException if group or role is not present.
  -       */
  +       * Grants a Group a Role
  +       *
  +       * @param group the Group.
  +       * @param role the Role.
  +       * @throws DataBackendException if there was an error accessing the data
  +       *         backend.
  +       * @throws UnknownEntityException if group or role is not present.
  +       */
       public synchronized void grant(Group group, Role role) throws 
DataBackendException, UnknownEntityException
       {
           boolean groupExists = false;
  @@ -464,9 +488,16 @@
       public void compose(ComponentManager manager) throws ComponentException
       {
           this.manager = manager;
  +        hibernateService = (HibernateService) manager.lookup(HibernateService.ROLE);
       }
  -    private int getUniqueId()
  +    /**
  +        * DESTRUCTION: step 2
  +        * @see org.apache.avalon.framework.activity.Disposable#dispose()
  +        */
  +    public void dispose()
       {
  -        return ++uniqueId;
  +        hibernateService = null;
  +        manager = null;
  +        roleManager = null;
       }
   }
  
  
  
  1.2       +128 -94   
jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernateRoleManagerImpl.java
  
  Index: HibernateRoleManagerImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernateRoleManagerImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HibernateRoleManagerImpl.java     23 Aug 2003 15:30:12 -0000      1.1
  +++ HibernateRoleManagerImpl.java     23 Aug 2003 21:09:52 -0000      1.2
  @@ -52,10 +52,15 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -import java.util.ArrayList;
  -import java.util.Iterator;
   import java.util.List;
   
  +import net.sf.hibernate.Hibernate;
  +import net.sf.hibernate.HibernateException;
  +import net.sf.hibernate.Session;
  +import net.sf.hibernate.Transaction;
  +import net.sf.hibernate.avalon.HibernateService;
  +
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.component.Composable;
  @@ -67,7 +72,7 @@
   import org.apache.fulcrum.security.entity.Permission;
   import org.apache.fulcrum.security.entity.Role;
   import org.apache.fulcrum.security.model.simple.entity.SimpleRole;
  -import org.apache.fulcrum.security.model.simple.manager.*;
  +import org.apache.fulcrum.security.model.simple.manager.SimpleRoleManager;
   import org.apache.fulcrum.security.util.DataBackendException;
   import org.apache.fulcrum.security.util.EntityExistsException;
   import org.apache.fulcrum.security.util.PermissionSet;
  @@ -80,18 +85,18 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
    * @version $Id$
    */
  -public class HibernateRoleManagerImpl extends AbstractLogEnabled implements 
SimpleRoleManager, Composable
  +public class HibernateRoleManagerImpl extends AbstractLogEnabled implements 
SimpleRoleManager, Composable, Disposable
   {
       boolean composed = false;
       /** Logging */
       private static Log log = LogFactory.getLog(HibernateRoleManagerImpl.class);
  -    /** List to store all our roles in */
  -    private static List roles = new ArrayList();
  -    private ComponentManager manager = null;
       /** Our permissionManager **/
       private PermissionManager permissionManager;
  -    /** Our Unique ID counter */
  -    private static int uniqueId = 0;
  +    /** Hibernate components */
  +    private HibernateService hibernateService;
  +    private Session session;
  +    private Transaction transaction;
  +    private ComponentManager manager = null;
       /**
        * @return
        */
  @@ -212,7 +217,6 @@
           {
               throw new DataBackendException("grant(Role,Permission) failed", e);
           }
  -       
           if (!roleExists)
           {
               throw new UnknownEntityException("Unknown role '" + role.getName() + 
"'");
  @@ -310,9 +314,8 @@
               roleExists = checkExists(role);
               if (roleExists)
               {
  -                roles.remove(role);
                   role.setName(name);
  -                roles.add(role);
  +                saveRole(role);
                   return;
               }
           }
  @@ -336,27 +339,43 @@
         */
       public boolean checkExists(Role role) throws DataBackendException
       {
  -        boolean exists = false;
  -        for (Iterator i = roles.iterator(); i.hasNext();)
  +        List roles;
  +        try
           {
  -            Role r = (Role) i.next();
  -            if (r.getName().equalsIgnoreCase(role.getName()) | r.getId() == 
role.getId())
  -            {
  -                exists = true;
  -            }
  +            session = hibernateService.openSession();
  +            roles = session.find("from SimpleRole sr where sr.name=?", 
role.getName(), Hibernate.STRING);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving role information", e);
  +        }
  +        if (roles.size() > 1)
  +        {
  +            throw new DataBackendException("Multiple roles with same name '" + 
role.getName() + "'");
           }
  -        return exists;
  +        return (roles.size() == 1);
       }
       /**
  -              * Retrieves all roles defined in the system.
  -              *
  -              * @return the names of all roles defined in the system.
  -              * @throws DataBackendException if there was an error accessing the
  -              *         data backend.
  -              */
  +      * Retrieves all roles defined in the system.
  +      *
  +      * @return the names of all roles defined in the system.
  +      * @throws DataBackendException if there was an error accessing the
  +      *         data backend.
  +      */
       public RoleSet getAllRoles() throws DataBackendException
       {
  -        return new RoleSet(roles);
  +        RoleSet roleSet = new RoleSet();
  +        try
  +        {
  +            session = hibernateService.openSession();
  +            List roles = session.find("from SimpleRole");
  +            roleSet.add(roles);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving role information", e);
  +        }
  +        return roleSet;
       }
       /**
         * Retrieves all permissions associated with a role.
  @@ -388,14 +407,14 @@
           throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
       }
       /**
  -      *       if the <code>Permission</code> exists in the security system.
  -      *
  -      * @param permission a <code>Permission</code> value
  -      * @return true if the permission exists in the system, false otherwise
  -      * @throws DataBackendException when more than one Permission with
  -      *         the same name exists.
  -      * @throws Exception A generic exception.
  -      */
  +     *        if the <code>Permission</code> exists in the security system.
  +     *
  +     * @param permission a <code>Permission</code> value
  +     * @return true if the permission exists in the system, false otherwise
  +     * @throws DataBackendException when more than one Permission with
  +     *         the same name exists.
  +     * @throws Exception A generic exception.
  +     */
       public boolean checkExists(Permission permission) throws DataBackendException
       {
           try
  @@ -408,14 +427,14 @@
           }
       }
       /**
  -     * Creates a new role with specified attributes.
  -     *
  -     * @param role the object describing the role to be created.
  -     * @return a new Role object that has id set up properly.
  -     * @throws DataBackendException if there was an error accessing the data
  -     *         backend.
  -     * @throws EntityExistsException if the role already exists.
  -     */
  +    * Creates a new role with specified attributes.
  +    *
  +    * @param role the object describing the role to be created.
  +    * @return a new Role object that has id set up properly.
  +    * @throws DataBackendException if there was an error accessing the data
  +    *         backend.
  +    * @throws EntityExistsException if the role already exists.
  +    */
       public synchronized Role addRole(Role role) throws DataBackendException, 
EntityExistsException
       {
           boolean roleExists = false;
  @@ -427,59 +446,70 @@
           {
               throw new DataBackendException("Could not create a role with an id!");
           }
  -        try
  +        if (checkExists(role))
           {
  -            roleExists = checkExists(role);
  -            if (!roleExists)
  -            {
  -                role.setId(getUniqueId());
  -                roles.add(role);
  -                // add the role to system-wide cache
  -                getAllRoles().add(role);
  -                // return the object with correct id
  -                return role;
  -            }
  +            throw new EntityExistsException("The role '" + role.getName() + "' 
already exists");
           }
  -        catch (Exception e)
  +        try
           {
  -            throw new DataBackendException("addRole(Role) failed", e);
  +            session = hibernateService.openSession();
  +            transaction = session.beginTransaction();
  +            session.save(role);
  +            transaction.commit();
           }
  -        finally
  +        catch (HibernateException e)
           {
  +            log.error("Error adding role", e);
  +            try
  +            {
  +                transaction.rollback();
  +            }
  +            catch (HibernateException he)
  +            {
  +            }
  +            throw new DataBackendException("Failed to create role '" + 
role.getName() + "'", e);
           }
  -        // the only way we could get here without return/throw tirggered
  -        // is that the roleExists was true.
  -        throw new EntityExistsException("Role '" + role + "' already exists");
  +        return role;
       }
       /**
  -       * Stores Role's attributes. The Roles is required to exist in the system.
  -       *
  -       * @param role The Role to be stored.
  -       * @throws DataBackendException if there was an error accessing the data
  -       *         backend.
  -       * @throws UnknownEntityException if the role does not exist.
  -       */
  +    * Stores Role's attributes. The Roles is required to exist in the system.
  +    *
  +    * @param role The Role to be stored.
  +    * @throws DataBackendException if there was an error accessing the data
  +    *         backend.
  +    * @throws UnknownEntityException if the role does not exist.
  +    */
       public void saveRole(Role role) throws DataBackendException, 
UnknownEntityException
       {
           boolean roleExists = false;
  -        roleExists = checkExists(role);
  -        if (roleExists)
  +        try
           {
  -            roles.add(role);
  +            roleExists = checkExists(role);
  +            if (roleExists)
  +            {
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.update(role);
  +                transaction.commit();
  +            }
  +            else
  +            {
  +                throw new UnknownEntityException("Unknown role '" + role + "'");
  +            }
           }
  -        else
  +        catch (Exception e)
           {
  -            throw new UnknownEntityException("Unknown role '" + role + "'");
  +            throw new DataBackendException("saveRole(Role) failed", e);
           }
       }
       /**
  -     * Removes a Role from the system.
  -     *
  -     * @param role The object describing the role to be removed.
  -     * @throws DataBackendException if there was an error accessing the data
  -     *         backend.
  -     * @throws UnknownEntityException if the role does not exist.
  -     */
  +    * Removes a Role from the system.
  +    *
  +    * @param role The object describing the role to be removed.
  +    * @throws DataBackendException if there was an error accessing the data
  +    *         backend.
  +    * @throws UnknownEntityException if the role does not exist.
  +    */
       public synchronized void removeRole(Role role) throws DataBackendException, 
UnknownEntityException
       {
           boolean roleExists = false;
  @@ -488,31 +518,35 @@
               roleExists = checkExists(role);
               if (roleExists)
               {
  -                // revoke all permissions from the role to be deleted
  -                revokeAll(role);
  -                roles.remove(role);
  -                getAllRoles().remove(role);
  -                return;
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.delete(role);
  +                transaction.commit();
  +            }
  +            else
  +            {
  +                throw new UnknownEntityException("Unknown role '" + role + "'");
               }
           }
           catch (Exception e)
           {
  -            throw new DataBackendException("removeRole(Role)", e);
  +            log.error("Failed to delete a Role");
  +            log.error(e);
  +            throw new DataBackendException("removeRole(Role) failed", e);
           }
  -        finally
  -        {
  -        }
  -        throw new UnknownEntityException("Unknown role '" + role + "'");
       }
       /**
  -   * Avalon component lifecycle method
  -   */
  +    * Avalon component lifecycle method
  +    */
       public void compose(ComponentManager manager) throws ComponentException
       {
           this.manager = manager;
  +        hibernateService = (HibernateService) manager.lookup(HibernateService.ROLE);
       }
  -    private int getUniqueId()
  +    public void dispose()
       {
  -        return ++uniqueId;
  +        hibernateService = null;
  +        manager = null;
  +        permissionManager = null;
       }
   }
  
  
  
  1.2       +124 -141  
jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernateUserManagerImpl.java
  
  Index: HibernateUserManagerImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernateUserManagerImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HibernateUserManagerImpl.java     23 Aug 2003 15:30:12 -0000      1.1
  +++ HibernateUserManagerImpl.java     23 Aug 2003 21:09:52 -0000      1.2
  @@ -52,16 +52,16 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  -
  +import net.sf.hibernate.Hibernate;
  +import net.sf.hibernate.HibernateException;
   import net.sf.hibernate.Session;
   import net.sf.hibernate.Transaction;
   import net.sf.hibernate.avalon.HibernateService;
  -
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.component.Composable;
  @@ -80,7 +80,7 @@
   import org.apache.fulcrum.security.model.simple.entity.SimpleGroup;
   import org.apache.fulcrum.security.model.simple.entity.SimpleRole;
   import org.apache.fulcrum.security.model.simple.entity.SimpleUser;
  -import org.apache.fulcrum.security.model.simple.manager.*;
  +import org.apache.fulcrum.security.model.simple.manager.SimpleUserManager;
   import org.apache.fulcrum.security.util.DataBackendException;
   import org.apache.fulcrum.security.util.EntityExistsException;
   import org.apache.fulcrum.security.util.GroupSet;
  @@ -94,13 +94,10 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
    * @version $Id$
    */
  -public class HibernateUserManagerImpl extends AbstractLogEnabled implements 
SimpleUserManager, Composable
  +public class HibernateUserManagerImpl extends AbstractLogEnabled implements 
SimpleUserManager, Composable, Disposable
   {
       /** Logging */
       private static Log log = LogFactory.getLog(HibernateUserManagerImpl.class);
  -    private static List users = new ArrayList();
  -    /** The class of ACL the SecurityService uses */
  -    private Class aclClass = DefaultAccessControlList.class;
       /** A factory to construct ACL Objects */
       private FactoryService aclFactoryService = null;
       private ComponentManager manager = null;
  @@ -108,12 +105,10 @@
       private GroupManager groupManager;
       /** Our roleManager **/
       private RoleManager roleManager;
  -    /** Our Unique ID counter */
  -    private static int uniqueId = 0;
  -     /** Hibernate components */
  -       private HibernateService persistence;
  -       private Session session;
  -       private Transaction transaction;
  +    /** Hibernate components */
  +    private HibernateService hibernateService;
  +    private Session session;
  +    private Transaction transaction;
       /**
        * @return
        */
  @@ -148,16 +143,7 @@
        */
       public boolean checkExists(User user) throws DataBackendException
       {
  -        boolean exists = false;
  -        for (Iterator i = users.iterator(); i.hasNext();)
  -        {
  -            User u = (User) i.next();
  -            if (u.getName().equalsIgnoreCase(user.getName()) | u.getId() == 
user.getId())
  -            {
  -                exists = true;
  -            }
  -        }
  -        return exists;
  +        return checkExists(user.getName());
       }
       /**
        * Check whether a specified user's account exists.
  @@ -171,20 +157,21 @@
        */
       public boolean checkExists(String userName) throws DataBackendException
       {
  -        List tempUsers = new ArrayList();
  -        for (Iterator i = users.iterator(); i.hasNext();)
  +        List users = null;
  +        try
           {
  -            User user = (User) i.next();
  -            if (user.getName().equalsIgnoreCase(userName))
  -            {
  -                tempUsers.add(user);
  -            }
  +            session = hibernateService.openSession();
  +            users = session.find("from SimpleUser su where su.name=?", userName, 
Hibernate.STRING);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving user information", e);
           }
  -        if (tempUsers.size() > 1)
  +        if (users.size() > 1)
           {
               throw new DataBackendException("Multiple Users with same username '" + 
userName + "'");
           }
  -        return (tempUsers.size() == 1);
  +        return (users.size() == 1);
       }
       /**
        * Retrieve a user from persistent storage using username as the
  @@ -199,22 +186,23 @@
        */
       public User getUser(String userName) throws UnknownEntityException, 
DataBackendException
       {
  -        List tempUsers = new ArrayList();
  -        for (Iterator i = users.iterator(); i.hasNext();)
  +        List users = null;
  +        try
           {
  -            User user = (User) i.next();
  -            if (user.getName().equalsIgnoreCase(userName))
  -            {
  -                tempUsers.add(user);
  -            }
  +            session = hibernateService.openSession();
  +            users = session.find("from SimpleUser su where su.name=?", userName, 
Hibernate.STRING);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving user information", e);
           }
  -        if (tempUsers.size() > 1)
  +        if (users.size() > 1)
           {
               throw new DataBackendException("Multiple Users with same username '" + 
userName + "'");
           }
  -        if (tempUsers.size() == 1)
  +        if (users.size() == 1)
           {
  -            return (User) tempUsers.get(0);
  +            return (User) users.get(0);
           }
           throw new UnknownEntityException("Unknown user '" + userName + "'");
       }
  @@ -354,12 +342,21 @@
           user.setPassword(initialPassword);
           try
           {
  -            users.remove(user);
  -            user.setId(getUniqueId());
  -            users.add(user);
  +            session = hibernateService.openSession();
  +            transaction = session.beginTransaction();
  +            session.save(user);
  +            transaction.commit();
           }
  -        catch (Exception e)
  +        catch (HibernateException e)
           {
  +            log.error("Error adding user", e);
  +            try
  +            {
  +                transaction.rollback();
  +            }
  +            catch (HibernateException he)
  +            {
  +            }
               throw new DataBackendException("Failed to create account '" + 
user.getName() + "'", e);
           }
       }
  @@ -498,85 +495,44 @@
        * @throws UnknownEntityException if user account, group or role is not
        *         present.
        */
  -    /*
  -     * 
  -     public synchronized void revoke(User user, Group group, Role role)
  -        throws DataBackendException, UnknownEntityException
  -    {
  -        boolean userExists = false;
  -        boolean groupExists = false;
  -        boolean roleExists = false;
  -        try
  -        {
  -            userExists = checkExists(user);
  -            groupExists = checkExists(group);
  -            roleExists = checkExists(role);
  -            if (userExists && groupExists && roleExists)
  -            {
  -                ((SimpleUser) user).getGroups().remove(group);
  -                ((SimpleUser) user).getRoles().remove(role);
  -                return;
  -            }
  -        }
  -        catch (Exception e)
  -        {
  -            throw new DataBackendException("revoke(User,Role,Group) failed", e);
  -        }
  -        finally
  -        {
  -        }
  -        if (!userExists)
  -        {
  -            throw new UnknownEntityException("Unknown user '" + user.getName() + 
"'");
  -        }
  -        if (!groupExists)
  -        {
  -            throw new UnknownEntityException("Unknown group '" + group.getName() + 
"'");
  -        }
  -        if (!roleExists)
  -        {
  -            throw new UnknownEntityException("Unknown role '" + role.getName() + 
"'");
  -        }
  -    }
  -    */
       /**
  -     * Determines if the <code>Group</code> exists in the security system.
  -     *
  -     * @param group a <code>Group</code> value
  -     * @return true if the group exists in the system, false otherwise
  -     * @throws DataBackendException when more than one Group with
  -     *         the same name exists.
  -     * @throws Exception A generic exception.
  -     */
  +    * Determines if the <code>Group</code> exists in the security system.
  +    *
  +    * @param group a <code>Group</code> value
  +    * @return true if the group exists in the system, false otherwise
  +    * @throws DataBackendException when more than one Group with
  +    *         the same name exists.
  +    * @throws Exception A generic exception.
  +    */
       private boolean checkExists(Group group) throws DataBackendException, Exception
       {
           return getGroupManager().checkExists(group);
       }
       /**
  -     * Determines if the <code>Role</code> exists in the security system.
  -     *
  -     * @param role a <code>Role</code> value
  -     * @return true if the role exists in the system, false otherwise
  -     * @throws DataBackendException when more than one Role with
  -     *         the same name exists.
  -     * @throws Exception A generic exception.
  -     */
  +     * Determines if the <code>Role</code> exists in the security system.
  +     *
  +     * @param role a <code>Role</code> value
  +     * @return true if the role exists in the system, false otherwise
  +     * @throws DataBackendException when more than one Role with
  +     *         the same name exists.
  +     * @throws Exception A generic exception.
  +     */
       private boolean checkExists(Role role) throws DataBackendException, Exception
       {
           return getRoleManager().checkExists(role);
       }
       /**
  -     * This method provides client-side encryption of passwords.
  -     *
  -     * If <code>secure.passwords</code> are enabled in TurbineResources,
  -     * the password will be encrypted, if not, it will be returned unchanged.
  -     * The <code>secure.passwords.algorithm</code> property can be used
  -     * to chose which digest algorithm should be used for performing the
  -     * encryption. <code>SHA</code> is used by default.
  -     *
  -     * @param password the password to process
  -     * @return processed password
  -     */
  +     * This method provides client-side encryption of passwords.
  +     *
  +     * If <code>secure.passwords</code> are enabled in TurbineResources,
  +     * the password will be encrypted, if not, it will be returned unchanged.
  +     * The <code>secure.passwords.algorithm</code> property can be used
  +     * to chose which digest algorithm should be used for performing the
  +     * encryption. <code>SHA</code> is used by default.
  +     *
  +     * @param password the password to process
  +     * @return processed password
  +     */
       public String encryptPassword(String password)
       {
           return encryptPassword(password, null);
  @@ -618,22 +574,6 @@
           return (result == null) ? false : result.equals(encpw);
       }
       /**
  -      * Return a Class object representing the system's chosen implementation of
  -      * of ACL interface.
  -      *
  -      * @return systems's chosen implementation of ACL interface.
  -      * @throws UnknownEntityException if the implementation of ACL interface
  -      *         could not be determined, or does not exist.
  -      */
  -    public Class getAclClass() throws UnknownEntityException
  -    {
  -        if (aclClass == null)
  -        {
  -            throw new UnknownEntityException("Failed to create a Class object for 
ACL implementation");
  -        }
  -        return aclClass;
  -    }
  -    /**
        * Construct a new ACL object.
        *
        * This constructs a new ACL object from the configured class and
  @@ -652,13 +592,13 @@
           AccessControlList accessControlList;
           try
           {
  -             /*
  -              * 
  -              @todo I think this is overkill for now..
  +            /*
  +             * 
  +             @todo I think this is overkill for now..
               accessControlList =
                   (AccessControlList) 
aclFactoryService.getInstance(aclClass.getName(), objects, signatures);
                   */
  -                accessControlList = new DefaultAccessControlList(roles,permissions);
  +            accessControlList = new DefaultAccessControlList(roles, permissions);
           }
           catch (Exception e)
           {
  @@ -702,7 +642,25 @@
       {
           // revoke all roles form the user
           revokeAll(user);
  -        users.remove(user);
  +        try
  +        {
  +            session = hibernateService.openSession();
  +            transaction = session.beginTransaction();
  +            session.delete(user);
  +            transaction.commit();
  +        }
  +        catch (HibernateException e)
  +        {
  +            log.error("Error deleting user", e);
  +            try
  +            {
  +                transaction.rollback();
  +            }
  +            catch (HibernateException he)
  +            {
  +            }
  +            throw new DataBackendException("Failed to remove account '" + 
user.getName() + "'", e);
  +        }
       }
       /**
          * Creates new user account with specified attributes.
  @@ -732,8 +690,25 @@
           userExists = checkExists(user);
           if (userExists)
           {
  -            users.remove(user);
  -            users.add(user);
  +            try
  +            {
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.update(user);
  +                transaction.commit();
  +            }
  +            catch (HibernateException e)
  +            {
  +                log.error("Error adding user", e);
  +                try
  +                {
  +                    transaction.rollback();
  +                }
  +                catch (HibernateException he)
  +                {
  +                }
  +                throw new DataBackendException("Failed to create account '" + 
user.getName() + "'", e);
  +            }
           }
           else
           {
  @@ -826,9 +801,17 @@
       public void compose(ComponentManager manager) throws ComponentException
       {
           this.manager = manager;
  +        hibernateService = (HibernateService) manager.lookup(HibernateService.ROLE);
       }
  -    private int getUniqueId()
  +    /**
  +     * DESTRUCTION: step 2
  +     * @see org.apache.avalon.framework.activity.Disposable#dispose()
  +     */
  +    public void dispose()
       {
  -        return ++uniqueId;
  +        hibernateService = null;
  +        manager = null;
  +        groupManager = null;
  +        roleManager = null;
       }
   }
  
  
  
  1.2       +133 -98   
jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernatePermissionManagerImpl.java
  
  Index: HibernatePermissionManagerImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/security/src/java/org/apache/fulcrum/security/spi/hibernate/simple/HibernatePermissionManagerImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HibernatePermissionManagerImpl.java       23 Aug 2003 15:30:12 -0000      1.1
  +++ HibernatePermissionManagerImpl.java       23 Aug 2003 21:09:52 -0000      1.2
  @@ -52,10 +52,15 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -import java.util.ArrayList;
  -import java.util.Iterator;
   import java.util.List;
   
  +import net.sf.hibernate.Hibernate;
  +import net.sf.hibernate.HibernateException;
  +import net.sf.hibernate.Session;
  +import net.sf.hibernate.Transaction;
  +import net.sf.hibernate.avalon.HibernateService;
  +
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.component.Composable;
  @@ -78,15 +83,18 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
    * @version $Id$
    */
  -public class HibernatePermissionManagerImpl extends AbstractLogEnabled implements 
PermissionManager, Composable
  +public class HibernatePermissionManagerImpl
  +    extends AbstractLogEnabled
  +    implements PermissionManager, Composable, Disposable
   {
       /** Logging */
       private static Log log = 
LogFactory.getLog(HibernatePermissionManagerImpl.class);
  -    private static List permissions = new ArrayList();
  -    private ComponentManager manager = null;
       private RoleManager roleManager = null;
  -    /** Our Unique ID counter */
  -    private static int uniqueId = 0;
  +    /** Hibernate components */
  +    private HibernateService hibernateService;
  +    private Session session;
  +    private Transaction transaction;
  +    private ComponentManager manager = null;
       /**
         * @return
         */
  @@ -177,25 +185,36 @@
           return permission;
       }
       /**
  -     * Retrieves all permissions defined in the system.
  -     *
  -     * @return the names of all roles defined in the system.
  -     * @throws DataBackendException if there was an error accessing the
  -     *         data backend.
  -     */
  +    * Retrieves all permissions defined in the system.
  +    *
  +    * @return the names of all roles defined in the system.
  +    * @throws DataBackendException if there was an error accessing the
  +    *         data backend.
  +    */
       public PermissionSet getAllPermissions() throws DataBackendException
       {
  -        return new PermissionSet(permissions);
  +        PermissionSet permissionSet = new PermissionSet();
  +        try
  +        {
  +            session = hibernateService.openSession();
  +            List permissions = session.find("from SimplePermission");
  +            permissionSet.add(permissions);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving permission 
information", e);
  +        }
  +        return permissionSet;
       }
       /**
  -     * Renames an existing Permission.
  -     *
  -     * @param permission The object describing the permission to be renamed.
  -     * @param name the new name for the permission.
  -     * @throws DataBackendException if there was an error accessing the data
  -     *         backend.
  -     * @throws UnknownEntityException if the permission does not exist.
  -     */
  +    * Renames an existing Permission.
  +    *
  +    * @param permission The object describing the permission to be renamed.
  +    * @param name the new name for the permission.
  +    * @throws DataBackendException if there was an error accessing the data
  +    *         backend.
  +    * @throws UnknownEntityException if the permission does not exist.
  +    */
       public synchronized void renamePermission(Permission permission, String name)
           throws DataBackendException, UnknownEntityException
       {
  @@ -205,9 +224,8 @@
               permissionExists = checkExists(permission);
               if (permissionExists)
               {
  -                permissions.remove(permission);
                   permission.setName(name);
  -                permissions.add(permission);
  +                savePermission(permission);
                   return;
               }
           }
  @@ -221,36 +239,42 @@
           throw new UnknownEntityException("Unknown permission '" + permission + "'");
       }
       /**
  -   * Determines if the <code>Permission</code> exists in the security system.
  -   *
  -   * @param permission a <code>Permission</code> value
  -   * @return true if the permission exists in the system, false otherwise
  -   * @throws DataBackendException when more than one Permission with
  -   *         the same name exists.
  -   * @throws Exception A generic exception.
  -   */
  +    * Determines if the <code>Permission</code> exists in the security system.
  +    *
  +    * @param permission a <code>Permission</code> value
  +    * @return true if the permission exists in the system, false otherwise
  +    * @throws DataBackendException when more than one Permission with
  +    *         the same name exists.
  +    * @throws Exception A generic exception.
  +    */
       public boolean checkExists(Permission permission) throws DataBackendException
       {
  -        boolean exists = false;
  -        for (Iterator i = permissions.iterator(); i.hasNext();)
  +        List permissions;
  +        try
           {
  -            Permission p = (Permission) i.next();
  -            if (p.getName().equalsIgnoreCase(permission.getName()) | p.getId() == 
permission.getId())
  -            {
  -                exists = true;
  -            }
  +            session = hibernateService.openSession();
  +            permissions =
  +                session.find("from SimplePermission sr where sr.name=?", 
permission.getName(), Hibernate.STRING);
  +        }
  +        catch (HibernateException e)
  +        {
  +            throw new DataBackendException("Error retriving permission 
information", e);
           }
  -        return exists;
  +        if (permissions.size() > 1)
  +        {
  +            throw new DataBackendException("Multiple permissions with same name '" 
+ permission.getName() + "'");
  +        }
  +        return (permissions.size() == 1);
       }
       /**
  -     * Stores Permission's attributes. The Permissions is required to exist in
  -     * the system.
  -     *
  -     * @param permission The Permission to be stored.
  -     * @throws DataBackendException if there was an error accessing the data
  -     *         backend.
  -     * @throws UnknownEntityException if the permission does not exist.
  -     */
  +    * Stores Permission's attributes. The Permissions is required to exist in
  +    * the system.
  +    *
  +    * @param permission The Permission to be stored.
  +    * @throws DataBackendException if there was an error accessing the data
  +    *         backend.
  +    * @throws UnknownEntityException if the permission does not exist.
  +    */
       public void savePermission(Permission permission) throws DataBackendException, 
UnknownEntityException
       {
           boolean permissionExists = false;
  @@ -259,8 +283,10 @@
               permissionExists = checkExists(permission);
               if (permissionExists)
               {
  -                permissions.remove(permission);
  -                permissions.add(permission);
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.update(permission);
  +                transaction.commit();
               }
               else
               {
  @@ -289,7 +315,10 @@
               permissionExists = checkExists(permission);
               if (permissionExists)
               {
  -                permissions.remove(permission);
  +                session = hibernateService.openSession();
  +                transaction = session.beginTransaction();
  +                session.delete(permission);
  +                transaction.commit();
               }
               else
               {
  @@ -298,21 +327,20 @@
           }
           catch (Exception e)
           {
  -            throw new DataBackendException("removePermission(Permission)", e);
  -        }
  -        finally
  -        {
  +            log.error("Failed to delete a Permission");
  +            log.error(e);
  +            throw new DataBackendException("removePermission(Permission) failed", 
e);
           }
       }
       /**
  -     * Creates a new permission with specified attributes.
  -     *
  -     * @param permission the object describing the permission to be created.
  -     * @return a new Permission object that has id set up properly.
  -     * @throws DataBackendException if there was an error accessing the data
  -     *         backend.
  -     * @throws EntityExistsException if the permission already exists.
  -     */
  +    * Creates a new permission with specified attributes.
  +    *
  +    * @param permission the object describing the permission to be created.
  +    * @return a new Permission object that has id set up properly.
  +    * @throws DataBackendException if there was an error accessing the data
  +    *         backend.
  +    * @throws EntityExistsException if the permission already exists.
  +    */
       public synchronized Permission addPermission(Permission permission)
           throws DataBackendException, EntityExistsException
       {
  @@ -325,36 +353,40 @@
           {
               throw new DataBackendException("Could not create a permission with an 
id!");
           }
  -        try
  +        if (checkExists(permission))
           {
  -            permissionExists = checkExists(permission);
  -            if (!permissionExists)
  -            {
  -                permission.setId(getUniqueId());
  -                permissions.add(permission);
  -                return permission;
  -            }
  +            throw new EntityExistsException("The permission '" + 
permission.getName() + "' already exists");
           }
  -        catch (Exception e)
  +        try
           {
  -            throw new DataBackendException("addPermission(Permission) failed", e);
  +            session = hibernateService.openSession();
  +            transaction = session.beginTransaction();
  +            session.save(permission);
  +            transaction.commit();
           }
  -        finally
  +        catch (HibernateException e)
           {
  +            log.error("Error adding permission", e);
  +            try
  +            {
  +                transaction.rollback();
  +            }
  +            catch (HibernateException he)
  +            {
  +            }
  +            throw new DataBackendException("Failed to create permission '" + 
permission.getName() + "'", e);
           }
  -        // the only way we could get here without return/throw tirggered
  -        // is that the permissionExists was true.
  -        throw new EntityExistsException("Permission '" + permission + "' already 
exists");
  +        return permission;
       }
       /**
  -      * Retrieves all permissions associated with a role.
  -      *
  -      * @param role the role name, for which the permissions are to be retrieved.
  -      * @return A Permission set for the Role.
  -      * @throws DataBackendException if there was an error accessing the data
  -      *         backend.
  -      * @throws UnknownEntityException if the role is not present.
  -      */
  +      * Retrieves all permissions associated with a role.
  +      *
  +      * @param role the role name, for which the permissions are to be retrieved.
  +      * @return A Permission set for the Role.
  +      * @throws DataBackendException if there was an error accessing the data
  +      *         backend.
  +      * @throws UnknownEntityException if the role is not present.
  +      */
       public PermissionSet getPermissions(Role role) throws DataBackendException, 
UnknownEntityException
       {
           boolean roleExists = false;
  @@ -376,14 +408,14 @@
           throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
       }
       /**
  -     * Determines if the <code>Role</code> exists in the security system.
  -     *
  -     * @param role a <code>Role</code> value
  -     * @return true if the role exists in the system, false otherwise
  -     * @throws DataBackendException when more than one Role with
  -     *         the same name exists.
  -     * @throws Exception A generic exception.
  -     */
  +     * Determines if the <code>Role</code> exists in the security system.
  +     *
  +     * @param role a <code>Role</code> value
  +     * @return true if the role exists in the system, false otherwise
  +     * @throws DataBackendException when more than one Role with
  +     *         the same name exists.
  +     * @throws Exception A generic exception.
  +     */
       public boolean checkExists(Role role) throws DataBackendException
       {
           try
  @@ -396,14 +428,17 @@
           }
       }
       /**
  -       * Avalon component lifecycle method
  -       */
  +    * Avalon component lifecycle method
  +    */
       public void compose(ComponentManager manager) throws ComponentException
       {
           this.manager = manager;
  +        hibernateService = (HibernateService) manager.lookup(HibernateService.ROLE);
       }
  -    private int getUniqueId()
  +    public void dispose()
       {
  -        return ++uniqueId;
  +        hibernateService = null;
  +        manager = null;
  +        roleManager = null;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to