You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/05/01 03:29:30 UTC

svn commit: rev 10464 - in incubator/directory/rms/trunk/je/src: java/org/apache/rms/je/profile test/org/apache/rms/je/profile

Author: akarasulu
Date: Fri Apr 30 18:29:30 2004
New Revision: 10464

Modified:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/JeProfileRoleLinkDAO.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitorAdapter.java
   incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/profile/JeProfileRoleLinkDAOTest.java
Log:
Commit changes ...

 o completed create() and has() methods on the role link DAO
 o completed and passed tests for create() and has()
 o added methods to monitor for both create() operations
 o added no-op implementations to monitor adapter



Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/JeProfileRoleLinkDAO.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/JeProfileRoleLinkDAO.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/JeProfileRoleLinkDAO.java	Fri Apr 30 18:29:30 2004
@@ -17,12 +17,21 @@
 package org.apache.rms.je.profile ;
 
 
+import org.apache.rms.je.JeUtils ;
 import org.apache.rms.RmsException ;
+import org.apache.rms.je.sequence.Sequence ;
 
 import org.apache.commons.lang.NotImplementedException ;
+import org.apache.commons.lang.Validate ;
 
+import java.util.List ;
 import java.util.Iterator ;
 
+import java.io.IOException ;
+import java.io.UnsupportedEncodingException ;
+
+import com.sleepycat.je.* ;
+
 
 /**
  * A Je based Profile to Role link data access object implementation.
@@ -32,6 +41,88 @@
  */
 public class JeProfileRoleLinkDAO implements ProfileRoleLinkDAO
 {
+    /** the binding used by this profile role link DAO */
+    public static final JeProfileRoleLinkBinding BINDING =
+            new JeProfileRoleLinkBinding() ;
+    /** the name of secondary db used to associate links with an appName */
+    public static final String APPNAME_SECDB  = "roleLinkByAppName" ;
+    /** the name of secondary db used to associate links with an userName */
+    public static final String USERNAME_SECDB = "roleLinkByUserName" ;
+    /** the name of secondary db used to associate links with an roleName */
+    public static final String ROLENAME_SECDB = "roleLinkByRoleName" ;
+
+    /** the primary database used to store profile role links */
+    private final Database db ;
+    /** the sequence used for link row ids */
+    private final Sequence sq ;
+
+    /** the secondary database for looking up links by appName */
+    private SecondaryDatabase byAppName ;
+    /** the secondary database for looking up links by userName */
+    private SecondaryDatabase byUserName ;
+    /** the secondary database for looking up links by roleName */
+    private SecondaryDatabase byRoleName ;
+
+    /** callback used to for monitoring noteable events */
+    private ProfileRoleLinkDAOMonitor monitor = null ;
+
+
+    // -----------------------------------------------------------------------
+    // C O N S T R U C T O R S
+    // -----------------------------------------------------------------------
+
+
+    /**
+     * Creates a JeProfileRoleLinkDAO using an existing database and sequence
+     * for row ids.
+     *
+     * @param db the primary database for profile to role link entries
+     * @param sq the sequence to use for getting sequential row ids
+     */
+    public JeProfileRoleLinkDAO( Database db, Sequence sq ) throws RmsException
+    {
+        this.db = db ;
+        this.sq = sq ;
+        this.monitor = new ProfileRoleLinkDAOMonitorAdapter() ;
+
+        try
+        {
+            List list = this.db.getSecondaryDatabases() ;
+
+            for ( int ii = 0; ii < list.size(); ii++ )
+            {
+                SecondaryDatabase secdb = ( SecondaryDatabase ) list.get( ii ) ;
+
+                if ( secdb.getDatabaseName().equals( APPNAME_SECDB ) )
+                {
+                    byAppName = secdb ;
+                }
+                else if ( secdb.getDatabaseName().equals( USERNAME_SECDB ) )
+                {
+                    byUserName = secdb ;
+                }
+                else if ( secdb.getDatabaseName().equals( ROLENAME_SECDB ) )
+                {
+                    byRoleName = secdb ;
+                }
+            }
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        Validate.notNull( byAppName ) ;
+        Validate.notNull( byUserName ) ;
+        Validate.notNull( byRoleName ) ;
+    }
+
+
+    // -----------------------------------------------------------------------
+    // Data access object interface methods
+    // -----------------------------------------------------------------------
+
+
     /**
      * Creates a link associating a user's application profile with a role.
      *
@@ -44,7 +135,55 @@
     public void create( String appName, String userName, String roleName )
             throws RmsException
     {
-        throw new NotImplementedException( "STUB" ) ;
+        if ( has( appName, userName, roleName ) )
+        {
+            monitor.roleLinkExists( this, "create", appName, userName,
+                    roleName ) ;
+            throw new RmsException( "User " + userName + " is already in role "
+                + roleName + " for application " + appName ) ;
+        }
+
+
+        DatabaseEntry key = new DatabaseEntry() ;
+        DatabaseEntry value = new DatabaseEntry() ;
+        OperationStatus status = null ;
+        JeUtils.encodeInt( sq.getNextValue(), key ) ;
+
+        try
+        {
+            BINDING.membersToEntry( value, appName, userName, roleName ) ;
+            status = db.put( null, key, value ) ;
+        }
+        catch ( IOException e )
+        {
+            monitor.failedLinkBinding( this, "create", appName, userName,
+                    roleName, e ) ;
+            throw new RmsException( e ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            monitor.failedOnCreate( this, appName, userName, roleName, e ) ;
+            throw new RmsException( e ) ;
+        }
+
+        if ( status == OperationStatus.SUCCESS )
+        {
+            monitor.roleLinkCreated( this, appName, userName, roleName ) ;
+        }
+        else if ( status == OperationStatus.KEYEXIST )
+        {
+            monitor.roleLinkExists( this, "create", appName, userName,
+                    roleName ) ;
+            throw new RmsException( "User " + userName + " is already in role "
+                + roleName + " for application " + appName ) ;
+        }
+        else
+        {
+            monitor.failedOnCreate( this, status, appName, userName,
+                    roleName ) ;
+            throw new RmsException( "role link db.put() failed with status of "
+                    + status ) ;
+        }
     }
 
 
@@ -78,7 +217,86 @@
     public boolean has( String appName, String userName, String roleName )
             throws RmsException
     {
-        throw new NotImplementedException( "STUB" ) ;
+        OperationStatus status = null ;
+        DatabaseEntry key = new DatabaseEntry() ;
+        DatabaseEntry value = new DatabaseEntry() ;
+        DatabaseEntry appNameKey = new DatabaseEntry() ;
+        DatabaseEntry userNameKey = new DatabaseEntry() ;
+        DatabaseEntry roleNameKey = new DatabaseEntry() ;
+
+        try
+        {
+            appNameKey.setData( appName.getBytes( "UTF-8" ) ) ;
+            userNameKey.setData( userName.getBytes( "UTF-8" ) ) ;
+            roleNameKey.setData( roleName.getBytes( "UTF-8" ) ) ;
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        JoinCursor cursor = null ;
+        SecondaryCursor appNameCursor = null ;
+        SecondaryCursor userNameCursor = null ;
+        SecondaryCursor roleNameCursor = null ;
+
+        try
+        {
+            // -o- setup the application name cursor -o-
+            appNameCursor = byAppName.openSecondaryCursor( null, null ) ;
+            status = appNameCursor
+                    .getSearchKey( appNameKey, value, LockMode.DEFAULT ) ;
+            if ( status != OperationStatus.SUCCESS )
+            {
+                return false ;
+            }
+
+            // -o- setup the user name cursor -o-
+            userNameCursor = byUserName.openSecondaryCursor( null, null ) ;
+            status = userNameCursor.getSearchKey( userNameKey, value,
+                    LockMode.DEFAULT ) ;
+            if ( status != OperationStatus.SUCCESS )
+            {
+                return false ;
+            }
+
+            // -o- setup the role name cursor -o-
+            roleNameCursor = byRoleName.openSecondaryCursor( null, null ) ;
+            status = roleNameCursor.getSearchKey( roleNameKey, value,
+                    LockMode.DEFAULT ) ;
+            if ( status != OperationStatus.SUCCESS )
+            {
+                return false ;
+            }
+
+            // -o- prepare the cursor array and the join cursor -o-
+            SecondaryCursor [] secCursors = {
+                appNameCursor,
+                userNameCursor,
+                roleNameCursor
+            } ;
+
+            cursor = db.join( secCursors, null ) ;
+            status = cursor.getNext( key, value, LockMode.DEFAULT ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        finally
+        {
+            closeNoError( cursor ) ;
+            closeNoError( appNameCursor ) ;
+            closeNoError( userNameCursor ) ;
+            closeNoError( roleNameCursor ) ;
+        }
+
+        if ( status != OperationStatus.SUCCESS )
+        {
+            return false ;
+        }
+
+        return true ;
     }
 
 
@@ -144,4 +362,91 @@
     {
         throw new NotImplementedException( "STUB" ) ;
     }
+
+
+    // -----------------------------------------------------------------------
+    // Utility Methods
+    // -----------------------------------------------------------------------
+
+
+    /**
+     * Closes a cursor without throwing an error.
+     *
+     * @param cursor the cursor to close.
+     */
+    private void closeNoError( Cursor cursor )
+    {
+        if ( cursor == null )
+        {
+            monitor.cleanedUp( this, "close", cursor ) ;
+            return ;
+        }
+
+        try
+        {
+            cursor.close() ;
+        }
+        catch ( DatabaseException e )
+        {
+            monitor.failedOnCleanupOperation( this, "close", cursor, e ) ;
+        }
+    }
+
+
+    /**
+     * Closes a cursor without throwing an error.
+     *
+     * @param cursor the cursor to close.
+     */
+    private void closeNoError( JoinCursor cursor )
+    {
+        if ( cursor == null )
+        {
+            monitor.cleanedUp( this, "close", cursor ) ;
+            return ;
+        }
+
+        try
+        {
+            cursor.close() ;
+        }
+        catch ( DatabaseException e )
+        {
+            monitor.failedOnCleanupOperation( this, "close", cursor, e ) ;
+        }
+    }
+
+
+    // -----------------------------------------------------------------------
+    // Monitor Getter/Setter Pair
+    // -----------------------------------------------------------------------
+
+
+    /**
+     * Gets the monitor notified by this DAO.
+     *
+     * @return the monitor notified by this DAO
+     */
+    public ProfileRoleLinkDAOMonitor getMonitor()
+    {
+        return monitor ;
+    }
+
+
+    /**
+     * Sets the monitor notified by this DAO.
+     *
+     * @param monitor the new monitor to be notified by this DAO
+     */
+    public void setMonitor( ProfileRoleLinkDAOMonitor monitor )
+    {
+        this.monitor = monitor ;
+    }
+
+
+    // -----------------------------------------------------------------------
+    // Cursor Iterator
+    // -----------------------------------------------------------------------
+
+
 }

Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitor.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitor.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitor.java	Fri Apr 30 18:29:30 2004
@@ -25,4 +25,73 @@
  */
 public interface ProfileRoleLinkDAOMonitor
 {
+    /**
+     * Used to monitor situations where an operation fails because a role
+     * already exists.
+     *
+     * @param dao the data access object performing the operation
+     * @param op the operation being performed
+     * @param appName the name of the application
+     * @param userName the name of the user
+     * @param roleName the name of the role
+     */
+    void roleLinkExists( ProfileRoleLinkDAO dao, String op, String appName,
+                         String userName, String roleName ) ;
+
+    /**
+     * Used to monitor situations where an operation fails because a link
+     * could not be bound to or from a database entry.
+     *
+     * @param dao the data access object performing the operation
+     * @param op the operation being performed
+     * @param appName the name of the application
+     * @param userName the name of the user
+     * @param roleName the name of the role
+     * @param fault the faulting throwable that caused the failure
+     */
+    void failedLinkBinding( ProfileRoleLinkDAO dao, String op, String appName,
+                            String userName, String roleName,
+                            Throwable fault ) ;
+
+    /**
+     * Monitor callback notified when role links are successfully created.
+     *
+     * @param dao the data access object that created the role link
+     * @param appName the name of the application
+     * @param userName the name of the user
+     * @param roleName the name of the role
+     */
+    void roleLinkCreated( ProfileRoleLinkDAO dao, String appName,
+                          String userName, String roleName ) ;
+
+    /**
+     * Monitor callback notified when role links fail to be created.
+     *
+     * @param dao the data access object that failed to create the role link
+     * @param appName the name of the application
+     * @param userName the name of the user
+     * @param roleName the name of the role
+     * @param fault the faulting throwable that caused the failure
+     */
+    void failedOnCreate( ProfileRoleLinkDAO dao, String appName,
+                         String userName, String roleName, Throwable fault ) ;
+
+    /**
+     * Monitor callback notified when role links fail to be created.
+     *
+     * @param dao
+     * @param info
+     * @param appName the name of the application
+     * @param userName the name of the user
+     * @param roleName the name of the role
+     */
+    void failedOnCreate( ProfileRoleLinkDAO dao, Object info, String appName,
+                         String userName, String roleName ) ;
+
+
+    void cleanedUp( ProfileRoleLinkDAO dao, String op, Object resource ) ;
+
+
+    void failedOnCleanupOperation( ProfileRoleLinkDAO dao, String op,
+                                   Object resource, Throwable fault ) ;
 }

Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitorAdapter.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitorAdapter.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/profile/ProfileRoleLinkDAOMonitorAdapter.java	Fri Apr 30 18:29:30 2004
@@ -27,4 +27,47 @@
 public class ProfileRoleLinkDAOMonitorAdapter
         implements ProfileRoleLinkDAOMonitor
 {
+    public void roleLinkExists( ProfileRoleLinkDAO dao, String op,
+                                String appName, String userName,
+                                String roleName )
+    {
+    }
+
+
+    public void failedLinkBinding( ProfileRoleLinkDAO dao, String op,
+                                   String appName, String userName,
+                                   String roleName, Throwable fault )
+    {
+    }
+
+
+    public void roleLinkCreated( ProfileRoleLinkDAO dao, String appName,
+                                 String userName, String roleName )
+    {
+    }
+
+
+    public void failedOnCreate( ProfileRoleLinkDAO dao, String appName,
+                                String userName, String roleName,
+                                Throwable fault )
+    {
+    }
+
+
+    public void failedOnCreate( ProfileRoleLinkDAO dao, Object info,
+                                String appName, String userName,
+                                String roleName )
+    {
+    }
+
+
+    public void cleanedUp( ProfileRoleLinkDAO dao, String op, Object resource )
+    {
+    }
+
+
+    public void failedOnCleanupOperation( ProfileRoleLinkDAO dao, String op,
+                                          Object resource, Throwable fault )
+    {
+    }
 }

Modified: incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/profile/JeProfileRoleLinkDAOTest.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/profile/JeProfileRoleLinkDAOTest.java	(original)
+++ incubator/directory/rms/trunk/je/src/test/org/apache/rms/je/profile/JeProfileRoleLinkDAOTest.java	Fri Apr 30 18:29:30 2004
@@ -18,6 +18,11 @@
 
 
 import org.apache.rms.je.AbstractJeTest ;
+import org.apache.rms.je.sequence.JeSequenceDao;
+import org.apache.rms.je.sequence.Sequence;
+import org.apache.rms.je.sequence.JeSequenceFactory;
+import com.sleepycat.je.SecondaryConfig;
+import com.sleepycat.je.Database;
 
 
 /**
@@ -34,7 +39,42 @@
 
     protected void setUp() throws Exception
     {
+        setupDb( "sqDb" ) ;
+
         super.setUp() ;
+
+        SecondaryConfig sconf = null ;
+        String name = null;
+
+        // -o- create the appName secondary db -o-
+        name = JeProfileRoleLinkDAO.APPNAME_SECDB ;
+        sconf = new SecondaryConfig() ;
+        sconf.setAllowCreate( true ) ;
+        sconf.setAllowDuplicates( true ) ;
+        sconf.setKeyCreator( new JeLinkAppNameKeyCreator() ) ;
+        env.openSecondaryDatabase( null, name, sconf, db ) ;
+
+        // -o- create the appName secondary db -o-
+        name = JeProfileRoleLinkDAO.USERNAME_SECDB ;
+        sconf = new SecondaryConfig() ;
+        sconf.setAllowCreate( true ) ;
+        sconf.setAllowDuplicates( true ) ;
+        sconf.setKeyCreator( new JeLinkUserNameKeyCreator() ) ;
+        env.openSecondaryDatabase( null, name, sconf, db ) ;
+
+        // -o- create the appName secondary db -o-
+        name = JeProfileRoleLinkDAO.ROLENAME_SECDB ;
+        sconf = new SecondaryConfig() ;
+        sconf.setAllowCreate( true ) ;
+        sconf.setAllowDuplicates( true ) ;
+        sconf.setKeyCreator( new JeLinkRoleNameKeyCreator() ) ;
+        env.openSecondaryDatabase( null, name, sconf, db ) ;
+
+        Database sqDb = getDb( "sqDb" ) ;
+        JeSequenceDao sqDao = new JeSequenceDao( sqDb ) ;
+        JeSequenceFactory sqFactory = new JeSequenceFactory( sqDao ) ;
+        Sequence sq = sqFactory.create( "roleLinkDbSeq" ) ;
+        dao = new JeProfileRoleLinkDAO( db, sq ) ;
     }
 
 
@@ -42,5 +82,24 @@
     {
         super.tearDown() ;
         dao = null ;
+    }
+
+
+    public void testCreateAndHas() throws Exception
+    {
+        assertFalse( dao.has( "app1", "user1", "roleA" ) ) ;
+        assertFalse( dao.has( "app1", "user2", "roleA" ) ) ;
+        assertFalse( dao.has( "app2", "user1", "roleB" ) ) ;
+        assertFalse( dao.has( "app2", "user2", "roleB" ) ) ;
+
+        dao.create( "app1", "user1", "roleA" ) ;
+        dao.create( "app1", "user2", "roleA" ) ;
+        dao.create( "app2", "user1", "roleB" ) ;
+        dao.create( "app2", "user2", "roleB" ) ;
+
+        assertTrue( dao.has( "app1", "user1", "roleA" ) ) ;
+        assertTrue( dao.has( "app1", "user2", "roleA" ) ) ;
+        assertTrue( dao.has( "app2", "user1", "roleB" ) ) ;
+        assertTrue( dao.has( "app2", "user2", "roleB" ) ) ;
     }
 }