1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.procore;
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.Properties;
21 import org.simantics.db.Database;
22 import org.simantics.db.DatabaseUserAgent;
23 import org.simantics.db.Driver;
24 import org.simantics.db.Driver.Management;
25 import org.simantics.db.ServerI;
26 import org.simantics.db.Session;
27 import org.simantics.db.authentication.UserAuthenticationAgent;
28 import org.simantics.db.authentication.UserAuthenticator;
29 import org.simantics.db.common.auth.UserAuthenticationAgents;
30 import org.simantics.db.common.auth.UserAuthenticators;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.exception.InternalException;
33 import org.simantics.db.exception.SDBException;
34 import org.simantics.db.server.DatabaseManager;
35 import org.simantics.db.server.ProCoreException;
37 import fi.vtt.simantics.procore.BackdoorAuthenticator;
38 import fi.vtt.simantics.procore.ProCoreServerReference;
39 import fi.vtt.simantics.procore.ProCoreSessionReference;
40 import fi.vtt.simantics.procore.SessionManagerSource;
42 public class ProCoreDriver implements Driver {
43 private static long sessionId = SessionManagerSource.NullSessionId;
44 public static final String ProCoreDriverName = "procore";
45 public static final String ProCoreDriverNameVirtual = "virtual";
48 public final String getName() {
49 return ProCoreDriverName;
52 public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {
53 Path dbFolder = Paths.get(address);
54 return DatabaseManager.getDatabase(dbFolder).getUserAgent();
57 public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {
58 Path dbFolder = Paths.get(address);
59 DatabaseManager.getDatabase(dbFolder).setUserAgent(dbUserAgent);
62 public Session getSession(String address, Properties properties) throws SDBException {
63 Path dbFolder = Paths.get(address);
64 if (!Files.isDirectory(dbFolder))
65 throw new ProCoreException("Database folder does not exist. folder=" + dbFolder);
66 Database db = DatabaseManager.getDatabase(dbFolder);
68 throw new ProCoreException("Database folder is not ok. folder=" + dbFolder);
71 if (!db.isConnected())
74 ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);
75 ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);
77 String user = properties.getProperty("user");
78 String password= properties.getProperty("password");
80 throw new ProCoreException("'user' property not provided");
82 throw new ProCoreException("'password' property not provided");
84 UserAuthenticator authenticator = UserAuthenticators.byNameAndPassword(user, password);
85 // FIXME: remove this hack once the server gets proper authentication
86 if (properties.getProperty("hyshys") != null)
87 authenticator = new BackdoorAuthenticator();
88 UserAuthenticationAgent agent = UserAuthenticationAgents.staticAgent(authenticator);
90 session = SessionManagerSource.getSessionManager().createSession(sessionReference, agent);
91 if (!properties.containsKey("clientId"))
92 properties.put("clientId", dbFolder.toFile().getAbsolutePath());
93 session.registerService(Properties.class, properties);
94 Session s = session.peekService(Session.class);
96 session.registerService(Session.class, session);
99 } catch (IOException e) {
100 throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);
101 } catch (org.simantics.db.exception.DatabaseException e) {
102 throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);
107 public ServerI getServer(String address, Properties notUsed) throws ProCoreException {
108 Path dbFolder = Paths.get(address);
109 return new ProCoreServer(dbFolder.toFile());
112 public Management getManagement(String address, Properties properties) throws DatabaseException {
113 Path dbFolder = Paths.get(address);
114 return new ProCoreManagement(dbFolder, properties);
117 class ProCoreServer implements ServerI {
118 private final Database db;
119 ProCoreServer(File dbFolder) throws ProCoreException {
120 db = DatabaseManager.getDatabase(dbFolder.toPath());
123 public String execute(String aCommand) throws InternalException {
124 return db.execute(aCommand);
127 public String executeAndDisconnect(String command) throws InternalException {
130 t = execute(command);
137 public boolean isActive() throws InternalException {
141 } catch (InternalException e) {
146 public synchronized void start() throws InternalException {
150 public synchronized void stop() throws InternalException {
152 throw new InternalException("Could not stop database.");
155 public String getAddress() throws DatabaseException {
156 return db.getFolder().getAbsolutePath();
159 // public synchronized IServerAddress getServerAddress() throws InternalException {
160 // return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());
163 class ProCoreManagement implements Management {
164 private final Database db;
165 private final Properties properties;
166 ProCoreManagement(Path dbFolder, Properties properties) throws ProCoreException {
167 db = DatabaseManager.getDatabase(dbFolder);
168 this.properties = properties;
171 public boolean exist() throws DatabaseException {
172 return db.isFolderOk();
175 public void delete() throws DatabaseException {
178 throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());
181 public void create() throws DatabaseException {
182 db.initFolder(properties);
184 throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());
187 public void purge() throws DatabaseException {
191 public void shutdown() throws DatabaseException {