--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.nio.file.Files;\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
+import java.util.Properties;\r
+\r
+import org.simantics.db.Database;\r
+import org.simantics.db.DatabaseUserAgent;\r
+import org.simantics.db.Driver;\r
+import org.simantics.db.Driver.Management;\r
+import org.simantics.db.ServerI;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.authentication.UserAuthenticationAgent;\r
+import org.simantics.db.authentication.UserAuthenticator;\r
+import org.simantics.db.common.auth.UserAuthenticationAgents;\r
+import org.simantics.db.common.auth.UserAuthenticators;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.InternalException;\r
+import org.simantics.db.exception.SDBException;\r
+import org.simantics.db.server.DatabaseManager;\r
+import org.simantics.db.server.ProCoreException;\r
+\r
+import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
+import fi.vtt.simantics.procore.ProCoreServerReference;\r
+import fi.vtt.simantics.procore.ProCoreSessionReference;\r
+import fi.vtt.simantics.procore.SessionManagerSource;\r
+\r
+public class ProCoreDriver implements Driver {\r
+ private static long sessionId = SessionManagerSource.NullSessionId;\r
+ public static final String ProCoreDriverName = "procore";\r
+ public static final String ProCoreDriverNameVirtual = "virtual";\r
+\r
+ @Override\r
+ public final String getName() {\r
+ return ProCoreDriverName;\r
+ }\r
+ @Override\r
+ public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {\r
+ Path dbFolder = Paths.get(address);\r
+ return DatabaseManager.getDatabase(dbFolder).getUserAgent();\r
+ }\r
+ @Override\r
+ public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {\r
+ Path dbFolder = Paths.get(address);\r
+ DatabaseManager.getDatabase(dbFolder).setUserAgent(dbUserAgent);\r
+ }\r
+ @Override\r
+ public Session getSession(String address, Properties properties) throws SDBException {\r
+ Path dbFolder = Paths.get(address);\r
+ if (!Files.isDirectory(dbFolder))\r
+ throw new ProCoreException("Database folder does not exist. folder=" + dbFolder);\r
+ Database db = DatabaseManager.getDatabase(dbFolder);\r
+ if (!db.isFolderOk())\r
+ throw new ProCoreException("Database folder is not ok. folder=" + dbFolder);\r
+ if (!db.isRunning())\r
+ db.start();\r
+ if (!db.isConnected())\r
+ db.connect();\r
+ Session session;\r
+ ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);\r
+ ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);\r
+ try {\r
+ String user = properties.getProperty("user");\r
+ String password= properties.getProperty("password");\r
+ if (user == null)\r
+ throw new ProCoreException("'user' property not provided");\r
+ if (password == null)\r
+ throw new ProCoreException("'password' property not provided");\r
+\r
+ UserAuthenticator authenticator = UserAuthenticators.byNameAndPassword(user, password);\r
+ // FIXME: remove this hack once the server gets proper authentication\r
+ if (properties.getProperty("hyshys") != null)\r
+ authenticator = new BackdoorAuthenticator();\r
+ UserAuthenticationAgent agent = UserAuthenticationAgents.staticAgent(authenticator);\r
+\r
+ session = SessionManagerSource.getSessionManager().createSession(sessionReference, agent);\r
+ if (!properties.containsKey("clientId"))\r
+ properties.put("clientId", dbFolder.toFile().getAbsolutePath());\r
+ session.registerService(Properties.class, properties);\r
+ Session s = session.peekService(Session.class);\r
+ if (null == s)\r
+ session.registerService(Session.class, session);\r
+ return session;\r
+\r
+ } catch (IOException e) {\r
+ throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);\r
+ } catch (org.simantics.db.exception.DatabaseException e) {\r
+ throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public ServerI getServer(String address, Properties notUsed) throws ProCoreException {\r
+ Path dbFolder = Paths.get(address);\r
+ return new ProCoreServer(dbFolder.toFile());\r
+ }\r
+ @Override\r
+ public Management getManagement(String address, Properties properties) throws DatabaseException {\r
+ Path dbFolder = Paths.get(address);\r
+ return new ProCoreManagement(dbFolder, properties);\r
+ }\r
+}\r
+class ProCoreServer implements ServerI {\r
+ private final Database db;\r
+ ProCoreServer(File dbFolder) throws ProCoreException {\r
+ db = DatabaseManager.getDatabase(dbFolder.toPath());\r
+ }\r
+ @Override\r
+ public String execute(String aCommand) throws InternalException {\r
+ return db.execute(aCommand);\r
+ }\r
+ @Override\r
+ public String executeAndDisconnect(String command) throws InternalException {\r
+ String t = "";\r
+ try {\r
+ t = execute(command);\r
+ } finally {\r
+ db.disconnect();\r
+ }\r
+ return t;\r
+ }\r
+ @Override\r
+ public boolean isActive() throws InternalException {\r
+ try {\r
+ db.execute("");\r
+ return true;\r
+ } catch (InternalException e) {\r
+ return false;\r
+ }\r
+ }\r
+ @Override\r
+ public synchronized void start() throws InternalException {\r
+ db.start();\r
+ }\r
+ @Override\r
+ public synchronized void stop() throws InternalException {\r
+ if (!db.tryToStop())\r
+ throw new InternalException("Could not stop database.");\r
+ }\r
+ @Override\r
+ public String getAddress() throws DatabaseException {\r
+ return db.getFolder().getAbsolutePath();\r
+ }\r
+// @Override\r
+// public synchronized IServerAddress getServerAddress() throws InternalException {\r
+// return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());\r
+// }\r
+}\r
+class ProCoreManagement implements Management {\r
+ private final Database db;\r
+ private final Properties properties;\r
+ ProCoreManagement(Path dbFolder, Properties properties) throws ProCoreException {\r
+ db = DatabaseManager.getDatabase(dbFolder);\r
+ this.properties = properties;\r
+ }\r
+ @Override\r
+ public boolean exist() throws DatabaseException {\r
+ return db.isFolderOk();\r
+ }\r
+ @Override\r
+ public void delete() throws DatabaseException {\r
+ db.deleteFiles();\r
+ if (exist())\r
+ throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());\r
+ }\r
+ @Override\r
+ public void create() throws DatabaseException {\r
+ db.initFolder(properties);\r
+ if (!exist())\r
+ throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());\r
+ }\r
+ @Override\r
+ public void purge() throws DatabaseException {\r
+ db.purgeDatabase();\r
+ }\r
+ @Override\r
+ public void shutdown() throws DatabaseException {\r
+ db.tryToStop();\r
+ db.disconnect();\r
+ }\r
+}\r