]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java
Fixed bad bug related to AcornBackupProvider
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / AcornDatabase.java
index be505c6039712e322ce80ffa653c1b4c6babac13..12f0552e434e96638d6bdd7bcd22d440d3d9a674 100644 (file)
@@ -2,14 +2,16 @@ package org.simantics.acorn.internal;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.nio.file.DirectoryStream;
+import java.nio.file.FileAlreadyExistsException;
 import java.nio.file.FileVisitOption;
 import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.EnumSet;
 import java.util.Properties;
@@ -18,19 +20,28 @@ import org.simantics.acorn.GraphClientImpl2;
 import org.simantics.db.Database;
 import org.simantics.db.DatabaseUserAgent;
 import org.simantics.db.ServiceLocator;
-import org.simantics.db.common.utils.Logger;
+import org.simantics.db.server.DatabaseStartException;
 import org.simantics.db.server.ProCoreException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Tuukka Lehtonen
  */
 public class AcornDatabase implements Database {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(AcornDatabase.class);
+
+    private static final String LOCK_FILE_NAME = "lock";
+
     private final Path folder;
+    private final Path lockFile;
+
+    private GraphClientImpl2 currentClient;
 
     private DatabaseUserAgent userAgent;
 
-    private RandomAccessFile raLockFile;
+    private FileChannel lockFileChannel;
 
     private FileLock lock;
 
@@ -38,6 +49,7 @@ public class AcornDatabase implements Database {
 
     public AcornDatabase(Path folder) {
         this.folder = folder;
+        this.lockFile = folder.resolve(LOCK_FILE_NAME);
     }
 
     @Override
@@ -85,7 +97,7 @@ public class AcornDatabase implements Database {
         try (DirectoryStream<Path> folderStream = Files.newDirectoryStream(path)) {
             return !folderStream.iterator().hasNext();
         } catch (IOException e) {
-            Logger.defaultLogError("Failed to open folder stream. folder=" + path, e);
+            LOGGER.error("Failed to open folder stream. folder=" + path, e);
             return false;
         }
     }
@@ -105,22 +117,25 @@ public class AcornDatabase implements Database {
     }
 
     @Override
-    public void start() throws ProCoreException {
-        Path lockFile = folder.resolve("lock");
+    public synchronized void start() throws ProCoreException {
         try {
-            if (!Files.exists(lockFile))
-                Files.createFile(lockFile);
-            
-            raLockFile = new RandomAccessFile(lockFile.toFile(), "rw");
-            lock = raLockFile.getChannel().tryLock();
+            try {
+                lockFileChannel = lockFile.getFileSystem().provider().newFileChannel(lockFile,
+                        EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE));
+            } catch (FileAlreadyExistsException e) {
+                throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!", e);
+            }
+
+            lock = lockFileChannel.tryLock();
             if (lock == null) {
+                safeLoggingClose(lockFileChannel, lockFile);
                 throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!");
             }
-            
+
             isRunning = true;
-            
         } catch (IOException e) {
-            e.printStackTrace();
+            LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
+            safeLoggingClose(lockFileChannel, lockFile);
         }
     }
 
@@ -130,19 +145,19 @@ public class AcornDatabase implements Database {
     }
 
     @Override
-    public boolean tryToStop() throws ProCoreException {
+    public synchronized boolean tryToStop() throws ProCoreException {
+        if (!isRunning)
+            return false;
         try {
-            lock.release();
-            raLockFile.close();
-            
-            Files.deleteIfExists(folder.resolve("lock"));
-            
+            safeLoggingClose(lock, lockFile);
+            lock = null;
+            safeLoggingClose(lockFileChannel, lockFile);
+            lockFileChannel = null;
+            Files.deleteIfExists(lockFile);
             isRunning = false;
-            
         } catch (IOException e) {
-            e.printStackTrace();
+            LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
         }
-        
         return true;
     }
 
@@ -190,22 +205,22 @@ public class AcornDatabase implements Database {
 
     @Override
     public void purgeDatabase() throws ProCoreException {
-        // TODO: implement
-        throw new UnsupportedOperationException();
+       if(currentClient == null) throw new IllegalStateException("No current session.");
+       currentClient.purgeDatabase();
     }
 
     @Override
     public long serverGetTailChangeSetId() throws ProCoreException {
-        // "We have it all"
-        // But after purging we don't so beware.
-        // TODO: beware for purge
-        return 1;
+       if(currentClient == null) throw new IllegalStateException("No current session.");
+       return currentClient.getTailChangeSetId();
     }
 
     @Override
     public Session newSession(ServiceLocator locator) throws ProCoreException {
         try {
-            return new GraphClientImpl2(this, folder, locator);
+               if(currentClient != null) throw new DatabaseStartException(folder.toFile(), "A session is already running. Only one session is supported.");
+               currentClient = new GraphClientImpl2(this, folder, locator); 
+            return currentClient;
         } catch (IOException e) {
             throw new ProCoreException(e);
         }
@@ -260,4 +275,13 @@ public class AcornDatabase implements Database {
                return "LZ4";
        }
 
+    private static void safeLoggingClose(AutoCloseable closeable, Path file) {
+        if (closeable == null)
+            return;
+        try (AutoCloseable c = closeable) {
+        } catch (Exception e) {
+            LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath());
+        }
+    }
+
 }