1 package org.simantics.acorn;
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.HashMap;
7 import java.util.Properties;
9 import org.simantics.db.Database;
10 import org.simantics.db.DatabaseUserAgent;
11 import org.simantics.db.Driver;
12 import org.simantics.db.ServerI;
13 import org.simantics.db.ServerReference;
14 import org.simantics.db.Session;
15 import org.simantics.db.SessionReference;
16 import org.simantics.db.exception.DatabaseException;
18 public class AcornDriver implements Driver {
20 public static final String AcornDriverName = "acorn";
22 private Map<String, ServerI> servers = new HashMap<>();
23 private Map<String, Management> managements = new HashMap<>();
26 public String getName() {
27 return AcornDriverName;
31 public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {
32 return AcornDatabaseManager.getDatabase(Paths.get(address)).getUserAgent();
36 public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {
37 AcornDatabaseManager.getDatabase(Paths.get(address)).setUserAgent(dbUserAgent);
41 public Session getSession(String address, Properties properties) throws DatabaseException {
42 Path dbFolder = Paths.get(address);
43 Session session = AcornSessionManagerImpl.getInstance().createSession(new SessionReference() {
46 public ServerReference getServerReference() {
47 return new ServerReference() {
50 public Path getDBFolder() {
57 public long getSessionId() {
61 if (!properties.containsKey("clientId"))
62 properties.put("clientId", dbFolder.toAbsolutePath().toString());
63 session.registerService(Properties.class, properties);
64 Session s = session.peekService(Session.class);
66 session.registerService(Session.class, session);
71 public ServerI getServer(String address, Properties properties) throws DatabaseException {
72 ServerI server = servers.get(address);
74 server = new AcornServerI(AcornDatabaseManager.getDatabase(Paths.get(address)), address);
75 servers.put(address, server);
81 public Management getManagement(String address, Properties properties) throws DatabaseException {
82 Management mgmt = managements.get(address);
84 mgmt = new AcornManagement(AcornDatabaseManager.getDatabase(Paths.get(address)), properties);
85 managements.put(address, mgmt);
90 private static class AcornServerI implements ServerI {
92 private Database database;
93 private String address;
95 public AcornServerI(Database db, String address) {
97 this.address = address;
101 public void stop() throws DatabaseException {
102 database.tryToStop();
106 public void start() throws DatabaseException {
111 public boolean isActive() throws DatabaseException {
112 return database.isRunning();
116 public String getAddress() throws DatabaseException {
121 public String executeAndDisconnect(String command) throws DatabaseException {
126 public String execute(String command) throws DatabaseException {