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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.procore;
\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
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
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
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
48 public final String getName() {
\r
49 return ProCoreDriverName;
\r
52 public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {
\r
53 Path dbFolder = Paths.get(address);
\r
54 return DatabaseManager.getDatabase(dbFolder).getUserAgent();
\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
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
71 if (!db.isConnected())
\r
74 ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);
\r
75 ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);
\r
77 String user = properties.getProperty("user");
\r
78 String password= properties.getProperty("password");
\r
80 throw new ProCoreException("'user' property not provided");
\r
81 if (password == null)
\r
82 throw new ProCoreException("'password' property not provided");
\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
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
96 session.registerService(Session.class, session);
\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
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
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
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
123 public String execute(String aCommand) throws InternalException {
\r
124 return db.execute(aCommand);
\r
127 public String executeAndDisconnect(String command) throws InternalException {
\r
130 t = execute(command);
\r
137 public boolean isActive() throws InternalException {
\r
141 } catch (InternalException e) {
\r
146 public synchronized void start() throws InternalException {
\r
150 public synchronized void stop() throws InternalException {
\r
151 if (!db.tryToStop())
\r
152 throw new InternalException("Could not stop database.");
\r
155 public String getAddress() throws DatabaseException {
\r
156 return db.getFolder().getAbsolutePath();
\r
159 // public synchronized IServerAddress getServerAddress() throws InternalException {
\r
160 // return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());
\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
171 public boolean exist() throws DatabaseException {
\r
172 return db.isFolderOk();
\r
175 public void delete() throws DatabaseException {
\r
178 throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());
\r
181 public void create() throws DatabaseException {
\r
182 db.initFolder(properties);
\r
184 throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());
\r
187 public void purge() throws DatabaseException {
\r
188 db.purgeDatabase();
\r
191 public void shutdown() throws DatabaseException {
\r