]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/ProCoreDriver.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / ProCoreDriver.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.procore;\r
13 \r
14 import java.io.File;\r
15 import java.io.IOException;\r
16 import java.nio.file.Files;\r
17 import java.nio.file.Path;\r
18 import java.nio.file.Paths;\r
19 import java.util.Properties;\r
20 \r
21 import org.simantics.db.Database;\r
22 import org.simantics.db.DatabaseUserAgent;\r
23 import org.simantics.db.Driver;\r
24 import org.simantics.db.Driver.Management;\r
25 import org.simantics.db.ServerI;\r
26 import org.simantics.db.Session;\r
27 import org.simantics.db.authentication.UserAuthenticationAgent;\r
28 import org.simantics.db.authentication.UserAuthenticator;\r
29 import org.simantics.db.common.auth.UserAuthenticationAgents;\r
30 import org.simantics.db.common.auth.UserAuthenticators;\r
31 import org.simantics.db.exception.DatabaseException;\r
32 import org.simantics.db.exception.InternalException;\r
33 import org.simantics.db.exception.SDBException;\r
34 import org.simantics.db.server.DatabaseManager;\r
35 import org.simantics.db.server.ProCoreException;\r
36 \r
37 import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
38 import fi.vtt.simantics.procore.ProCoreServerReference;\r
39 import fi.vtt.simantics.procore.ProCoreSessionReference;\r
40 import fi.vtt.simantics.procore.SessionManagerSource;\r
41 \r
42 public class ProCoreDriver implements Driver {\r
43     private static long sessionId = SessionManagerSource.NullSessionId;\r
44     public static final String ProCoreDriverName = "procore";\r
45     public static final String ProCoreDriverNameVirtual = "virtual";\r
46 \r
47     @Override\r
48     public final String getName() {\r
49         return ProCoreDriverName;\r
50     }\r
51     @Override\r
52     public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {\r
53         Path dbFolder = Paths.get(address);\r
54         return DatabaseManager.getDatabase(dbFolder).getUserAgent();\r
55     }\r
56     @Override\r
57     public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {\r
58         Path dbFolder = Paths.get(address);\r
59         DatabaseManager.getDatabase(dbFolder).setUserAgent(dbUserAgent);\r
60     }\r
61     @Override\r
62     public Session getSession(String address, Properties properties) throws SDBException {\r
63         Path dbFolder = Paths.get(address);\r
64         if (!Files.isDirectory(dbFolder))\r
65             throw new ProCoreException("Database folder does not exist. folder=" + dbFolder);\r
66         Database db = DatabaseManager.getDatabase(dbFolder);\r
67         if (!db.isFolderOk())\r
68             throw new ProCoreException("Database folder is not ok. folder=" + dbFolder);\r
69         if (!db.isRunning())\r
70             db.start();\r
71         if (!db.isConnected())\r
72             db.connect();\r
73         Session session;\r
74         ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);\r
75         ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);\r
76         try {\r
77             String user = properties.getProperty("user");\r
78             String password= properties.getProperty("password");\r
79             if (user == null)\r
80                 throw new ProCoreException("'user' property not provided");\r
81             if (password == null)\r
82                 throw new ProCoreException("'password' property not provided");\r
83 \r
84             UserAuthenticator authenticator = UserAuthenticators.byNameAndPassword(user, password);\r
85             // FIXME: remove this hack once the server gets proper authentication\r
86             if (properties.getProperty("hyshys") != null)\r
87                 authenticator = new BackdoorAuthenticator();\r
88             UserAuthenticationAgent agent = UserAuthenticationAgents.staticAgent(authenticator);\r
89 \r
90             session = SessionManagerSource.getSessionManager().createSession(sessionReference, agent);\r
91             if (!properties.containsKey("clientId"))\r
92                 properties.put("clientId", dbFolder.toFile().getAbsolutePath());\r
93             session.registerService(Properties.class, properties);\r
94             Session s = session.peekService(Session.class);\r
95             if (null == s)\r
96                 session.registerService(Session.class, session);\r
97             return session;\r
98 \r
99         } catch (IOException e) {\r
100             throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);\r
101         } catch (org.simantics.db.exception.DatabaseException e) {\r
102             throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);\r
103         }\r
104     }\r
105 \r
106     @Override\r
107     public ServerI getServer(String address, Properties notUsed) throws ProCoreException {\r
108         Path dbFolder = Paths.get(address);\r
109         return new ProCoreServer(dbFolder.toFile());\r
110     }\r
111     @Override\r
112     public Management getManagement(String address, Properties properties) throws DatabaseException {\r
113         Path dbFolder = Paths.get(address);\r
114         return new ProCoreManagement(dbFolder, properties);\r
115     }\r
116 }\r
117 class ProCoreServer implements ServerI {\r
118     private final Database db;\r
119     ProCoreServer(File dbFolder) throws ProCoreException {\r
120         db = DatabaseManager.getDatabase(dbFolder.toPath());\r
121     }\r
122     @Override\r
123     public String execute(String aCommand) throws InternalException {\r
124         return db.execute(aCommand);\r
125     }\r
126     @Override\r
127     public String executeAndDisconnect(String command) throws InternalException {\r
128         String t = "";\r
129         try {\r
130             t = execute(command);\r
131         } finally {\r
132             db.disconnect();\r
133         }\r
134         return t;\r
135     }\r
136     @Override\r
137     public boolean isActive() throws InternalException {\r
138         try {\r
139             db.execute("");\r
140             return true;\r
141         } catch (InternalException e) {\r
142             return false;\r
143         }\r
144     }\r
145     @Override\r
146     public synchronized void start() throws InternalException {\r
147         db.start();\r
148     }\r
149     @Override\r
150     public synchronized void stop() throws InternalException {\r
151         if (!db.tryToStop())\r
152             throw new InternalException("Could not stop database.");\r
153     }\r
154     @Override\r
155     public String getAddress() throws DatabaseException {\r
156         return db.getFolder().getAbsolutePath();\r
157     }\r
158 //    @Override\r
159 //    public synchronized IServerAddress getServerAddress() throws InternalException {\r
160 //        return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());\r
161 //    }\r
162 }\r
163 class ProCoreManagement implements Management {\r
164     private final Database db;\r
165     private final Properties properties;\r
166     ProCoreManagement(Path dbFolder, Properties properties) throws ProCoreException {\r
167         db = DatabaseManager.getDatabase(dbFolder);\r
168         this.properties = properties;\r
169     }\r
170     @Override\r
171     public boolean exist() throws DatabaseException {\r
172         return db.isFolderOk();\r
173     }\r
174     @Override\r
175     public void delete() throws DatabaseException {\r
176         db.deleteFiles();\r
177         if (exist())\r
178             throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());\r
179     }\r
180     @Override\r
181     public void create() throws DatabaseException {\r
182         db.initFolder(properties);\r
183         if (!exist())\r
184             throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());\r
185     }\r
186     @Override\r
187     public void purge() throws DatabaseException {\r
188         db.purgeDatabase();\r
189     }\r
190     @Override\r
191     public void shutdown() throws DatabaseException {\r
192         db.tryToStop();\r
193         db.disconnect();\r
194     }\r
195 }\r