]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/AcornDriver.java
ListIndexRoots request
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / AcornDriver.java
1 package org.simantics.acorn;
2
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Properties;
8
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;
17
18 public class AcornDriver implements Driver {
19
20     public static final String AcornDriverName = "acorn";
21
22     private Map<String, ServerI> servers = new HashMap<>();
23     private Map<String, Management> managements = new HashMap<>();
24     
25     @Override
26     public String getName() {
27         return AcornDriverName;
28     }
29
30     @Override
31     public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {
32         return AcornDatabaseManager.getDatabase(Paths.get(address)).getUserAgent();
33     }
34
35     @Override
36     public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {
37         AcornDatabaseManager.getDatabase(Paths.get(address)).setUserAgent(dbUserAgent);
38     }
39
40     @Override
41     public Session getSession(String address, Properties properties) throws DatabaseException {
42         Path dbFolder = Paths.get(address);
43         Session session = AcornSessionManagerImpl.getInstance().createSession(new SessionReference() {
44             
45             @Override
46             public ServerReference getServerReference() {
47                 return new ServerReference() {
48                     
49                     @Override
50                     public Path getDBFolder() {
51                         return dbFolder;
52                     }
53                 };
54             }
55
56             @Override
57             public long getSessionId() {
58                 return 0L;
59             }
60         }, null);
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);
65         if (null == s)
66             session.registerService(Session.class, session);
67         return session;
68     }
69
70     @Override
71     public ServerI getServer(String address, Properties properties) throws DatabaseException {
72         ServerI server = servers.get(address);
73         if (server == null) {
74             server = new AcornServerI(AcornDatabaseManager.getDatabase(Paths.get(address)), address);
75             servers.put(address, server);
76         }
77         return server;
78     }
79
80     @Override
81     public Management getManagement(String address, Properties properties) throws DatabaseException {
82         Management mgmt = managements.get(address);
83         if (mgmt == null) {
84             mgmt = new AcornManagement(AcornDatabaseManager.getDatabase(Paths.get(address)), properties);
85             managements.put(address, mgmt);
86         }
87         return mgmt;
88     }
89     
90     private static class AcornServerI implements ServerI {
91         
92         private Database database;
93         private String address;
94
95         public AcornServerI(Database db, String address) {
96             this.database = db;
97             this.address = address;
98         }
99         
100         @Override
101         public void stop() throws DatabaseException {
102             database.tryToStop();
103         }
104         
105         @Override
106         public void start() throws DatabaseException {
107             database.start();
108         }
109         
110         @Override
111         public boolean isActive() throws DatabaseException {
112             return database.isRunning();
113         }
114         
115         @Override
116         public String getAddress() throws DatabaseException {
117             return address;
118         }
119         
120         @Override
121         public String executeAndDisconnect(String command) throws DatabaseException {
122             return "";
123         }
124         
125         @Override
126         public String execute(String command) throws DatabaseException {
127             return "";
128         }
129     }
130
131 }