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 2007/05/05 08:58:51 UTC
svn commit: r535465 [29/49] - in /jakarta/turbine/fulcrum/trunk: ./ bsf/
bsf/src/java/org/apache/fulcrum/bsf/ bsf/src/test/ bsf/xdocs/ cache/
cache/src/java/org/apache/fulcrum/cache/
cache/src/java/org/apache/fulcrum/cache/impl/ cache/src/test/ cache/s...
Modified: 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=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractPermissionManager.java Fri May 4 23:58:06 2007
@@ -1,377 +1,380 @@
-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.Iterator;
-import java.util.List;
-
-import org.apache.fulcrum.security.entity.Permission;
-import org.apache.fulcrum.security.spi.AbstractPermissionManager;
-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.TooManyRowsException;
-import org.apache.torque.TorqueException;
-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
-{
- /**
- * Get all specialized Permissions
- *
- * @param con a database connection
- *
- * @return a List of Permission instances
- *
- * @throws TorqueException if any database error occurs
- */
- protected abstract List doSelectAllPermissions(Connection con)
- throws TorqueException;
-
- /**
- * Get a specialized Permission by name
- *
- * @param name the name of the group
- * @param con a database connection
- *
- * @return a Permission instance
- *
- * @throws NoRowsException if no such group exists
- * @throws TooManyRowsException if multiple groups with the given name exist
- * @throws TorqueException if any other database error occurs
- */
- protected abstract Permission doSelectByName(String name, Connection con)
- throws NoRowsException, TooManyRowsException, TorqueException;
-
- /**
- * Get a specialized Permission by id
- *
- * @param id the id of the group
- * @param con a database connection
- *
- * @return a Permission instance
- *
- * @throws NoRowsException if no such group exists
- * @throws TooManyRowsException if multiple groups with the given id exist
- * @throws TorqueException if any other database error occurs
- */
- protected abstract Permission doSelectById(Integer id, Connection con)
- throws NoRowsException, TooManyRowsException, TorqueException;
-
- /**
- * 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
- {
- TorqueAbstractSecurityEntity p = (TorqueAbstractSecurityEntity)permission;
- p.setNew(false);
- p.save();
- }
- catch (Exception e)
- {
- throw new DataBackendException("Renaming Permission '" + permission.getName() + "' failed", e);
- }
- }
- else
- {
- throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
- }
- }
-
- /**
- * 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
- {
- ((TorqueAbstractSecurityEntity)permission).delete();
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Removing Permission '" + permission.getName() + "' failed", e);
- }
- }
- else
- {
- throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
- }
- }
-
- /**
- * 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
- {
- ((TorqueAbstractSecurityEntity)permission).save();
- }
- catch (Exception e)
- {
- throw new DataBackendException("Adding Permission '" + permission.getName() + "' failed", e);
- }
-
- 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.
- */
- public PermissionSet getAllPermissions() throws DataBackendException
- {
- PermissionSet permissionSet = new PermissionSet();
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
-
- List permissions = doSelectAllPermissions(con);
-
- for (Iterator i = permissions.iterator(); i.hasNext();)
- {
- Permission p = (Permission)i.next();
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)p).retrieveAttachedObjects(con);
-
- permissionSet.add(p);
- }
-
- Transaction.commit(con);
- con = null;
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving permission information", e);
- }
- catch (UnknownEntityException e)
- {
- throw new DataBackendException("Error retrieving permission information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return permissionSet;
- }
-
- /**
- * 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
- {
- boolean exists = false;
-
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
-
- doSelectByName(permissionName, con);
-
- Transaction.commit(con);
- con = null;
-
- exists = true;
- }
- catch (NoRowsException e)
- {
- exists = false;
- }
- catch (TooManyRowsException e)
- {
- throw new DataBackendException("Multiple permissions with same name '" + permissionName + "'");
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving permission information", e);
- }
- catch (UnknownEntityException e)
- {
- throw new DataBackendException("Error retrieving permission information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return exists;
- }
-
- /**
- * 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;
-
- if (id != null && id instanceof Integer)
- {
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
-
- permission = doSelectById((Integer)id, con);
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)permission).retrieveAttachedObjects(con);
-
- Transaction.commit(con);
- con = null;
- }
- catch (NoRowsException e)
- {
- throw new UnknownEntityException("Permission with id '" + id + "' does not exist.", e);
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving permission information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
- }
- else
- {
- throw new UnknownEntityException("Invalid permission id '" + id + "'");
- }
-
- 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 = null;
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
-
- permission = doSelectByName(name, con);
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)permission).retrieveAttachedObjects(con);
-
- Transaction.commit(con);
- con = null;
- }
- catch (NoRowsException e)
- {
- throw new UnknownEntityException("Could not find permission " + name);
- }
- catch (TooManyRowsException e)
- {
- throw new DataBackendException("Multiple Permissions with same name '" + name + "'");
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving permission information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return permission;
- }
-}
+package org.apache.fulcrum.security.torque;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.Iterator;
+import java.util.List;
+
+import org.apache.fulcrum.security.entity.Permission;
+import org.apache.fulcrum.security.spi.AbstractPermissionManager;
+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.TooManyRowsException;
+import org.apache.torque.TorqueException;
+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
+{
+ /**
+ * Get all specialized Permissions
+ *
+ * @param con a database connection
+ *
+ * @return a List of Permission instances
+ *
+ * @throws TorqueException if any database error occurs
+ */
+ protected abstract List doSelectAllPermissions(Connection con)
+ throws TorqueException;
+
+ /**
+ * Get a specialized Permission by name
+ *
+ * @param name the name of the group
+ * @param con a database connection
+ *
+ * @return a Permission instance
+ *
+ * @throws NoRowsException if no such group exists
+ * @throws TooManyRowsException if multiple groups with the given name exist
+ * @throws TorqueException if any other database error occurs
+ */
+ protected abstract Permission doSelectByName(String name, Connection con)
+ throws NoRowsException, TooManyRowsException, TorqueException;
+
+ /**
+ * Get a specialized Permission by id
+ *
+ * @param id the id of the group
+ * @param con a database connection
+ *
+ * @return a Permission instance
+ *
+ * @throws NoRowsException if no such group exists
+ * @throws TooManyRowsException if multiple groups with the given id exist
+ * @throws TorqueException if any other database error occurs
+ */
+ protected abstract Permission doSelectById(Integer id, Connection con)
+ throws NoRowsException, TooManyRowsException, TorqueException;
+
+ /**
+ * 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
+ {
+ TorqueAbstractSecurityEntity p = (TorqueAbstractSecurityEntity)permission;
+ p.setNew(false);
+ p.save();
+ }
+ catch (Exception e)
+ {
+ throw new DataBackendException("Renaming Permission '" + permission.getName() + "' failed", e);
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
+ }
+ }
+
+ /**
+ * 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
+ {
+ ((TorqueAbstractSecurityEntity)permission).delete();
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Removing Permission '" + permission.getName() + "' failed", e);
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Unknown permission '" + permission.getName() + "'");
+ }
+ }
+
+ /**
+ * 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
+ {
+ ((TorqueAbstractSecurityEntity)permission).save();
+ }
+ catch (Exception e)
+ {
+ throw new DataBackendException("Adding Permission '" + permission.getName() + "' failed", e);
+ }
+
+ 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.
+ */
+ public PermissionSet getAllPermissions() throws DataBackendException
+ {
+ PermissionSet permissionSet = new PermissionSet();
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
+
+ List permissions = doSelectAllPermissions(con);
+
+ for (Iterator i = permissions.iterator(); i.hasNext();)
+ {
+ Permission p = (Permission)i.next();
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)p).retrieveAttachedObjects(con);
+
+ permissionSet.add(p);
+ }
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving permission information", e);
+ }
+ catch (UnknownEntityException e)
+ {
+ throw new DataBackendException("Error retrieving permission information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return permissionSet;
+ }
+
+ /**
+ * 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
+ {
+ boolean exists = false;
+
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
+
+ doSelectByName(permissionName, con);
+
+ Transaction.commit(con);
+ con = null;
+
+ exists = true;
+ }
+ catch (NoRowsException e)
+ {
+ exists = false;
+ }
+ catch (TooManyRowsException e)
+ {
+ throw new DataBackendException("Multiple permissions with same name '" + permissionName + "'");
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving permission information", e);
+ }
+ catch (UnknownEntityException e)
+ {
+ throw new DataBackendException("Error retrieving permission information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return exists;
+ }
+
+ /**
+ * 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;
+
+ if (id != null && id instanceof Integer)
+ {
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
+
+ permission = doSelectById((Integer)id, con);
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)permission).retrieveAttachedObjects(con);
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (NoRowsException e)
+ {
+ throw new UnknownEntityException("Permission with id '" + id + "' does not exist.", e);
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving permission information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Invalid permission id '" + id + "'");
+ }
+
+ 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 = null;
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getPermissionInstance()).getDatabaseName());
+
+ permission = doSelectByName(name, con);
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)permission).retrieveAttachedObjects(con);
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (NoRowsException e)
+ {
+ throw new UnknownEntityException("Could not find permission " + name);
+ }
+ catch (TooManyRowsException e)
+ {
+ throw new DataBackendException("Multiple Permissions with same name '" + name + "'");
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving permission information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return permission;
+ }
+}
Modified: 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=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractRoleManager.java Fri May 4 23:58:06 2007
@@ -1,361 +1,364 @@
-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.Iterator;
-import java.util.List;
-
-import org.apache.fulcrum.security.entity.Role;
-import org.apache.fulcrum.security.spi.AbstractRoleManager;
-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.TooManyRowsException;
-import org.apache.torque.TorqueException;
-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
-{
- /**
- * Get all specialized Roles
- *
- * @param con a database connection
- *
- * @return a List of Role instances
- *
- * @throws TorqueException if any database error occurs
- */
- protected abstract List doSelectAllRoles(Connection con)
- throws TorqueException;
-
- /**
- * Get a specialized Role by name
- *
- * @param name the name of the group
- * @param con a database connection
- *
- * @return a Role instance
- *
- * @throws NoRowsException if no such group exists
- * @throws TooManyRowsException if multiple groups with the given name exist
- * @throws TorqueException if any other database error occurs
- */
- protected abstract Role doSelectByName(String name, Connection con)
- throws NoRowsException, TooManyRowsException, TorqueException;
-
- /**
- * Get a specialized Role by id
- *
- * @param id the id of the group
- * @param con a database connection
- *
- * @return a Role instance
- *
- * @throws NoRowsException if no such group exists
- * @throws TooManyRowsException if multiple groups with the given id exist
- * @throws TorqueException if any other database error occurs
- */
- protected abstract Role doSelectById(Integer id, Connection con)
- throws NoRowsException, TooManyRowsException, TorqueException;
-
-
- /**
- * 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
- {
- TorqueAbstractSecurityEntity r = (TorqueAbstractSecurityEntity)role;
- r.setNew(false);
- r.save();
- }
- catch (Exception e)
- {
- throw new DataBackendException("Renaming Role '" + role.getName() + "' failed", e);
- }
- }
- else
- {
- throw new UnknownEntityException("Unknown Role '" + role.getName() + "'");
- }
- }
-
- /**
- * 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
- {
- ((TorqueAbstractSecurityEntity)role).save();
- }
- catch (Exception e)
- {
- throw new DataBackendException("Adding Role '" + role.getName() + "' 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
- {
- ((TorqueAbstractSecurityEntity)role).delete();
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Removing Role '" + role.getName() + "' failed", e);
- }
- }
- else
- {
- throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
- }
- }
-
- /**
- * 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
- {
- boolean exists = false;
-
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
-
- doSelectByName(roleName, con);
-
- Transaction.commit(con);
- con = null;
-
- exists = true;
- }
- catch (NoRowsException e)
- {
- exists = false;
- }
- catch (TooManyRowsException e)
- {
- throw new DataBackendException("Multiple roles with same name '" + roleName + "'");
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving role information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return exists;
- }
-
- /**
- * 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(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
-
- List roles = doSelectAllRoles(con);
-
- for (Iterator i = roles.iterator(); i.hasNext();)
- {
- Role role = (Role)i.next();
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con);
-
- roleSet.add(role);
- }
-
- Transaction.commit(con);
- con = null;
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving role information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return roleSet;
- }
-
- /**
- * 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;
-
- if (id != null && id instanceof Integer)
- {
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
-
- role = doSelectById((Integer)id, con);
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con);
-
- Transaction.commit(con);
- con = null;
- }
- catch (NoRowsException e)
- {
- throw new UnknownEntityException("Role with id '" + id + "' does not exist.", e);
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving role information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
- }
- else
- {
- throw new UnknownEntityException("Invalid role id '" + id + "'");
- }
-
- 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 = null;
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
-
- role = doSelectByName(name, con);
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con);
-
- Transaction.commit(con);
- con = null;
- }
- catch (NoRowsException e)
- {
- throw new UnknownEntityException("Could not find role" + name);
- }
- catch (TooManyRowsException e)
- {
- throw new DataBackendException("Multiple Roles with same name '" + name + "'");
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving role information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return role;
- }
-}
+package org.apache.fulcrum.security.torque;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.Iterator;
+import java.util.List;
+
+import org.apache.fulcrum.security.entity.Role;
+import org.apache.fulcrum.security.spi.AbstractRoleManager;
+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.TooManyRowsException;
+import org.apache.torque.TorqueException;
+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
+{
+ /**
+ * Get all specialized Roles
+ *
+ * @param con a database connection
+ *
+ * @return a List of Role instances
+ *
+ * @throws TorqueException if any database error occurs
+ */
+ protected abstract List doSelectAllRoles(Connection con)
+ throws TorqueException;
+
+ /**
+ * Get a specialized Role by name
+ *
+ * @param name the name of the group
+ * @param con a database connection
+ *
+ * @return a Role instance
+ *
+ * @throws NoRowsException if no such group exists
+ * @throws TooManyRowsException if multiple groups with the given name exist
+ * @throws TorqueException if any other database error occurs
+ */
+ protected abstract Role doSelectByName(String name, Connection con)
+ throws NoRowsException, TooManyRowsException, TorqueException;
+
+ /**
+ * Get a specialized Role by id
+ *
+ * @param id the id of the group
+ * @param con a database connection
+ *
+ * @return a Role instance
+ *
+ * @throws NoRowsException if no such group exists
+ * @throws TooManyRowsException if multiple groups with the given id exist
+ * @throws TorqueException if any other database error occurs
+ */
+ protected abstract Role doSelectById(Integer id, Connection con)
+ throws NoRowsException, TooManyRowsException, TorqueException;
+
+
+ /**
+ * 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
+ {
+ TorqueAbstractSecurityEntity r = (TorqueAbstractSecurityEntity)role;
+ r.setNew(false);
+ r.save();
+ }
+ catch (Exception e)
+ {
+ throw new DataBackendException("Renaming Role '" + role.getName() + "' failed", e);
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Unknown Role '" + role.getName() + "'");
+ }
+ }
+
+ /**
+ * 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
+ {
+ ((TorqueAbstractSecurityEntity)role).save();
+ }
+ catch (Exception e)
+ {
+ throw new DataBackendException("Adding Role '" + role.getName() + "' 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
+ {
+ ((TorqueAbstractSecurityEntity)role).delete();
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Removing Role '" + role.getName() + "' failed", e);
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
+ }
+ }
+
+ /**
+ * 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
+ {
+ boolean exists = false;
+
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
+
+ doSelectByName(roleName, con);
+
+ Transaction.commit(con);
+ con = null;
+
+ exists = true;
+ }
+ catch (NoRowsException e)
+ {
+ exists = false;
+ }
+ catch (TooManyRowsException e)
+ {
+ throw new DataBackendException("Multiple roles with same name '" + roleName + "'");
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving role information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return exists;
+ }
+
+ /**
+ * 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(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
+
+ List roles = doSelectAllRoles(con);
+
+ for (Iterator i = roles.iterator(); i.hasNext();)
+ {
+ Role role = (Role)i.next();
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con);
+
+ roleSet.add(role);
+ }
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving role information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return roleSet;
+ }
+
+ /**
+ * 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;
+
+ if (id != null && id instanceof Integer)
+ {
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
+
+ role = doSelectById((Integer)id, con);
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con);
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (NoRowsException e)
+ {
+ throw new UnknownEntityException("Role with id '" + id + "' does not exist.", e);
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving role information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Invalid role id '" + id + "'");
+ }
+
+ 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 = null;
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getRoleInstance()).getDatabaseName());
+
+ role = doSelectByName(name, con);
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con);
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (NoRowsException e)
+ {
+ throw new UnknownEntityException("Could not find role" + name);
+ }
+ catch (TooManyRowsException e)
+ {
+ throw new DataBackendException("Multiple Roles with same name '" + name + "'");
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving role information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return role;
+ }
+}
Modified: jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractSecurityEntity.java
URL: http://svn.apache.org/viewvc/jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractSecurityEntity.java?view=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractSecurityEntity.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractSecurityEntity.java Fri May 4 23:58:06 2007
@@ -1,128 +1,131 @@
-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 org.apache.fulcrum.security.entity.SecurityEntity;
-import org.apache.torque.TorqueException;
-import org.apache.torque.om.BaseObject;
-/**
- * This abstract class provides the SecurityInterface to the managers.
- *
- * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
- * @version $Id:$
- */
-public abstract class TorqueAbstractSecurityEntity extends BaseObject
- implements SecurityEntity
-{
- /**
- * Get a numeric entity id
- *
- * @return numeric id of this entity
- */
- public abstract Integer getEntityId();
-
- /**
- * Set a numeric entity id
- *
- * @param id numeric id of this entity
- */
- public abstract void setEntityId(Integer id) throws TorqueException;
-
- /**
- * Get the name of the entity
- *
- * @return name of this entity
- */
- public abstract String getEntityName();
-
- /**
- * Set the name of the entity
- *
- * @param name the name of the entity
- */
- public abstract void setEntityName(String name);
-
- /**
- * Retrieve attached objects such as users, permissions, ...
- *
- * @param con A database connection
- */
- public abstract void retrieveAttachedObjects(Connection con) throws TorqueException;
-
- /**
- * Update this instance to the database with all dependend objects
- *
- * @param con A database connection
- */
- public abstract void update(Connection con) throws TorqueException;
-
- /**
- * Get the name of the connection pool associated to this object
- *
- * @return the logical Torque database name
- */
- public abstract String getDatabaseName();
-
- /**
- * Delete this entity
- *
- * @throws TorqueException if any database operation fails
- */
- public abstract void delete() throws TorqueException;
-
- /**
- * @see org.apache.fulcrum.security.entity.SecurityEntity#getId()
- */
- public Object getId()
- {
- return getEntityId();
- }
-
- /**
- * @see org.apache.fulcrum.security.entity.SecurityEntity#setId(java.lang.Object)
- */
- public void setId(Object id)
- {
- try
- {
- setEntityId((Integer)id);
- }
- catch (TorqueException e)
- {
- // should not happen
- }
- }
-
- /**
- * @see org.apache.fulcrum.security.entity.SecurityEntity#getName()
- */
- public String getName()
- {
- return getEntityName();
- }
-
- /**
- * @see org.apache.fulcrum.security.entity.SecurityEntity#setName(java.lang.String)
- */
- public void setName(String name)
- {
- if (name != null)
- {
- setEntityName(name.toLowerCase());
- }
- }
-}
\ No newline at end of file
+package org.apache.fulcrum.security.torque;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fulcrum.security.entity.SecurityEntity;
+import org.apache.torque.TorqueException;
+import org.apache.torque.om.BaseObject;
+/**
+ * This abstract class provides the SecurityInterface to the managers.
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id:$
+ */
+public abstract class TorqueAbstractSecurityEntity extends BaseObject
+ implements SecurityEntity
+{
+ /**
+ * Get a numeric entity id
+ *
+ * @return numeric id of this entity
+ */
+ public abstract Integer getEntityId();
+
+ /**
+ * Set a numeric entity id
+ *
+ * @param id numeric id of this entity
+ */
+ public abstract void setEntityId(Integer id) throws TorqueException;
+
+ /**
+ * Get the name of the entity
+ *
+ * @return name of this entity
+ */
+ public abstract String getEntityName();
+
+ /**
+ * Set the name of the entity
+ *
+ * @param name the name of the entity
+ */
+ public abstract void setEntityName(String name);
+
+ /**
+ * Retrieve attached objects such as users, permissions, ...
+ *
+ * @param con A database connection
+ */
+ public abstract void retrieveAttachedObjects(Connection con) throws TorqueException;
+
+ /**
+ * Update this instance to the database with all dependend objects
+ *
+ * @param con A database connection
+ */
+ public abstract void update(Connection con) throws TorqueException;
+
+ /**
+ * Get the name of the connection pool associated to this object
+ *
+ * @return the logical Torque database name
+ */
+ public abstract String getDatabaseName();
+
+ /**
+ * Delete this entity
+ *
+ * @throws TorqueException if any database operation fails
+ */
+ public abstract void delete() throws TorqueException;
+
+ /**
+ * @see org.apache.fulcrum.security.entity.SecurityEntity#getId()
+ */
+ public Object getId()
+ {
+ return getEntityId();
+ }
+
+ /**
+ * @see org.apache.fulcrum.security.entity.SecurityEntity#setId(java.lang.Object)
+ */
+ public void setId(Object id)
+ {
+ try
+ {
+ setEntityId((Integer)id);
+ }
+ catch (TorqueException e)
+ {
+ // should not happen
+ }
+ }
+
+ /**
+ * @see org.apache.fulcrum.security.entity.SecurityEntity#getName()
+ */
+ public String getName()
+ {
+ return getEntityName();
+ }
+
+ /**
+ * @see org.apache.fulcrum.security.entity.SecurityEntity#setName(java.lang.String)
+ */
+ public void setName(String name)
+ {
+ if (name != null)
+ {
+ setEntityName(name.toLowerCase());
+ }
+ }
+}
Modified: 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=diff&rev=535465&r1=535464&r2=535465
==============================================================================
--- jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java (original)
+++ jakarta/turbine/fulcrum/trunk/security/torque/src/java/org/apache/fulcrum/security/torque/TorqueAbstractUserManager.java Fri May 4 23:58:06 2007
@@ -1,355 +1,358 @@
-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.Iterator;
-import java.util.List;
-
-import org.apache.fulcrum.security.entity.User;
-import org.apache.fulcrum.security.spi.AbstractUserManager;
-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.TooManyRowsException;
-import org.apache.torque.TorqueException;
-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
-{
- /**
- * Get all specialized Users
- *
- * @param con a database connection
- *
- * @return a List of User instances
- *
- * @throws TorqueException if any database error occurs
- */
- protected abstract List doSelectAllUsers(Connection con)
- throws TorqueException;
-
- /**
- * Get a specialized User by name
- *
- * @param name the name of the group
- * @param con a database connection
- *
- * @return a User instance
- *
- * @throws NoRowsException if no such group exists
- * @throws TooManyRowsException if multiple groups with the given name exist
- * @throws TorqueException if any other database error occurs
- */
- protected abstract User doSelectByName(String name, Connection con)
- throws NoRowsException, TooManyRowsException, TorqueException;
-
- /**
- * Get a specialized User by id
- *
- * @param id the id of the group
- * @param con a database connection
- *
- * @return a User instance
- *
- * @throws NoRowsException if no such group exists
- * @throws TooManyRowsException if multiple groups with the given id exist
- * @throws TorqueException if any other database error occurs
- */
- protected abstract User doSelectById(Integer id, Connection con)
- throws NoRowsException, TooManyRowsException, TorqueException;
-
- /**
- * 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
- {
- ((TorqueAbstractSecurityEntity)user).delete();
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Removing User '" + user.getName() + "' 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
- {
- ((TorqueAbstractSecurityEntity)user).save();
- }
- catch (Exception e)
- {
- throw new DataBackendException("Adding User '" + user.getName() + "' 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
- {
- TorqueAbstractSecurityEntity u = (TorqueAbstractSecurityEntity)user;
- u.setNew(false);
- u.save();
- }
- catch (Exception e)
- {
- throw new DataBackendException("Saving User '" + user.getName() + "' failed", e);
- }
- }
- else
- {
- throw new UnknownEntityException("Unknown user '" + user + "'");
- }
- }
-
- /**
- * 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
- {
- boolean exists = false;
-
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
-
- doSelectByName(userName, con);
-
- Transaction.commit(con);
- con = null;
-
- exists = true;
- }
- catch (NoRowsException e)
- {
- exists = false;
- }
- catch (TooManyRowsException e)
- {
- throw new DataBackendException("Multiple Users with same username '" + userName + "'");
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving user information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return exists;
- }
-
- /**
- * 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 = null;
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
-
- user = doSelectByName(userName.toLowerCase(), con);
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)user).retrieveAttachedObjects(con);
-
- Transaction.commit(con);
- con = null;
- }
- catch (NoRowsException e)
- {
- throw new UnknownEntityException("Unknown user '" + userName + "'");
- }
- catch (TooManyRowsException e)
- {
- throw new DataBackendException("Multiple Users with same username '" + userName + "'");
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving user information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- 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(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
-
- List users = doSelectAllUsers(con);
-
- for (Iterator i = users.iterator(); i.hasNext();)
- {
- User user = (User)i.next();
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)user).retrieveAttachedObjects(con);
-
- userSet.add(user);
- }
-
- Transaction.commit(con);
- con = null;
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving all users", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
-
- return userSet;
- }
-
- /**
- * 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;
-
- if (id != null && id instanceof Integer)
- {
- Connection con = null;
-
- try
- {
- con = Transaction.begin(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
-
- user = doSelectById((Integer)id, con);
-
- // Add attached objects if they exist
- ((TorqueAbstractSecurityEntity)user).retrieveAttachedObjects(con);
-
- Transaction.commit(con);
- con = null;
- }
- catch (NoRowsException e)
- {
- throw new UnknownEntityException("User with id '" + id + "' does not exist.", e);
- }
- catch (TorqueException e)
- {
- throw new DataBackendException("Error retrieving user information", e);
- }
- finally
- {
- if (con != null)
- {
- Transaction.safeRollback(con);
- }
- }
- }
- else
- {
- throw new UnknownEntityException("Invalid user id '" + id + "'");
- }
-
- return user;
- }
-}
+package org.apache.fulcrum.security.torque;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.Iterator;
+import java.util.List;
+
+import org.apache.fulcrum.security.entity.User;
+import org.apache.fulcrum.security.spi.AbstractUserManager;
+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.TooManyRowsException;
+import org.apache.torque.TorqueException;
+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
+{
+ /**
+ * Get all specialized Users
+ *
+ * @param con a database connection
+ *
+ * @return a List of User instances
+ *
+ * @throws TorqueException if any database error occurs
+ */
+ protected abstract List doSelectAllUsers(Connection con)
+ throws TorqueException;
+
+ /**
+ * Get a specialized User by name
+ *
+ * @param name the name of the group
+ * @param con a database connection
+ *
+ * @return a User instance
+ *
+ * @throws NoRowsException if no such group exists
+ * @throws TooManyRowsException if multiple groups with the given name exist
+ * @throws TorqueException if any other database error occurs
+ */
+ protected abstract User doSelectByName(String name, Connection con)
+ throws NoRowsException, TooManyRowsException, TorqueException;
+
+ /**
+ * Get a specialized User by id
+ *
+ * @param id the id of the group
+ * @param con a database connection
+ *
+ * @return a User instance
+ *
+ * @throws NoRowsException if no such group exists
+ * @throws TooManyRowsException if multiple groups with the given id exist
+ * @throws TorqueException if any other database error occurs
+ */
+ protected abstract User doSelectById(Integer id, Connection con)
+ throws NoRowsException, TooManyRowsException, TorqueException;
+
+ /**
+ * 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
+ {
+ ((TorqueAbstractSecurityEntity)user).delete();
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Removing User '" + user.getName() + "' 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
+ {
+ ((TorqueAbstractSecurityEntity)user).save();
+ }
+ catch (Exception e)
+ {
+ throw new DataBackendException("Adding User '" + user.getName() + "' 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
+ {
+ TorqueAbstractSecurityEntity u = (TorqueAbstractSecurityEntity)user;
+ u.setNew(false);
+ u.save();
+ }
+ catch (Exception e)
+ {
+ throw new DataBackendException("Saving User '" + user.getName() + "' failed", e);
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Unknown user '" + user + "'");
+ }
+ }
+
+ /**
+ * 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
+ {
+ boolean exists = false;
+
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
+
+ doSelectByName(userName, con);
+
+ Transaction.commit(con);
+ con = null;
+
+ exists = true;
+ }
+ catch (NoRowsException e)
+ {
+ exists = false;
+ }
+ catch (TooManyRowsException e)
+ {
+ throw new DataBackendException("Multiple Users with same username '" + userName + "'");
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving user information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return exists;
+ }
+
+ /**
+ * 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 = null;
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
+
+ user = doSelectByName(userName.toLowerCase(), con);
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)user).retrieveAttachedObjects(con);
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (NoRowsException e)
+ {
+ throw new UnknownEntityException("Unknown user '" + userName + "'");
+ }
+ catch (TooManyRowsException e)
+ {
+ throw new DataBackendException("Multiple Users with same username '" + userName + "'");
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving user information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ 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(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
+
+ List users = doSelectAllUsers(con);
+
+ for (Iterator i = users.iterator(); i.hasNext();)
+ {
+ User user = (User)i.next();
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)user).retrieveAttachedObjects(con);
+
+ userSet.add(user);
+ }
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving all users", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+
+ return userSet;
+ }
+
+ /**
+ * 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;
+
+ if (id != null && id instanceof Integer)
+ {
+ Connection con = null;
+
+ try
+ {
+ con = Transaction.begin(((TorqueAbstractSecurityEntity)getUserInstance()).getDatabaseName());
+
+ user = doSelectById((Integer)id, con);
+
+ // Add attached objects if they exist
+ ((TorqueAbstractSecurityEntity)user).retrieveAttachedObjects(con);
+
+ Transaction.commit(con);
+ con = null;
+ }
+ catch (NoRowsException e)
+ {
+ throw new UnknownEntityException("User with id '" + id + "' does not exist.", e);
+ }
+ catch (TorqueException e)
+ {
+ throw new DataBackendException("Error retrieving user information", e);
+ }
+ finally
+ {
+ if (con != null)
+ {
+ Transaction.safeRollback(con);
+ }
+ }
+ }
+ else
+ {
+ throw new UnknownEntityException("Invalid user id '" + id + "'");
+ }
+
+ return user;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org