]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/AcornDatabaseManager.java
Sharing org.simantics.acorn for everyone to use
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / AcornDatabaseManager.java
diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/AcornDatabaseManager.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/AcornDatabaseManager.java
new file mode 100644 (file)
index 0000000..db2c167
--- /dev/null
@@ -0,0 +1,40 @@
+package org.simantics.acorn;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.simantics.acorn.internal.AcornDatabase;
+import org.simantics.db.Database;
+import org.simantics.db.server.ProCoreException;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class AcornDatabaseManager {
+
+    private static Map<String, Database> dbs = new HashMap<String, Database>();
+
+    public static synchronized Database getDatabase(Path folder) throws ProCoreException {
+        Path canonical;
+        try {
+            if (!Files.exists(folder))
+                Files.createDirectories(folder);
+            canonical = folder.toRealPath();
+        } catch (IOException e) {
+            throw new ProCoreException("Could not get canonical path.", e);
+        }
+
+        String canonicalPath = canonical.toString();
+        Database db = dbs.get(canonicalPath);
+        if (null != db)
+            return db;
+
+        db = new AcornDatabase(canonical);
+        dbs.put(canonicalPath, db);
+        return db;
+    }
+
+}
\ No newline at end of file