directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r600724 - in /directory/apacheds/branches/bigbang/core/src: main/java/org/apache/directory/server/core/ main/java/org/apache/directory/server/core/changelog/ test/java/org/apache/directory/server/core/authz/support/ test/java/org/apache/dir...
Date Mon, 03 Dec 2007 23:31:51 GMT
Author: akarasulu
Date: Mon Dec  3 15:31:50 2007
New Revision: 600724

URL: http://svn.apache.org/viewvc?rev=600724&view=rev
Log:
changes to changelog service ...

 o added convenience method on DirectoryService to revert to the latest tag if one exists
 o added some life cycle methods to changeLog store 
 o added very primitive load and dump capability to the in memory implementation of the 
   default change log store: dumps revision, tags and changelog as serialized change events
   on sync and shutdown. Loads them up from these files on startup.  Writes to file ovewrite
   what was there before: ONLY FOR TESTING!!!
 o equals on Tag
  

Modified:
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DirectoryService.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLog.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLogStore.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/DefaultChangeLog.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/MemoryChangeLogStore.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/Tag.java
    directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/authz/support/MaxImmSubFilterTest.java
    directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/interceptor/InterceptorChainTest.java

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
Mon Dec  3 15:31:50 2007
@@ -24,10 +24,7 @@
 import org.apache.directory.server.core.authn.LdapPrincipal;
 import org.apache.directory.server.core.authz.AciAuthorizationInterceptor;
 import org.apache.directory.server.core.authz.DefaultAuthorizationInterceptor;
-import org.apache.directory.server.core.changelog.ChangeLog;
-import org.apache.directory.server.core.changelog.ChangeLogEvent;
-import org.apache.directory.server.core.changelog.ChangeLogInterceptor;
-import org.apache.directory.server.core.changelog.DefaultChangeLog;
+import org.apache.directory.server.core.changelog.*;
 import org.apache.directory.server.core.collective.CollectiveAttributeInterceptor;
 import org.apache.directory.server.core.cursor.Cursor;
 import org.apache.directory.server.core.event.EventInterceptor;
@@ -566,6 +563,31 @@
     }
 
 
+    public long revert() throws NamingException
+    {
+        if ( changeLog == null || ! changeLog.isEnabled() )
+        {
+            throw new IllegalStateException( "The change log must be enabled to revert to
previous log revisions." );
+        }
+
+        Tag latest = changeLog.getLatest();
+        if ( null != latest )
+        {
+            if ( latest.getRevision() < changeLog.getCurrentRevision() )
+            {
+                return revert( latest.getRevision() );
+            }
+            else
+            {
+                LOG.info( "Ignoring request to revert without changes since the latest tag."
);
+                return changeLog.getCurrentRevision();
+            }
+        }
+
+        throw new IllegalStateException( "There must be at least one tag to revert to the
latest tag." );
+    }
+
+
     public long revert( long revision ) throws NamingException
     {
         if ( changeLog == null || ! changeLog.isEnabled() )
@@ -707,6 +729,7 @@
             return;
         }
 
+        this.changeLog.sync();
         this.partitionNexus.sync();
     }
 
@@ -718,6 +741,9 @@
             return;
         }
 
+        this.changeLog.sync();
+        this.changeLog.destroy();
+
         this.partitionNexus.sync();
         this.partitionNexus.destroy();
         this.interceptorChain.destroy();
@@ -1329,6 +1355,11 @@
 
         interceptorChain = new InterceptorChain();
         interceptorChain.init( this );
+
+        if ( changeLog.isEnabled() )
+        {
+            changeLog.init( this );
+        }
 
         if ( LOG.isDebugEnabled() )
         {

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DirectoryService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DirectoryService.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DirectoryService.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/DirectoryService.java
Mon Dec  3 15:31:50 2007
@@ -70,6 +70,23 @@
     long revert( long revision ) throws NamingException;
 
 
+    /**
+     * Reverts the server's state to the latest tagged snapshot if one was taken.  If
+     * there is no tag a illegal state exception will result.  If the latest revision
+     * is not earlier than the current revision (both are same), then no changes were
+     * made to the directory to be reverted.  In this case we return the current
+     * revision and do nothiig loggin the fact that we ignored the request to revert.
+     *
+     * @return the new revision reached by applying all changes needed to revert
+     * to the new state or the same version before this call if no revert actually
+     * took place
+     * @throws NamingException if there are problems reverting back to the earlier state
+     * @throws UnsupportedOperationException if this feature is not supported by the
+     * change log
+     */
+    long revert() throws NamingException;
+
+
     PartitionNexus getPartitionNexus();
 
 

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLog.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLog.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLog.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLog.java
Mon Dec  3 15:31:50 2007
@@ -20,6 +20,7 @@
 package org.apache.directory.server.core.changelog;
 
 
+import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.authn.LdapPrincipal;
 import org.apache.directory.shared.ldap.ldif.Entry;
 
@@ -172,4 +173,10 @@
     Tag tag() throws NamingException;
 
     Tag getLatest() throws NamingException;
+
+    void init( DirectoryService service ) throws NamingException;
+
+    void sync() throws NamingException;
+
+    void destroy() throws NamingException;
 }

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLogStore.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLogStore.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLogStore.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/ChangeLogStore.java
Mon Dec  3 15:31:50 2007
@@ -22,6 +22,7 @@
 
 import org.apache.directory.server.core.authn.LdapPrincipal;
 import org.apache.directory.server.core.cursor.Cursor;
+import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.shared.ldap.ldif.Entry;
 
 import javax.naming.NamingException;
@@ -36,6 +37,15 @@
  */
 public interface ChangeLogStore 
 {
+    void init( DirectoryService service ) throws NamingException;
+
+
+    void sync() throws NamingException;
+
+
+    void destroy() throws NamingException;
+
+
     /**
      * Gets the current revision of the server (a.k.a. the HEAD revision).
      *

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/DefaultChangeLog.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/DefaultChangeLog.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/DefaultChangeLog.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/DefaultChangeLog.java
Mon Dec  3 15:31:50 2007
@@ -20,6 +20,7 @@
 
 
 import org.apache.directory.server.core.authn.LdapPrincipal;
+import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.shared.ldap.ldif.Entry;
 
 import javax.naming.NamingException;
@@ -175,5 +176,32 @@
         }
 
         return null;
+    }
+
+
+    public void init( DirectoryService service ) throws NamingException
+    {
+        if ( enabled )
+        {
+            store.init( service );
+        }
+    }
+
+
+    public void sync() throws NamingException
+    {
+        if ( enabled )
+        {
+            store.sync();
+        }
+    }
+
+
+    public void destroy() throws NamingException
+    {
+        if ( enabled )
+        {
+            store.destroy();
+        }
     }
 }

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/MemoryChangeLogStore.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/MemoryChangeLogStore.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/MemoryChangeLogStore.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/MemoryChangeLogStore.java
Mon Dec  3 15:31:50 2007
@@ -18,6 +18,7 @@
  */
 package org.apache.directory.server.core.changelog;
 
+import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.authn.LdapPrincipal;
 import org.apache.directory.server.core.cursor.Cursor;
 import org.apache.directory.server.core.cursor.ListCursor;
@@ -25,10 +26,8 @@
 import org.apache.directory.shared.ldap.util.DateUtils;
 
 import javax.naming.NamingException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.*;
+import java.util.*;
 
 
 /**
@@ -41,10 +40,15 @@
  */
 public class MemoryChangeLogStore implements TaggableChangeLogStore
 {
+    private static final String REV_FILE = "revision";
+    private static final String TAG_FILE = "tags";
+    private static final String CHANGELOG_FILE = "changelog.dat";
+
     private long currentRevision;
     private Tag latest;
     private final Map<Long,Tag> tags = new HashMap<Long,Tag>( 100 );
     private final List<ChangeLogEvent> events = new ArrayList<ChangeLogEvent>();
+    private File workingDirectory;
 
 
     public Tag tag( long revision ) throws NamingException
@@ -54,7 +58,9 @@
             return tags.get( revision );
         }
 
-        return latest = new Tag( revision, null );
+        latest = new Tag( revision, null );
+        tags.put( revision, latest );
+        return latest;
     }
 
 
@@ -65,7 +71,9 @@
             return latest;
         }
 
-        return latest = new Tag( currentRevision, null );
+        latest = new Tag( currentRevision, null );
+        tags.put( currentRevision, latest );
+        return latest;
     }
 
 
@@ -79,6 +87,319 @@
         latest = new Tag( currentRevision, description );
         tags.put( currentRevision, latest );
         return latest;
+    }
+
+
+    public void init( DirectoryService service ) throws NamingException
+    {
+        workingDirectory = service.getWorkingDirectory();
+        loadRevision();
+        loadTags();
+        loadChangeLog();
+    }
+
+
+    private void loadRevision() throws NamingException
+    {
+        File revFile = new File( workingDirectory, REV_FILE );
+        if ( revFile.exists() )
+        {
+            BufferedReader reader = null;
+            try
+            {
+                reader = new BufferedReader( new FileReader( revFile ) );
+                String line = reader.readLine();
+                currentRevision = Long.valueOf( line );
+            }
+            catch ( IOException e )
+            {
+                throw new NamingException( "Failed to open stream to read from revfile: "
+ revFile.getAbsolutePath() );
+            }
+            finally
+            {
+                if ( reader != null )
+                {
+                    //noinspection EmptyCatchBlock
+                    try
+                    {
+                        reader.close();
+                    }
+                    catch ( IOException e )
+                    {
+                    }
+                }
+            }
+        }
+    }
+
+
+    private void saveRevision() throws NamingException
+    {
+        File revFile = new File( workingDirectory, REV_FILE );
+        if ( revFile.exists() )
+        {
+            revFile.delete();
+        }
+
+        PrintWriter out = null;
+        try
+        {
+            out = new PrintWriter( new FileWriter( revFile ) );
+            out.println( currentRevision );
+            out.flush();
+        }
+        catch ( IOException e )
+        {
+            throw new NamingException( "Failed to write out revision file." );
+        }
+        finally
+        {
+            if ( out != null )
+            {
+                out.close();
+            }
+        }
+    }
+
+
+    private void saveTags() throws NamingException
+    {
+        File tagFile = new File( workingDirectory, TAG_FILE );
+        if ( tagFile.exists() )
+        {
+            tagFile.delete();
+        }
+
+        FileOutputStream out = null;
+        try
+        {
+            out = new FileOutputStream( tagFile );
+
+            Properties props = new Properties();
+            for ( Tag tag : tags.values() )
+            {
+                String key = String.valueOf( tag.getRevision() );
+                if ( tag.getDescription() == null )
+                {
+                    props.setProperty( key, "null" );
+                }
+                else
+                {
+                    props.setProperty( key, tag.getDescription() );
+                }
+            }
+
+            props.store( out, null );
+            out.flush();
+        }
+        catch ( IOException e )
+        {
+            throw new NamingException( "Failed to write out revision file." );
+        }
+        finally
+        {
+            if ( out != null )
+            {
+                //noinspection EmptyCatchBlock
+                try
+                {
+                    out.close();
+                }
+                catch ( IOException e )
+                {
+                }
+            }
+        }
+    }
+
+
+    private void loadTags() throws NamingException
+    {
+        File revFile = new File( workingDirectory, REV_FILE );
+        if ( revFile.exists() )
+        {
+            Properties props = new Properties();
+            FileInputStream in = null;
+            try
+            {
+                in = new FileInputStream( revFile );
+                props.load( in );
+                ArrayList<Long> revList = new ArrayList<Long>();
+                for ( Object key : props.keySet() )
+                {
+                    revList.add( Long.valueOf( ( String ) key ) );
+                }
+
+                Collections.sort( revList );
+                Tag tag = null;
+
+                // @todo need some serious syncrhoization here on tags
+                tags.clear();
+                for ( Long lkey : revList )
+                {
+                    String rev = String.valueOf( lkey );
+                    String desc = props.getProperty( rev );
+
+                    if ( desc != null && desc.equals( "null" ) )
+                    {
+                        tag = new Tag( lkey, null );
+                    }
+                    else
+                    {
+                        tag = new Tag( lkey, desc );
+                    }
+
+                    tags.put( lkey, tag );
+                }
+
+                latest = tag;
+            }
+            catch ( IOException e )
+            {
+                throw new NamingException( "Failed to open stream to read from revfile: "
+ revFile.getAbsolutePath() );
+            }
+            finally
+            {
+                if ( in != null )
+                {
+                    //noinspection EmptyCatchBlock
+                    try
+                    {
+                        in.close();
+                    }
+                    catch ( IOException e )
+                    {
+                    }
+                }
+            }
+        }
+    }
+
+
+    private void loadChangeLog() throws NamingException
+    {
+        File file = new File( workingDirectory, CHANGELOG_FILE );
+        if ( file.exists() )
+        {
+            ObjectInputStream in = null;
+
+            try
+            {
+                in = new ObjectInputStream( new FileInputStream( file ) );
+                ArrayList<ChangeLogEvent> changeLogEvents = new ArrayList<ChangeLogEvent>();
+
+                while ( true )
+                {
+                    try
+                    {
+                        ChangeLogEvent event = ( ChangeLogEvent ) in.readObject();
+                        changeLogEvents.add( event );
+                    }
+                    catch ( EOFException eofe )
+                    {
+                        break;
+                    }
+                }
+
+                // @todo man o man we need some synchronization later after getting this
to work
+                this.events.clear();
+                this.events.addAll( changeLogEvents );
+            }
+            catch ( Exception e )
+            {
+                NamingException ne = new NamingException( "Failed to open stream to read
from changelog file: "
+                        + file.getAbsolutePath() );
+                ne.setRootCause( e );
+                throw ne;
+            }
+            finally
+            {
+                if ( in != null )
+                {
+                    //noinspection EmptyCatchBlock
+                    try
+                    {
+                        in.close();
+                    }
+                    catch ( IOException e )
+                    {
+                    }
+                }
+            }
+        }
+    }
+
+
+    private void saveChangeLog() throws NamingException
+    {
+        File file = new File( workingDirectory, CHANGELOG_FILE );
+        if ( file.exists() )
+        {
+            file.delete();
+        }
+
+        try
+        {
+            file.createNewFile();
+        }
+        catch ( IOException e )
+        {
+            NamingException ne = new NamingException( "Failed to create new file for changelog:
"
+                    + file.getAbsolutePath() );
+            ne.setRootCause( e );
+            throw ne;
+        }
+
+        ObjectOutputStream out = null;
+
+        try
+        {
+            out = new ObjectOutputStream( new FileOutputStream( file ) );
+
+            for ( ChangeLogEvent event : events )
+            {
+                out.writeObject( event );
+            }
+
+            out.flush();
+        }
+        catch ( Exception e )
+        {
+            NamingException ne = new NamingException( "Failed to open stream to write to
changelog file: "
+                    + file.getAbsolutePath() );
+            ne.setRootCause( e );
+            throw ne;
+        }
+        finally
+        {
+            if ( out != null )
+            {
+                //noinspection EmptyCatchBlock
+                try
+                {
+                    out.close();
+                }
+                catch ( IOException e )
+                {
+                }
+            }
+        }
+    }
+
+
+    public void sync() throws NamingException
+    {
+        saveRevision();
+        saveTags();
+        saveChangeLog();
+    }
+
+
+    public void destroy() throws NamingException
+    {
+        saveRevision();
+        saveTags();
+        saveChangeLog();
     }
 
 

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/Tag.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/Tag.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/Tag.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/changelog/Tag.java
Mon Dec  3 15:31:50 2007
@@ -60,4 +60,24 @@
     {
         return description;
     }
+
+
+    public boolean equals( Object other )
+    {
+        if ( other instanceof Tag )
+        {
+            Tag ot = ( Tag ) other;
+
+            if ( description != null && ot.getDescription() != null )
+            {
+                return revision == ot.getRevision() && description.equals( ot.getDescription()
);
+            }
+            else if ( description == null && ot.getDescription() == null )
+            {
+                return revision == ot.getRevision();
+            }
+        }
+
+        return false;
+    }
 }

Modified: directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/authz/support/MaxImmSubFilterTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/authz/support/MaxImmSubFilterTest.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/authz/support/MaxImmSubFilterTest.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/authz/support/MaxImmSubFilterTest.java
Mon Dec  3 15:31:50 2007
@@ -201,6 +201,12 @@
         }
 
 
+        public long revert() throws NamingException
+        {
+            return 0;
+        }
+
+
         public PartitionNexus getPartitionNexus()
         {
             return null;

Modified: directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/interceptor/InterceptorChainTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/interceptor/InterceptorChainTest.java?rev=600724&r1=600723&r2=600724&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/interceptor/InterceptorChainTest.java
(original)
+++ directory/apacheds/branches/bigbang/core/src/test/java/org/apache/directory/server/core/interceptor/InterceptorChainTest.java
Mon Dec  3 15:31:50 2007
@@ -261,6 +261,12 @@
         }
 
 
+        public long revert() throws NamingException
+        {
+            return 0;
+        }
+
+
         public PartitionNexus getPartitionNexus()
         {
             return null;



Mime
View raw message