directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 10359 - in incubator/directory/rms/trunk/je/src: java/org/apache/rms/je/role test/org/apache/rms/je/role
Date Wed, 28 Apr 2004 13:58:47 GMT
Author: akarasulu
Date: Wed Apr 28 06:58:46 2004
New Revision: 10359

Modified:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleBinding.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleDAO.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitorAdapter.java
   incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/role/JeRoleDAOTest.java
Log:
Commit changes ...

 o completed RoleDAO create method
 o tested create and has together in unit test
 o found and fixed bug in role binding with order of role vs. app names
 o added extra RoleDAOMonitor methods for create 
 o implemented methods in monitor's no-op adapter



Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleBinding.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleBinding.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleBinding.java	Wed
Apr 28 06:58:46 2004
@@ -116,8 +116,8 @@
             return ;
         }
 
-        tupleOutput.writeString( role.getName() ) ;
         tupleOutput.writeString( role.getApplicationName() ) ;
+        tupleOutput.writeString( role.getName() ) ;
         tupleOutput.write( role.getGrantBits() ) ;
     }
 
@@ -126,17 +126,17 @@
      * Marshals the components of a Role into a serialized Tuple.
      *
      * @param entry the entry to populate
-     * @param appName the name of the application
-     * @param name the name of the role
+     * @param appName the roleName of the application
+     * @param roleName the roleName of the role
      * @param grants the permission grants for the role
      * @throws IOException if there are failures writing the data
      */
     public void objectToEntry( DatabaseEntry entry, String appName,
-                               String name, byte[] grants ) throws IOException
+                               String roleName, byte[] grants ) throws IOException
     {
         TupleOutput output = new TupleOutput() ;
-        output.writeString( name ) ;
         output.writeString( appName ) ;
+        output.writeString( roleName ) ;
         output.write( grants ) ;
         entry.setData( output.getBufferBytes(), output.getBufferOffset(),
                 output.getBufferLength() ) ;

Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleDAO.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleDAO.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/JeRoleDAO.java	Wed Apr
28 06:58:46 2004
@@ -18,16 +18,22 @@
 
 
 import org.apache.rms.Role ;
+import org.apache.rms.Application ;
 import org.apache.rms.RmsException ;
-import org.apache.rms.Application;
+
+import org.apache.rms.je.JeUtils ;
+import org.apache.rms.je.JeRmsException ;
 import org.apache.rms.je.sequence.Sequence ;
 
 import org.apache.commons.lang.Validate ;
+import org.apache.commons.lang.ArrayUtils ;
 import org.apache.commons.lang.NotImplementedException ;
 
 import java.util.List ;
 import java.util.Iterator ;
-import java.io.UnsupportedEncodingException;
+
+import java.io.IOException ;
+import java.io.UnsupportedEncodingException ;
 
 import com.sleepycat.je.*;
 
@@ -46,8 +52,7 @@
     static final String ROLE_SECDB = "byRoleName" ;
 
     /** A Role binding */
-    private static final JeRoleBinding BINDING =
-            new JeRoleBinding() ;
+    private static final JeRoleBinding BINDING = new JeRoleBinding() ;
 
     /** the JE database to use for this DAO */
     private final Database db ;
@@ -123,7 +128,49 @@
      */
     public void create( Application application, String roleName ) throws RmsException
     {
-        throw new NotImplementedException( "STUB" ) ;
+
+        String appName = application.getName() ;
+
+        if ( has( appName, roleName ) )
+        {
+            monitor.roleExists( this, "create", appName, roleName ) ;
+            throw new RmsException( "The role " + roleName
+                    + " already exists for application " + appName ) ;
+        }
+
+        OperationStatus status = null ;
+        int rowId = seq.getNextValue() ;
+        DatabaseEntry key = new DatabaseEntry( JeUtils.encodeInt( rowId ) ) ;
+        DatabaseEntry data = new DatabaseEntry() ;
+
+        try
+        {
+            BINDING.objectToEntry( data, appName, roleName,
+                    ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+            status = db.put( null, key, data ) ;
+        }
+        catch ( IOException e )
+        {
+            monitor.failedOnCreate( this, appName, roleName, e ) ;
+            throw new RmsException( e ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            monitor.failedOnCreate( this, appName, roleName, e ) ;
+            throw new RmsException( e ) ;
+        }
+
+        if ( status == OperationStatus.SUCCESS )
+        {
+            monitor.roleCreated( this, appName, roleName ) ;
+        }
+        else
+        {
+            monitor.roleExists( this, "create", appName, roleName ) ;
+            throw new JeRmsException( status,
+                    "Create operation failed with status: " +
+                    status.toString() ) ;
+        }
     }
 
 
@@ -339,6 +386,108 @@
     // -----------------------------------------------------------------------
     // Utility Methods
     // -----------------------------------------------------------------------
+
+
+    /**
+     * Gets the key or value of an entry within the primary role database.
+     *
+     * @param appName the roleName of the application
+     * @param roleName the roleName of the role
+     * @param getKey if true gets the key, if false gets the value
+     * @return the key of the entry in the primary database
+     * @throws RmsException if there are failures accessing the underlying JE
+     * databases, or the application and/or the role roleName does not exist
+     */
+    DatabaseEntry getRow( String appName, String roleName, boolean getKey ) throws RmsException
+    {
+        DatabaseEntry key = new DatabaseEntry() ;
+        DatabaseEntry value = new DatabaseEntry() ;
+        DatabaseEntry nameEntry = new DatabaseEntry() ;
+        DatabaseEntry appNameEntry = new DatabaseEntry() ;
+        OperationStatus status = null ;
+
+        try
+        {
+            nameEntry.setData( roleName.getBytes( "UTF-8" ) ) ;
+            appNameEntry.setData( appName.getBytes( "UTF-8" ) ) ;
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        JoinCursor cursor = null ;
+        SecondaryCursor appNameCursor = null ;
+        SecondaryCursor roleNameCursor = null ;
+
+        try
+        {
+            appNameCursor = byAppName.openSecondaryCursor( null, null ) ;
+            roleNameCursor = byRoleName.openSecondaryCursor( null, null ) ;
+
+            status = roleNameCursor
+                    .getSearchKey( nameEntry, value, LockMode.DEFAULT ) ;
+            if ( status == OperationStatus.NOTFOUND )
+            {
+                throw new JeRmsException( status,
+                        "A role by the name of "
+                        + roleName + " does not exist" ) ;
+            }
+            else if ( status != OperationStatus.SUCCESS )
+            {
+                throw new JeRmsException( status,
+                        "failed to position role cursor "
+                        + "with status of " + status.toString() ) ;
+            }
+
+            status = appNameCursor
+                    .getSearchKey( appNameEntry, value, LockMode.DEFAULT ) ;
+            if ( status == OperationStatus.NOTFOUND )
+            {
+                throw new JeRmsException( status,
+                        "An application by the name of "
+                        + appName + " does not exist" ) ;
+            }
+            else if ( status != OperationStatus.SUCCESS )
+            {
+                throw new JeRmsException( status,
+                        "failed to position app cursor with status of "
+                        + status.toString() ) ;
+            }
+
+            SecondaryCursor [] secCursors = { roleNameCursor, appNameCursor } ;
+            cursor = db.join( secCursors, null ) ;
+            status = cursor.getNext( key, value, LockMode.DEFAULT ) ;
+
+            if ( status != OperationStatus.SUCCESS )
+            {
+                throw new JeRmsException( status,
+                        "failed to get role " + roleName
+                        + " rowId for application " + appName
+                        + " with status of " + status.toString() ) ;
+            }
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        finally
+        {
+            closeNoError( cursor ) ;
+            closeNoError( roleNameCursor ) ;
+            closeNoError( appNameCursor ) ;
+        }
+
+
+        if ( getKey )
+        {
+            return key ;
+        }
+        else
+        {
+            return value ;
+        }
+    }
 
 
     /**

Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitor.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitor.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitor.java	Wed
Apr 28 06:58:46 2004
@@ -16,6 +16,8 @@
  */
 package org.apache.rms.je.role ;
 
+import java.io.IOException;
+
 
 /**
  * A monitor interface for Role data access objects.
@@ -46,4 +48,36 @@
      */
     void failedOnCleanupOperation( RoleDAO dao, String op, Object resource,
                                    Throwable fault ) ;
+
+    /**
+     * Monitors events where the DAO fails to create a new application role.
+     *
+     * @param dao the data access object
+     * @param appName the name of the application
+     * @param roleName the name of the role
+     * @param fault the fault that caused the failure
+     */
+    void failedOnCreate( JeRoleDAO dao, String appName, String roleName,
+                         Throwable fault ) ;
+
+    /**
+     * Monitors events where the DAO successfully creates a new application
+     * role.
+     *
+     * @param dao the data access object
+     * @param appName the name of the application
+     * @param roleName the name of the role
+     */
+    void roleCreated( JeRoleDAO dao, String appName, String roleName ) ;
+
+    /**
+     * Monitors events where the DAO attempts to perform an operation but
+     * fails because of the existance of an application role.
+     *
+     * @param dao the data access object
+     * @param op the operation performed on the resource to 'cleanup'
+     * @param appName the name of the application
+     * @param roleName the name of the role
+     */
+    void roleExists( JeRoleDAO dao, String op, String appName, String roleName ) ;
 }

Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitorAdapter.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitorAdapter.java
(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/role/RoleDAOMonitorAdapter.java
Wed Apr 28 06:58:46 2004
@@ -14,11 +14,13 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je.role;
+package org.apache.rms.je.role ;
 
 
 /**
- * Put some documentation here.
+ * A do nothing adapter for a RoleDAO monitor.  At a bare minimum this adapter
+ * prints out stack traces to the console.  If this is not desired use a Logging
+ * monitor with the RoleDAO instead.
  * 
  * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  * @version $Rev$
@@ -53,5 +55,50 @@
         {
             fault.printStackTrace() ; 
         }
+    }
+
+
+    /**
+     * Monitors events where the DAO fails to create a new application role.
+     *
+     * @param dao      the data access object
+     * @param appName  the name of the application
+     * @param roleName the name of the role
+     * @param fault    the fault that caused the failure
+     */
+    public void failedOnCreate( JeRoleDAO dao, String appName, String roleName,
+                                Throwable fault )
+    {
+        if ( fault != null )
+        {
+            fault.printStackTrace() ;
+        }
+    }
+
+
+    /**
+     * Monitors events where the DAO successfully creates a new
+     * application role.
+     *
+     * @param dao      the data access object
+     * @param appName  the name of the application
+     * @param roleName the name of the role
+     */
+    public void roleCreated( JeRoleDAO dao, String appName, String roleName )
+    {
+    }
+
+    /**
+     * Monitors events where the DAO attempts to perform an operation but
+     * fails because of the existance of an application role.
+     *
+     * @param dao      the data access object
+     * @param op       the operation performed on the resource to 'cleanup'
+     * @param appName  the name of the application
+     * @param roleName the name of the role
+     */
+    public void roleExists( JeRoleDAO dao, String op, String appName,
+                            String roleName )
+    {
     }
 }

Modified: incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/role/JeRoleDAOTest.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/role/JeRoleDAOTest.java	(original)
+++ incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/role/JeRoleDAOTest.java	Wed
Apr 28 06:58:46 2004
@@ -83,11 +83,17 @@
     {
         assertFalse( roleDAO.has( "app1", "roleA" ) ) ;
 
-//        DefaultApplication application = new JeApplication( "app1" ) ;
-//        roleDAO.create( application, "roleA" ) ;
-//        roleDAO.create( application, "roleB" ) ;
-//        roleDAO.create( application, "roleC" ) ;
-//        roleDAO.create( application, "roleD" ) ;
+        DefaultApplication application = new JeApplication( "app1" ) ;
+        roleDAO.create( application, "roleA" ) ;
+        roleDAO.create( application, "roleB" ) ;
+        roleDAO.create( application, "roleC" ) ;
+        roleDAO.create( application, "roleD" ) ;
 
+        assertTrue( roleDAO.has( "app1", "roleA" ) ) ;
+        assertTrue( roleDAO.has( "app1", "roleB" ) ) ;
+        assertTrue( roleDAO.has( "app1", "roleC" ) ) ;
+        assertTrue( roleDAO.has( "app1", "roleD" ) ) ;
+
+        
     }
 }

Mime
View raw message