directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r979420 - /directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
Date Mon, 26 Jul 2010 19:56:24 GMT
Author: kayyagari
Date: Mon Jul 26 19:56:24 2010
New Revision: 979420

URL: http://svn.apache.org/viewvc?rev=979420&view=rev
Log:
o fix for DIRSERVER-1354

Modified:
    directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java?rev=979420&r1=979419&r2=979420&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/DefaultDirectoryService.java
Mon Jul 26 19:56:24 2010
@@ -22,13 +22,16 @@ package org.apache.directory.server.core
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.io.StringReader;
+import java.nio.channels.FileLock;
+import java.nio.channels.OverlappingFileLockException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
@@ -92,7 +95,6 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.name.DN;
 import org.apache.directory.shared.ldap.name.RDN;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
-import org.apache.directory.shared.ldap.schema.normalizers.OidNormalizer;
 import org.apache.directory.shared.ldap.util.DateUtils;
 import org.apache.directory.shared.ldap.util.StringTools;
 import org.slf4j.Logger;
@@ -232,6 +234,11 @@ public class DefaultDirectoryService imp
     /** the value of last successful add/update operation's CSN */
     private String contextCsn;
     
+    /** lock file for directory service's working directory */
+    private RandomAccessFile lockFile = null;
+    
+    private static final String LOCK_FILE_NAME = ".dirservice.lock";
+    
     /**
      * The synchronizer thread. It flush data on disk periodically.
      */
@@ -876,6 +883,8 @@ public class DefaultDirectoryService imp
             return;
         }
 
+        lockWorkDir();
+        
         if ( shutdownHookEnabled )
         {
             Runtime.getRuntime().addShutdownHook( new Thread( new Runnable()
@@ -998,6 +1007,20 @@ public class DefaultDirectoryService imp
         // And shutdown the server
         // --------------------------------------------------------------------
         interceptorChain.destroy();
+        
+        if ( lockFile != null )
+        {
+            try
+            {
+                lockFile.close();
+                // no need to delete the lock file
+            }
+            catch( IOException e )
+            {
+                LOG.warn( "couldn't delete the lock file {}", LOCK_FILE_NAME );
+            }
+        }
+        
         started = false;
         setDefaultInterceptorConfigurations();
     }
@@ -1698,4 +1721,45 @@ public class DefaultDirectoryService imp
     {
         this.contextCsn = lastKnownCsn;
     }
+    
+    
+    /**
+     * checks if the working directory is already in use by some other directory service,
if yes
+     * then throws a runtime exception else will obtain the lock on the working directory
+     */
+    private void lockWorkDir()
+    {
+        FileLock fileLock = null;
+        
+        try
+        {
+            lockFile = new RandomAccessFile( workingDirectory.getAbsolutePath() + File.separator
+ LOCK_FILE_NAME, "rw" );
+            try
+            {
+                fileLock = lockFile.getChannel().tryLock( 0, 1, false );
+            }
+            catch ( IOException e )
+            {
+                // shoudn't happen, but log
+                LOG.error( "failed to lock the work directory", e );
+            }
+            catch ( OverlappingFileLockException e ) // thrown if we can't get a lock
+            {
+                fileLock = null;
+            }
+        }
+        catch ( FileNotFoundException e )
+        {
+            // shouldn't happen, but log anyway
+            LOG.error( "failed to lock the work directory", e );
+        }
+        
+        if ( ( fileLock == null ) || ( !fileLock.isValid() ) )
+        {
+            String message = "the working directory " + workingDirectory + " has been locked
by another directory service.";
+            LOG.error( message );
+            throw new RuntimeException( message );
+        }
+        
+    }
 }



Mime
View raw message