]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/AcornDatabase.java
Fixing problems in the database unit testing environment with Acorn
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / AcornDatabase.java
index db83f1395d005babeb09dbd500f5034d2326f503..8109df6cd25fd8a4ca2f4a9cdacd405ff6b60b7c 100644 (file)
@@ -2,19 +2,18 @@ package org.simantics.acorn.internal;
 
 import java.io.File;
 import java.io.IOException;
-import java.nio.channels.FileChannel;
+import java.io.RandomAccessFile;
 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;
+import java.util.stream.Stream;
 
 import org.simantics.acorn.GraphClientImpl2;
 import org.simantics.db.Database;
@@ -25,6 +24,8 @@ import org.simantics.db.server.ProCoreException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import fi.vtt.simantics.procore.internal.StaticSessionProperties;
+
 /**
  * @author Tuukka Lehtonen
  */
@@ -41,7 +42,7 @@ public class AcornDatabase implements Database {
 
     private DatabaseUserAgent userAgent;
 
-    private FileChannel lockFileChannel;
+    private RandomAccessFile raLockFile;
 
     private FileLock lock;
 
@@ -114,24 +115,31 @@ public class AcornDatabase implements Database {
     @Override
     public void deleteFiles() throws ProCoreException {
         deleteTree(folder);
+        File vgPath = StaticSessionProperties.virtualGraphStoragePath;
+        if (vgPath != null) {
+            try (Stream<Path> vgs = Files.list(vgPath.toPath())) {
+                for (Path p : vgs.toArray(Path[]::new))
+                    deleteTree(p);
+            } catch (IOException e) {
+                throw new ProCoreException(e);
+            }
+        }
     }
 
     @Override
     public synchronized void start() throws ProCoreException {
         try {
-            lockFileChannel = lockFile.getFileSystem().provider().newFileChannel(lockFile,
-                    EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));
-
-            lock = lockFileChannel.tryLock();
+            raLockFile = new RandomAccessFile(lockFile.toFile(), "rw");
+            lock = raLockFile.getChannel().tryLock();
             if (lock == null) {
-                safeLoggingClose(lockFileChannel, lockFile);
+                safeLoggingClose(raLockFile, lockFile);
                 throw new ProCoreException("The database in folder " + folder.toAbsolutePath() + " is already in use!");
             }
-
             isRunning = true;
         } catch (IOException e) {
             LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
-            safeLoggingClose(lockFileChannel, lockFile);
+            safeLoggingClose(raLockFile, lockFile);
+            throw new ProCoreException("Failed to start database at " + folder.toAbsolutePath(), e);
         }
     }
 
@@ -147,10 +155,12 @@ public class AcornDatabase implements Database {
         try {
             safeLoggingClose(lock, lockFile);
             lock = null;
-            safeLoggingClose(lockFileChannel, lockFile);
-            lockFileChannel = null;
+            safeLoggingClose(raLockFile, lockFile);
+            raLockFile = null;
             Files.deleteIfExists(lockFile);
             isRunning = false;
+            safeLoggingClose(currentClient, currentClient.getDbFolder());
+            currentClient = null;
         } catch (IOException e) {
             LOGGER.error("Failed to start database at " + folder.toAbsolutePath(), e);
         }
@@ -228,39 +238,37 @@ public class AcornDatabase implements Database {
         throw new UnsupportedOperationException();
     }
 
-    private static void deleteTree(Path path) throws ProCoreException {
-        if (!Files.exists(path))
-            return;
-
-        class Visitor extends SimpleFileVisitor<Path> {
-            @Override
-            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+    static class Visitor extends SimpleFileVisitor<Path> {
+        @Override
+        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+            try {
+                Files.delete(file);
+            } catch (IOException ioe) {
+                LOGGER.error("Failed to delete file {}", file, ioe);
+                throw ioe;
+            }
+            return FileVisitResult.CONTINUE;
+        }
+        @Override
+        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
+            if (e == null) {
                 try {
-                    Files.delete(file);
+                    Files.delete(dir);
                 } catch (IOException ioe) {
-                    ioe.printStackTrace();
+                    LOGGER.error("Failed to delete directory {}", dir, ioe);
                     throw ioe;
                 }
                 return FileVisitResult.CONTINUE;
             }
-            @Override
-            public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
-                if (e == null) {
-                    try {
-                        Files.delete(dir);
-                    } catch (IOException ioe) {
-                        ioe.printStackTrace();
-                        throw ioe;
-                    }
-                    return FileVisitResult.CONTINUE;
-                }
-                throw e;
-            }
+            throw e;
         }
+    }
+
+    private static void deleteTree(Path path) throws ProCoreException {
+        if (!Files.exists(path))
+            return;
         try {
-            Visitor v = new Visitor();
-            EnumSet<FileVisitOption> opts = EnumSet.noneOf(FileVisitOption.class);
-            Files.walkFileTree(path, opts, Integer.MAX_VALUE, v);
+            Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new Visitor());
         } catch (IOException e) {
             throw new ProCoreException("Could not delete " + path, e);
         }
@@ -276,7 +284,17 @@ public class AcornDatabase implements Database {
             return;
         try (AutoCloseable c = closeable) {
         } catch (Exception e) {
-            LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath());
+            LOGGER.error("Failed to close " + closeable.getClass() + " of " + file.toAbsolutePath(), e);
+        }
+    }
+
+    private static void safeLoggingClose(Database.Session session, Path file) {
+        if (session == null)
+            return;
+        try {
+            session.close();
+        } catch (Exception e) {
+            LOGGER.error("Failed to close " + session.getClass() + " of " + file.toAbsolutePath(), e);
         }
     }