You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by tv...@apache.org on 2006/10/08 21:54:42 UTC

svn commit: r454199 - in /jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque: TorqueAbstractGroupManager.java TorqueAbstractPermissionManager.java TorqueAbstractRoleManager.java TorqueAbstractUserManager.java

Author: tv
Date: Sun Oct  8 12:54:42 2006
New Revision: 454199

URL: http://svn.apache.org/viewvc?view=rev&rev=454199
Log:
Refactored stuff for better code re-use

Added:
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractGroupManager.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java
    jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java

Added: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractGroupManager.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractGroupManager.java?view=auto&rev=454199
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractGroupManager.java (added)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractGroupManager.java Sun Oct  8 12:54:42 2006
@@ -0,0 +1,343 @@
+package org.apache.fulcrum.security.torque;
+/*
+ *  Copyright 2001-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+import java.sql.Connection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.fulcrum.security.entity.Group;
+import org.apache.fulcrum.security.spi.AbstractGroupManager;
+import org.apache.fulcrum.security.torque.om.TorqueGroup;
+import org.apache.fulcrum.security.torque.om.TorqueGroupPeer;
+import org.apache.fulcrum.security.util.DataBackendException;
+import org.apache.fulcrum.security.util.EntityExistsException;
+import org.apache.fulcrum.security.util.GroupSet;
+import org.apache.fulcrum.security.util.UnknownEntityException;
+import org.apache.torque.NoRowsException;
+import org.apache.torque.TorqueException;
+import org.apache.torque.om.SimpleKey;
+import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Transaction;
+/**
+ * This implementation persists to a database via Torque.
+ *
+ * The names of the group class, group peer class and the
+ * columns of the group class can be overridden in a 
+ * configuration like this:
+ * <pre>
+ * &lt;groupClass&gt;
+ *     &lt;className&gt;org.fulcrum.security.torque.om.TorqueGroup&lt;/className&gt;
+ *     &lt;peerName&gt;org.fulcrum.security.torque.om.TorqueGroupPeer&lt;/peerName&gt;
+ *     &lt;idColumn&gt;GROUP_ID&lt;/idColumn&gt;
+ *     &lt;nameColumn&gt;GROUP_NAME&lt;/nameColumn&gt;
+ * &lt;/group&gt;
+ * </pre>
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id:$
+ */
+public abstract class TorqueAbstractGroupManager extends AbstractGroupManager
+{
+    public static final String GROUP = "group";
+    
+    /**
+     * Provides the attached object lists for the given group
+     *  
+     * @param group the group for which the lists should be retrieved  
+     * @param con a database connection
+     */
+    protected abstract void attachObjectsForGroup(Group group, Connection con)
+        throws TorqueException, DataBackendException;
+    
+    /**
+     * @see org.apache.fulcrum.security.spi.AbstractEntityManager#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
+    public void configure(Configuration conf) throws ConfigurationException
+    {
+        super.configure(conf);
+    }
+
+    /**
+     * Retrieve a Group object with specified name.
+     *
+     * @param name the name of the Group.
+     * @return an object representing the Group with specified name.
+     * @throws DataBackendException if there was an error accessing the
+     *         data backend.
+     * @throws UnknownEntityException if the group does not exist.
+     */
+    public Group getGroupByName(String name)
+        throws DataBackendException, UnknownEntityException
+    {
+        Group group = getGroupInstance();
+        List groups = Collections.EMPTY_LIST;
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
+            
+            Criteria criteria = new Criteria();
+            criteria.add(TorqueGroupPeer.GROUP_NAME, name);
+
+            groups = TorqueGroupPeer.doSelect(criteria, con);
+
+            if (groups.size() == 1)
+            {
+                TorqueGroup g = (TorqueGroup) groups.get(0);
+                
+                group.setId(g.getId());
+                group.setName(g.getName());
+                
+                // Add dependent objects if they exist
+                attachObjectsForGroup(group, con);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving group information", e);
+        }
+
+        if (groups.size() == 0)
+        {
+            throw new UnknownEntityException("Could not find group" + name);
+        }
+
+        if (groups.size() > 1)
+        {
+            throw new DataBackendException("Multiple Groups with same name '" + name + "'");
+        }
+
+        return group;
+    }
+
+    /**
+     * Retrieves all groups defined in the system.
+     *
+     * @return the names of all groups defined in the system.
+     * @throws DataBackendException if there was an error accessing the
+     *         data backend.
+     */
+    public GroupSet getAllGroups() throws DataBackendException
+    {
+        GroupSet groupSet = new GroupSet();
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
+
+            List groups = TorqueGroupPeer.doSelect(new Criteria(), con);
+            
+            for (Iterator i = groups.iterator(); i.hasNext();)
+            {
+                Group group = getGroupInstance();
+                TorqueGroup g = (TorqueGroup)i.next();
+                group.setId(g.getId());
+                group.setName(g.getName());
+                
+                // Add dependent objects if they exist
+                attachObjectsForGroup(group, con);
+
+                groupSet.add(group);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving group information", e);
+        }
+
+        return groupSet;
+    }
+
+    /**
+        * Removes a Group from the system.
+        *
+        * @param group The object describing the group to be removed.
+        * @throws DataBackendException if there was an error accessing the data
+        *         backend.
+        * @throws UnknownEntityException if the group does not exist.
+        */
+    public synchronized void removeGroup(Group group)
+        throws DataBackendException, UnknownEntityException
+    {
+        try
+        {
+            TorqueGroupPeer.doDelete(SimpleKey.keyFor((Integer)group.getId()));
+        }
+        catch (TorqueException e)
+        {
+            throw new DataBackendException("Removing Group '" + group + "' failed", e);
+        }
+    }
+
+    /**
+        * Renames an existing Group.
+        *
+        * @param group The object describing the group to be renamed.
+        * @param name the new name for the group.
+        * @throws DataBackendException if there was an error accessing the data
+        *         backend.
+        * @throws UnknownEntityException if the group does not exist.
+        */
+    public synchronized void renameGroup(Group group, String name)
+        throws DataBackendException, UnknownEntityException
+    {
+        if (checkExists(group))
+        {
+            group.setName(name);
+
+            try
+            {
+                TorqueGroup g = new TorqueGroup();
+                g.setId((Integer)group.getId());
+                g.setName(name);
+                g.setNew(false);
+                g.save();
+            }
+            catch (Exception e)
+            {
+                throw new DataBackendException("Renaming Group '" + group + "' failed", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Unknown group '" + group + "'");
+        }
+    }
+
+    /**
+     * Determines if the <code>Group</code> exists in the security system.
+     *
+     * @param groupName a <code>Group</code> value
+     * @return true if the group name exists in the system, false otherwise
+     * @throws DataBackendException when more than one Group with
+     *         the same name exists.
+     */
+    public boolean checkExists(String groupName) throws DataBackendException
+    {
+        List groups;
+
+        try
+        {
+            Criteria criteria = new Criteria();
+            criteria.add(TorqueGroupPeer.GROUP_NAME, groupName);
+
+            groups = TorqueGroupPeer.doSelect(criteria);
+        }
+        catch (TorqueException e)
+        {
+            throw new DataBackendException("Error retrieving group information", e);
+        }
+
+        if (groups.size() > 1)
+        {
+            throw new DataBackendException(
+                    "Multiple groups with same name '" + groupName + "'");
+        }
+
+        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.
+    */
+    protected synchronized Group persistNewGroup(Group group)
+        throws DataBackendException
+    {
+        try
+        {
+            TorqueGroup g = new TorqueGroup();
+            g.setName(group.getName());
+            g.save();
+            
+            group.setId(g.getId());
+        }
+        catch (Exception e)
+        {
+            throw new DataBackendException("Adding Group '" + group + "' failed", e);
+        }
+        
+        return group;
+    }
+
+    /**
+     * Retrieve a Group object with specified id.
+     *
+     * @param id
+     *            the id of the Group.
+     * @return an object representing the Group with specified id.
+     * @throws DataBackendException
+     *             if there was an error accessing the data backend.
+     * @throws UnknownEntityException
+     *             if the group does not exist.
+     */
+    public Group getGroupById(Object id)
+        throws DataBackendException, UnknownEntityException
+    {
+        Group group = getGroupInstance();
+
+        if (id != null && id instanceof Integer)
+        {
+            Connection con = null;
+
+            try
+            {
+                con = Transaction.begin(TorqueGroupPeer.DATABASE_NAME);
+                
+                TorqueGroup g = TorqueGroupPeer.retrieveByPK((Integer)id, con);
+                
+                group.setId(g.getId());
+                group.setName(g.getName());
+                
+                // Add dependent objects if they exist
+                attachObjectsForGroup(group, con);
+
+                Transaction.commit(con);
+            }
+            catch (NoRowsException e)
+            {
+                Transaction.safeRollback(con);
+                throw new UnknownEntityException("Group with id '" + id + "' does not exist.", e);
+            }
+            catch (TorqueException e)
+            {
+                Transaction.safeRollback(con);
+                throw new DataBackendException("Error retrieving group information", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Invalid group id '" + group.getId() + "'");
+        }
+
+        return group;
+    }
+}
\ No newline at end of file

Added: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java?view=auto&rev=454199
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java (added)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java Sun Oct  8 12:54:42 2006
@@ -0,0 +1,337 @@
+package org.apache.fulcrum.security.torque;
+/*
+ *  Copyright 2001-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+import java.sql.Connection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.fulcrum.security.entity.Permission;
+import org.apache.fulcrum.security.spi.AbstractPermissionManager;
+import org.apache.fulcrum.security.torque.om.TorquePermission;
+import org.apache.fulcrum.security.torque.om.TorquePermissionPeer;
+import org.apache.fulcrum.security.util.DataBackendException;
+import org.apache.fulcrum.security.util.EntityExistsException;
+import org.apache.fulcrum.security.util.PermissionSet;
+import org.apache.fulcrum.security.util.UnknownEntityException;
+import org.apache.torque.NoRowsException;
+import org.apache.torque.TorqueException;
+import org.apache.torque.om.SimpleKey;
+import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Transaction;
+/**
+ * This implementation persists to a database via Torque.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id:$
+ */
+public abstract class TorqueAbstractPermissionManager extends AbstractPermissionManager
+{
+    /**
+     * Provides the attached object lists for the given permission
+     *  
+     * @param permission the permission for which the lists should be retrieved  
+     * @param con a database connection
+     */
+    protected abstract void attachObjectsForPermission(Permission permission, Connection con)
+        throws TorqueException, DataBackendException;
+
+    /**
+     * 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
+    {
+        PermissionSet permissionSet = new PermissionSet();
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorquePermissionPeer.DATABASE_NAME);
+            
+            List permissions = TorquePermissionPeer.doSelect(new Criteria(), con);
+            
+            for (Iterator i = permissions.iterator(); i.hasNext();)
+            {
+                TorquePermission p = (TorquePermission)i.next();
+
+                // TODO: This throws UnknownEntityException.
+                Permission permission = getPermissionInstance();
+                permission.setId(p.getId());
+                permission.setName(p.getName());
+
+                // Add attached objects if they exist
+                attachObjectsForPermission(permission, con);
+
+                permissionSet.add(permission);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (Exception e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving 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.
+     */
+    public synchronized void renamePermission(Permission permission, String name)
+        throws DataBackendException, UnknownEntityException
+    {
+        if (checkExists(permission))
+        {
+            permission.setName(name);
+            
+            try
+            {
+                TorquePermission p = new TorquePermission();
+                p.setId((Integer)permission.getId());
+                p.setName(name);
+                p.setNew(false);
+                p.save();
+            }
+            catch (Exception e)
+            {
+                throw new DataBackendException("Renaming Permission '" + permission + "' failed", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Unknown permission '" + permission + "'");
+        }
+    }
+
+    /**
+     * Determines if the <code>Permission</code> exists in the security
+     * system.
+     *
+     * @param permissionName
+     *            a <code>Permission</code> value
+     * @return true if the permission name exists in the system, false otherwise
+     * @throws DataBackendException
+     *             when more than one Permission with the same name exists.
+     */
+    public boolean checkExists(String permissionName) throws DataBackendException
+    {
+        List permissions;
+        
+        try
+        {
+            Criteria criteria = new Criteria();
+            criteria.add(TorquePermissionPeer.PERMISSION_NAME, permissionName);
+
+            permissions = TorquePermissionPeer.doSelect(criteria);
+        }
+        catch (TorqueException e)
+        {
+            throw new DataBackendException("Error retrieving permission information", e);
+        }
+
+        if (permissions.size() > 1)
+        {
+            throw new DataBackendException("Multiple permissions with same name '" + permissionName + "'");
+        }
+
+        return (permissions.size() == 1);
+    }
+
+    /**
+     * Removes a Permission from the system.
+     *
+     * @param permission
+     *            The object describing the permission to be removed.
+     * @throws DataBackendException
+     *             if there was an error accessing the data backend.
+     * @throws UnknownEntityException
+     *             if the permission does not exist.
+     */
+    public synchronized void removePermission(Permission permission)
+        throws DataBackendException, UnknownEntityException
+    {
+        if (checkExists(permission))
+        {
+            try
+            {
+                TorquePermissionPeer.doDelete(SimpleKey.keyFor((Integer)permission.getId()));
+            }
+            catch (TorqueException e)
+            {
+                throw new DataBackendException("Removing Permission '" + permission + "' failed", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Unknown permission '" + permission + "'");
+        }
+    }
+
+    /**
+     * 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.
+     */
+    protected synchronized Permission persistNewPermission(Permission permission)
+        throws DataBackendException
+    {
+        try
+        {
+            TorquePermission p = new TorquePermission();
+            p.setName(permission.getName());
+            p.save();
+            
+            permission.setId(p.getId());
+        }
+        catch (Exception e)
+        {
+            throw new DataBackendException("Adding Permission '" + permission + "' failed", e);
+        }
+
+        return permission;
+    }
+
+    /**
+     * Retrieve a Permission object with specified id.
+     *
+     * @param id
+     *            the id of the Permission.
+     * @return an object representing the Permission with specified id.
+     * @throws DataBackendException
+     *             if there was an error accessing the data backend.
+     * @throws UnknownEntityException
+     *             if the permission does not exist.
+     */
+    public Permission getPermissionById(Object id)
+        throws DataBackendException, UnknownEntityException
+    {
+        Permission permission = getPermissionInstance();
+
+        if (id != null && id instanceof Integer)
+        {
+            Connection con = null;
+
+            try
+            {
+                con = Transaction.begin(TorquePermissionPeer.DATABASE_NAME);
+                
+                TorquePermission p = 
+                    TorquePermissionPeer.retrieveByPK((Integer)id, con);
+                permission.setId(p.getId());
+                permission.setName(p.getName());
+
+                // Add attached objects if they exist
+                attachObjectsForPermission(permission, con);
+
+                Transaction.commit(con);
+            }
+            catch (NoRowsException e)
+            {
+                Transaction.safeRollback(con);
+                throw new UnknownEntityException("Permission with id '" + id + "' does not exist.", e);
+            }
+            catch (TorqueException e)
+            {
+                Transaction.safeRollback(con);
+                throw new DataBackendException("Error retrieving permission information", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Invalid permission id '" + permission.getId() + "'");
+        }
+
+        return permission;
+    }
+
+    /**
+     * Retrieve a Permission object with specified name.
+     *
+     * @param name the name of the Group.
+     * @return an object representing the Group with specified name.
+     * @throws DataBackendException if there was an error accessing the
+     *         data backend.
+     * @throws UnknownEntityException if the group does not exist.
+     */
+    public Permission getPermissionByName(String name)
+        throws DataBackendException, UnknownEntityException
+    {
+        Permission permission = getPermissionInstance();
+        List permissions = Collections.EMPTY_LIST;
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorquePermissionPeer.DATABASE_NAME);
+            
+            Criteria criteria = new Criteria();
+            criteria.add(TorquePermissionPeer.PERMISSION_NAME, name);
+
+            permissions = TorquePermissionPeer.doSelect(criteria, con);
+
+            if (permissions.size() == 1)
+            {
+                TorquePermission p = (TorquePermission) permissions.get(0);
+                
+                permission.setId(p.getId());
+                permission.setName(p.getName());
+                
+                // Add attached objects if they exist
+                attachObjectsForPermission(permission, con);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving permission information", e);
+        }
+
+        if (permissions.size() == 0)
+        {
+            throw new UnknownEntityException("Could not find permission " + name);
+        }
+
+        if (permissions.size() > 1)
+        {
+            throw new DataBackendException("Multiple Permissions with same name '" + name + "'");
+        }
+
+        return permission;
+    }
+}

Added: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java?view=auto&rev=454199
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java (added)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java Sun Oct  8 12:54:42 2006
@@ -0,0 +1,329 @@
+package org.apache.fulcrum.security.torque;
+/*
+ *  Copyright 2001-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+import java.sql.Connection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.fulcrum.security.entity.Role;
+import org.apache.fulcrum.security.spi.AbstractRoleManager;
+import org.apache.fulcrum.security.torque.om.TorqueRole;
+import org.apache.fulcrum.security.torque.om.TorqueRolePeer;
+import org.apache.fulcrum.security.util.DataBackendException;
+import org.apache.fulcrum.security.util.EntityExistsException;
+import org.apache.fulcrum.security.util.RoleSet;
+import org.apache.fulcrum.security.util.UnknownEntityException;
+import org.apache.torque.NoRowsException;
+import org.apache.torque.TorqueException;
+import org.apache.torque.om.SimpleKey;
+import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Transaction;
+/**
+ * This implementation persists to a database via Torque.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id:$
+ */
+public abstract class TorqueAbstractRoleManager extends AbstractRoleManager
+{
+    /**
+     * Provides the attached object lists for the given role
+     *  
+     * @param role the role for which the lists should be retrieved  
+     * @param con a database connection
+     */
+    protected abstract void attachObjectsForRole(Role role, Connection con)
+        throws TorqueException, DataBackendException, UnknownEntityException;
+
+    /**
+    * Renames an existing Role.
+    *
+    * @param role The object describing the role to be renamed.
+    * @param name the new name for the role.
+    * @throws DataBackendException if there was an error accessing the data
+    *         backend.
+    * @throws UnknownEntityException if the role does not exist.
+    */
+    public synchronized void renameRole(Role role, String name)
+        throws DataBackendException, UnknownEntityException
+    {
+        if (checkExists(role))
+        {
+            role.setName(name);
+            
+            try
+            {
+                TorqueRole r = new TorqueRole();
+                r.setId((Integer)role.getId());
+                r.setName(name);
+                r.setNew(false);
+                r.save();
+            }
+            catch (Exception e)
+            {
+                throw new DataBackendException("Renaming Role '" + role + "' failed", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Unknown Role '" + role + "'");
+        }
+    }
+
+    /**
+      * Determines if the <code>Role</code> exists in the security system.
+      *
+      * @param roleName a <code>Role</code> value
+      * @return true if the role name exists in the system, false otherwise
+      * @throws DataBackendException when more than one Role with
+      *         the same name exists.
+      */
+    public boolean checkExists(String roleName) throws DataBackendException
+    {
+        List roles;
+
+        try
+        {
+            Criteria criteria = new Criteria();
+            criteria.add(TorqueRolePeer.ROLE_NAME, roleName);
+
+            roles = TorqueRolePeer.doSelect(criteria);
+        }
+        catch (TorqueException e)
+        {
+            throw new DataBackendException("Error retrieving role information", e);
+        }
+
+        if (roles.size() > 1)
+        {
+            throw new DataBackendException("Multiple roles with same name '" + roleName + "'");
+        }
+        
+        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.
+     */
+    public RoleSet getAllRoles() throws DataBackendException
+    {
+        RoleSet roleSet = new RoleSet();
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorqueRolePeer.DATABASE_NAME);
+            
+            List roles = TorqueRolePeer.doSelect(new Criteria(), con);
+            
+            for (Iterator i = roles.iterator(); i.hasNext();)
+            {
+                Role role = getRoleInstance();
+                TorqueRole r = (TorqueRole)i.next();
+                role.setId(r.getId());
+                role.setName(r.getName());
+
+                // Add attached objects if they exist
+                attachObjectsForRole(role, con);
+
+                roleSet.add(role);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving role information", e);
+        }
+        catch (UnknownEntityException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error creating permission instance", e);
+        }
+        
+        return roleSet;
+    }
+
+    /**
+    * 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.
+    */
+    protected synchronized Role persistNewRole(Role role) throws DataBackendException
+    {
+        try
+        {
+            TorqueRole r = new TorqueRole();
+            r.setName(role.getName());
+            r.save();
+            
+            role.setId(r.getId());
+        }
+        catch (Exception e)
+        {
+            throw new DataBackendException("Adding Role '" + role + "' failed", e);
+        }
+        
+        return role;
+    }
+
+    /**
+    * 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
+    {
+        if (checkExists(role))
+        {
+            try
+            {
+                TorqueRolePeer.doDelete(SimpleKey.keyFor((Integer)role.getId()));
+            }
+            catch (TorqueException e)
+            {
+                throw new DataBackendException("Removing Role '" + role + "' failed", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Unknown role '" + role + "'");
+        }
+    }
+
+    /**
+     * Retrieve a Role object with specified id.
+     *
+     * @param id
+     *            the id of the Role.
+     * @return an object representing the Role with specified id.
+     * @throws DataBackendException
+     *             if there was an error accessing the data backend.
+     * @throws UnknownEntityException
+     *             if the role does not exist.
+     */
+    public Role getRoleById(Object id)
+        throws DataBackendException, UnknownEntityException 
+    {
+        Role role = getRoleInstance();
+
+        if (id != null && id instanceof Integer)
+        {
+            Connection con = null;
+
+            try
+            {
+                con = Transaction.begin(TorqueRolePeer.DATABASE_NAME);
+                
+                TorqueRole r = 
+                    TorqueRolePeer.retrieveByPK((Integer)id, con);
+                role.setId(r.getId());
+                role.setName(r.getName());
+                
+                // Add attached objects if they exist
+                attachObjectsForRole(role, con);
+
+                Transaction.commit(con);
+            }
+            catch (NoRowsException e)
+            {
+                Transaction.safeRollback(con);
+                throw new UnknownEntityException("Role with id '" + id + "' does not exist.", e);
+            }
+            catch (TorqueException e)
+            {
+                Transaction.safeRollback(con);
+                throw new DataBackendException("Error retrieving role information", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Invalid role id '" + role.getId() + "'");
+        }
+
+        return role;
+    }
+
+    /**
+     * Retrieve a Role object with specified name.
+     *
+     * @param name the name of the Role.
+     * @return an object representing the Role with specified name.
+     * @throws DataBackendException if there was an error accessing the
+     *         data backend.
+     * @throws UnknownEntityException if the role does not exist.
+     */
+    public Role getRoleByName(String name)
+        throws DataBackendException, UnknownEntityException
+    {
+        Role role = getRoleInstance();
+        List roles = Collections.EMPTY_LIST;
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorqueRolePeer.DATABASE_NAME);
+            
+            Criteria criteria = new Criteria();
+            criteria.add(TorqueRolePeer.ROLE_NAME, name);
+
+            roles = TorqueRolePeer.doSelect(criteria, con);
+
+            if (roles.size() == 1)
+            {
+                TorqueRole r = (TorqueRole) roles.get(0);
+                
+                role.setId(r.getId());
+                role.setName(r.getName());
+                
+                // Add attached objects if they exist
+                attachObjectsForRole(role, con);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving role information", e);
+        }
+
+        if (roles.size() == 0)
+        {
+            throw new UnknownEntityException("Could not find role" + name);
+        }
+
+        if (roles.size() > 1)
+        {
+            throw new DataBackendException("Multiple Roles with same name '" + name + "'");
+        }
+
+        return role;
+    }
+}

Added: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java?view=auto&rev=454199
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java (added)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java Sun Oct  8 12:54:42 2006
@@ -0,0 +1,324 @@
+package org.apache.fulcrum.security.torque;
+/*
+ *  Copyright 2001-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+import java.sql.Connection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.fulcrum.security.entity.User;
+import org.apache.fulcrum.security.spi.AbstractUserManager;
+import org.apache.fulcrum.security.torque.om.TorqueUser;
+import org.apache.fulcrum.security.torque.om.TorqueUserPeer;
+import org.apache.fulcrum.security.util.DataBackendException;
+import org.apache.fulcrum.security.util.EntityExistsException;
+import org.apache.fulcrum.security.util.UnknownEntityException;
+import org.apache.fulcrum.security.util.UserSet;
+import org.apache.torque.NoRowsException;
+import org.apache.torque.TorqueException;
+import org.apache.torque.om.SimpleKey;
+import org.apache.torque.util.Criteria;
+import org.apache.torque.util.Transaction;
+/**
+ * This implementation persists to a database via Torque.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id:$
+ */
+public abstract class TorqueAbstractUserManager extends AbstractUserManager
+{
+    /**
+     * Provides the attached object lists for the given user
+     *  
+     * @param user the user for which the lists should be retrieved  
+     * @param con a database connection
+     */
+    protected abstract void attachObjectsForUser(User user, Connection con)
+        throws TorqueException, DataBackendException;
+    
+    /**
+     * Check whether a specified user's account exists.
+     *
+     * The login name is used for looking up the account.
+     *
+     * @param userName The name of the user to be checked.
+     * @return true if the specified account exists
+     * @throws DataBackendException if there was an error accessing
+     *         the data backend.
+     */
+    public boolean checkExists(String userName) throws DataBackendException
+    {
+        List users;
+
+        try
+        {
+            Criteria criteria = new Criteria();
+            criteria.add(TorqueUserPeer.LOGIN_NAME, userName.toLowerCase());
+
+            users = TorqueUserPeer.doSelect(criteria);
+        }
+        catch (TorqueException e)
+        {
+            throw new DataBackendException("Error retrieving user information", e);
+        }
+
+        if (users.size() > 1)
+        {
+            throw new DataBackendException("Multiple Users with same username '" + userName + "'");
+        }
+        
+        return (users.size() == 1);
+    }
+
+    /**
+     * Retrieve a user from persistent storage using username as the
+     * key.
+     *
+     * @param userName the name of the user.
+     * @return an User object.
+     * @exception UnknownEntityException if the user's account does not
+     *            exist in the database.
+     * @exception DataBackendException if there is a problem accessing the
+     *            storage.
+     */
+    public User getUser(String userName) throws UnknownEntityException, DataBackendException
+    {
+        User user = getUserInstance();
+        List users = Collections.EMPTY_LIST;
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
+            
+            Criteria criteria = new Criteria();
+            criteria.add(TorqueUserPeer.LOGIN_NAME, userName.toLowerCase());
+
+            users = TorqueUserPeer.doSelect(criteria, con);
+
+            if (users.size() == 1)
+            {
+                TorqueUser u = (TorqueUser) users.get(0);
+                
+                user.setId(u.getId());
+                user.setName(u.getName());
+                user.setPassword(u.getPassword());
+                
+                // Add attached objects if they exist
+                attachObjectsForUser(user, con);
+            }
+            
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving user information", e);
+        }
+
+        if (users.size() == 0)
+        {
+            throw new UnknownEntityException("Unknown user '" + userName + "'");
+        }
+
+        if (users.size() > 1)
+        {
+            throw new DataBackendException("Multiple Users with same username '" + userName + "'");
+        }
+
+        return user;
+    }
+
+    /**
+       * Retrieves all users defined in the system.
+       *
+       * @return the names of all users defined in the system.
+       * @throws DataBackendException if there was an error accessing the data
+       *         backend.
+       */
+    public UserSet getAllUsers() throws DataBackendException
+    {
+        UserSet userSet = new UserSet();
+        Connection con = null;
+
+        try
+        {
+            con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
+            
+            List users = TorqueUserPeer.doSelect(new Criteria(), con);
+
+            for (Iterator i = users.iterator(); i.hasNext();)
+            {
+                User user = getUserInstance();
+                TorqueUser u = (TorqueUser)i.next();
+                user.setId(u.getId());
+                user.setName(u.getName());
+                user.setPassword(u.getPassword());
+
+                // Add attached objects if they exist
+                attachObjectsForUser(user, con);
+                
+                userSet.add(user);
+            }
+
+            Transaction.commit(con);
+        }
+        catch (TorqueException e)
+        {
+            Transaction.safeRollback(con);
+            throw new DataBackendException("Error retrieving all users", e);
+        }
+        
+        return userSet;
+    }
+
+    /**
+    * Removes an user account from the system.
+    *
+    * @param user the object describing the account to be removed.
+    * @throws DataBackendException if there was an error accessing the data
+    *         backend.
+    * @throws UnknownEntityException if the user account is not present.
+    */
+    public synchronized void removeUser(User user) throws DataBackendException, UnknownEntityException
+    {
+        try
+        {
+            TorqueUserPeer.doDelete(SimpleKey.keyFor((Integer)user.getId()));
+        }
+        catch (TorqueException e)
+        {
+            throw new DataBackendException("Removing User '" + user + "' failed", e);
+        }
+    }
+
+    /**
+       * Creates new user account with specified attributes.
+       *
+       * @param user the object describing account to be created.
+       * @param password The password to use for the account.
+       *
+       * @throws DataBackendException if there was an error accessing the
+       *         data backend.
+       * @throws EntityExistsException if the user account already exists.
+       */
+    protected synchronized User persistNewUser(User user) throws DataBackendException
+    {
+        try
+        {
+            TorqueUser u = new TorqueUser();
+            
+            u.setName(user.getName());
+            u.setPassword(user.getPassword());
+            u.save();
+
+            user.setId(u.getId());
+        }
+        catch (Exception e)
+        {
+            throw new DataBackendException("Adding User '" + user + "' failed", e);
+        }
+        
+        return user;
+    }
+
+    /**
+       * Stores User attributes. The User is required to exist in the system.
+       *
+       * @param role The User to be stored.
+       * @throws DataBackendException if there was an error accessing the data
+       *         backend.
+       * @throws UnknownEntityException if the role does not exist.
+       */
+    public synchronized void saveUser(User user) throws DataBackendException, UnknownEntityException
+    {
+        if (checkExists(user))
+        {
+            try
+            {
+                TorqueUser u = new TorqueUser();
+                
+                u.setId((Integer)user.getId());
+                u.setName(user.getName());
+                u.setPassword(user.getPassword());
+                u.setNew(false);
+                u.save();
+            }
+            catch (Exception e)
+            {
+                throw new DataBackendException("Saving User '" + user + "' failed", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Unknown user '" + user + "'");
+        }
+    }
+
+    /**
+     * Retrieve a User object with specified id.
+     *
+     * @param id
+     *            the id of the User.
+     * @return an object representing the User with specified id.
+     * @throws DataBackendException
+     *             if there was an error accessing the data backend.
+     * @throws UnknownEntityException
+     *             if the user does not exist.
+     */
+    public User getUserById(Object id)
+        throws DataBackendException, UnknownEntityException
+    {
+        User user = getUserInstance();
+
+        if (id != null && id instanceof Integer)
+        {
+            Connection con = null;
+            
+            try
+            {
+                con = Transaction.begin(TorqueUserPeer.DATABASE_NAME);
+                
+                TorqueUser u = TorqueUserPeer.retrieveByPK((Integer)id, con);
+
+                user.setId(u.getId());
+                user.setName(u.getName());
+                user.setPassword(u.getPassword());
+
+                // Add attached objects if they exist
+                attachObjectsForUser(user, con);
+                
+                Transaction.commit(con);
+            }
+            catch (NoRowsException e)
+            {
+                Transaction.safeRollback(con);
+                throw new UnknownEntityException("User with id '" + id + "' does not exist.", e);
+            }
+            catch (TorqueException e)
+            {
+                Transaction.safeRollback(con);
+                throw new DataBackendException("Error retrieving user information", e);
+            }
+        }
+        else
+        {
+            throw new UnknownEntityException("Invalid user id '" + user.getId() + "'");
+        }
+
+        return user;
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org